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