]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
83c60c9f LP |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <fcntl.h> | |
f94ea366 | 27 | #include <sys/epoll.h> |
034c6ed7 | 28 | #include <signal.h> |
4f2d528d | 29 | #include <arpa/inet.h> |
916abb21 | 30 | #include <mqueue.h> |
83c60c9f | 31 | |
87f0e418 | 32 | #include "unit.h" |
5cb5a6ff | 33 | #include "socket.h" |
cebf8b20 | 34 | #include "netinet/tcp.h" |
83c60c9f | 35 | #include "log.h" |
23a177ef LP |
36 | #include "load-dropin.h" |
37 | #include "load-fragment.h" | |
9e2f7c11 | 38 | #include "strv.h" |
4f2d528d | 39 | #include "unit-name.h" |
4139c1b2 | 40 | #include "dbus-socket.h" |
4fd5948e | 41 | #include "missing.h" |
a40eb732 | 42 | #include "special.h" |
398ef8ba | 43 | #include "bus-errors.h" |
e51bc1a2 | 44 | #include "label.h" |
9a57c629 | 45 | #include "exit-status.h" |
f6a6225e | 46 | #include "def.h" |
83c60c9f | 47 | |
acbb0225 | 48 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { |
87f0e418 LP |
49 | [SOCKET_DEAD] = UNIT_INACTIVE, |
50 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
51 | [SOCKET_START_POST] = UNIT_ACTIVATING, | |
52 | [SOCKET_LISTENING] = UNIT_ACTIVE, | |
53 | [SOCKET_RUNNING] = UNIT_ACTIVE, | |
54 | [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, | |
55 | [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, | |
56 | [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, | |
57 | [SOCKET_STOP_POST] = UNIT_DEACTIVATING, | |
80876c20 LP |
58 | [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
59 | [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 60 | [SOCKET_FAILED] = UNIT_FAILED |
83c60c9f | 61 | }; |
5cb5a6ff | 62 | |
a16e1123 LP |
63 | static void socket_init(Unit *u) { |
64 | Socket *s = SOCKET(u); | |
65 | ||
66 | assert(u); | |
67 | assert(u->meta.load_state == UNIT_STUB); | |
68 | ||
a16e1123 LP |
69 | s->backlog = SOMAXCONN; |
70 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
71 | s->directory_mode = 0755; | |
9131f660 | 72 | s->socket_mode = 0666; |
a16e1123 | 73 | |
6cf6bbc2 LP |
74 | s->max_connections = 64; |
75 | ||
4fd5948e | 76 | s->priority = -1; |
4fd5948e LP |
77 | s->ip_tos = -1; |
78 | s->ip_ttl = -1; | |
4fd5948e | 79 | s->mark = -1; |
4fd5948e | 80 | |
a16e1123 | 81 | exec_context_init(&s->exec_context); |
0a494f1f LP |
82 | s->exec_context.std_output = u->meta.manager->default_std_output; |
83 | s->exec_context.std_error = u->meta.manager->default_std_error; | |
a16e1123 LP |
84 | |
85 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
86 | } | |
acbb0225 | 87 | |
5e94833f LP |
88 | static void socket_unwatch_control_pid(Socket *s) { |
89 | assert(s); | |
90 | ||
91 | if (s->control_pid <= 0) | |
92 | return; | |
93 | ||
94 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
95 | s->control_pid = 0; | |
96 | } | |
97 | ||
87f0e418 LP |
98 | static void socket_done(Unit *u) { |
99 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
100 | SocketPort *p; |
101 | ||
102 | assert(s); | |
103 | ||
104 | while ((p = s->ports)) { | |
105 | LIST_REMOVE(SocketPort, port, s->ports, p); | |
106 | ||
a16e1123 LP |
107 | if (p->fd >= 0) { |
108 | unit_unwatch_fd(UNIT(s), &p->fd_watch); | |
109 | close_nointr_nofail(p->fd); | |
110 | } | |
111 | ||
034c6ed7 LP |
112 | free(p->path); |
113 | free(p); | |
114 | } | |
115 | ||
116 | exec_context_done(&s->exec_context); | |
e537352b | 117 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
034c6ed7 LP |
118 | s->control_command = NULL; |
119 | ||
5e94833f | 120 | socket_unwatch_control_pid(s); |
034c6ed7 | 121 | |
57020a3a | 122 | unit_ref_unset(&s->service); |
034c6ed7 | 123 | |
cebf8b20 TT |
124 | free(s->tcp_congestion); |
125 | s->tcp_congestion = NULL; | |
126 | ||
acbb0225 | 127 | free(s->bind_to_device); |
e537352b | 128 | s->bind_to_device = NULL; |
acbb0225 LP |
129 | |
130 | unit_unwatch_timer(u, &s->timer_watch); | |
5cb5a6ff LP |
131 | } |
132 | ||
b15bdda8 LP |
133 | static int socket_instantiate_service(Socket *s) { |
134 | char *prefix, *name; | |
135 | int r; | |
136 | Unit *u; | |
137 | ||
138 | assert(s); | |
139 | ||
140 | /* This fills in s->service if it isn't filled in yet. For | |
141 | * Accept=yes sockets we create the next connection service | |
142 | * here. For Accept=no this is mostly a NOP since the service | |
143 | * is figured out at load time anyway. */ | |
144 | ||
57020a3a | 145 | if (UNIT_DEREF(s->service)) |
b15bdda8 LP |
146 | return 0; |
147 | ||
148 | assert(s->accept); | |
149 | ||
150 | if (!(prefix = unit_name_to_prefix(s->meta.id))) | |
151 | return -ENOMEM; | |
152 | ||
153 | r = asprintf(&name, "%s@%u.service", prefix, s->n_accepted); | |
154 | free(prefix); | |
155 | ||
156 | if (r < 0) | |
157 | return -ENOMEM; | |
158 | ||
159 | r = manager_load_unit(s->meta.manager, name, NULL, NULL, &u); | |
160 | free(name); | |
161 | ||
162 | if (r < 0) | |
163 | return r; | |
164 | ||
7b4bf06b LP |
165 | #ifdef HAVE_SYSV_COMPAT |
166 | if (SERVICE(u)->sysv_path) { | |
167 | log_error("Using SysV services for socket activation is not supported. Refusing."); | |
168 | return -ENOENT; | |
169 | } | |
170 | #endif | |
171 | ||
6c073082 | 172 | u->meta.no_gc = true; |
57020a3a LP |
173 | unit_ref_set(&s->service, u); |
174 | ||
175 | return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false); | |
b15bdda8 LP |
176 | } |
177 | ||
4f2d528d LP |
178 | static bool have_non_accept_socket(Socket *s) { |
179 | SocketPort *p; | |
180 | ||
181 | assert(s); | |
182 | ||
183 | if (!s->accept) | |
184 | return true; | |
185 | ||
dd5ad9d4 LP |
186 | LIST_FOREACH(port, p, s->ports) { |
187 | ||
188 | if (p->type != SOCKET_SOCKET) | |
189 | return true; | |
190 | ||
4f2d528d LP |
191 | if (!socket_address_can_accept(&p->address)) |
192 | return true; | |
dd5ad9d4 | 193 | } |
4f2d528d LP |
194 | |
195 | return false; | |
196 | } | |
197 | ||
198 | static int socket_verify(Socket *s) { | |
199 | assert(s); | |
200 | ||
4cd1fbcc | 201 | if (s->meta.load_state != UNIT_LOADED) |
4f2d528d LP |
202 | return 0; |
203 | ||
204 | if (!s->ports) { | |
4cd1fbcc | 205 | log_error("%s lacks Listen setting. Refusing.", s->meta.id); |
4f2d528d LP |
206 | return -EINVAL; |
207 | } | |
208 | ||
0009d2a6 LP |
209 | if (s->accept && have_non_accept_socket(s)) { |
210 | log_error("%s configured for accepting sockets, but sockets are non-accepting. Refusing.", s->meta.id); | |
211 | return -EINVAL; | |
212 | } | |
213 | ||
6cf6bbc2 | 214 | if (s->accept && s->max_connections <= 0) { |
4d0e5dbd LP |
215 | log_error("%s's MaxConnection setting too small. Refusing.", s->meta.id); |
216 | return -EINVAL; | |
217 | } | |
218 | ||
57020a3a | 219 | if (s->accept && UNIT_DEREF(s->service)) { |
d9ff321a LP |
220 | log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", s->meta.id); |
221 | return -EINVAL; | |
222 | } | |
223 | ||
2e22afe9 | 224 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { |
4d0e5dbd | 225 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); |
6cf6bbc2 LP |
226 | return -EINVAL; |
227 | } | |
228 | ||
4f2d528d LP |
229 | return 0; |
230 | } | |
231 | ||
6e2ef85b LP |
232 | static bool socket_needs_mount(Socket *s, const char *prefix) { |
233 | SocketPort *p; | |
234 | ||
235 | assert(s); | |
236 | ||
237 | LIST_FOREACH(port, p, s->ports) { | |
238 | ||
239 | if (p->type == SOCKET_SOCKET) { | |
240 | if (socket_address_needs_mount(&p->address, prefix)) | |
241 | return true; | |
916abb21 | 242 | } else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL) { |
6e2ef85b LP |
243 | if (path_startswith(p->path, prefix)) |
244 | return true; | |
245 | } | |
246 | } | |
247 | ||
248 | return false; | |
249 | } | |
250 | ||
251 | int socket_add_one_mount_link(Socket *s, Mount *m) { | |
252 | int r; | |
253 | ||
254 | assert(s); | |
255 | assert(m); | |
256 | ||
257 | if (s->meta.load_state != UNIT_LOADED || | |
258 | m->meta.load_state != UNIT_LOADED) | |
259 | return 0; | |
260 | ||
261 | if (!socket_needs_mount(s, m->where)) | |
262 | return 0; | |
263 | ||
2c966c03 | 264 | if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0) |
6e2ef85b LP |
265 | return r; |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | static int socket_add_mount_links(Socket *s) { | |
271 | Meta *other; | |
272 | int r; | |
273 | ||
274 | assert(s); | |
275 | ||
ab5c3e3f | 276 | LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_MOUNT]) |
6e2ef85b LP |
277 | if ((r = socket_add_one_mount_link(s, (Mount*) other)) < 0) |
278 | return r; | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static int socket_add_device_link(Socket *s) { | |
284 | char *t; | |
285 | int r; | |
286 | ||
287 | assert(s); | |
288 | ||
289 | if (!s->bind_to_device) | |
290 | return 0; | |
291 | ||
292 | if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0) | |
293 | return -ENOMEM; | |
294 | ||
295 | r = unit_add_node_link(UNIT(s), t, false); | |
296 | free(t); | |
297 | ||
298 | return r; | |
299 | } | |
300 | ||
a40eb732 LP |
301 | static int socket_add_default_dependencies(Socket *s) { |
302 | int r; | |
303 | assert(s); | |
304 | ||
2a77d31d LP |
305 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { |
306 | if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0) | |
307 | return r; | |
308 | ||
a40eb732 LP |
309 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) |
310 | return r; | |
2a77d31d | 311 | } |
a40eb732 | 312 | |
ead8e478 | 313 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
314 | } |
315 | ||
4cfc6dbe LP |
316 | static bool socket_has_exec(Socket *s) { |
317 | unsigned i; | |
318 | assert(s); | |
319 | ||
320 | for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++) | |
321 | if (s->exec_command[i]) | |
322 | return true; | |
323 | ||
324 | return false; | |
325 | } | |
326 | ||
e537352b LP |
327 | static int socket_load(Unit *u) { |
328 | Socket *s = SOCKET(u); | |
329 | int r; | |
44d8db9e | 330 | |
e537352b LP |
331 | assert(u); |
332 | assert(u->meta.load_state == UNIT_STUB); | |
44d8db9e | 333 | |
e537352b | 334 | if ((r = unit_load_fragment_and_dropin(u)) < 0) |
23a177ef | 335 | return r; |
44d8db9e | 336 | |
23a177ef | 337 | /* This is a new unit? Then let's add in some extras */ |
e537352b | 338 | if (u->meta.load_state == UNIT_LOADED) { |
44d8db9e | 339 | |
4f2d528d | 340 | if (have_non_accept_socket(s)) { |
d9ff321a | 341 | |
57020a3a LP |
342 | if (!UNIT_DEREF(s->service)) { |
343 | Unit *x; | |
344 | ||
345 | r = unit_load_related_unit(u, ".service", &x); | |
346 | if (r < 0) | |
d9ff321a | 347 | return r; |
23a177ef | 348 | |
57020a3a LP |
349 | unit_ref_set(&s->service, x); |
350 | } | |
351 | ||
352 | r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true); | |
353 | if (r < 0) | |
4f2d528d LP |
354 | return r; |
355 | } | |
44d8db9e | 356 | |
6e2ef85b LP |
357 | if ((r = socket_add_mount_links(s)) < 0) |
358 | return r; | |
359 | ||
360 | if ((r = socket_add_device_link(s)) < 0) | |
361 | return r; | |
362 | ||
4cfc6dbe LP |
363 | if (socket_has_exec(s)) |
364 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) | |
365 | return r; | |
23a177ef | 366 | |
d686d8a9 | 367 | if ((r = unit_add_default_cgroups(u)) < 0) |
23a177ef | 368 | return r; |
a40eb732 LP |
369 | |
370 | if (s->meta.default_dependencies) | |
371 | if ((r = socket_add_default_dependencies(s)) < 0) | |
372 | return r; | |
23a177ef LP |
373 | } |
374 | ||
4f2d528d | 375 | return socket_verify(s); |
44d8db9e LP |
376 | } |
377 | ||
7a22745a LP |
378 | static const char* listen_lookup(int family, int type) { |
379 | ||
380 | if (family == AF_NETLINK) | |
381 | return "ListenNetlink"; | |
542563ba LP |
382 | |
383 | if (type == SOCK_STREAM) | |
384 | return "ListenStream"; | |
385 | else if (type == SOCK_DGRAM) | |
386 | return "ListenDatagram"; | |
387 | else if (type == SOCK_SEQPACKET) | |
388 | return "ListenSequentialPacket"; | |
389 | ||
034c6ed7 | 390 | assert_not_reached("Unknown socket type"); |
542563ba LP |
391 | return NULL; |
392 | } | |
393 | ||
87f0e418 | 394 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 395 | |
5cb5a6ff | 396 | SocketExecCommand c; |
87f0e418 | 397 | Socket *s = SOCKET(u); |
542563ba | 398 | SocketPort *p; |
82ba9f08 LP |
399 | const char *prefix2; |
400 | char *p2; | |
5cb5a6ff LP |
401 | |
402 | assert(s); | |
fa068367 | 403 | assert(f); |
5cb5a6ff | 404 | |
82ba9f08 LP |
405 | p2 = strappend(prefix, "\t"); |
406 | prefix2 = p2 ? p2 : prefix; | |
c43d20a0 | 407 | |
5cb5a6ff LP |
408 | fprintf(f, |
409 | "%sSocket State: %s\n" | |
542563ba | 410 | "%sBindIPv6Only: %s\n" |
b5a0699f LP |
411 | "%sBacklog: %u\n" |
412 | "%sSocketMode: %04o\n" | |
4fd5948e LP |
413 | "%sDirectoryMode: %04o\n" |
414 | "%sKeepAlive: %s\n" | |
cebf8b20 | 415 | "%sFreeBind: %s\n" |
6b6d2dee | 416 | "%sTransparent: %s\n" |
ec6370a2 | 417 | "%sBroadcast: %s\n" |
271b032a | 418 | "%sPassCrededentials: %s\n" |
cebf8b20 | 419 | "%sTCPCongestion: %s\n", |
a16e1123 | 420 | prefix, socket_state_to_string(s->state), |
c0120d99 | 421 | prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only), |
b5a0699f LP |
422 | prefix, s->backlog, |
423 | prefix, s->socket_mode, | |
4fd5948e LP |
424 | prefix, s->directory_mode, |
425 | prefix, yes_no(s->keep_alive), | |
cebf8b20 | 426 | prefix, yes_no(s->free_bind), |
6b6d2dee | 427 | prefix, yes_no(s->transparent), |
ec6370a2 | 428 | prefix, yes_no(s->broadcast), |
d68af586 | 429 | prefix, yes_no(s->pass_cred), |
83a95334 | 430 | prefix, strna(s->tcp_congestion)); |
542563ba | 431 | |
70123e68 LP |
432 | if (s->control_pid > 0) |
433 | fprintf(f, | |
bb00e604 LP |
434 | "%sControl PID: %lu\n", |
435 | prefix, (unsigned long) s->control_pid); | |
70123e68 | 436 | |
acbb0225 LP |
437 | if (s->bind_to_device) |
438 | fprintf(f, | |
439 | "%sBindToDevice: %s\n", | |
440 | prefix, s->bind_to_device); | |
441 | ||
4f2d528d LP |
442 | if (s->accept) |
443 | fprintf(f, | |
6cf6bbc2 LP |
444 | "%sAccepted: %u\n" |
445 | "%sNConnections: %u\n" | |
446 | "%sMaxConnections: %u\n", | |
447 | prefix, s->n_accepted, | |
448 | prefix, s->n_connections, | |
449 | prefix, s->max_connections); | |
4f2d528d | 450 | |
4fd5948e LP |
451 | if (s->priority >= 0) |
452 | fprintf(f, | |
453 | "%sPriority: %i\n", | |
454 | prefix, s->priority); | |
455 | ||
456 | if (s->receive_buffer > 0) | |
457 | fprintf(f, | |
458 | "%sReceiveBuffer: %zu\n", | |
459 | prefix, s->receive_buffer); | |
460 | ||
461 | if (s->send_buffer > 0) | |
462 | fprintf(f, | |
463 | "%sSendBuffer: %zu\n", | |
464 | prefix, s->send_buffer); | |
465 | ||
466 | if (s->ip_tos >= 0) | |
467 | fprintf(f, | |
468 | "%sIPTOS: %i\n", | |
469 | prefix, s->ip_tos); | |
470 | ||
471 | if (s->ip_ttl >= 0) | |
472 | fprintf(f, | |
473 | "%sIPTTL: %i\n", | |
474 | prefix, s->ip_ttl); | |
475 | ||
476 | if (s->pipe_size > 0) | |
477 | fprintf(f, | |
478 | "%sPipeSize: %zu\n", | |
479 | prefix, s->pipe_size); | |
480 | ||
481 | if (s->mark >= 0) | |
482 | fprintf(f, | |
483 | "%sMark: %i\n", | |
484 | prefix, s->mark); | |
485 | ||
916abb21 LP |
486 | if (s->mq_maxmsg > 0) |
487 | fprintf(f, | |
488 | "%sMessageQueueMaxMessages: %li\n", | |
489 | prefix, s->mq_maxmsg); | |
490 | ||
491 | if (s->mq_msgsize > 0) | |
492 | fprintf(f, | |
493 | "%sMessageQueueMessageSize: %li\n", | |
494 | prefix, s->mq_msgsize); | |
495 | ||
034c6ed7 | 496 | LIST_FOREACH(port, p, s->ports) { |
5cb5a6ff | 497 | |
542563ba LP |
498 | if (p->type == SOCKET_SOCKET) { |
499 | const char *t; | |
500 | int r; | |
e364ad06 | 501 | char *k = NULL; |
542563ba LP |
502 | |
503 | if ((r = socket_address_print(&p->address, &k)) < 0) | |
504 | t = strerror(-r); | |
505 | else | |
506 | t = k; | |
507 | ||
7a22745a | 508 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t); |
542563ba | 509 | free(k); |
b0a3f2bc LP |
510 | } else if (p->type == SOCKET_SPECIAL) |
511 | fprintf(f, "%sListenSpecial: %s\n", prefix, p->path); | |
916abb21 LP |
512 | else if (p->type == SOCKET_MQUEUE) |
513 | fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path); | |
b0a3f2bc | 514 | else |
542563ba LP |
515 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); |
516 | } | |
5cb5a6ff LP |
517 | |
518 | exec_context_dump(&s->exec_context, f, prefix); | |
519 | ||
e537352b | 520 | for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { |
c43d20a0 LP |
521 | if (!s->exec_command[c]) |
522 | continue; | |
5cb5a6ff | 523 | |
40d50879 | 524 | fprintf(f, "%s-> %s:\n", |
a16e1123 | 525 | prefix, socket_exec_command_to_string(c)); |
c43d20a0 LP |
526 | |
527 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 528 | } |
c43d20a0 | 529 | |
82ba9f08 | 530 | free(p2); |
5cb5a6ff LP |
531 | } |
532 | ||
4f2d528d LP |
533 | static int instance_from_socket(int fd, unsigned nr, char **instance) { |
534 | socklen_t l; | |
535 | char *r; | |
536 | union { | |
537 | struct sockaddr sa; | |
538 | struct sockaddr_un un; | |
539 | struct sockaddr_in in; | |
540 | struct sockaddr_in6 in6; | |
541 | struct sockaddr_storage storage; | |
542 | } local, remote; | |
543 | ||
544 | assert(fd >= 0); | |
545 | assert(instance); | |
546 | ||
547 | l = sizeof(local); | |
548 | if (getsockname(fd, &local.sa, &l) < 0) | |
549 | return -errno; | |
550 | ||
551 | l = sizeof(remote); | |
552 | if (getpeername(fd, &remote.sa, &l) < 0) | |
553 | return -errno; | |
554 | ||
555 | switch (local.sa.sa_family) { | |
556 | ||
557 | case AF_INET: { | |
558 | uint32_t | |
559 | a = ntohl(local.in.sin_addr.s_addr), | |
560 | b = ntohl(remote.in.sin_addr.s_addr); | |
561 | ||
562 | if (asprintf(&r, | |
b15bdda8 | 563 | "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
4f2d528d LP |
564 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, |
565 | ntohs(local.in.sin_port), | |
566 | b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF, | |
567 | ntohs(remote.in.sin_port)) < 0) | |
568 | return -ENOMEM; | |
569 | ||
570 | break; | |
571 | } | |
572 | ||
573 | case AF_INET6: { | |
2b061f5a LP |
574 | static const char ipv4_prefix[] = { |
575 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |
576 | }; | |
577 | ||
578 | if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 && | |
579 | memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
580 | const uint8_t | |
581 | *a = local.in6.sin6_addr.s6_addr+12, | |
582 | *b = remote.in6.sin6_addr.s6_addr+12; | |
583 | ||
584 | if (asprintf(&r, | |
b15bdda8 | 585 | "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
2b061f5a LP |
586 | a[0], a[1], a[2], a[3], |
587 | ntohs(local.in6.sin6_port), | |
588 | b[0], b[1], b[2], b[3], | |
589 | ntohs(remote.in6.sin6_port)) < 0) | |
590 | return -ENOMEM; | |
591 | } else { | |
592 | char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN]; | |
593 | ||
594 | if (asprintf(&r, | |
b15bdda8 | 595 | "%s:%u-%s:%u", |
2b061f5a LP |
596 | inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)), |
597 | ntohs(local.in6.sin6_port), | |
598 | inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)), | |
599 | ntohs(remote.in6.sin6_port)) < 0) | |
600 | return -ENOMEM; | |
601 | } | |
4f2d528d LP |
602 | |
603 | break; | |
604 | } | |
605 | ||
606 | case AF_UNIX: { | |
607 | struct ucred ucred; | |
608 | ||
609 | l = sizeof(ucred); | |
610 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) | |
611 | return -errno; | |
612 | ||
613 | if (asprintf(&r, | |
bb00e604 | 614 | "%u-%lu-%lu", |
4f2d528d | 615 | nr, |
bb00e604 LP |
616 | (unsigned long) ucred.pid, |
617 | (unsigned long) ucred.uid) < 0) | |
4f2d528d LP |
618 | return -ENOMEM; |
619 | ||
620 | break; | |
621 | } | |
622 | ||
623 | default: | |
624 | assert_not_reached("Unhandled socket type."); | |
625 | } | |
626 | ||
627 | *instance = r; | |
628 | return 0; | |
629 | } | |
630 | ||
034c6ed7 | 631 | static void socket_close_fds(Socket *s) { |
83c60c9f LP |
632 | SocketPort *p; |
633 | ||
634 | assert(s); | |
635 | ||
034c6ed7 | 636 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f LP |
637 | if (p->fd < 0) |
638 | continue; | |
639 | ||
acbb0225 | 640 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
a16e1123 LP |
641 | close_nointr_nofail(p->fd); |
642 | ||
643 | /* One little note: we should never delete any sockets | |
644 | * in the file system here! After all some other | |
645 | * process we spawned might still have a reference of | |
646 | * this fd and wants to continue to use it. Therefore | |
647 | * we delete sockets in the file system before we | |
648 | * create a new one, not after we stopped using | |
649 | * one! */ | |
9152c765 | 650 | |
83c60c9f LP |
651 | p->fd = -1; |
652 | } | |
653 | } | |
654 | ||
4fd5948e LP |
655 | static void socket_apply_socket_options(Socket *s, int fd) { |
656 | assert(s); | |
657 | assert(fd >= 0); | |
658 | ||
659 | if (s->keep_alive) { | |
660 | int b = s->keep_alive; | |
661 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0) | |
662 | log_warning("SO_KEEPALIVE failed: %m"); | |
663 | } | |
664 | ||
ec6370a2 LP |
665 | if (s->broadcast) { |
666 | int one = 1; | |
667 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0) | |
668 | log_warning("SO_BROADCAST failed: %m"); | |
669 | } | |
670 | ||
d68af586 MS |
671 | if (s->pass_cred) { |
672 | int one = 1; | |
673 | if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) | |
674 | log_warning("SO_PASSCRED failed: %m"); | |
675 | } | |
676 | ||
4fd5948e LP |
677 | if (s->priority >= 0) |
678 | if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0) | |
679 | log_warning("SO_PRIORITY failed: %m"); | |
680 | ||
681 | if (s->receive_buffer > 0) { | |
682 | int value = (int) s->receive_buffer; | |
b8cef44e KS |
683 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0) |
684 | log_warning("SO_RCVBUFFORCE failed: %m"); | |
4fd5948e LP |
685 | } |
686 | ||
687 | if (s->send_buffer > 0) { | |
688 | int value = (int) s->send_buffer; | |
b8cef44e KS |
689 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0) |
690 | log_warning("SO_SNDBUFFORCE failed: %m"); | |
4fd5948e LP |
691 | } |
692 | ||
693 | if (s->mark >= 0) | |
694 | if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0) | |
695 | log_warning("SO_MARK failed: %m"); | |
696 | ||
697 | if (s->ip_tos >= 0) | |
698 | if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0) | |
699 | log_warning("IP_TOS failed: %m"); | |
700 | ||
46925ac5 LP |
701 | if (s->ip_ttl >= 0) { |
702 | int r, x; | |
703 | ||
704 | r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl)); | |
5bfcc1c6 FF |
705 | |
706 | if (socket_ipv6_is_supported()) | |
707 | x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl)); | |
708 | else { | |
709 | x = -1; | |
710 | errno = EAFNOSUPPORT; | |
711 | } | |
46925ac5 LP |
712 | |
713 | if (r < 0 && x < 0) | |
714 | log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m"); | |
715 | } | |
cebf8b20 TT |
716 | |
717 | if (s->tcp_congestion) | |
718 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
719 | log_warning("TCP_CONGESTION failed: %m"); | |
4fd5948e LP |
720 | } |
721 | ||
b15bdda8 | 722 | static void socket_apply_fifo_options(Socket *s, int fd) { |
4fd5948e LP |
723 | assert(s); |
724 | assert(fd >= 0); | |
725 | ||
726 | if (s->pipe_size > 0) | |
727 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
728 | log_warning("F_SETPIPE_SZ: %m"); | |
729 | } | |
730 | ||
b15bdda8 LP |
731 | static int fifo_address_create( |
732 | const char *path, | |
733 | mode_t directory_mode, | |
734 | mode_t socket_mode, | |
b15bdda8 LP |
735 | int *_fd) { |
736 | ||
7a58bfa4 | 737 | int fd = -1, r = 0; |
b15bdda8 LP |
738 | struct stat st; |
739 | mode_t old_mask; | |
740 | ||
741 | assert(path); | |
742 | assert(_fd); | |
743 | ||
744 | mkdir_parents(path, directory_mode); | |
745 | ||
c4e2ceae | 746 | if ((r = label_fifofile_set(path)) < 0) |
56cf987f | 747 | goto fail; |
b15bdda8 LP |
748 | |
749 | /* Enforce the right access mode for the fifo */ | |
750 | old_mask = umask(~ socket_mode); | |
751 | ||
752 | /* Include the original umask in our mask */ | |
753 | umask(~socket_mode | old_mask); | |
754 | ||
755 | r = mkfifo(path, socket_mode); | |
756 | umask(old_mask); | |
757 | ||
94bc2731 | 758 | if (r < 0 && errno != EEXIST) { |
b15bdda8 LP |
759 | r = -errno; |
760 | goto fail; | |
761 | } | |
762 | ||
763 | if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
764 | r = -errno; | |
765 | goto fail; | |
766 | } | |
767 | ||
56cf987f | 768 | label_file_clear(); |
7a58bfa4 | 769 | |
b15bdda8 LP |
770 | if (fstat(fd, &st) < 0) { |
771 | r = -errno; | |
772 | goto fail; | |
773 | } | |
774 | ||
775 | if (!S_ISFIFO(st.st_mode) || | |
de0200fc | 776 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || |
b15bdda8 LP |
777 | st.st_uid != getuid() || |
778 | st.st_gid != getgid()) { | |
779 | ||
780 | r = -EEXIST; | |
781 | goto fail; | |
782 | } | |
783 | ||
784 | *_fd = fd; | |
785 | return 0; | |
786 | ||
787 | fail: | |
56cf987f DW |
788 | label_file_clear(); |
789 | ||
b15bdda8 LP |
790 | if (fd >= 0) |
791 | close_nointr_nofail(fd); | |
792 | ||
793 | return r; | |
794 | } | |
795 | ||
b0a3f2bc LP |
796 | static int special_address_create( |
797 | const char *path, | |
798 | int *_fd) { | |
799 | ||
800 | int fd = -1, r = 0; | |
801 | struct stat st; | |
802 | ||
803 | assert(path); | |
804 | assert(_fd); | |
805 | ||
806 | if ((fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) { | |
807 | r = -errno; | |
808 | goto fail; | |
809 | } | |
810 | ||
811 | if (fstat(fd, &st) < 0) { | |
812 | r = -errno; | |
813 | goto fail; | |
814 | } | |
815 | ||
816 | /* Check whether this is a /proc, /sys or /dev file or char device */ | |
817 | if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) { | |
818 | r = -EEXIST; | |
819 | goto fail; | |
820 | } | |
821 | ||
822 | *_fd = fd; | |
823 | return 0; | |
824 | ||
825 | fail: | |
826 | if (fd >= 0) | |
827 | close_nointr_nofail(fd); | |
828 | ||
829 | return r; | |
830 | } | |
831 | ||
916abb21 LP |
832 | static int mq_address_create( |
833 | const char *path, | |
834 | mode_t mq_mode, | |
835 | long maxmsg, | |
836 | long msgsize, | |
837 | int *_fd) { | |
838 | ||
839 | int fd = -1, r = 0; | |
840 | struct stat st; | |
841 | mode_t old_mask; | |
842 | struct mq_attr _attr, *attr = NULL; | |
843 | ||
844 | assert(path); | |
845 | assert(_fd); | |
846 | ||
847 | if (maxmsg > 0 && msgsize > 0) { | |
848 | zero(_attr); | |
849 | _attr.mq_flags = O_NONBLOCK; | |
850 | _attr.mq_maxmsg = maxmsg; | |
851 | _attr.mq_msgsize = msgsize; | |
852 | attr = &_attr; | |
853 | } | |
854 | ||
855 | /* Enforce the right access mode for the mq */ | |
856 | old_mask = umask(~ mq_mode); | |
857 | ||
858 | /* Include the original umask in our mask */ | |
859 | umask(~mq_mode | old_mask); | |
860 | ||
861 | fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr); | |
862 | umask(old_mask); | |
863 | ||
8ea913b2 | 864 | if (fd < 0) { |
916abb21 LP |
865 | r = -errno; |
866 | goto fail; | |
867 | } | |
868 | ||
869 | if (fstat(fd, &st) < 0) { | |
870 | r = -errno; | |
871 | goto fail; | |
872 | } | |
873 | ||
874 | if ((st.st_mode & 0777) != (mq_mode & ~old_mask) || | |
875 | st.st_uid != getuid() || | |
876 | st.st_gid != getgid()) { | |
877 | ||
878 | r = -EEXIST; | |
879 | goto fail; | |
880 | } | |
881 | ||
882 | *_fd = fd; | |
883 | return 0; | |
884 | ||
885 | fail: | |
886 | if (fd >= 0) | |
887 | close_nointr_nofail(fd); | |
888 | ||
889 | return r; | |
890 | } | |
891 | ||
034c6ed7 | 892 | static int socket_open_fds(Socket *s) { |
83c60c9f LP |
893 | SocketPort *p; |
894 | int r; | |
56cf987f | 895 | char *label = NULL; |
049f8642 | 896 | bool know_label = false; |
83c60c9f LP |
897 | |
898 | assert(s); | |
899 | ||
034c6ed7 | 900 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f | 901 | |
034c6ed7 LP |
902 | if (p->fd >= 0) |
903 | continue; | |
83c60c9f LP |
904 | |
905 | if (p->type == SOCKET_SOCKET) { | |
906 | ||
049f8642 LP |
907 | if (!know_label) { |
908 | ||
909 | if ((r = socket_instantiate_service(s)) < 0) | |
910 | return r; | |
911 | ||
57020a3a LP |
912 | if (UNIT_DEREF(s->service) && |
913 | SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) { | |
914 | r = label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label); | |
189583d7 LP |
915 | |
916 | if (r < 0) { | |
a7444eda LP |
917 | if (r != -EPERM) |
918 | return r; | |
919 | } | |
189583d7 | 920 | } |
049f8642 LP |
921 | |
922 | know_label = true; | |
923 | } | |
924 | ||
b5a0699f LP |
925 | if ((r = socket_address_listen( |
926 | &p->address, | |
927 | s->backlog, | |
928 | s->bind_ipv6_only, | |
929 | s->bind_to_device, | |
4fd5948e | 930 | s->free_bind, |
6b6d2dee | 931 | s->transparent, |
b5a0699f LP |
932 | s->directory_mode, |
933 | s->socket_mode, | |
56cf987f | 934 | label, |
b5a0699f | 935 | &p->fd)) < 0) |
83c60c9f LP |
936 | goto rollback; |
937 | ||
4fd5948e LP |
938 | socket_apply_socket_options(s, p->fd); |
939 | ||
b0a3f2bc LP |
940 | } else if (p->type == SOCKET_SPECIAL) { |
941 | ||
942 | if ((r = special_address_create( | |
943 | p->path, | |
944 | &p->fd)) < 0) | |
945 | goto rollback; | |
946 | ||
b15bdda8 | 947 | } else if (p->type == SOCKET_FIFO) { |
83c60c9f | 948 | |
b15bdda8 LP |
949 | if ((r = fifo_address_create( |
950 | p->path, | |
951 | s->directory_mode, | |
952 | s->socket_mode, | |
b15bdda8 | 953 | &p->fd)) < 0) |
83c60c9f | 954 | goto rollback; |
83c60c9f | 955 | |
b15bdda8 | 956 | socket_apply_fifo_options(s, p->fd); |
916abb21 | 957 | } else if (p->type == SOCKET_MQUEUE) { |
83c60c9f | 958 | |
916abb21 LP |
959 | if ((r = mq_address_create( |
960 | p->path, | |
961 | s->socket_mode, | |
962 | s->mq_maxmsg, | |
963 | s->mq_msgsize, | |
964 | &p->fd)) < 0) | |
965 | goto rollback; | |
b15bdda8 LP |
966 | } else |
967 | assert_not_reached("Unknown port type"); | |
034c6ed7 LP |
968 | } |
969 | ||
56cf987f | 970 | label_free(label); |
034c6ed7 LP |
971 | return 0; |
972 | ||
973 | rollback: | |
974 | socket_close_fds(s); | |
56cf987f | 975 | label_free(label); |
034c6ed7 LP |
976 | return r; |
977 | } | |
978 | ||
979 | static void socket_unwatch_fds(Socket *s) { | |
980 | SocketPort *p; | |
9152c765 | 981 | |
034c6ed7 LP |
982 | assert(s); |
983 | ||
984 | LIST_FOREACH(port, p, s->ports) { | |
985 | if (p->fd < 0) | |
986 | continue; | |
987 | ||
acbb0225 | 988 | unit_unwatch_fd(UNIT(s), &p->fd_watch); |
83c60c9f | 989 | } |
034c6ed7 LP |
990 | } |
991 | ||
992 | static int socket_watch_fds(Socket *s) { | |
993 | SocketPort *p; | |
994 | int r; | |
995 | ||
996 | assert(s); | |
83c60c9f | 997 | |
034c6ed7 LP |
998 | LIST_FOREACH(port, p, s->ports) { |
999 | if (p->fd < 0) | |
1000 | continue; | |
1001 | ||
cabab516 | 1002 | p->fd_watch.socket_accept = |
4f2d528d | 1003 | s->accept && |
dd5ad9d4 | 1004 | p->type == SOCKET_SOCKET && |
4f2d528d LP |
1005 | socket_address_can_accept(&p->address); |
1006 | ||
f94ea366 | 1007 | if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) |
034c6ed7 LP |
1008 | goto fail; |
1009 | } | |
83c60c9f | 1010 | |
542563ba | 1011 | return 0; |
83c60c9f | 1012 | |
034c6ed7 LP |
1013 | fail: |
1014 | socket_unwatch_fds(s); | |
1015 | return r; | |
1016 | } | |
1017 | ||
1018 | static void socket_set_state(Socket *s, SocketState state) { | |
1019 | SocketState old_state; | |
1020 | assert(s); | |
1021 | ||
1022 | old_state = s->state; | |
1023 | s->state = state; | |
1024 | ||
1025 | if (state != SOCKET_START_PRE && | |
1026 | state != SOCKET_START_POST && | |
1027 | state != SOCKET_STOP_PRE && | |
1028 | state != SOCKET_STOP_PRE_SIGTERM && | |
1029 | state != SOCKET_STOP_PRE_SIGKILL && | |
1030 | state != SOCKET_STOP_POST && | |
80876c20 LP |
1031 | state != SOCKET_FINAL_SIGTERM && |
1032 | state != SOCKET_FINAL_SIGKILL) { | |
acbb0225 | 1033 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
5e94833f | 1034 | socket_unwatch_control_pid(s); |
034c6ed7 | 1035 | s->control_command = NULL; |
a16e1123 | 1036 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
e537352b | 1037 | } |
034c6ed7 | 1038 | |
a16e1123 LP |
1039 | if (state != SOCKET_LISTENING) |
1040 | socket_unwatch_fds(s); | |
1041 | ||
034c6ed7 LP |
1042 | if (state != SOCKET_START_POST && |
1043 | state != SOCKET_LISTENING && | |
1044 | state != SOCKET_RUNNING && | |
1045 | state != SOCKET_STOP_PRE && | |
1046 | state != SOCKET_STOP_PRE_SIGTERM && | |
1047 | state != SOCKET_STOP_PRE_SIGKILL) | |
1048 | socket_close_fds(s); | |
1049 | ||
e537352b | 1050 | if (state != old_state) |
40d50879 | 1051 | log_debug("%s changed %s -> %s", |
a16e1123 LP |
1052 | s->meta.id, |
1053 | socket_state_to_string(old_state), | |
1054 | socket_state_to_string(state)); | |
acbb0225 | 1055 | |
e2f3b44c | 1056 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true); |
034c6ed7 LP |
1057 | } |
1058 | ||
a16e1123 LP |
1059 | static int socket_coldplug(Unit *u) { |
1060 | Socket *s = SOCKET(u); | |
1061 | int r; | |
1062 | ||
1063 | assert(s); | |
1064 | assert(s->state == SOCKET_DEAD); | |
1065 | ||
1066 | if (s->deserialized_state != s->state) { | |
1067 | ||
1068 | if (s->deserialized_state == SOCKET_START_PRE || | |
1069 | s->deserialized_state == SOCKET_START_POST || | |
1070 | s->deserialized_state == SOCKET_STOP_PRE || | |
1071 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
1072 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || | |
1073 | s->deserialized_state == SOCKET_STOP_POST || | |
1074 | s->deserialized_state == SOCKET_FINAL_SIGTERM || | |
1075 | s->deserialized_state == SOCKET_FINAL_SIGKILL) { | |
1076 | ||
1077 | if (s->control_pid <= 0) | |
1078 | return -EBADMSG; | |
1079 | ||
1080 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
1081 | return r; | |
1082 | ||
1083 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
1084 | return r; | |
1085 | } | |
1086 | ||
1087 | if (s->deserialized_state == SOCKET_START_POST || | |
1088 | s->deserialized_state == SOCKET_LISTENING || | |
1089 | s->deserialized_state == SOCKET_RUNNING || | |
1090 | s->deserialized_state == SOCKET_STOP_PRE || | |
1091 | s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || | |
1092 | s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) | |
1093 | if ((r = socket_open_fds(s)) < 0) | |
1094 | return r; | |
1095 | ||
1096 | if (s->deserialized_state == SOCKET_LISTENING) | |
1097 | if ((r = socket_watch_fds(s)) < 0) | |
1098 | return r; | |
1099 | ||
1100 | socket_set_state(s, s->deserialized_state); | |
1101 | } | |
1102 | ||
1103 | return 0; | |
1104 | } | |
1105 | ||
e537352b | 1106 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
034c6ed7 LP |
1107 | pid_t pid; |
1108 | int r; | |
9e2f7c11 | 1109 | char **argv; |
034c6ed7 LP |
1110 | |
1111 | assert(s); | |
1112 | assert(c); | |
1113 | assert(_pid); | |
1114 | ||
e537352b LP |
1115 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
1116 | goto fail; | |
034c6ed7 | 1117 | |
9e2f7c11 LP |
1118 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { |
1119 | r = -ENOMEM; | |
1120 | goto fail; | |
1121 | } | |
1122 | ||
1123 | r = exec_spawn(c, | |
1124 | argv, | |
1125 | &s->exec_context, | |
1126 | NULL, 0, | |
1137a57c | 1127 | s->meta.manager->environment, |
9e2f7c11 LP |
1128 | true, |
1129 | true, | |
1e3ad081 | 1130 | true, |
4cd1fbcc LP |
1131 | s->meta.manager->confirm_spawn, |
1132 | s->meta.cgroup_bondings, | |
ab1f0633 | 1133 | s->meta.cgroup_attributes, |
9e2f7c11 LP |
1134 | &pid); |
1135 | ||
1136 | strv_free(argv); | |
1137 | if (r < 0) | |
034c6ed7 LP |
1138 | goto fail; |
1139 | ||
87f0e418 | 1140 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
1141 | /* FIXME: we need to do something here */ |
1142 | goto fail; | |
83c60c9f | 1143 | |
034c6ed7 LP |
1144 | *_pid = pid; |
1145 | ||
1146 | return 0; | |
1147 | ||
1148 | fail: | |
e537352b | 1149 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
83c60c9f LP |
1150 | |
1151 | return r; | |
542563ba LP |
1152 | } |
1153 | ||
034c6ed7 LP |
1154 | static void socket_enter_dead(Socket *s, bool success) { |
1155 | assert(s); | |
1156 | ||
1157 | if (!success) | |
1158 | s->failure = true; | |
1159 | ||
fdf20a31 | 1160 | socket_set_state(s, s->failure ? SOCKET_FAILED : SOCKET_DEAD); |
034c6ed7 LP |
1161 | } |
1162 | ||
80876c20 LP |
1163 | static void socket_enter_signal(Socket *s, SocketState state, bool success); |
1164 | ||
034c6ed7 LP |
1165 | static void socket_enter_stop_post(Socket *s, bool success) { |
1166 | int r; | |
1167 | assert(s); | |
1168 | ||
1169 | if (!success) | |
1170 | s->failure = true; | |
1171 | ||
5e94833f LP |
1172 | socket_unwatch_control_pid(s); |
1173 | ||
a16e1123 LP |
1174 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
1175 | ||
80876c20 | 1176 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { |
e537352b | 1177 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1178 | goto fail; |
1179 | ||
80876c20 LP |
1180 | socket_set_state(s, SOCKET_STOP_POST); |
1181 | } else | |
1182 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true); | |
034c6ed7 LP |
1183 | |
1184 | return; | |
1185 | ||
1186 | fail: | |
6cf6bbc2 | 1187 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); |
80876c20 | 1188 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1189 | } |
1190 | ||
1191 | static void socket_enter_signal(Socket *s, SocketState state, bool success) { | |
1192 | int r; | |
ca949c9d LP |
1193 | Set *pid_set = NULL; |
1194 | bool wait_for_exit = false; | |
034c6ed7 LP |
1195 | |
1196 | assert(s); | |
1197 | ||
1198 | if (!success) | |
1199 | s->failure = true; | |
1200 | ||
2e22afe9 LP |
1201 | if (s->exec_context.kill_mode != KILL_NONE) { |
1202 | int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
034c6ed7 | 1203 | |
ca949c9d | 1204 | if (s->control_pid > 0) { |
cd25cce9 | 1205 | if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH) |
50159e6a | 1206 | |
ca949c9d LP |
1207 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); |
1208 | else | |
1209 | wait_for_exit = true; | |
034c6ed7 | 1210 | } |
50159e6a | 1211 | |
ca949c9d LP |
1212 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { |
1213 | ||
1214 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { | |
1215 | r = -ENOMEM; | |
50159e6a LP |
1216 | goto fail; |
1217 | } | |
ca949c9d LP |
1218 | |
1219 | /* Exclude the control pid from being killed via the cgroup */ | |
1220 | if (s->control_pid > 0) | |
1221 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
1222 | goto fail; | |
1223 | ||
430c18ed | 1224 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) { |
ca949c9d LP |
1225 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) |
1226 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
1227 | } else if (r > 0) | |
1228 | wait_for_exit = true; | |
1229 | ||
1230 | set_free(pid_set); | |
da19d5c1 | 1231 | pid_set = NULL; |
ca949c9d | 1232 | } |
d6ea93e3 | 1233 | } |
034c6ed7 | 1234 | |
ca949c9d | 1235 | if (wait_for_exit) { |
80876c20 LP |
1236 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
1237 | goto fail; | |
d6ea93e3 | 1238 | |
80876c20 LP |
1239 | socket_set_state(s, state); |
1240 | } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1241 | socket_enter_stop_post(s, true); | |
1242 | else | |
034c6ed7 LP |
1243 | socket_enter_dead(s, true); |
1244 | ||
1245 | return; | |
1246 | ||
1247 | fail: | |
9e2f7c11 | 1248 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1249 | |
1250 | if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) | |
1251 | socket_enter_stop_post(s, false); | |
1252 | else | |
1253 | socket_enter_dead(s, false); | |
ca949c9d LP |
1254 | |
1255 | if (pid_set) | |
1256 | set_free(pid_set); | |
034c6ed7 LP |
1257 | } |
1258 | ||
1259 | static void socket_enter_stop_pre(Socket *s, bool success) { | |
1260 | int r; | |
1261 | assert(s); | |
1262 | ||
1263 | if (!success) | |
1264 | s->failure = true; | |
1265 | ||
5e94833f LP |
1266 | socket_unwatch_control_pid(s); |
1267 | ||
a16e1123 LP |
1268 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
1269 | ||
80876c20 | 1270 | if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { |
e537352b | 1271 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1272 | goto fail; |
1273 | ||
80876c20 LP |
1274 | socket_set_state(s, SOCKET_STOP_PRE); |
1275 | } else | |
034c6ed7 LP |
1276 | socket_enter_stop_post(s, true); |
1277 | ||
1278 | return; | |
1279 | ||
1280 | fail: | |
6cf6bbc2 | 1281 | log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1282 | socket_enter_stop_post(s, false); |
1283 | } | |
1284 | ||
e9af15c3 LP |
1285 | static void socket_enter_listening(Socket *s) { |
1286 | int r; | |
1287 | assert(s); | |
1288 | ||
1289 | if ((r = socket_watch_fds(s)) < 0) { | |
9e2f7c11 | 1290 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); |
e9af15c3 LP |
1291 | goto fail; |
1292 | } | |
1293 | ||
1294 | socket_set_state(s, SOCKET_LISTENING); | |
1295 | return; | |
1296 | ||
1297 | fail: | |
1298 | socket_enter_stop_pre(s, false); | |
1299 | } | |
1300 | ||
034c6ed7 LP |
1301 | static void socket_enter_start_post(Socket *s) { |
1302 | int r; | |
1303 | assert(s); | |
1304 | ||
e9af15c3 | 1305 | if ((r = socket_open_fds(s)) < 0) { |
9e2f7c11 | 1306 | log_warning("%s failed to listen on sockets: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1307 | goto fail; |
1308 | } | |
1309 | ||
5e94833f LP |
1310 | socket_unwatch_control_pid(s); |
1311 | ||
a16e1123 LP |
1312 | s->control_command_id = SOCKET_EXEC_START_POST; |
1313 | ||
80876c20 | 1314 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { |
e537352b | 1315 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) { |
6cf6bbc2 | 1316 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1317 | goto fail; |
1318 | } | |
1319 | ||
80876c20 LP |
1320 | socket_set_state(s, SOCKET_START_POST); |
1321 | } else | |
e9af15c3 | 1322 | socket_enter_listening(s); |
034c6ed7 LP |
1323 | |
1324 | return; | |
1325 | ||
1326 | fail: | |
1327 | socket_enter_stop_pre(s, false); | |
1328 | } | |
1329 | ||
1330 | static void socket_enter_start_pre(Socket *s) { | |
1331 | int r; | |
1332 | assert(s); | |
1333 | ||
5e94833f LP |
1334 | socket_unwatch_control_pid(s); |
1335 | ||
a16e1123 LP |
1336 | s->control_command_id = SOCKET_EXEC_START_PRE; |
1337 | ||
80876c20 | 1338 | if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { |
e537352b | 1339 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1340 | goto fail; |
1341 | ||
80876c20 LP |
1342 | socket_set_state(s, SOCKET_START_PRE); |
1343 | } else | |
034c6ed7 LP |
1344 | socket_enter_start_post(s); |
1345 | ||
1346 | return; | |
1347 | ||
1348 | fail: | |
6cf6bbc2 | 1349 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1350 | socket_enter_dead(s, false); |
1351 | } | |
1352 | ||
4f2d528d | 1353 | static void socket_enter_running(Socket *s, int cfd) { |
034c6ed7 | 1354 | int r; |
398ef8ba | 1355 | DBusError error; |
034c6ed7 LP |
1356 | |
1357 | assert(s); | |
398ef8ba | 1358 | dbus_error_init(&error); |
034c6ed7 | 1359 | |
ba3e67a7 LP |
1360 | /* We don't take connections anymore if we are supposed to |
1361 | * shut down anyway */ | |
18ffdfda | 1362 | if (unit_pending_inactive(UNIT(s))) { |
5d909e3e LP |
1363 | log_debug("Suppressing connection request on %s since unit stop is scheduled.", s->meta.id); |
1364 | ||
7c610628 LP |
1365 | if (cfd >= 0) |
1366 | close_nointr_nofail(cfd); | |
1367 | else { | |
1368 | /* Flush all sockets by closing and reopening them */ | |
1369 | socket_close_fds(s); | |
1370 | ||
1371 | if ((r = socket_watch_fds(s)) < 0) { | |
1372 | log_warning("%s failed to watch sockets: %s", s->meta.id, strerror(-r)); | |
1373 | socket_enter_stop_pre(s, false); | |
1374 | } | |
1375 | } | |
1376 | ||
ba3e67a7 LP |
1377 | return; |
1378 | } | |
1379 | ||
4f2d528d | 1380 | if (cfd < 0) { |
57020a3a LP |
1381 | Iterator i; |
1382 | Unit *u; | |
f976f3f6 | 1383 | bool pending = false; |
f976f3f6 LP |
1384 | |
1385 | /* If there's already a start pending don't bother to | |
1386 | * do anything */ | |
57020a3a LP |
1387 | SET_FOREACH(u, s->meta.dependencies[UNIT_TRIGGERS], i) |
1388 | if (unit_pending_active(u)) { | |
1389 | pending = true; | |
1390 | break; | |
1391 | } | |
f976f3f6 LP |
1392 | |
1393 | if (!pending) | |
57020a3a | 1394 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, true, &error, NULL)) < 0) |
f976f3f6 | 1395 | goto fail; |
4f2d528d LP |
1396 | |
1397 | socket_set_state(s, SOCKET_RUNNING); | |
1398 | } else { | |
b4f10a5e | 1399 | char *prefix, *instance = NULL, *name; |
b15bdda8 | 1400 | Service *service; |
4f2d528d | 1401 | |
6cf6bbc2 LP |
1402 | if (s->n_connections >= s->max_connections) { |
1403 | log_warning("Too many incoming connections (%u)", s->n_connections); | |
1404 | close_nointr_nofail(cfd); | |
1405 | return; | |
1406 | } | |
1407 | ||
b15bdda8 LP |
1408 | if ((r = socket_instantiate_service(s)) < 0) |
1409 | goto fail; | |
1410 | ||
1411 | if ((r = instance_from_socket(cfd, s->n_accepted, &instance)) < 0) | |
4f2d528d LP |
1412 | goto fail; |
1413 | ||
4cd1fbcc | 1414 | if (!(prefix = unit_name_to_prefix(s->meta.id))) { |
4f2d528d LP |
1415 | free(instance); |
1416 | r = -ENOMEM; | |
1417 | goto fail; | |
1418 | } | |
1419 | ||
1420 | name = unit_name_build(prefix, instance, ".service"); | |
1421 | free(prefix); | |
1422 | free(instance); | |
1423 | ||
b6dbbe1c | 1424 | if (!name) { |
4f2d528d | 1425 | r = -ENOMEM; |
b6dbbe1c LP |
1426 | goto fail; |
1427 | } | |
4f2d528d | 1428 | |
57020a3a | 1429 | if ((r = unit_add_name(UNIT_DEREF(s->service), name)) < 0) { |
b15bdda8 | 1430 | free(name); |
4f2d528d | 1431 | goto fail; |
b15bdda8 LP |
1432 | } |
1433 | ||
57020a3a LP |
1434 | service = SERVICE(UNIT_DEREF(s->service)); |
1435 | unit_ref_unset(&s->service); | |
b15bdda8 | 1436 | s->n_accepted ++; |
4f2d528d | 1437 | |
6c073082 LP |
1438 | service->meta.no_gc = false; |
1439 | ||
b15bdda8 LP |
1440 | unit_choose_id(UNIT(service), name); |
1441 | free(name); | |
1442 | ||
1443 | if ((r = service_set_socket_fd(service, cfd, s)) < 0) | |
4f2d528d LP |
1444 | goto fail; |
1445 | ||
1446 | cfd = -1; | |
6cf6bbc2 LP |
1447 | s->n_connections ++; |
1448 | ||
b15bdda8 | 1449 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL)) < 0) |
4f2d528d | 1450 | goto fail; |
c4e2ceae LP |
1451 | |
1452 | /* Notify clients about changed counters */ | |
1453 | unit_add_to_dbus_queue(UNIT(s)); | |
4f2d528d | 1454 | } |
034c6ed7 | 1455 | |
034c6ed7 LP |
1456 | return; |
1457 | ||
1458 | fail: | |
398ef8ba | 1459 | log_warning("%s failed to queue socket startup job: %s", s->meta.id, bus_error(&error, r)); |
80876c20 | 1460 | socket_enter_stop_pre(s, false); |
4f2d528d LP |
1461 | |
1462 | if (cfd >= 0) | |
1463 | close_nointr_nofail(cfd); | |
398ef8ba LP |
1464 | |
1465 | dbus_error_free(&error); | |
034c6ed7 LP |
1466 | } |
1467 | ||
1468 | static void socket_run_next(Socket *s, bool success) { | |
1469 | int r; | |
1470 | ||
1471 | assert(s); | |
1472 | assert(s->control_command); | |
1473 | assert(s->control_command->command_next); | |
1474 | ||
1475 | if (!success) | |
1476 | s->failure = true; | |
1477 | ||
5e94833f LP |
1478 | socket_unwatch_control_pid(s); |
1479 | ||
034c6ed7 LP |
1480 | s->control_command = s->control_command->command_next; |
1481 | ||
e537352b | 1482 | if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) |
034c6ed7 LP |
1483 | goto fail; |
1484 | ||
1485 | return; | |
1486 | ||
1487 | fail: | |
6cf6bbc2 | 1488 | log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r)); |
80876c20 LP |
1489 | |
1490 | if (s->state == SOCKET_START_POST) | |
1491 | socket_enter_stop_pre(s, false); | |
034c6ed7 LP |
1492 | else if (s->state == SOCKET_STOP_POST) |
1493 | socket_enter_dead(s, false); | |
1494 | else | |
80876c20 | 1495 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1496 | } |
1497 | ||
87f0e418 LP |
1498 | static int socket_start(Unit *u) { |
1499 | Socket *s = SOCKET(u); | |
83c60c9f LP |
1500 | |
1501 | assert(s); | |
1502 | ||
034c6ed7 LP |
1503 | /* We cannot fulfill this request right now, try again later |
1504 | * please! */ | |
1505 | if (s->state == SOCKET_STOP_PRE || | |
1506 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1507 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1508 | s->state == SOCKET_STOP_POST || | |
80876c20 LP |
1509 | s->state == SOCKET_FINAL_SIGTERM || |
1510 | s->state == SOCKET_FINAL_SIGKILL) | |
034c6ed7 LP |
1511 | return -EAGAIN; |
1512 | ||
83c60c9f LP |
1513 | if (s->state == SOCKET_START_PRE || |
1514 | s->state == SOCKET_START_POST) | |
034c6ed7 | 1515 | return 0; |
83c60c9f | 1516 | |
034c6ed7 | 1517 | /* Cannot run this without the service being around */ |
57020a3a LP |
1518 | if (UNIT_DEREF(s->service)) { |
1519 | Service *service; | |
1520 | ||
1521 | service = SERVICE(UNIT_DEREF(s->service)); | |
1522 | ||
1523 | if (service->meta.load_state != UNIT_LOADED) { | |
1524 | log_error("Socket service %s not loaded, refusing.", service->meta.id); | |
4f2d528d | 1525 | return -ENOENT; |
4ac9236f | 1526 | } |
4f2d528d | 1527 | |
35b8ca3a | 1528 | /* If the service is already active we cannot start the |
4f2d528d | 1529 | * socket */ |
57020a3a LP |
1530 | if (service->state != SERVICE_DEAD && |
1531 | service->state != SERVICE_FAILED && | |
1532 | service->state != SERVICE_AUTO_RESTART) { | |
1533 | log_error("Socket service %s already active, refusing.", service->meta.id); | |
4f2d528d | 1534 | return -EBUSY; |
4ac9236f | 1535 | } |
7b4bf06b LP |
1536 | |
1537 | #ifdef HAVE_SYSV_COMPAT | |
57020a3a | 1538 | if (service->sysv_path) { |
7b4bf06b LP |
1539 | log_error("Using SysV services for socket activation is not supported. Refusing."); |
1540 | return -ENOENT; | |
1541 | } | |
1542 | #endif | |
4f2d528d | 1543 | } |
e537352b | 1544 | |
fdf20a31 | 1545 | assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED); |
83c60c9f | 1546 | |
034c6ed7 LP |
1547 | s->failure = false; |
1548 | socket_enter_start_pre(s); | |
1549 | return 0; | |
1550 | } | |
83c60c9f | 1551 | |
87f0e418 LP |
1552 | static int socket_stop(Unit *u) { |
1553 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
1554 | |
1555 | assert(s); | |
1556 | ||
e537352b LP |
1557 | /* Already on it */ |
1558 | if (s->state == SOCKET_STOP_PRE || | |
1559 | s->state == SOCKET_STOP_PRE_SIGTERM || | |
1560 | s->state == SOCKET_STOP_PRE_SIGKILL || | |
1561 | s->state == SOCKET_STOP_POST || | |
80876c20 | 1562 | s->state == SOCKET_FINAL_SIGTERM || |
3f6c78dc | 1563 | s->state == SOCKET_FINAL_SIGKILL) |
e537352b LP |
1564 | return 0; |
1565 | ||
3f6c78dc LP |
1566 | /* If there's already something running we go directly into |
1567 | * kill mode. */ | |
1568 | if (s->state == SOCKET_START_PRE || | |
1569 | s->state == SOCKET_START_POST) { | |
1570 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, true); | |
1571 | return -EAGAIN; | |
1572 | } | |
1573 | ||
034c6ed7 | 1574 | assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING); |
83c60c9f | 1575 | |
034c6ed7 | 1576 | socket_enter_stop_pre(s, true); |
542563ba LP |
1577 | return 0; |
1578 | } | |
1579 | ||
a16e1123 LP |
1580 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
1581 | Socket *s = SOCKET(u); | |
1582 | SocketPort *p; | |
1583 | int r; | |
1584 | ||
1585 | assert(u); | |
1586 | assert(f); | |
1587 | assert(fds); | |
1588 | ||
1589 | unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); | |
1590 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
1591 | unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); | |
1592 | ||
1593 | if (s->control_pid > 0) | |
5925dd3c | 1594 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 LP |
1595 | |
1596 | if (s->control_command_id >= 0) | |
1597 | unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
1598 | ||
1599 | LIST_FOREACH(port, p, s->ports) { | |
1600 | int copy; | |
1601 | ||
1602 | if (p->fd < 0) | |
1603 | continue; | |
1604 | ||
1605 | if ((copy = fdset_put_dup(fds, p->fd)) < 0) | |
1606 | return copy; | |
1607 | ||
1608 | if (p->type == SOCKET_SOCKET) { | |
1609 | char *t; | |
1610 | ||
1611 | if ((r = socket_address_print(&p->address, &t)) < 0) | |
1612 | return r; | |
1613 | ||
7a22745a LP |
1614 | if (socket_address_family(&p->address) == AF_NETLINK) |
1615 | unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t); | |
1616 | else | |
1617 | unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); | |
a16e1123 | 1618 | free(t); |
b0a3f2bc LP |
1619 | } else if (p->type == SOCKET_SPECIAL) |
1620 | unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path); | |
1621 | else { | |
a16e1123 LP |
1622 | assert(p->type == SOCKET_FIFO); |
1623 | unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); | |
1624 | } | |
1625 | } | |
1626 | ||
1627 | return 0; | |
1628 | } | |
1629 | ||
1630 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1631 | Socket *s = SOCKET(u); | |
a16e1123 LP |
1632 | |
1633 | assert(u); | |
1634 | assert(key); | |
1635 | assert(value); | |
1636 | assert(fds); | |
1637 | ||
1638 | if (streq(key, "state")) { | |
1639 | SocketState state; | |
1640 | ||
1641 | if ((state = socket_state_from_string(value)) < 0) | |
1642 | log_debug("Failed to parse state value %s", value); | |
1643 | else | |
1644 | s->deserialized_state = state; | |
1645 | } else if (streq(key, "failure")) { | |
1646 | int b; | |
1647 | ||
1648 | if ((b = parse_boolean(value)) < 0) | |
1649 | log_debug("Failed to parse failure value %s", value); | |
1650 | else | |
1651 | s->failure = b || s->failure; | |
1652 | ||
1653 | } else if (streq(key, "n-accepted")) { | |
1654 | unsigned k; | |
1655 | ||
e364ad06 | 1656 | if (safe_atou(value, &k) < 0) |
a16e1123 LP |
1657 | log_debug("Failed to parse n-accepted value %s", value); |
1658 | else | |
1659 | s->n_accepted += k; | |
1660 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 1661 | pid_t pid; |
a16e1123 | 1662 | |
e364ad06 | 1663 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
1664 | log_debug("Failed to parse control-pid value %s", value); |
1665 | else | |
5925dd3c | 1666 | s->control_pid = pid; |
a16e1123 LP |
1667 | } else if (streq(key, "control-command")) { |
1668 | SocketExecCommand id; | |
1669 | ||
1670 | if ((id = socket_exec_command_from_string(value)) < 0) | |
1671 | log_debug("Failed to parse exec-command value %s", value); | |
1672 | else { | |
1673 | s->control_command_id = id; | |
1674 | s->control_command = s->exec_command[id]; | |
1675 | } | |
1676 | } else if (streq(key, "fifo")) { | |
1677 | int fd, skip = 0; | |
1678 | SocketPort *p; | |
1679 | ||
1680 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
1681 | log_debug("Failed to parse fifo value %s", value); | |
1682 | else { | |
1683 | ||
1684 | LIST_FOREACH(port, p, s->ports) | |
b0a3f2bc LP |
1685 | if (p->type == SOCKET_FIFO && |
1686 | streq_ptr(p->path, value+skip)) | |
1687 | break; | |
1688 | ||
1689 | if (p) { | |
1690 | if (p->fd >= 0) | |
1691 | close_nointr_nofail(p->fd); | |
1692 | p->fd = fdset_remove(fds, fd); | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | } else if (streq(key, "special")) { | |
1697 | int fd, skip = 0; | |
1698 | SocketPort *p; | |
1699 | ||
1700 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
1701 | log_debug("Failed to parse special value %s", value); | |
1702 | else { | |
1703 | ||
1704 | LIST_FOREACH(port, p, s->ports) | |
1705 | if (p->type == SOCKET_SPECIAL && | |
1706 | streq_ptr(p->path, value+skip)) | |
a16e1123 LP |
1707 | break; |
1708 | ||
1709 | if (p) { | |
1710 | if (p->fd >= 0) | |
1711 | close_nointr_nofail(p->fd); | |
1712 | p->fd = fdset_remove(fds, fd); | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | } else if (streq(key, "socket")) { | |
27ca8d7a | 1717 | int fd, type, skip = 0; |
a16e1123 LP |
1718 | SocketPort *p; |
1719 | ||
27ca8d7a | 1720 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
a16e1123 LP |
1721 | log_debug("Failed to parse socket value %s", value); |
1722 | else { | |
1723 | ||
1724 | LIST_FOREACH(port, p, s->ports) | |
27ca8d7a | 1725 | if (socket_address_is(&p->address, value+skip, type)) |
a16e1123 LP |
1726 | break; |
1727 | ||
1728 | if (p) { | |
1729 | if (p->fd >= 0) | |
1730 | close_nointr_nofail(p->fd); | |
1731 | p->fd = fdset_remove(fds, fd); | |
1732 | } | |
1733 | } | |
1734 | ||
7a22745a LP |
1735 | } else if (streq(key, "netlink")) { |
1736 | int fd, skip = 0; | |
1737 | SocketPort *p; | |
1738 | ||
1739 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
1740 | log_debug("Failed to parse socket value %s", value); | |
1741 | else { | |
1742 | ||
1743 | LIST_FOREACH(port, p, s->ports) | |
1744 | if (socket_address_is_netlink(&p->address, value+skip)) | |
1745 | break; | |
1746 | ||
1747 | if (p) { | |
1748 | if (p->fd >= 0) | |
1749 | close_nointr_nofail(p->fd); | |
1750 | p->fd = fdset_remove(fds, fd); | |
1751 | } | |
1752 | } | |
1753 | ||
a16e1123 LP |
1754 | } else |
1755 | log_debug("Unknown serialization key '%s'", key); | |
1756 | ||
1757 | return 0; | |
1758 | } | |
1759 | ||
87f0e418 LP |
1760 | static UnitActiveState socket_active_state(Unit *u) { |
1761 | assert(u); | |
5cb5a6ff | 1762 | |
acbb0225 | 1763 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
1764 | } |
1765 | ||
10a94420 LP |
1766 | static const char *socket_sub_state_to_string(Unit *u) { |
1767 | assert(u); | |
1768 | ||
a16e1123 | 1769 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
1770 | } |
1771 | ||
6cf6bbc2 LP |
1772 | static bool socket_check_gc(Unit *u) { |
1773 | Socket *s = SOCKET(u); | |
1774 | ||
1775 | assert(u); | |
1776 | ||
1777 | return s->n_connections > 0; | |
1778 | } | |
1779 | ||
acbb0225 | 1780 | static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { |
87f0e418 | 1781 | Socket *s = SOCKET(u); |
4f2d528d | 1782 | int cfd = -1; |
9152c765 | 1783 | |
034c6ed7 | 1784 | assert(s); |
8d567588 | 1785 | assert(fd >= 0); |
9152c765 | 1786 | |
871d7de4 LP |
1787 | if (s->state != SOCKET_LISTENING) |
1788 | return; | |
1789 | ||
9e2f7c11 | 1790 | log_debug("Incoming traffic on %s", u->meta.id); |
9152c765 | 1791 | |
4f2d528d | 1792 | if (events != EPOLLIN) { |
641e01dc LP |
1793 | |
1794 | if (events & EPOLLHUP) | |
1795 | log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u->meta.id); | |
1796 | else | |
1797 | log_error("%s: Got unexpected poll event (0x%x) on socket.", u->meta.id, events); | |
1798 | ||
8d567588 | 1799 | goto fail; |
4f2d528d LP |
1800 | } |
1801 | ||
cabab516 | 1802 | if (w->socket_accept) { |
4f2d528d LP |
1803 | for (;;) { |
1804 | ||
1805 | if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) { | |
1806 | ||
1807 | if (errno == EINTR) | |
1808 | continue; | |
1809 | ||
1810 | log_error("Failed to accept socket: %m"); | |
8d567588 | 1811 | goto fail; |
4f2d528d LP |
1812 | } |
1813 | ||
1814 | break; | |
1815 | } | |
4fd5948e LP |
1816 | |
1817 | socket_apply_socket_options(s, cfd); | |
4f2d528d | 1818 | } |
9152c765 | 1819 | |
4f2d528d | 1820 | socket_enter_running(s, cfd); |
8d567588 LP |
1821 | return; |
1822 | ||
1823 | fail: | |
1824 | socket_enter_stop_pre(s, false); | |
9152c765 LP |
1825 | } |
1826 | ||
87f0e418 LP |
1827 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1828 | Socket *s = SOCKET(u); | |
034c6ed7 | 1829 | bool success; |
5cb5a6ff LP |
1830 | |
1831 | assert(s); | |
034c6ed7 | 1832 | assert(pid >= 0); |
5cb5a6ff | 1833 | |
8c47c732 LP |
1834 | if (pid != s->control_pid) |
1835 | return; | |
542563ba | 1836 | |
034c6ed7 LP |
1837 | s->control_pid = 0; |
1838 | ||
8c47c732 | 1839 | success = is_clean_exit(code, status); |
8c47c732 | 1840 | |
b708e7ce | 1841 | if (s->control_command) { |
6ea832a2 | 1842 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 1843 | |
b708e7ce LP |
1844 | if (s->control_command->ignore) |
1845 | success = true; | |
1846 | } | |
1847 | ||
92abbefb LP |
1848 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
1849 | "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
b708e7ce | 1850 | s->failure = s->failure || !success; |
034c6ed7 | 1851 | |
a16e1123 LP |
1852 | if (s->control_command && s->control_command->command_next && success) { |
1853 | log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state)); | |
034c6ed7 | 1854 | socket_run_next(s, success); |
acbb0225 | 1855 | } else { |
a16e1123 LP |
1856 | s->control_command = NULL; |
1857 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
1858 | ||
034c6ed7 LP |
1859 | /* No further commands for this step, so let's figure |
1860 | * out what to do next */ | |
5cb5a6ff | 1861 | |
a16e1123 | 1862 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state)); |
acbb0225 | 1863 | |
034c6ed7 LP |
1864 | switch (s->state) { |
1865 | ||
1866 | case SOCKET_START_PRE: | |
1867 | if (success) | |
acbb0225 | 1868 | socket_enter_start_post(s); |
034c6ed7 | 1869 | else |
80876c20 | 1870 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1871 | break; |
1872 | ||
1873 | case SOCKET_START_POST: | |
1874 | if (success) | |
e9af15c3 | 1875 | socket_enter_listening(s); |
034c6ed7 LP |
1876 | else |
1877 | socket_enter_stop_pre(s, false); | |
1878 | break; | |
1879 | ||
1880 | case SOCKET_STOP_PRE: | |
1881 | case SOCKET_STOP_PRE_SIGTERM: | |
1882 | case SOCKET_STOP_PRE_SIGKILL: | |
1883 | socket_enter_stop_post(s, success); | |
1884 | break; | |
1885 | ||
1886 | case SOCKET_STOP_POST: | |
80876c20 LP |
1887 | case SOCKET_FINAL_SIGTERM: |
1888 | case SOCKET_FINAL_SIGKILL: | |
034c6ed7 LP |
1889 | socket_enter_dead(s, success); |
1890 | break; | |
1891 | ||
1892 | default: | |
1893 | assert_not_reached("Uh, control process died at wrong time."); | |
1894 | } | |
1895 | } | |
c4e2ceae LP |
1896 | |
1897 | /* Notify clients about changed exit status */ | |
1898 | unit_add_to_dbus_queue(u); | |
034c6ed7 | 1899 | } |
5cb5a6ff | 1900 | |
acbb0225 | 1901 | static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) { |
87f0e418 | 1902 | Socket *s = SOCKET(u); |
5cb5a6ff | 1903 | |
034c6ed7 LP |
1904 | assert(s); |
1905 | assert(elapsed == 1); | |
acbb0225 | 1906 | assert(w == &s->timer_watch); |
034c6ed7 LP |
1907 | |
1908 | switch (s->state) { | |
1909 | ||
1910 | case SOCKET_START_PRE: | |
9e2f7c11 | 1911 | log_warning("%s starting timed out. Terminating.", u->meta.id); |
80876c20 | 1912 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
da19d5c1 | 1913 | break; |
80876c20 | 1914 | |
034c6ed7 | 1915 | case SOCKET_START_POST: |
9e2f7c11 | 1916 | log_warning("%s starting timed out. Stopping.", u->meta.id); |
034c6ed7 LP |
1917 | socket_enter_stop_pre(s, false); |
1918 | break; | |
1919 | ||
1920 | case SOCKET_STOP_PRE: | |
9e2f7c11 | 1921 | log_warning("%s stopping timed out. Terminating.", u->meta.id); |
034c6ed7 LP |
1922 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false); |
1923 | break; | |
1924 | ||
1925 | case SOCKET_STOP_PRE_SIGTERM: | |
ba035df2 LP |
1926 | if (s->exec_context.send_sigkill) { |
1927 | log_warning("%s stopping timed out. Killing.", u->meta.id); | |
1928 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false); | |
1929 | } else { | |
1930 | log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u->meta.id); | |
1931 | socket_enter_stop_post(s, false); | |
1932 | } | |
034c6ed7 LP |
1933 | break; |
1934 | ||
1935 | case SOCKET_STOP_PRE_SIGKILL: | |
9e2f7c11 | 1936 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); |
034c6ed7 LP |
1937 | socket_enter_stop_post(s, false); |
1938 | break; | |
1939 | ||
1940 | case SOCKET_STOP_POST: | |
9e2f7c11 | 1941 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); |
80876c20 | 1942 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); |
034c6ed7 LP |
1943 | break; |
1944 | ||
80876c20 | 1945 | case SOCKET_FINAL_SIGTERM: |
ba035df2 LP |
1946 | if (s->exec_context.send_sigkill) { |
1947 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); | |
1948 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false); | |
1949 | } else { | |
1950 | log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->meta.id); | |
1951 | socket_enter_dead(s, false); | |
1952 | } | |
034c6ed7 LP |
1953 | break; |
1954 | ||
80876c20 | 1955 | case SOCKET_FINAL_SIGKILL: |
fdf20a31 | 1956 | log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id); |
034c6ed7 LP |
1957 | socket_enter_dead(s, false); |
1958 | break; | |
1959 | ||
1960 | default: | |
1961 | assert_not_reached("Timeout at wrong time."); | |
1962 | } | |
5cb5a6ff LP |
1963 | } |
1964 | ||
44d8db9e LP |
1965 | int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { |
1966 | int *rfds; | |
1967 | unsigned rn_fds, k; | |
1968 | SocketPort *p; | |
1969 | ||
1970 | assert(s); | |
1971 | assert(fds); | |
1972 | assert(n_fds); | |
1973 | ||
1974 | /* Called from the service code for requesting our fds */ | |
1975 | ||
1976 | rn_fds = 0; | |
1977 | LIST_FOREACH(port, p, s->ports) | |
1978 | if (p->fd >= 0) | |
1979 | rn_fds++; | |
1980 | ||
de3756ab LP |
1981 | if (rn_fds <= 0) { |
1982 | *fds = NULL; | |
1983 | *n_fds = 0; | |
1984 | return 0; | |
1985 | } | |
1986 | ||
e364ad06 | 1987 | if (!(rfds = new(int, rn_fds))) |
44d8db9e LP |
1988 | return -ENOMEM; |
1989 | ||
1990 | k = 0; | |
1991 | LIST_FOREACH(port, p, s->ports) | |
1992 | if (p->fd >= 0) | |
1993 | rfds[k++] = p->fd; | |
1994 | ||
1995 | assert(k == rn_fds); | |
1996 | ||
1997 | *fds = rfds; | |
1998 | *n_fds = rn_fds; | |
1999 | ||
2000 | return 0; | |
2001 | } | |
2002 | ||
ceee3d82 LP |
2003 | void socket_notify_service_dead(Socket *s) { |
2004 | assert(s); | |
2005 | ||
6cf6bbc2 LP |
2006 | /* The service is dead. Dang! |
2007 | * | |
2008 | * This is strictly for one-instance-for-all-connections | |
2009 | * services. */ | |
ceee3d82 LP |
2010 | |
2011 | if (s->state == SOCKET_RUNNING) { | |
9e2f7c11 | 2012 | log_debug("%s got notified about service death.", s->meta.id); |
ceee3d82 LP |
2013 | socket_enter_listening(s); |
2014 | } | |
2015 | } | |
2016 | ||
6cf6bbc2 LP |
2017 | void socket_connection_unref(Socket *s) { |
2018 | assert(s); | |
2019 | ||
2020 | /* The service is dead. Yay! | |
2021 | * | |
35b8ca3a | 2022 | * This is strictly for one-instance-per-connection |
6cf6bbc2 LP |
2023 | * services. */ |
2024 | ||
2025 | assert(s->n_connections > 0); | |
2026 | s->n_connections--; | |
2027 | ||
2028 | log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections); | |
2029 | } | |
2030 | ||
fdf20a31 | 2031 | static void socket_reset_failed(Unit *u) { |
5632e374 LP |
2032 | Socket *s = SOCKET(u); |
2033 | ||
2034 | assert(s); | |
2035 | ||
fdf20a31 | 2036 | if (s->state == SOCKET_FAILED) |
5632e374 LP |
2037 | socket_set_state(s, SOCKET_DEAD); |
2038 | ||
2039 | s->failure = false; | |
2040 | } | |
2041 | ||
8a0867d6 LP |
2042 | static int socket_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) { |
2043 | Socket *s = SOCKET(u); | |
2044 | int r = 0; | |
2045 | Set *pid_set = NULL; | |
2046 | ||
2047 | assert(s); | |
2048 | ||
2049 | if (who == KILL_MAIN) { | |
2050 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Socket units have no main processes"); | |
a17204af | 2051 | return -ESRCH; |
8a0867d6 LP |
2052 | } |
2053 | ||
2054 | if (s->control_pid <= 0 && who == KILL_CONTROL) { | |
2055 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
a17204af | 2056 | return -ESRCH; |
8a0867d6 LP |
2057 | } |
2058 | ||
3611581e LP |
2059 | if (who == KILL_CONTROL || who == KILL_ALL) |
2060 | if (s->control_pid > 0) | |
2061 | if (kill(s->control_pid, signo) < 0) | |
2062 | r = -errno; | |
8a0867d6 | 2063 | |
3611581e | 2064 | if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) { |
8a0867d6 LP |
2065 | int q; |
2066 | ||
2067 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) | |
2068 | return -ENOMEM; | |
2069 | ||
2070 | /* Exclude the control pid from being killed via the cgroup */ | |
2071 | if (s->control_pid > 0) | |
2072 | if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) { | |
2073 | r = q; | |
2074 | goto finish; | |
2075 | } | |
2076 | ||
430c18ed | 2077 | if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0) |
3611581e | 2078 | if (q != -EAGAIN && q != -ESRCH && q != -ENOENT) |
8a0867d6 LP |
2079 | r = q; |
2080 | } | |
2081 | ||
2082 | finish: | |
2083 | if (pid_set) | |
2084 | set_free(pid_set); | |
2085 | ||
2086 | return r; | |
2087 | } | |
2088 | ||
a16e1123 LP |
2089 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { |
2090 | [SOCKET_DEAD] = "dead", | |
2091 | [SOCKET_START_PRE] = "start-pre", | |
2092 | [SOCKET_START_POST] = "start-post", | |
2093 | [SOCKET_LISTENING] = "listening", | |
2094 | [SOCKET_RUNNING] = "running", | |
2095 | [SOCKET_STOP_PRE] = "stop-pre", | |
2096 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", | |
2097 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
2098 | [SOCKET_STOP_POST] = "stop-post", | |
2099 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
2100 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 2101 | [SOCKET_FAILED] = "failed" |
a16e1123 LP |
2102 | }; |
2103 | ||
2104 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
2105 | ||
2106 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
2107 | [SOCKET_EXEC_START_PRE] = "StartPre", | |
2108 | [SOCKET_EXEC_START_POST] = "StartPost", | |
2109 | [SOCKET_EXEC_STOP_PRE] = "StopPre", | |
2110 | [SOCKET_EXEC_STOP_POST] = "StopPost" | |
2111 | }; | |
2112 | ||
2113 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
2114 | ||
87f0e418 | 2115 | const UnitVTable socket_vtable = { |
5cb5a6ff | 2116 | .suffix = ".socket", |
7d17cfbc | 2117 | .object_size = sizeof(Socket), |
f975e971 LP |
2118 | .sections = |
2119 | "Unit\0" | |
2120 | "Socket\0" | |
2121 | "Install\0", | |
5cb5a6ff | 2122 | |
034c6ed7 LP |
2123 | .init = socket_init, |
2124 | .done = socket_done, | |
a16e1123 LP |
2125 | .load = socket_load, |
2126 | ||
8a0867d6 LP |
2127 | .kill = socket_kill, |
2128 | ||
a16e1123 | 2129 | .coldplug = socket_coldplug, |
034c6ed7 | 2130 | |
5cb5a6ff LP |
2131 | .dump = socket_dump, |
2132 | ||
542563ba LP |
2133 | .start = socket_start, |
2134 | .stop = socket_stop, | |
5cb5a6ff | 2135 | |
a16e1123 LP |
2136 | .serialize = socket_serialize, |
2137 | .deserialize_item = socket_deserialize_item, | |
2138 | ||
5cb5a6ff | 2139 | .active_state = socket_active_state, |
10a94420 | 2140 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 2141 | |
6cf6bbc2 LP |
2142 | .check_gc = socket_check_gc, |
2143 | ||
9152c765 | 2144 | .fd_event = socket_fd_event, |
034c6ed7 | 2145 | .sigchld_event = socket_sigchld_event, |
4139c1b2 LP |
2146 | .timer_event = socket_timer_event, |
2147 | ||
fdf20a31 | 2148 | .reset_failed = socket_reset_failed, |
5632e374 | 2149 | |
c4e2ceae LP |
2150 | .bus_interface = "org.freedesktop.systemd1.Socket", |
2151 | .bus_message_handler = bus_socket_message_handler, | |
2152 | .bus_invalidating_properties = bus_socket_invalidating_properties | |
5cb5a6ff | 2153 | }; |