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