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