]>
Commit | Line | Data |
---|---|---|
3efd4195 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <assert.h> | |
4 | #include <errno.h> | |
5 | #include <string.h> | |
6 | ||
7 | #include "name.h" | |
8 | #include "strv.h" | |
9 | #include "conf-parser.h" | |
10 | #include "load-fragment.h" | |
11 | ||
42f4e3c4 | 12 | static int config_parse_deps( |
3efd4195 LP |
13 | const char *filename, |
14 | unsigned line, | |
15 | const char *section, | |
16 | const char *lvalue, | |
17 | const char *rvalue, | |
18 | void *data, | |
19 | void *userdata) { | |
20 | ||
21 | Set **set = data; | |
22 | Name *name = userdata; | |
23 | char *w; | |
24 | size_t l; | |
25 | char *state; | |
26 | ||
27 | assert(filename); | |
28 | assert(lvalue); | |
29 | assert(rvalue); | |
30 | assert(data); | |
31 | ||
32 | FOREACH_WORD(w, &l, rvalue, state) { | |
33 | char *t; | |
34 | int r; | |
35 | Name *other; | |
36 | ||
37 | if (!(t = strndup(w, l))) | |
38 | return -ENOMEM; | |
39 | ||
40 | r = manager_load_name(name->meta.manager, t, &other); | |
41 | free(t); | |
42 | ||
43 | if (r < 0) | |
44 | return r; | |
45 | ||
46 | if (!*set) | |
47 | if (!(*set = set_new(trivial_hash_func, trivial_compare_func))) | |
48 | return -ENOMEM; | |
49 | ||
50 | if ((r = set_put(*set, other)) < 0) | |
51 | return r; | |
52 | } | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
42f4e3c4 | 57 | static int config_parse_names( |
87d1515d LP |
58 | const char *filename, |
59 | unsigned line, | |
60 | const char *section, | |
61 | const char *lvalue, | |
62 | const char *rvalue, | |
63 | void *data, | |
64 | void *userdata) { | |
65 | ||
66 | Set **set = data; | |
67 | Name *name = userdata; | |
68 | char *w; | |
69 | size_t l; | |
70 | char *state; | |
71 | ||
72 | assert(filename); | |
73 | assert(lvalue); | |
74 | assert(rvalue); | |
75 | assert(data); | |
76 | ||
77 | FOREACH_WORD(w, &l, rvalue, state) { | |
78 | char *t; | |
79 | int r; | |
80 | Name *other; | |
81 | ||
82 | if (!(t = strndup(w, l))) | |
83 | return -ENOMEM; | |
84 | ||
85 | other = manager_get_name(name->meta.manager, t); | |
86 | ||
87 | if (other) { | |
88 | ||
89 | if (other != name) { | |
90 | ||
91 | if (other->meta.state != NAME_STUB) { | |
92 | free(t); | |
93 | return -EEXIST; | |
94 | } | |
95 | ||
7fad411c | 96 | if ((r = name_merge(name, other)) < 0) { |
87d1515d LP |
97 | free(t); |
98 | return r; | |
99 | } | |
100 | } | |
101 | ||
102 | } else { | |
103 | ||
104 | if (!*set) | |
105 | if (!(*set = set_new(trivial_hash_func, trivial_compare_func))) { | |
106 | free(t); | |
107 | return -ENOMEM; | |
108 | } | |
109 | ||
110 | if ((r = set_put(*set, t)) < 0) { | |
111 | free(t); | |
112 | return r; | |
113 | } | |
11dd41ce LP |
114 | |
115 | t = NULL; | |
87d1515d LP |
116 | } |
117 | ||
118 | free(t); | |
119 | } | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
42f4e3c4 LP |
124 | static int config_parse_listen( |
125 | const char *filename, | |
126 | unsigned line, | |
127 | const char *section, | |
128 | const char *lvalue, | |
129 | const char *rvalue, | |
130 | void *data, | |
131 | void *userdata) { | |
132 | ||
133 | assert(filename); | |
134 | assert(lvalue); | |
135 | assert(rvalue); | |
136 | assert(data); | |
137 | ||
138 | return address_parse(data, rvalue); | |
139 | } | |
140 | ||
141 | static int config_parse_type( | |
142 | const char *filename, | |
143 | unsigned line, | |
144 | const char *section, | |
145 | const char *lvalue, | |
146 | const char *rvalue, | |
147 | void *data, | |
148 | void *userdata) { | |
149 | ||
150 | int *type = data; | |
151 | ||
152 | assert(filename); | |
153 | assert(lvalue); | |
154 | assert(rvalue); | |
155 | assert(data); | |
156 | ||
157 | if (streq(rvalue, "stream")) | |
158 | *type = SOCK_STREAM; | |
159 | else if (streq(rvalue, "dgram")) | |
160 | *type = SOCK_DGRAM; | |
161 | else | |
162 | return -EINVAL; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
3efd4195 LP |
167 | int name_load_fragment(Name *n) { |
168 | ||
42f4e3c4 LP |
169 | const char *const section_table[_NAME_TYPE_MAX] = { |
170 | [NAME_SERVICE] = "Service", | |
171 | [NAME_TIMER] = "Timer", | |
172 | [NAME_SOCKET] = "Socket", | |
173 | [NAME_MILESTONE] = "Milestone", | |
174 | [NAME_DEVICE] = "Device", | |
175 | [NAME_MOUNT] = "Mount", | |
176 | [NAME_AUTOMOUNT] = "Automount", | |
177 | [NAME_SNAPSHOT] = "Snapshot" | |
178 | }; | |
179 | ||
3efd4195 | 180 | const ConfigItem items[] = { |
42f4e3c4 LP |
181 | { "Names", config_parse_names, &n->meta.names, "Meta" }, |
182 | { "Description", config_parse_string, &n->meta.description, "Meta" }, | |
183 | { "Requires", config_parse_deps, n->meta.dependencies+NAME_REQUIRES, "Meta" }, | |
184 | { "SoftRequires", config_parse_deps, n->meta.dependencies+NAME_SOFT_REQUIRES, "Meta" }, | |
185 | { "Wants", config_parse_deps, n->meta.dependencies+NAME_WANTS, "Meta" }, | |
186 | { "Requisite", config_parse_deps, n->meta.dependencies+NAME_REQUISITE, "Meta" }, | |
187 | { "SoftRequisite", config_parse_deps, n->meta.dependencies+NAME_SOFT_REQUISITE, "Meta" }, | |
188 | { "Conflicts", config_parse_deps, n->meta.dependencies+NAME_CONFLICTS, "Meta" }, | |
189 | { "Before", config_parse_deps, n->meta.dependencies+NAME_BEFORE, "Meta" }, | |
190 | { "After", config_parse_deps, n->meta.dependencies+NAME_AFTER, "Meta" }, | |
191 | { "Listen", config_parse_listen, &n->socket.address, "Socket" }, | |
192 | { "Type", config_parse_type, &n->socket.address.type, "Socket" }, | |
3efd4195 LP |
193 | { NULL, NULL, NULL, NULL } |
194 | }; | |
195 | ||
42f4e3c4 LP |
196 | const |
197 | ||
87d1515d | 198 | char *t; |
3efd4195 | 199 | int r; |
87d1515d | 200 | void *state; |
42f4e3c4 | 201 | const char *sections[3]; |
3efd4195 LP |
202 | |
203 | assert(n); | |
204 | assert(n->meta.state == NAME_STUB); | |
205 | ||
42f4e3c4 LP |
206 | sections[0] = "Meta"; |
207 | sections[1] = section_table[n->meta.type]; | |
208 | sections[2] = NULL; | |
209 | ||
87d1515d | 210 | SET_FOREACH(t, n->meta.names, state) |
42f4e3c4 | 211 | if ((r = config_parse(t, sections, items, n)) < 0) |
3efd4195 LP |
212 | goto fail; |
213 | ||
87d1515d | 214 | r = 0; |
3efd4195 LP |
215 | |
216 | fail: | |
87d1515d | 217 | return r; |
3efd4195 | 218 | } |