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