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