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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*-*- Mode: C; c-basic-offset: 8 -*-*/
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "name.h"
#include "strv.h"
#include "conf-parser.h"
#include "load-fragment.h"
static int config_parse_deps(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
Set **set = data;
Name *name = userdata;
char *w;
size_t l;
char *state;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
FOREACH_WORD(w, &l, rvalue, state) {
char *t;
int r;
Name *other;
if (!(t = strndup(w, l)))
return -ENOMEM;
r = manager_load_name(name->meta.manager, t, &other);
free(t);
if (r < 0)
return r;
if (!*set)
if (!(*set = set_new(trivial_hash_func, trivial_compare_func)))
return -ENOMEM;
if ((r = set_put(*set, other)) < 0)
return r;
}
return 0;
}
static int config_parse_names(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
Set **set = data;
Name *name = userdata;
char *w;
size_t l;
char *state;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
FOREACH_WORD(w, &l, rvalue, state) {
char *t;
int r;
Name *other;
if (!(t = strndup(w, l)))
return -ENOMEM;
other = manager_get_name(name->meta.manager, t);
if (other) {
if (other != name) {
if (other->meta.state != NAME_STUB) {
free(t);
return -EEXIST;
}
if ((r = name_merge(name, other) < 0)) {
free(t);
return r;
}
}
} else {
if (!*set)
if (!(*set = set_new(trivial_hash_func, trivial_compare_func))) {
free(t);
return -ENOMEM;
}
if ((r = set_put(*set, t)) < 0) {
free(t);
return r;
}
t = NULL;
}
free(t);
}
return 0;
}
static int config_parse_listen(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
return address_parse(data, rvalue);
}
static int config_parse_type(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
const char *rvalue,
void *data,
void *userdata) {
int *type = data;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if (streq(rvalue, "stream"))
*type = SOCK_STREAM;
else if (streq(rvalue, "dgram"))
*type = SOCK_DGRAM;
else
return -EINVAL;
return 0;
}
int name_load_fragment(Name *n) {
const char *const section_table[_NAME_TYPE_MAX] = {
[NAME_SERVICE] = "Service",
[NAME_TIMER] = "Timer",
[NAME_SOCKET] = "Socket",
[NAME_MILESTONE] = "Milestone",
[NAME_DEVICE] = "Device",
[NAME_MOUNT] = "Mount",
[NAME_AUTOMOUNT] = "Automount",
[NAME_SNAPSHOT] = "Snapshot"
};
const ConfigItem items[] = {
{ "Names", config_parse_names, &n->meta.names, "Meta" },
{ "Description", config_parse_string, &n->meta.description, "Meta" },
{ "Requires", config_parse_deps, n->meta.dependencies+NAME_REQUIRES, "Meta" },
{ "SoftRequires", config_parse_deps, n->meta.dependencies+NAME_SOFT_REQUIRES, "Meta" },
{ "Wants", config_parse_deps, n->meta.dependencies+NAME_WANTS, "Meta" },
{ "Requisite", config_parse_deps, n->meta.dependencies+NAME_REQUISITE, "Meta" },
{ "SoftRequisite", config_parse_deps, n->meta.dependencies+NAME_SOFT_REQUISITE, "Meta" },
{ "Conflicts", config_parse_deps, n->meta.dependencies+NAME_CONFLICTS, "Meta" },
{ "Before", config_parse_deps, n->meta.dependencies+NAME_BEFORE, "Meta" },
{ "After", config_parse_deps, n->meta.dependencies+NAME_AFTER, "Meta" },
{ "Listen", config_parse_listen, &n->socket.address, "Socket" },
{ "Type", config_parse_type, &n->socket.address.type, "Socket" },
{ NULL, NULL, NULL, NULL }
};
const
char *t;
int r;
void *state;
const char *sections[3];
assert(n);
assert(n->meta.state == NAME_STUB);
sections[0] = "Meta";
sections[1] = section_table[n->meta.type];
sections[2] = NULL;
SET_FOREACH(t, n->meta.names, state)
if ((r = config_parse(t, sections, items, n)) < 0)
goto fail;
r = 0;
fail:
return r;
}
|