1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* Usurpation – server daemon settings handling
*
* Copyright (C) 2019 Gediminas Jakutis
* Copyright (C) 2019 Ramūnas Mažeikis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "settings.h"
#include "settings_private.h"
/* TODO: should be, in this order: looking at options provided via command
* line arguments, looking at at /etc/conf.d/usurpation.conf or
* using default. Right now just uses defaults.
*/
void settings_init(void)
{
unset_flag(flag_daemonize);
settings.port = 6996;
settings.verboselevel = USURP_VERBOSITY;
setting_readconf("./usurpation.conf");
}
int setting_detach(void)
{
return test_flag(flag_daemonize);
}
int setting_verbose(void)
{
return settings.verboselevel;
}
unsigned short int setting_port(void)
{
return settings.port;
}
/* could be a one-liner, but let's make the logic more obvious */
int test_flag(unsigned int flag)
{
int ret;
ret = settings.flags & flag;
ret = !!ret;
return ret;
}
void set_flag(unsigned int flag)
{
settings.flags |= flag;
}
void unset_flag(unsigned int flag)
{
settings.flags &= ~flag;
}
/* TODO: semi-stub! */
static int setting_readconf(const char * const path)
{
struct stat filestat;
char *buf;
char *line;
char *entry;
char *value;
char *line_state;
char *token_state;
int fd;
if ((fd = open(path, O_RDONLY | O_NOATIME)) == -1) {
if (USURP_VERBOSITY >= 2) {
perror("error while opening the configuration file");
return 1;
}
}
fstat(fd, &filestat);
buf = malloc(filestat.st_size);
if (read(fd, buf, filestat.st_size) != filestat.st_size) {
if (USURP_VERBOSITY >= 2) {
perror("partial read of configuration file detected");
free(buf);
return 1;
}
}
line = strtok_r(buf, "\n", &line_state);
while (line) {
if (line[0] != '#') {
entry = strtok_r(line, "=", &token_state);
value = strtok_r(NULL, "=", &token_state);
setting_handle_config_entry(entry, value);
}
line = strtok_r(NULL, "\n", &line_state);
}
free(buf);
return 0;
}
static int setting_handle_config_entry(const char * const entry, const char * const value)
{
size_t i;
for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) {
if (!(strcmp(entry, ent_table[i].name))) {
ent_table[i].set(value);
return 0;
}
}
return 1;
}
static void set_daemonize(const void * const arg)
{
const char * const a = arg;
atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize);
}
static void set_port(const void * const arg)
{
const char * const a = arg;
settings.port = strtoul(a, NULL, 0);
}
static void set_verbosity(const void * const arg)
{
const char * const a = arg;
settings.verboselevel = strtol(a, NULL, 0);
}
|