]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
e8da24a6 | 3 | #include <arpa/inet.h> |
83c60c9f LP |
4 | #include <errno.h> |
5 | #include <fcntl.h> | |
916abb21 | 6 | #include <mqueue.h> |
e8da24a6 | 7 | #include <netinet/tcp.h> |
e8da24a6 LP |
8 | #include <sys/epoll.h> |
9 | #include <sys/stat.h> | |
10 | #include <unistd.h> | |
62bc4efc | 11 | #include <linux/sctp.h> |
83c60c9f | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
a79279c7 | 14 | #include "bpf-firewall.h" |
e8da24a6 LP |
15 | #include "bus-error.h" |
16 | #include "bus-util.h" | |
17 | #include "copy.h" | |
18 | #include "dbus-socket.h" | |
6fcbec6f | 19 | #include "dbus-unit.h" |
e8da24a6 LP |
20 | #include "def.h" |
21 | #include "exit-status.h" | |
3ffd4af2 | 22 | #include "fd-util.h" |
f97b34a6 | 23 | #include "format-util.h" |
95f7fbbf | 24 | #include "fs-util.h" |
a79279c7 | 25 | #include "in-addr-util.h" |
60d9771c | 26 | #include "io-util.h" |
da96ad5a | 27 | #include "ip-protocol-list.h" |
e8da24a6 | 28 | #include "label.h" |
83c60c9f | 29 | #include "log.h" |
49e942b2 | 30 | #include "mkdir.h" |
6bedfcbb | 31 | #include "parse-util.h" |
9eb977db | 32 | #include "path-util.h" |
7b3e062c | 33 | #include "process-util.h" |
d7b8eec7 | 34 | #include "selinux-util.h" |
d68c645b | 35 | #include "serialize.h" |
24882e06 | 36 | #include "signal-util.h" |
e8da24a6 | 37 | #include "smack-util.h" |
d68c645b | 38 | #include "socket.h" |
5c3fa98d | 39 | #include "socket-netlink.h" |
e8da24a6 | 40 | #include "special.h" |
8b43440b | 41 | #include "string-table.h" |
07630cea | 42 | #include "string-util.h" |
e8da24a6 LP |
43 | #include "strv.h" |
44 | #include "unit-name.h" | |
e8da24a6 | 45 | #include "unit.h" |
b1d4f8e1 | 46 | #include "user-util.h" |
83c60c9f | 47 | |
166cf510 ZJS |
48 | struct SocketPeer { |
49 | unsigned n_ref; | |
50 | ||
51 | Socket *socket; | |
52 | union sockaddr_union peer; | |
41733ae1 | 53 | socklen_t peer_salen; |
166cf510 ZJS |
54 | }; |
55 | ||
acbb0225 | 56 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { |
87f0e418 LP |
57 | [SOCKET_DEAD] = UNIT_INACTIVE, |
58 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
3900e5fd | 59 | [SOCKET_START_CHOWN] = UNIT_ACTIVATING, |
87f0e418 LP |
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, | |
c968d76a YW |
69 | [SOCKET_FAILED] = UNIT_FAILED, |
70 | [SOCKET_CLEANING] = UNIT_MAINTENANCE, | |
83c60c9f | 71 | }; |
5cb5a6ff | 72 | |
718db961 LP |
73 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
74 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
75 | ||
a16e1123 LP |
76 | static void socket_init(Unit *u) { |
77 | Socket *s = SOCKET(u); | |
78 | ||
79 | assert(u); | |
ac155bb8 | 80 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 81 | |
a16e1123 | 82 | s->backlog = SOMAXCONN; |
1f19a534 | 83 | s->timeout_usec = u->manager->default_timeout_start_usec; |
a16e1123 | 84 | s->directory_mode = 0755; |
9131f660 | 85 | s->socket_mode = 0666; |
a16e1123 | 86 | |
6cf6bbc2 LP |
87 | s->max_connections = 64; |
88 | ||
4fd5948e | 89 | s->priority = -1; |
4fd5948e LP |
90 | s->ip_tos = -1; |
91 | s->ip_ttl = -1; | |
4fd5948e | 92 | s->mark = -1; |
4fd5948e | 93 | |
ac155bb8 MS |
94 | s->exec_context.std_output = u->manager->default_std_output; |
95 | s->exec_context.std_error = u->manager->default_std_error; | |
085afe36 | 96 | |
a16e1123 | 97 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
8b26cdbd | 98 | |
1f15ce28 LP |
99 | s->trigger_limit.interval = USEC_INFINITY; |
100 | s->trigger_limit.burst = (unsigned) -1; | |
a16e1123 | 101 | } |
acbb0225 | 102 | |
5e94833f LP |
103 | static void socket_unwatch_control_pid(Socket *s) { |
104 | assert(s); | |
105 | ||
106 | if (s->control_pid <= 0) | |
107 | return; | |
108 | ||
109 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
110 | s->control_pid = 0; | |
111 | } | |
112 | ||
15087cdb | 113 | static void socket_cleanup_fd_list(SocketPort *p) { |
3ffd4af2 | 114 | assert(p); |
15087cdb | 115 | |
3ffd4af2 | 116 | close_many(p->auxiliary_fds, p->n_auxiliary_fds); |
15087cdb PS |
117 | p->auxiliary_fds = mfree(p->auxiliary_fds); |
118 | p->n_auxiliary_fds = 0; | |
119 | } | |
120 | ||
74051b9b | 121 | void socket_free_ports(Socket *s) { |
034c6ed7 LP |
122 | SocketPort *p; |
123 | ||
124 | assert(s); | |
125 | ||
126 | while ((p = s->ports)) { | |
71fda00f | 127 | LIST_REMOVE(port, s->ports, p); |
034c6ed7 | 128 | |
718db961 LP |
129 | sd_event_source_unref(p->event_source); |
130 | ||
15087cdb | 131 | socket_cleanup_fd_list(p); |
03e334a1 | 132 | safe_close(p->fd); |
034c6ed7 LP |
133 | free(p->path); |
134 | free(p); | |
135 | } | |
74051b9b LP |
136 | } |
137 | ||
138 | static void socket_done(Unit *u) { | |
139 | Socket *s = SOCKET(u); | |
9d565427 | 140 | SocketPeer *p; |
74051b9b LP |
141 | |
142 | assert(s); | |
143 | ||
144 | socket_free_ports(s); | |
034c6ed7 | 145 | |
9a73653c | 146 | while ((p = set_steal_first(s->peers_by_address))) |
9d565427 SS |
147 | p->socket = NULL; |
148 | ||
9a73653c | 149 | s->peers_by_address = set_free(s->peers_by_address); |
9d565427 | 150 | |
e8a565cb | 151 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, false); |
e537352b | 152 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
034c6ed7 LP |
153 | s->control_command = NULL; |
154 | ||
29206d46 LP |
155 | dynamic_creds_unref(&s->dynamic_creds); |
156 | ||
5e94833f | 157 | socket_unwatch_control_pid(s); |
034c6ed7 | 158 | |
57020a3a | 159 | unit_ref_unset(&s->service); |
034c6ed7 | 160 | |
a1e58e8e LP |
161 | s->tcp_congestion = mfree(s->tcp_congestion); |
162 | s->bind_to_device = mfree(s->bind_to_device); | |
acbb0225 | 163 | |
0a78712e DM |
164 | s->smack = mfree(s->smack); |
165 | s->smack_ip_in = mfree(s->smack_ip_in); | |
166 | s->smack_ip_out = mfree(s->smack_ip_out); | |
0eb59ccf | 167 | |
811ba7a0 LP |
168 | strv_free(s->symlinks); |
169 | ||
0a78712e DM |
170 | s->user = mfree(s->user); |
171 | s->group = mfree(s->group); | |
3900e5fd | 172 | |
a97b23d6 DM |
173 | s->fdname = mfree(s->fdname); |
174 | ||
718db961 LP |
175 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
176 | } | |
177 | ||
36c16a7c | 178 | static int socket_arm_timer(Socket *s, usec_t usec) { |
718db961 LP |
179 | int r; |
180 | ||
181 | assert(s); | |
182 | ||
718db961 | 183 | if (s->timer_event_source) { |
36c16a7c | 184 | r = sd_event_source_set_time(s->timer_event_source, usec); |
718db961 LP |
185 | if (r < 0) |
186 | return r; | |
187 | ||
188 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
189 | } | |
190 | ||
36c16a7c LP |
191 | if (usec == USEC_INFINITY) |
192 | return 0; | |
193 | ||
7dfbe2e3 | 194 | r = sd_event_add_time( |
6a0f1f6d LP |
195 | UNIT(s)->manager->event, |
196 | &s->timer_event_source, | |
197 | CLOCK_MONOTONIC, | |
36c16a7c | 198 | usec, 0, |
6a0f1f6d | 199 | socket_dispatch_timer, s); |
7dfbe2e3 TG |
200 | if (r < 0) |
201 | return r; | |
202 | ||
203 | (void) sd_event_source_set_description(s->timer_event_source, "socket-timer"); | |
204 | ||
205 | return 0; | |
5cb5a6ff LP |
206 | } |
207 | ||
8b835fcc ZJS |
208 | int socket_instantiate_service(Socket *s) { |
209 | _cleanup_free_ char *prefix = NULL, *name = NULL; | |
b15bdda8 LP |
210 | int r; |
211 | Unit *u; | |
212 | ||
213 | assert(s); | |
214 | ||
215 | /* This fills in s->service if it isn't filled in yet. For | |
216 | * Accept=yes sockets we create the next connection service | |
217 | * here. For Accept=no this is mostly a NOP since the service | |
218 | * is figured out at load time anyway. */ | |
219 | ||
7410616c | 220 | if (UNIT_DEREF(s->service)) |
b15bdda8 LP |
221 | return 0; |
222 | ||
7410616c LP |
223 | if (!s->accept) |
224 | return 0; | |
225 | ||
226 | r = unit_name_to_prefix(UNIT(s)->id, &prefix); | |
227 | if (r < 0) | |
228 | return r; | |
b15bdda8 | 229 | |
315db1a8 | 230 | if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0) |
b15bdda8 LP |
231 | return -ENOMEM; |
232 | ||
1124fe6f | 233 | r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u); |
b15bdda8 LP |
234 | if (r < 0) |
235 | return r; | |
236 | ||
7f7d01ed | 237 | unit_ref_set(&s->service, UNIT(s), u); |
57020a3a | 238 | |
eef85c4a | 239 | return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false, UNIT_DEPENDENCY_IMPLICIT); |
b15bdda8 LP |
240 | } |
241 | ||
4f2d528d LP |
242 | static bool have_non_accept_socket(Socket *s) { |
243 | SocketPort *p; | |
244 | ||
245 | assert(s); | |
246 | ||
247 | if (!s->accept) | |
248 | return true; | |
249 | ||
dd5ad9d4 LP |
250 | LIST_FOREACH(port, p, s->ports) { |
251 | ||
252 | if (p->type != SOCKET_SOCKET) | |
253 | return true; | |
254 | ||
4f2d528d LP |
255 | if (!socket_address_can_accept(&p->address)) |
256 | return true; | |
dd5ad9d4 | 257 | } |
4f2d528d LP |
258 | |
259 | return false; | |
260 | } | |
261 | ||
eef85c4a | 262 | static int socket_add_mount_dependencies(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); | |
60252446 | 273 | else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION)) |
a57f7e2c | 274 | path = p->path; |
6e2ef85b | 275 | |
a57f7e2c LP |
276 | if (!path) |
277 | continue; | |
6e2ef85b | 278 | |
eef85c4a | 279 | r = unit_require_mounts_for(UNIT(s), path, UNIT_DEPENDENCY_FILE); |
b87705cd | 280 | if (r < 0) |
6e2ef85b | 281 | return r; |
b87705cd | 282 | } |
6e2ef85b LP |
283 | |
284 | return 0; | |
285 | } | |
286 | ||
eef85c4a | 287 | static int socket_add_device_dependencies(Socket *s) { |
6e2ef85b | 288 | char *t; |
6e2ef85b LP |
289 | |
290 | assert(s); | |
291 | ||
7d0c710d | 292 | if (!s->bind_to_device || streq(s->bind_to_device, "lo")) |
6e2ef85b LP |
293 | return 0; |
294 | ||
63c372cb | 295 | t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device); |
d336ba9f | 296 | return unit_add_node_dependency(UNIT(s), t, UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE); |
6e2ef85b LP |
297 | } |
298 | ||
a40eb732 LP |
299 | static int socket_add_default_dependencies(Socket *s) { |
300 | int r; | |
301 | assert(s); | |
302 | ||
4c9ea260 LP |
303 | if (!UNIT(s)->default_dependencies) |
304 | return 0; | |
305 | ||
35d8c19a | 306 | r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
307 | if (r < 0) |
308 | return r; | |
2a77d31d | 309 | |
463d0d15 | 310 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
5a724170 | 311 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 | 312 | if (r < 0) |
a40eb732 | 313 | return r; |
2a77d31d | 314 | } |
a40eb732 | 315 | |
5a724170 | 316 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
317 | } |
318 | ||
44a6b1b6 | 319 | _pure_ static bool socket_has_exec(Socket *s) { |
4cfc6dbe LP |
320 | unsigned i; |
321 | assert(s); | |
322 | ||
323 | for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++) | |
324 | if (s->exec_command[i]) | |
325 | return true; | |
326 | ||
327 | return false; | |
328 | } | |
329 | ||
e821075a LP |
330 | static int socket_add_extras(Socket *s) { |
331 | Unit *u = UNIT(s); | |
e537352b | 332 | int r; |
44d8db9e | 333 | |
e821075a | 334 | assert(s); |
57020a3a | 335 | |
1f15ce28 LP |
336 | /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit |
337 | * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept() | |
338 | * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly | |
339 | * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic | |
340 | * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to | |
341 | * process whatever is queued in one go, and thus should normally never have to be started frequently. This is | |
342 | * different for Accept=yes where each connection is processed by a new service instance, and thus frequent | |
343 | * service starts are typical. */ | |
344 | ||
345 | if (s->trigger_limit.interval == USEC_INFINITY) | |
346 | s->trigger_limit.interval = 2 * USEC_PER_SEC; | |
347 | ||
348 | if (s->trigger_limit.burst == (unsigned) -1) { | |
349 | if (s->accept) | |
350 | s->trigger_limit.burst = 200; | |
351 | else | |
352 | s->trigger_limit.burst = 20; | |
353 | } | |
354 | ||
e821075a | 355 | if (have_non_accept_socket(s)) { |
23a177ef | 356 | |
e821075a LP |
357 | if (!UNIT_DEREF(s->service)) { |
358 | Unit *x; | |
57020a3a | 359 | |
e821075a | 360 | r = unit_load_related_unit(u, ".service", &x); |
57020a3a | 361 | if (r < 0) |
4f2d528d | 362 | return r; |
e821075a | 363 | |
7f7d01ed | 364 | unit_ref_set(&s->service, u, x); |
4f2d528d | 365 | } |
44d8db9e | 366 | |
eef85c4a | 367 | r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true, UNIT_DEPENDENCY_IMPLICIT); |
e821075a | 368 | if (r < 0) |
6e2ef85b | 369 | return r; |
e821075a | 370 | } |
6e2ef85b | 371 | |
eef85c4a | 372 | r = socket_add_mount_dependencies(s); |
e821075a LP |
373 | if (r < 0) |
374 | return r; | |
6e2ef85b | 375 | |
eef85c4a | 376 | r = socket_add_device_dependencies(s); |
e821075a LP |
377 | if (r < 0) |
378 | return r; | |
379 | ||
598459ce | 380 | r = unit_patch_contexts(u); |
e821075a LP |
381 | if (r < 0) |
382 | return r; | |
383 | ||
384 | if (socket_has_exec(s)) { | |
385 | r = unit_add_exec_dependencies(u, &s->exec_context); | |
386 | if (r < 0) | |
387 | return r; | |
e821075a | 388 | } |
a016b922 | 389 | |
88af31f9 LP |
390 | r = unit_set_default_slice(u); |
391 | if (r < 0) | |
392 | return r; | |
393 | ||
4c9ea260 LP |
394 | r = socket_add_default_dependencies(s); |
395 | if (r < 0) | |
396 | return r; | |
e821075a LP |
397 | |
398 | return 0; | |
399 | } | |
400 | ||
811ba7a0 LP |
401 | static const char *socket_find_symlink_target(Socket *s) { |
402 | const char *found = NULL; | |
403 | SocketPort *p; | |
404 | ||
405 | LIST_FOREACH(port, p, s->ports) { | |
406 | const char *f = NULL; | |
407 | ||
408 | switch (p->type) { | |
409 | ||
410 | case SOCKET_FIFO: | |
411 | f = p->path; | |
412 | break; | |
413 | ||
414 | case SOCKET_SOCKET: | |
b9495e8d | 415 | f = socket_address_get_path(&p->address); |
811ba7a0 LP |
416 | break; |
417 | ||
418 | default: | |
419 | break; | |
420 | } | |
421 | ||
422 | if (f) { | |
423 | if (found) | |
424 | return NULL; | |
425 | ||
426 | found = f; | |
427 | } | |
428 | } | |
429 | ||
430 | return found; | |
431 | } | |
432 | ||
e821075a LP |
433 | static int socket_verify(Socket *s) { |
434 | assert(s); | |
75193d41 | 435 | assert(UNIT(s)->load_state == UNIT_LOADED); |
e821075a LP |
436 | |
437 | if (!s->ports) { | |
da3bddc9 | 438 | log_unit_error(UNIT(s), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing."); |
6f40aa45 | 439 | return -ENOEXEC; |
e821075a LP |
440 | } |
441 | ||
442 | if (s->accept && have_non_accept_socket(s)) { | |
f2341e0a | 443 | log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing."); |
6f40aa45 | 444 | return -ENOEXEC; |
e821075a LP |
445 | } |
446 | ||
447 | if (s->accept && s->max_connections <= 0) { | |
f2341e0a | 448 | log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing."); |
6f40aa45 | 449 | return -ENOEXEC; |
e821075a | 450 | } |
e06c73cc | 451 | |
e821075a | 452 | if (s->accept && UNIT_DEREF(s->service)) { |
f2341e0a | 453 | log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing."); |
6f40aa45 | 454 | return -ENOEXEC; |
e821075a LP |
455 | } |
456 | ||
457 | if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { | |
f2341e0a | 458 | log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing."); |
6f40aa45 | 459 | return -ENOEXEC; |
e821075a LP |
460 | } |
461 | ||
811ba7a0 | 462 | if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) { |
f2341e0a | 463 | log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing."); |
6f40aa45 | 464 | return -ENOEXEC; |
811ba7a0 LP |
465 | } |
466 | ||
e821075a LP |
467 | return 0; |
468 | } | |
469 | ||
7a08d314 | 470 | static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) { |
9d565427 SS |
471 | assert(s); |
472 | ||
473 | if (s->peer.sa.sa_family == AF_INET) | |
474 | siphash24_compress(&s->peer.in.sin_addr, sizeof(s->peer.in.sin_addr), state); | |
41733ae1 | 475 | else if (s->peer.sa.sa_family == AF_INET6) |
9d565427 | 476 | siphash24_compress(&s->peer.in6.sin6_addr, sizeof(s->peer.in6.sin6_addr), state); |
359a5bcf SH |
477 | else if (s->peer.sa.sa_family == AF_VSOCK) |
478 | siphash24_compress(&s->peer.vm.svm_cid, sizeof(s->peer.vm.svm_cid), state); | |
41733ae1 LP |
479 | else |
480 | assert_not_reached("Unknown address family."); | |
9d565427 SS |
481 | } |
482 | ||
7a08d314 | 483 | static int peer_address_compare_func(const SocketPeer *x, const SocketPeer *y) { |
a0edd02e | 484 | int r; |
9d565427 | 485 | |
a0edd02e FB |
486 | r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family); |
487 | if (r != 0) | |
488 | return r; | |
9d565427 SS |
489 | |
490 | switch(x->peer.sa.sa_family) { | |
491 | case AF_INET: | |
492 | return memcmp(&x->peer.in.sin_addr, &y->peer.in.sin_addr, sizeof(x->peer.in.sin_addr)); | |
493 | case AF_INET6: | |
494 | return memcmp(&x->peer.in6.sin6_addr, &y->peer.in6.sin6_addr, sizeof(x->peer.in6.sin6_addr)); | |
359a5bcf | 495 | case AF_VSOCK: |
6dd91b36 | 496 | return CMP(x->peer.vm.svm_cid, y->peer.vm.svm_cid); |
9d565427 | 497 | } |
166cf510 | 498 | assert_not_reached("Black sheep in the family!"); |
9d565427 SS |
499 | } |
500 | ||
7a08d314 | 501 | DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops, SocketPeer, peer_address_hash_func, peer_address_compare_func); |
9d565427 | 502 | |
e821075a LP |
503 | static int socket_load(Unit *u) { |
504 | Socket *s = SOCKET(u); | |
505 | int r; | |
506 | ||
507 | assert(u); | |
508 | assert(u->load_state == UNIT_STUB); | |
509 | ||
9a73653c | 510 | r = set_ensure_allocated(&s->peers_by_address, &peer_address_hash_ops); |
9d565427 SS |
511 | if (r < 0) |
512 | return r; | |
513 | ||
c3620770 | 514 | r = unit_load_fragment_and_dropin(u, true); |
e821075a LP |
515 | if (r < 0) |
516 | return r; | |
517 | ||
75193d41 ZJS |
518 | if (u->load_state != UNIT_LOADED) |
519 | return 0; | |
520 | ||
521 | /* This is a new unit? Then let's add in some extras */ | |
522 | r = socket_add_extras(s); | |
523 | if (r < 0) | |
524 | return r; | |
23a177ef | 525 | |
4f2d528d | 526 | return socket_verify(s); |
44d8db9e LP |
527 | } |
528 | ||
166cf510 ZJS |
529 | static SocketPeer *socket_peer_new(void) { |
530 | SocketPeer *p; | |
531 | ||
532 | p = new0(SocketPeer, 1); | |
533 | if (!p) | |
534 | return NULL; | |
535 | ||
536 | p->n_ref = 1; | |
537 | ||
538 | return p; | |
539 | } | |
540 | ||
8301aa0b YW |
541 | static SocketPeer *socket_peer_free(SocketPeer *p) { |
542 | assert(p); | |
166cf510 ZJS |
543 | |
544 | if (p->socket) | |
545 | set_remove(p->socket->peers_by_address, p); | |
546 | ||
547 | return mfree(p); | |
548 | } | |
549 | ||
8301aa0b YW |
550 | DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free); |
551 | ||
3ebcd323 | 552 | int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) { |
166cf510 ZJS |
553 | _cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL; |
554 | SocketPeer sa = {}, *i; | |
555 | socklen_t salen = sizeof(sa.peer); | |
556 | int r; | |
557 | ||
558 | assert(fd >= 0); | |
559 | assert(s); | |
560 | ||
561 | r = getpeername(fd, &sa.peer.sa, &salen); | |
562 | if (r < 0) | |
fc2d74ab | 563 | return log_unit_error_errno(UNIT(s), errno, "getpeername failed: %m"); |
166cf510 | 564 | |
359a5bcf | 565 | if (!IN_SET(sa.peer.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { |
166cf510 ZJS |
566 | *p = NULL; |
567 | return 0; | |
568 | } | |
569 | ||
570 | i = set_get(s->peers_by_address, &sa); | |
571 | if (i) { | |
572 | *p = socket_peer_ref(i); | |
573 | return 1; | |
574 | } | |
575 | ||
576 | remote = socket_peer_new(); | |
577 | if (!remote) | |
578 | return log_oom(); | |
579 | ||
580 | remote->peer = sa.peer; | |
41733ae1 | 581 | remote->peer_salen = salen; |
166cf510 ZJS |
582 | |
583 | r = set_put(s->peers_by_address, remote); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | remote->socket = s; | |
588 | ||
1cc6c93a | 589 | *p = TAKE_PTR(remote); |
166cf510 ZJS |
590 | |
591 | return 1; | |
592 | } | |
593 | ||
44a6b1b6 | 594 | _const_ static const char* listen_lookup(int family, int type) { |
7a22745a LP |
595 | |
596 | if (family == AF_NETLINK) | |
597 | return "ListenNetlink"; | |
542563ba LP |
598 | |
599 | if (type == SOCK_STREAM) | |
600 | return "ListenStream"; | |
601 | else if (type == SOCK_DGRAM) | |
602 | return "ListenDatagram"; | |
603 | else if (type == SOCK_SEQPACKET) | |
604 | return "ListenSequentialPacket"; | |
605 | ||
034c6ed7 | 606 | assert_not_reached("Unknown socket type"); |
542563ba LP |
607 | return NULL; |
608 | } | |
609 | ||
87f0e418 | 610 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { |
209e9dcd | 611 | char time_string[FORMAT_TIMESPAN_MAX]; |
5cb5a6ff | 612 | SocketExecCommand c; |
87f0e418 | 613 | Socket *s = SOCKET(u); |
542563ba | 614 | SocketPort *p; |
827d9bf2 | 615 | const char *prefix2, *str; |
5cb5a6ff LP |
616 | |
617 | assert(s); | |
fa068367 | 618 | assert(f); |
5cb5a6ff | 619 | |
4c940960 | 620 | prefix = strempty(prefix); |
63c372cb | 621 | prefix2 = strjoina(prefix, "\t"); |
c43d20a0 | 622 | |
5cb5a6ff LP |
623 | fprintf(f, |
624 | "%sSocket State: %s\n" | |
81a5c6d0 | 625 | "%sResult: %s\n" |
c968d76a | 626 | "%sClean Result: %s\n" |
542563ba | 627 | "%sBindIPv6Only: %s\n" |
b5a0699f LP |
628 | "%sBacklog: %u\n" |
629 | "%sSocketMode: %04o\n" | |
4fd5948e LP |
630 | "%sDirectoryMode: %04o\n" |
631 | "%sKeepAlive: %s\n" | |
4427c3f4 | 632 | "%sNoDelay: %s\n" |
cebf8b20 | 633 | "%sFreeBind: %s\n" |
6b6d2dee | 634 | "%sTransparent: %s\n" |
ec6370a2 | 635 | "%sBroadcast: %s\n" |
ede3deb4 | 636 | "%sPassCredentials: %s\n" |
54ecda32 | 637 | "%sPassSecurity: %s\n" |
bd1fe7c7 | 638 | "%sTCPCongestion: %s\n" |
16115b0a | 639 | "%sRemoveOnStop: %s\n" |
55301ec0 | 640 | "%sWritable: %s\n" |
827d9bf2 | 641 | "%sFileDescriptorName: %s\n" |
16115b0a | 642 | "%sSELinuxContextFromNet: %s\n", |
a16e1123 | 643 | prefix, socket_state_to_string(s->state), |
81a5c6d0 | 644 | prefix, socket_result_to_string(s->result), |
c968d76a | 645 | prefix, socket_result_to_string(s->clean_result), |
c0120d99 | 646 | prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only), |
b5a0699f LP |
647 | prefix, s->backlog, |
648 | prefix, s->socket_mode, | |
4fd5948e LP |
649 | prefix, s->directory_mode, |
650 | prefix, yes_no(s->keep_alive), | |
4427c3f4 | 651 | prefix, yes_no(s->no_delay), |
cebf8b20 | 652 | prefix, yes_no(s->free_bind), |
6b6d2dee | 653 | prefix, yes_no(s->transparent), |
ec6370a2 | 654 | prefix, yes_no(s->broadcast), |
d68af586 | 655 | prefix, yes_no(s->pass_cred), |
54ecda32 | 656 | prefix, yes_no(s->pass_sec), |
bd1fe7c7 | 657 | prefix, strna(s->tcp_congestion), |
16115b0a | 658 | prefix, yes_no(s->remove_on_stop), |
55301ec0 | 659 | prefix, yes_no(s->writable), |
8dd4c05b | 660 | prefix, socket_fdname(s), |
16115b0a | 661 | prefix, yes_no(s->selinux_context_from_net)); |
542563ba | 662 | |
70123e68 LP |
663 | if (s->control_pid > 0) |
664 | fprintf(f, | |
de0671ee ZJS |
665 | "%sControl PID: "PID_FMT"\n", |
666 | prefix, s->control_pid); | |
70123e68 | 667 | |
acbb0225 LP |
668 | if (s->bind_to_device) |
669 | fprintf(f, | |
670 | "%sBindToDevice: %s\n", | |
671 | prefix, s->bind_to_device); | |
672 | ||
4f2d528d LP |
673 | if (s->accept) |
674 | fprintf(f, | |
6cf6bbc2 LP |
675 | "%sAccepted: %u\n" |
676 | "%sNConnections: %u\n" | |
827d9bf2 YW |
677 | "%sMaxConnections: %u\n" |
678 | "%sMaxConnectionsPerSource: %u\n", | |
6cf6bbc2 LP |
679 | prefix, s->n_accepted, |
680 | prefix, s->n_connections, | |
827d9bf2 YW |
681 | prefix, s->max_connections, |
682 | prefix, s->max_connections_per_source); | |
4f2d528d | 683 | |
4fd5948e LP |
684 | if (s->priority >= 0) |
685 | fprintf(f, | |
686 | "%sPriority: %i\n", | |
687 | prefix, s->priority); | |
688 | ||
689 | if (s->receive_buffer > 0) | |
690 | fprintf(f, | |
691 | "%sReceiveBuffer: %zu\n", | |
692 | prefix, s->receive_buffer); | |
693 | ||
694 | if (s->send_buffer > 0) | |
695 | fprintf(f, | |
696 | "%sSendBuffer: %zu\n", | |
697 | prefix, s->send_buffer); | |
698 | ||
699 | if (s->ip_tos >= 0) | |
700 | fprintf(f, | |
701 | "%sIPTOS: %i\n", | |
702 | prefix, s->ip_tos); | |
703 | ||
704 | if (s->ip_ttl >= 0) | |
705 | fprintf(f, | |
706 | "%sIPTTL: %i\n", | |
707 | prefix, s->ip_ttl); | |
708 | ||
709 | if (s->pipe_size > 0) | |
710 | fprintf(f, | |
711 | "%sPipeSize: %zu\n", | |
712 | prefix, s->pipe_size); | |
713 | ||
714 | if (s->mark >= 0) | |
715 | fprintf(f, | |
716 | "%sMark: %i\n", | |
717 | prefix, s->mark); | |
718 | ||
916abb21 LP |
719 | if (s->mq_maxmsg > 0) |
720 | fprintf(f, | |
721 | "%sMessageQueueMaxMessages: %li\n", | |
722 | prefix, s->mq_maxmsg); | |
723 | ||
724 | if (s->mq_msgsize > 0) | |
725 | fprintf(f, | |
726 | "%sMessageQueueMessageSize: %li\n", | |
727 | prefix, s->mq_msgsize); | |
728 | ||
718db961 | 729 | if (s->reuse_port) |
f7db7a69 SL |
730 | fprintf(f, |
731 | "%sReusePort: %s\n", | |
718db961 | 732 | prefix, yes_no(s->reuse_port)); |
f7db7a69 | 733 | |
0eb59ccf AK |
734 | if (s->smack) |
735 | fprintf(f, | |
736 | "%sSmackLabel: %s\n", | |
737 | prefix, s->smack); | |
738 | ||
739 | if (s->smack_ip_in) | |
740 | fprintf(f, | |
741 | "%sSmackLabelIPIn: %s\n", | |
742 | prefix, s->smack_ip_in); | |
743 | ||
744 | if (s->smack_ip_out) | |
745 | fprintf(f, | |
746 | "%sSmackLabelIPOut: %s\n", | |
747 | prefix, s->smack_ip_out); | |
748 | ||
3900e5fd LP |
749 | if (!isempty(s->user) || !isempty(s->group)) |
750 | fprintf(f, | |
d2a50e3b LP |
751 | "%sSocketUser: %s\n" |
752 | "%sSocketGroup: %s\n", | |
3900e5fd LP |
753 | prefix, strna(s->user), |
754 | prefix, strna(s->group)); | |
755 | ||
3cd761e4 | 756 | if (s->keep_alive_time > 0) |
209e9dcd | 757 | fprintf(f, |
3cd761e4 LP |
758 | "%sKeepAliveTimeSec: %s\n", |
759 | prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_time, USEC_PER_SEC)); | |
209e9dcd | 760 | |
7be9df7d | 761 | if (s->keep_alive_interval > 0) |
209e9dcd | 762 | fprintf(f, |
3cd761e4 LP |
763 | "%sKeepAliveIntervalSec: %s\n", |
764 | prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_interval, USEC_PER_SEC)); | |
209e9dcd | 765 | |
7be9df7d | 766 | if (s->keep_alive_cnt > 0) |
209e9dcd SS |
767 | fprintf(f, |
768 | "%sKeepAliveProbes: %u\n", | |
769 | prefix, s->keep_alive_cnt); | |
770 | ||
7be9df7d | 771 | if (s->defer_accept > 0) |
cc567c9b | 772 | fprintf(f, |
3cd761e4 LP |
773 | "%sDeferAcceptSec: %s\n", |
774 | prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->defer_accept, USEC_PER_SEC)); | |
cc567c9b | 775 | |
034c6ed7 | 776 | LIST_FOREACH(port, p, s->ports) { |
5cb5a6ff | 777 | |
7be9df7d YW |
778 | switch (p->type) { |
779 | case SOCKET_SOCKET: { | |
780 | _cleanup_free_ char *k = NULL; | |
542563ba LP |
781 | const char *t; |
782 | int r; | |
542563ba | 783 | |
e53fc357 LP |
784 | r = socket_address_print(&p->address, &k); |
785 | if (r < 0) | |
4bbccb02 | 786 | t = strerror_safe(r); |
542563ba LP |
787 | else |
788 | t = k; | |
789 | ||
7a22745a | 790 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t); |
7be9df7d YW |
791 | break; |
792 | } | |
793 | case SOCKET_SPECIAL: | |
b0a3f2bc | 794 | fprintf(f, "%sListenSpecial: %s\n", prefix, p->path); |
7be9df7d YW |
795 | break; |
796 | case SOCKET_USB_FUNCTION: | |
60252446 | 797 | fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path); |
7be9df7d YW |
798 | break; |
799 | case SOCKET_MQUEUE: | |
916abb21 | 800 | fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path); |
7be9df7d YW |
801 | break; |
802 | default: | |
542563ba | 803 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); |
7be9df7d | 804 | } |
542563ba | 805 | } |
5cb5a6ff | 806 | |
1745fa70 EV |
807 | fprintf(f, |
808 | "%sTriggerLimitIntervalSec: %s\n" | |
809 | "%sTriggerLimitBurst: %u\n", | |
810 | prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->trigger_limit.interval, USEC_PER_SEC), | |
811 | prefix, s->trigger_limit.burst); | |
812 | ||
da96ad5a | 813 | str = ip_protocol_to_name(s->socket_protocol); |
827d9bf2 YW |
814 | if (str) |
815 | fprintf(f, "%sSocketProtocol: %s\n", prefix, str); | |
816 | ||
817 | if (!strv_isempty(s->symlinks)) { | |
818 | char **q; | |
819 | ||
820 | fprintf(f, "%sSymlinks:", prefix); | |
821 | STRV_FOREACH(q, s->symlinks) | |
822 | fprintf(f, " %s", *q); | |
823 | ||
824 | fprintf(f, "\n"); | |
825 | } | |
826 | ||
827 | fprintf(f, | |
828 | "%sTimeoutSec: %s\n", | |
829 | prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->timeout_usec, USEC_PER_SEC)); | |
830 | ||
5cb5a6ff | 831 | exec_context_dump(&s->exec_context, f, prefix); |
4819ff03 | 832 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff | 833 | |
e537352b | 834 | for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { |
c43d20a0 LP |
835 | if (!s->exec_command[c]) |
836 | continue; | |
5cb5a6ff | 837 | |
40d50879 | 838 | fprintf(f, "%s-> %s:\n", |
a16e1123 | 839 | prefix, socket_exec_command_to_string(c)); |
c43d20a0 LP |
840 | |
841 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 842 | } |
18f573aa | 843 | |
bc0623df | 844 | cgroup_context_dump(UNIT(s), f, prefix); |
5cb5a6ff LP |
845 | } |
846 | ||
4f2d528d LP |
847 | static int instance_from_socket(int fd, unsigned nr, char **instance) { |
848 | socklen_t l; | |
849 | char *r; | |
9d16d0c7 | 850 | union sockaddr_union local, remote; |
4f2d528d LP |
851 | |
852 | assert(fd >= 0); | |
853 | assert(instance); | |
854 | ||
855 | l = sizeof(local); | |
856 | if (getsockname(fd, &local.sa, &l) < 0) | |
857 | return -errno; | |
858 | ||
859 | l = sizeof(remote); | |
860 | if (getpeername(fd, &remote.sa, &l) < 0) | |
861 | return -errno; | |
862 | ||
863 | switch (local.sa.sa_family) { | |
864 | ||
865 | case AF_INET: { | |
866 | uint32_t | |
8e38570e LP |
867 | a = be32toh(local.in.sin_addr.s_addr), |
868 | b = be32toh(remote.in.sin_addr.s_addr); | |
4f2d528d LP |
869 | |
870 | if (asprintf(&r, | |
77b088c2 LP |
871 | "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
872 | nr, | |
4f2d528d | 873 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, |
8e38570e | 874 | be16toh(local.in.sin_port), |
4f2d528d | 875 | b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF, |
8e38570e | 876 | be16toh(remote.in.sin_port)) < 0) |
4f2d528d LP |
877 | return -ENOMEM; |
878 | ||
879 | break; | |
880 | } | |
881 | ||
882 | case AF_INET6: { | |
c65a0b14 | 883 | static const unsigned char ipv4_prefix[] = { |
2b061f5a LP |
884 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF |
885 | }; | |
886 | ||
887 | if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 && | |
888 | memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
889 | const uint8_t | |
890 | *a = local.in6.sin6_addr.s6_addr+12, | |
891 | *b = remote.in6.sin6_addr.s6_addr+12; | |
892 | ||
893 | if (asprintf(&r, | |
77b088c2 LP |
894 | "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", |
895 | nr, | |
2b061f5a | 896 | a[0], a[1], a[2], a[3], |
8e38570e | 897 | be16toh(local.in6.sin6_port), |
2b061f5a | 898 | b[0], b[1], b[2], b[3], |
8e38570e | 899 | be16toh(remote.in6.sin6_port)) < 0) |
2b061f5a LP |
900 | return -ENOMEM; |
901 | } else { | |
902 | char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN]; | |
903 | ||
904 | if (asprintf(&r, | |
77b088c2 LP |
905 | "%u-%s:%u-%s:%u", |
906 | nr, | |
2b061f5a | 907 | inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)), |
8e38570e | 908 | be16toh(local.in6.sin6_port), |
2b061f5a | 909 | inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)), |
8e38570e | 910 | be16toh(remote.in6.sin6_port)) < 0) |
2b061f5a LP |
911 | return -ENOMEM; |
912 | } | |
4f2d528d LP |
913 | |
914 | break; | |
915 | } | |
916 | ||
917 | case AF_UNIX: { | |
918 | struct ucred ucred; | |
eff05270 | 919 | int k; |
4f2d528d | 920 | |
eff05270 | 921 | k = getpeercred(fd, &ucred); |
d38f6e34 ZJS |
922 | if (k >= 0) { |
923 | if (asprintf(&r, | |
924 | "%u-"PID_FMT"-"UID_FMT, | |
925 | nr, ucred.pid, ucred.uid) < 0) | |
926 | return -ENOMEM; | |
927 | } else if (k == -ENODATA) { | |
928 | /* This handles the case where somebody is | |
929 | * connecting from another pid/uid namespace | |
930 | * (e.g. from outside of our container). */ | |
931 | if (asprintf(&r, | |
932 | "%u-unknown", | |
933 | nr) < 0) | |
934 | return -ENOMEM; | |
935 | } else | |
eff05270 | 936 | return k; |
2f20a8eb | 937 | |
2f20a8eb | 938 | break; |
4f2d528d LP |
939 | } |
940 | ||
359a5bcf SH |
941 | case AF_VSOCK: |
942 | if (asprintf(&r, | |
943 | "%u-%u:%u-%u:%u", | |
944 | nr, | |
945 | local.vm.svm_cid, local.vm.svm_port, | |
946 | remote.vm.svm_cid, remote.vm.svm_port) < 0) | |
947 | return -ENOMEM; | |
948 | ||
949 | break; | |
950 | ||
4f2d528d LP |
951 | default: |
952 | assert_not_reached("Unhandled socket type."); | |
953 | } | |
954 | ||
955 | *instance = r; | |
956 | return 0; | |
957 | } | |
958 | ||
034c6ed7 | 959 | static void socket_close_fds(Socket *s) { |
83c60c9f | 960 | SocketPort *p; |
811ba7a0 | 961 | char **i; |
83c60c9f LP |
962 | |
963 | assert(s); | |
964 | ||
034c6ed7 | 965 | LIST_FOREACH(port, p, s->ports) { |
18e854f8 | 966 | bool was_open; |
718db961 | 967 | |
18e854f8 | 968 | was_open = p->fd >= 0; |
83c60c9f | 969 | |
18e854f8 | 970 | p->event_source = sd_event_source_unref(p->event_source); |
03e334a1 | 971 | p->fd = safe_close(p->fd); |
15087cdb | 972 | socket_cleanup_fd_list(p); |
a16e1123 | 973 | |
18e854f8 LP |
974 | /* One little note: we should normally not delete any sockets in the file system here! After all some |
975 | * other process we spawned might still have a reference of this fd and wants to continue to use | |
976 | * it. Therefore we normally delete sockets in the file system before we create a new one, not after we | |
977 | * stopped using one! That all said, if the user explicitly requested this, we'll delete them here | |
978 | * anyway, but only then. */ | |
bd1fe7c7 | 979 | |
18e854f8 LP |
980 | if (!was_open || !s->remove_on_stop) |
981 | continue; | |
982 | ||
983 | switch (p->type) { | |
bd1fe7c7 | 984 | |
18e854f8 LP |
985 | case SOCKET_FIFO: |
986 | (void) unlink(p->path); | |
987 | break; | |
bd1fe7c7 | 988 | |
18e854f8 LP |
989 | case SOCKET_MQUEUE: |
990 | (void) mq_unlink(p->path); | |
991 | break; | |
bd1fe7c7 | 992 | |
18e854f8 LP |
993 | case SOCKET_SOCKET: |
994 | (void) socket_address_unlink(&p->address); | |
995 | break; | |
bd1fe7c7 | 996 | |
18e854f8 LP |
997 | default: |
998 | break; | |
bd1fe7c7 | 999 | } |
83c60c9f | 1000 | } |
811ba7a0 LP |
1001 | |
1002 | if (s->remove_on_stop) | |
1003 | STRV_FOREACH(i, s->symlinks) | |
18e854f8 | 1004 | (void) unlink(*i); |
83c60c9f LP |
1005 | } |
1006 | ||
4fd5948e | 1007 | static void socket_apply_socket_options(Socket *s, int fd) { |
d53e386d LP |
1008 | int r; |
1009 | ||
4fd5948e LP |
1010 | assert(s); |
1011 | assert(fd >= 0); | |
1012 | ||
1013 | if (s->keep_alive) { | |
2ff48e98 LP |
1014 | r = setsockopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, true); |
1015 | if (r < 0) | |
1016 | log_unit_warning_errno(UNIT(s), r, "SO_KEEPALIVE failed: %m"); | |
4fd5948e LP |
1017 | } |
1018 | ||
7be9df7d | 1019 | if (s->keep_alive_time > 0) { |
9e5b6496 YW |
1020 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPIDLE, s->keep_alive_time / USEC_PER_SEC); |
1021 | if (r < 0) | |
1022 | log_unit_warning_errno(UNIT(s), r, "TCP_KEEPIDLE failed: %m"); | |
209e9dcd SS |
1023 | } |
1024 | ||
7be9df7d | 1025 | if (s->keep_alive_interval > 0) { |
9e5b6496 YW |
1026 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPINTVL, s->keep_alive_interval / USEC_PER_SEC); |
1027 | if (r < 0) | |
1028 | log_unit_warning_errno(UNIT(s), r, "TCP_KEEPINTVL failed: %m"); | |
209e9dcd SS |
1029 | } |
1030 | ||
7be9df7d | 1031 | if (s->keep_alive_cnt > 0) { |
9e5b6496 YW |
1032 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPCNT, s->keep_alive_cnt); |
1033 | if (r < 0) | |
1034 | log_unit_warning_errno(UNIT(s), r, "TCP_KEEPCNT failed: %m"); | |
209e9dcd SS |
1035 | } |
1036 | ||
7be9df7d | 1037 | if (s->defer_accept > 0) { |
9e5b6496 YW |
1038 | r = setsockopt_int(fd, SOL_TCP, TCP_DEFER_ACCEPT, s->defer_accept / USEC_PER_SEC); |
1039 | if (r < 0) | |
1040 | log_unit_warning_errno(UNIT(s), r, "TCP_DEFER_ACCEPT failed: %m"); | |
cc567c9b SS |
1041 | } |
1042 | ||
4427c3f4 | 1043 | if (s->no_delay) { |
62bc4efc | 1044 | if (s->socket_protocol == IPPROTO_SCTP) { |
2ff48e98 LP |
1045 | r = setsockopt_int(fd, SOL_SCTP, SCTP_NODELAY, true); |
1046 | if (r < 0) | |
1047 | log_unit_warning_errno(UNIT(s), r, "SCTP_NODELAY failed: %m"); | |
62bc4efc | 1048 | } else { |
2ff48e98 LP |
1049 | r = setsockopt_int(fd, SOL_TCP, TCP_NODELAY, true); |
1050 | if (r < 0) | |
1051 | log_unit_warning_errno(UNIT(s), r, "TCP_NODELAY failed: %m"); | |
62bc4efc | 1052 | } |
4427c3f4 SS |
1053 | } |
1054 | ||
ec6370a2 | 1055 | if (s->broadcast) { |
2ff48e98 LP |
1056 | r = setsockopt_int(fd, SOL_SOCKET, SO_BROADCAST, true); |
1057 | if (r < 0) | |
1058 | log_unit_warning_errno(UNIT(s), r, "SO_BROADCAST failed: %m"); | |
ec6370a2 LP |
1059 | } |
1060 | ||
d68af586 | 1061 | if (s->pass_cred) { |
2ff48e98 LP |
1062 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
1063 | if (r < 0) | |
1064 | log_unit_warning_errno(UNIT(s), r, "SO_PASSCRED failed: %m"); | |
d68af586 MS |
1065 | } |
1066 | ||
54ecda32 | 1067 | if (s->pass_sec) { |
2ff48e98 LP |
1068 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSSEC, true); |
1069 | if (r < 0) | |
1070 | log_unit_warning_errno(UNIT(s), r, "SO_PASSSEC failed: %m"); | |
54ecda32 LP |
1071 | } |
1072 | ||
9e5b6496 YW |
1073 | if (s->priority >= 0) { |
1074 | r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, s->priority); | |
1075 | if (r < 0) | |
1076 | log_unit_warning_errno(UNIT(s), r, "SO_PRIORITY failed: %m"); | |
1077 | } | |
4fd5948e LP |
1078 | |
1079 | if (s->receive_buffer > 0) { | |
7d9eaa84 | 1080 | /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */ |
9e5b6496 YW |
1081 | if (setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, s->receive_buffer) < 0) { |
1082 | r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, s->receive_buffer); | |
1083 | if (r < 0) | |
1084 | log_unit_warning_errno(UNIT(s), r, "SO_RCVBUF failed: %m"); | |
1085 | } | |
4fd5948e LP |
1086 | } |
1087 | ||
1088 | if (s->send_buffer > 0) { | |
9e5b6496 YW |
1089 | if (setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, s->send_buffer) < 0) { |
1090 | r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, s->send_buffer); | |
1091 | if (r < 0) | |
1092 | log_unit_warning_errno(UNIT(s), r, "SO_SNDBUF failed: %m"); | |
1093 | } | |
4fd5948e LP |
1094 | } |
1095 | ||
9e5b6496 YW |
1096 | if (s->mark >= 0) { |
1097 | r = setsockopt_int(fd, SOL_SOCKET, SO_MARK, s->mark); | |
1098 | if (r < 0) | |
1099 | log_unit_warning_errno(UNIT(s), r, "SO_MARK failed: %m"); | |
1100 | } | |
4fd5948e | 1101 | |
9e5b6496 YW |
1102 | if (s->ip_tos >= 0) { |
1103 | r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, s->ip_tos); | |
1104 | if (r < 0) | |
1105 | log_unit_warning_errno(UNIT(s), r, "IP_TOS failed: %m"); | |
1106 | } | |
4fd5948e | 1107 | |
46925ac5 | 1108 | if (s->ip_ttl >= 0) { |
d53e386d | 1109 | int x; |
46925ac5 | 1110 | |
9e5b6496 | 1111 | r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, s->ip_ttl); |
5bfcc1c6 FF |
1112 | |
1113 | if (socket_ipv6_is_supported()) | |
9e5b6496 YW |
1114 | x = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, s->ip_ttl); |
1115 | else | |
1116 | x = -EAFNOSUPPORT; | |
46925ac5 LP |
1117 | |
1118 | if (r < 0 && x < 0) | |
9e5b6496 | 1119 | log_unit_warning_errno(UNIT(s), r, "IP_TTL/IPV6_UNICAST_HOPS failed: %m"); |
46925ac5 | 1120 | } |
cebf8b20 TT |
1121 | |
1122 | if (s->tcp_congestion) | |
1123 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
f2341e0a | 1124 | log_unit_warning_errno(UNIT(s), errno, "TCP_CONGESTION failed: %m"); |
0eb59ccf | 1125 | |
d53e386d | 1126 | if (s->smack_ip_in) { |
5ab58c20 | 1127 | r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in); |
d53e386d | 1128 | if (r < 0) |
f2341e0a | 1129 | log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_in_fd: %m"); |
d53e386d | 1130 | } |
9a4e038c | 1131 | |
d53e386d | 1132 | if (s->smack_ip_out) { |
5ab58c20 | 1133 | r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out); |
d53e386d | 1134 | if (r < 0) |
f2341e0a | 1135 | log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_out_fd: %m"); |
d53e386d | 1136 | } |
4fd5948e LP |
1137 | } |
1138 | ||
b15bdda8 | 1139 | static void socket_apply_fifo_options(Socket *s, int fd) { |
d53e386d LP |
1140 | int r; |
1141 | ||
4fd5948e LP |
1142 | assert(s); |
1143 | assert(fd >= 0); | |
1144 | ||
1145 | if (s->pipe_size > 0) | |
1146 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
ed460b07 | 1147 | log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m"); |
0eb59ccf | 1148 | |
d53e386d | 1149 | if (s->smack) { |
5ab58c20 | 1150 | r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack); |
d53e386d | 1151 | if (r < 0) |
ed460b07 | 1152 | log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m"); |
d53e386d | 1153 | } |
4fd5948e LP |
1154 | } |
1155 | ||
b15bdda8 LP |
1156 | static int fifo_address_create( |
1157 | const char *path, | |
1158 | mode_t directory_mode, | |
e8da24a6 | 1159 | mode_t socket_mode) { |
b15bdda8 | 1160 | |
e8da24a6 | 1161 | _cleanup_close_ int fd = -1; |
b15bdda8 | 1162 | mode_t old_mask; |
e8da24a6 LP |
1163 | struct stat st; |
1164 | int r; | |
b15bdda8 LP |
1165 | |
1166 | assert(path); | |
b15bdda8 | 1167 | |
1af87ab7 | 1168 | (void) mkdir_parents_label(path, directory_mode); |
b15bdda8 | 1169 | |
ecabcf8b | 1170 | r = mac_selinux_create_file_prepare(path, S_IFIFO); |
e9a5ef7c | 1171 | if (r < 0) |
e8da24a6 | 1172 | return r; |
b15bdda8 LP |
1173 | |
1174 | /* Enforce the right access mode for the fifo */ | |
7be9df7d | 1175 | old_mask = umask(~socket_mode); |
b15bdda8 LP |
1176 | |
1177 | /* Include the original umask in our mask */ | |
e8da24a6 | 1178 | (void) umask(~socket_mode | old_mask); |
b15bdda8 LP |
1179 | |
1180 | r = mkfifo(path, socket_mode); | |
e8da24a6 | 1181 | (void) umask(old_mask); |
b15bdda8 | 1182 | |
94bc2731 | 1183 | if (r < 0 && errno != EEXIST) { |
b15bdda8 LP |
1184 | r = -errno; |
1185 | goto fail; | |
1186 | } | |
1187 | ||
e8da24a6 | 1188 | fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW); |
3cc2aff1 | 1189 | if (fd < 0) { |
b15bdda8 LP |
1190 | r = -errno; |
1191 | goto fail; | |
1192 | } | |
1193 | ||
ecabcf8b | 1194 | mac_selinux_create_file_clear(); |
7a58bfa4 | 1195 | |
b15bdda8 LP |
1196 | if (fstat(fd, &st) < 0) { |
1197 | r = -errno; | |
1198 | goto fail; | |
1199 | } | |
1200 | ||
1201 | if (!S_ISFIFO(st.st_mode) || | |
de0200fc | 1202 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || |
e4f44e73 DR |
1203 | st.st_uid != getuid() || |
1204 | st.st_gid != getgid()) { | |
b15bdda8 LP |
1205 | r = -EEXIST; |
1206 | goto fail; | |
1207 | } | |
1208 | ||
c10d6bdb | 1209 | return TAKE_FD(fd); |
b15bdda8 LP |
1210 | |
1211 | fail: | |
ecabcf8b | 1212 | mac_selinux_create_file_clear(); |
b15bdda8 LP |
1213 | return r; |
1214 | } | |
1215 | ||
55301ec0 | 1216 | static int special_address_create(const char *path, bool writable) { |
e8da24a6 | 1217 | _cleanup_close_ int fd = -1; |
b0a3f2bc LP |
1218 | struct stat st; |
1219 | ||
1220 | assert(path); | |
b0a3f2bc | 1221 | |
55301ec0 | 1222 | fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW); |
e8da24a6 LP |
1223 | if (fd < 0) |
1224 | return -errno; | |
b0a3f2bc | 1225 | |
e8da24a6 LP |
1226 | if (fstat(fd, &st) < 0) |
1227 | return -errno; | |
b0a3f2bc LP |
1228 | |
1229 | /* Check whether this is a /proc, /sys or /dev file or char device */ | |
e8da24a6 LP |
1230 | if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) |
1231 | return -EEXIST; | |
b0a3f2bc | 1232 | |
c10d6bdb | 1233 | return TAKE_FD(fd); |
b0a3f2bc LP |
1234 | } |
1235 | ||
36078102 | 1236 | static int usbffs_address_create(const char *path) { |
60252446 PS |
1237 | _cleanup_close_ int fd = -1; |
1238 | struct stat st; | |
1239 | ||
1240 | assert(path); | |
60252446 PS |
1241 | |
1242 | fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW); | |
1243 | if (fd < 0) | |
1244 | return -errno; | |
1245 | ||
1246 | if (fstat(fd, &st) < 0) | |
1247 | return -errno; | |
1248 | ||
13e785f7 | 1249 | /* Check whether this is a regular file (ffs endpoint) */ |
60252446 PS |
1250 | if (!S_ISREG(st.st_mode)) |
1251 | return -EEXIST; | |
1252 | ||
c10d6bdb | 1253 | return TAKE_FD(fd); |
60252446 PS |
1254 | } |
1255 | ||
916abb21 LP |
1256 | static int mq_address_create( |
1257 | const char *path, | |
1258 | mode_t mq_mode, | |
1259 | long maxmsg, | |
e8da24a6 | 1260 | long msgsize) { |
916abb21 | 1261 | |
e8da24a6 | 1262 | _cleanup_close_ int fd = -1; |
916abb21 LP |
1263 | struct stat st; |
1264 | mode_t old_mask; | |
1265 | struct mq_attr _attr, *attr = NULL; | |
1266 | ||
1267 | assert(path); | |
916abb21 LP |
1268 | |
1269 | if (maxmsg > 0 && msgsize > 0) { | |
e8da24a6 LP |
1270 | _attr = (struct mq_attr) { |
1271 | .mq_flags = O_NONBLOCK, | |
1272 | .mq_maxmsg = maxmsg, | |
1273 | .mq_msgsize = msgsize, | |
1274 | }; | |
916abb21 LP |
1275 | attr = &_attr; |
1276 | } | |
1277 | ||
1278 | /* Enforce the right access mode for the mq */ | |
7be9df7d | 1279 | old_mask = umask(~mq_mode); |
916abb21 LP |
1280 | |
1281 | /* Include the original umask in our mask */ | |
e8da24a6 | 1282 | (void) umask(~mq_mode | old_mask); |
916abb21 | 1283 | fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr); |
e8da24a6 | 1284 | (void) umask(old_mask); |
916abb21 | 1285 | |
e8da24a6 LP |
1286 | if (fd < 0) |
1287 | return -errno; | |
916abb21 | 1288 | |
e8da24a6 LP |
1289 | if (fstat(fd, &st) < 0) |
1290 | return -errno; | |
916abb21 LP |
1291 | |
1292 | if ((st.st_mode & 0777) != (mq_mode & ~old_mask) || | |
1293 | st.st_uid != getuid() || | |
e8da24a6 LP |
1294 | st.st_gid != getgid()) |
1295 | return -EEXIST; | |
916abb21 | 1296 | |
c10d6bdb | 1297 | return TAKE_FD(fd); |
916abb21 LP |
1298 | } |
1299 | ||
811ba7a0 LP |
1300 | static int socket_symlink(Socket *s) { |
1301 | const char *p; | |
1302 | char **i; | |
95f7fbbf | 1303 | int r; |
811ba7a0 LP |
1304 | |
1305 | assert(s); | |
1306 | ||
1307 | p = socket_find_symlink_target(s); | |
1308 | if (!p) | |
1309 | return 0; | |
1310 | ||
95f7fbbf | 1311 | STRV_FOREACH(i, s->symlinks) { |
1af87ab7 LP |
1312 | (void) mkdir_parents_label(*i, s->directory_mode); |
1313 | ||
6c9c51e5 | 1314 | r = symlink_idempotent(p, *i, false); |
22b20752 LP |
1315 | |
1316 | if (r == -EEXIST && s->remove_on_stop) { | |
1317 | /* If there's already something where we want to create the symlink, and the destructive | |
1318 | * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try | |
1319 | * again. */ | |
1320 | ||
1321 | if (unlink(*i) >= 0) | |
6c9c51e5 | 1322 | r = symlink_idempotent(p, *i, false); |
22b20752 LP |
1323 | } |
1324 | ||
95f7fbbf LP |
1325 | if (r < 0) |
1326 | log_unit_warning_errno(UNIT(s), r, "Failed to create symlink %s → %s, ignoring: %m", p, *i); | |
1327 | } | |
811ba7a0 LP |
1328 | |
1329 | return 0; | |
1330 | } | |
1331 | ||
36078102 | 1332 | static int usbffs_write_descs(int fd, Service *s) { |
6b7e5923 PS |
1333 | int r; |
1334 | ||
1335 | if (!s->usb_function_descriptors || !s->usb_function_strings) | |
1336 | return -EINVAL; | |
1337 | ||
1c876927 | 1338 | r = copy_file_fd(s->usb_function_descriptors, fd, 0); |
6b7e5923 | 1339 | if (r < 0) |
e8da24a6 | 1340 | return r; |
6b7e5923 | 1341 | |
1c876927 | 1342 | return copy_file_fd(s->usb_function_strings, fd, 0); |
6b7e5923 PS |
1343 | } |
1344 | ||
36078102 | 1345 | static int usbffs_select_ep(const struct dirent *d) { |
60252446 PS |
1346 | return d->d_name[0] != '.' && !streq(d->d_name, "ep0"); |
1347 | } | |
1348 | ||
36078102 | 1349 | static int usbffs_dispatch_eps(SocketPort *p) { |
60252446 | 1350 | _cleanup_free_ struct dirent **ent = NULL; |
da6053d0 LP |
1351 | size_t n, k, i; |
1352 | int r; | |
60252446 | 1353 | |
00bb64ec | 1354 | r = scandir(p->path, &ent, usbffs_select_ep, alphasort); |
60252446 PS |
1355 | if (r < 0) |
1356 | return -errno; | |
1357 | ||
da6053d0 | 1358 | n = (size_t) r; |
60252446 | 1359 | p->auxiliary_fds = new(int, n); |
0de48764 YW |
1360 | if (!p->auxiliary_fds) { |
1361 | r = -ENOMEM; | |
1362 | goto clear; | |
1363 | } | |
60252446 PS |
1364 | |
1365 | p->n_auxiliary_fds = n; | |
1366 | ||
1367 | k = 0; | |
1368 | for (i = 0; i < n; ++i) { | |
1369 | _cleanup_free_ char *ep = NULL; | |
1370 | ||
00bb64ec | 1371 | ep = path_make_absolute(ent[i]->d_name, p->path); |
0de48764 YW |
1372 | if (!ep) { |
1373 | r = -ENOMEM; | |
1374 | goto fail; | |
1375 | } | |
60252446 | 1376 | |
858d36c1 | 1377 | path_simplify(ep, false); |
60252446 | 1378 | |
36078102 | 1379 | r = usbffs_address_create(ep); |
60252446 PS |
1380 | if (r < 0) |
1381 | goto fail; | |
1382 | ||
da6053d0 | 1383 | p->auxiliary_fds[k++] = r; |
60252446 PS |
1384 | } |
1385 | ||
0de48764 YW |
1386 | r = 0; |
1387 | goto clear; | |
60252446 PS |
1388 | |
1389 | fail: | |
e8da24a6 | 1390 | close_many(p->auxiliary_fds, k); |
60252446 PS |
1391 | p->auxiliary_fds = mfree(p->auxiliary_fds); |
1392 | p->n_auxiliary_fds = 0; | |
1393 | ||
0de48764 YW |
1394 | clear: |
1395 | for (i = 0; i < n; ++i) | |
1396 | free(ent[i]); | |
1397 | ||
60252446 PS |
1398 | return r; |
1399 | } | |
1400 | ||
d24e561d | 1401 | static int socket_determine_selinux_label(Socket *s, char **ret) { |
416be1a0 | 1402 | Service *service; |
d24e561d | 1403 | ExecCommand *c; |
2ef044ea | 1404 | _cleanup_free_ char *path = NULL; |
d24e561d LP |
1405 | int r; |
1406 | ||
1407 | assert(s); | |
1408 | assert(ret); | |
1409 | ||
1410 | if (s->selinux_context_from_net) { | |
1411 | /* If this is requested, get label from the network label */ | |
1412 | ||
1413 | r = mac_selinux_get_our_label(ret); | |
1414 | if (r == -EOPNOTSUPP) | |
1415 | goto no_label; | |
1416 | ||
1417 | } else { | |
1418 | /* Otherwise, get it from the executable we are about to start */ | |
1419 | r = socket_instantiate_service(s); | |
1420 | if (r < 0) | |
1421 | return r; | |
1422 | ||
1423 | if (!UNIT_ISSET(s->service)) | |
1424 | goto no_label; | |
1425 | ||
416be1a0 FB |
1426 | service = SERVICE(UNIT_DEREF(s->service)); |
1427 | c = service->exec_command[SERVICE_EXEC_START]; | |
d24e561d LP |
1428 | if (!c) |
1429 | goto no_label; | |
1430 | ||
a5648b80 | 1431 | r = chase_symlinks(c->path, service->exec_context.root_directory, CHASE_PREFIX_ROOT, &path, NULL); |
2ef044ea FB |
1432 | if (r < 0) |
1433 | goto no_label; | |
1434 | ||
416be1a0 | 1435 | r = mac_selinux_get_create_label_from_exe(path, ret); |
4c701096 | 1436 | if (IN_SET(r, -EPERM, -EOPNOTSUPP)) |
d24e561d LP |
1437 | goto no_label; |
1438 | } | |
1439 | ||
1440 | return r; | |
1441 | ||
1442 | no_label: | |
1443 | *ret = NULL; | |
1444 | return 0; | |
1445 | } | |
1446 | ||
a79279c7 LP |
1447 | static int socket_address_listen_do( |
1448 | Socket *s, | |
1449 | const SocketAddress *address, | |
1450 | const char *label) { | |
1451 | ||
1452 | assert(s); | |
1453 | assert(address); | |
1454 | ||
1455 | return socket_address_listen( | |
1456 | address, | |
1457 | SOCK_CLOEXEC|SOCK_NONBLOCK, | |
1458 | s->backlog, | |
1459 | s->bind_ipv6_only, | |
1460 | s->bind_to_device, | |
1461 | s->reuse_port, | |
1462 | s->free_bind, | |
1463 | s->transparent, | |
1464 | s->directory_mode, | |
1465 | s->socket_mode, | |
1466 | label); | |
1467 | } | |
1468 | ||
65486032 YW |
1469 | #define log_address_error_errno(u, address, error, fmt) \ |
1470 | ({ \ | |
1471 | _cleanup_free_ char *_t = NULL; \ | |
1472 | \ | |
1473 | (void) socket_address_print(address, &_t); \ | |
1474 | log_unit_error_errno(u, error, fmt, strna(_t)); \ | |
1475 | }) | |
ae05e1b6 | 1476 | |
7619cb32 LP |
1477 | static int fork_needed(const SocketAddress *address, const ExecContext *context) { |
1478 | int r; | |
1479 | ||
1480 | assert(address); | |
1481 | assert(context); | |
1482 | ||
1483 | /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */ | |
1484 | ||
1485 | if (IN_SET(address->sockaddr.sa.sa_family, AF_INET, AF_INET6)) { | |
1486 | r = bpf_firewall_supported(); | |
1487 | if (r < 0) | |
1488 | return r; | |
1489 | if (r != BPF_FIREWALL_UNSUPPORTED) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */ | |
1490 | return true; | |
1491 | } | |
1492 | ||
1493 | return context->private_network || context->network_namespace_path; | |
1494 | } | |
1495 | ||
a79279c7 LP |
1496 | static int socket_address_listen_in_cgroup( |
1497 | Socket *s, | |
1498 | const SocketAddress *address, | |
1499 | const char *label) { | |
1500 | ||
1501 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; | |
1502 | int fd, r; | |
1503 | pid_t pid; | |
1504 | ||
1505 | assert(s); | |
1506 | assert(address); | |
1507 | ||
7619cb32 LP |
1508 | /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the |
1509 | * socket's cgroup and network namespace in which the socket is actually created. This way we ensure | |
1510 | * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and | |
1511 | * such. */ | |
a79279c7 | 1512 | |
7619cb32 | 1513 | r = fork_needed(address, &s->exec_context); |
a79279c7 LP |
1514 | if (r < 0) |
1515 | return r; | |
7619cb32 LP |
1516 | if (r == 0) { |
1517 | /* Shortcut things... */ | |
1518 | fd = socket_address_listen_do(s, address, label); | |
1519 | if (fd < 0) | |
1520 | return log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m"); | |
1521 | ||
1522 | return fd; | |
1523 | } | |
1524 | ||
1525 | r = unit_setup_exec_runtime(UNIT(s)); | |
1526 | if (r < 0) | |
1527 | return log_unit_error_errno(UNIT(s), r, "Failed acquire runtime: %m"); | |
1528 | ||
1529 | if (s->exec_context.network_namespace_path && | |
1530 | s->exec_runtime && | |
1531 | s->exec_runtime->netns_storage_socket[0] >= 0) { | |
1532 | r = open_netns_path(s->exec_runtime->netns_storage_socket, s->exec_context.network_namespace_path); | |
1533 | if (r < 0) | |
1534 | return log_unit_error_errno(UNIT(s), r, "Failed to open network namespace path %s: %m", s->exec_context.network_namespace_path); | |
1535 | } | |
a79279c7 LP |
1536 | |
1537 | if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) | |
1538 | return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); | |
1539 | ||
4c253ed1 | 1540 | r = unit_fork_helper_process(UNIT(s), "(sd-listen)", &pid); |
a79279c7 LP |
1541 | if (r < 0) |
1542 | return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m"); | |
1543 | if (r == 0) { | |
1544 | /* Child */ | |
1545 | ||
1546 | pair[0] = safe_close(pair[0]); | |
1547 | ||
7619cb32 LP |
1548 | if ((s->exec_context.private_network || s->exec_context.network_namespace_path) && |
1549 | s->exec_runtime && | |
1550 | s->exec_runtime->netns_storage_socket[0] >= 0) { | |
1551 | ||
1552 | if (ns_type_supported(NAMESPACE_NET)) { | |
1553 | r = setup_netns(s->exec_runtime->netns_storage_socket); | |
1554 | if (r < 0) { | |
1555 | log_unit_error_errno(UNIT(s), r, "Failed to join network namespace: %m"); | |
1556 | _exit(EXIT_NETWORK); | |
1557 | } | |
1558 | } else if (s->exec_context.network_namespace_path) { | |
1559 | log_unit_error(UNIT(s), "Network namespace path configured but network namespaces not supported."); | |
1560 | _exit(EXIT_NETWORK); | |
1561 | } else | |
1562 | log_unit_warning(UNIT(s), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring."); | |
1563 | } | |
1564 | ||
a79279c7 LP |
1565 | fd = socket_address_listen_do(s, address, label); |
1566 | if (fd < 0) { | |
ae05e1b6 | 1567 | log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m"); |
a79279c7 LP |
1568 | _exit(EXIT_FAILURE); |
1569 | } | |
1570 | ||
1571 | r = send_one_fd(pair[1], fd, 0); | |
1572 | if (r < 0) { | |
ae05e1b6 | 1573 | log_address_error_errno(UNIT(s), address, r, "Failed to send listening socket (%s) to parent: %m"); |
a79279c7 LP |
1574 | _exit(EXIT_FAILURE); |
1575 | } | |
1576 | ||
1577 | _exit(EXIT_SUCCESS); | |
1578 | } | |
1579 | ||
1580 | pair[1] = safe_close(pair[1]); | |
1581 | fd = receive_one_fd(pair[0], 0); | |
1582 | ||
1583 | /* We synchronously wait for the helper, as it shouldn't be slow */ | |
d2e0ac3d | 1584 | r = wait_for_terminate_and_check("(sd-listen)", pid, WAIT_LOG_ABNORMAL); |
a79279c7 LP |
1585 | if (r < 0) { |
1586 | safe_close(fd); | |
1587 | return r; | |
1588 | } | |
1589 | ||
1590 | if (fd < 0) | |
ae05e1b6 | 1591 | return log_address_error_errno(UNIT(s), address, fd, "Failed to receive listening socket (%s): %m"); |
a79279c7 LP |
1592 | |
1593 | return fd; | |
a79279c7 LP |
1594 | } |
1595 | ||
e5417345 YW |
1596 | DEFINE_TRIVIAL_CLEANUP_FUNC(Socket *, socket_close_fds); |
1597 | ||
1598 | static int socket_open_fds(Socket *_s) { | |
1599 | _cleanup_(socket_close_fdsp) Socket *s = _s; | |
710a6b50 LP |
1600 | _cleanup_(mac_selinux_freep) char *label = NULL; |
1601 | bool know_label = false; | |
83c60c9f LP |
1602 | SocketPort *p; |
1603 | int r; | |
1604 | ||
1605 | assert(s); | |
1606 | ||
034c6ed7 | 1607 | LIST_FOREACH(port, p, s->ports) { |
83c60c9f | 1608 | |
034c6ed7 LP |
1609 | if (p->fd >= 0) |
1610 | continue; | |
83c60c9f | 1611 | |
00411a13 LP |
1612 | switch (p->type) { |
1613 | ||
1614 | case SOCKET_SOCKET: | |
049f8642 | 1615 | |
7f416dae | 1616 | if (!know_label) { |
d24e561d LP |
1617 | /* Figure out label, if we don't it know yet. We do it once, for the first socket where |
1618 | * we need this and remember it for the rest. */ | |
7f416dae | 1619 | |
d24e561d LP |
1620 | r = socket_determine_selinux_label(s, &label); |
1621 | if (r < 0) | |
ae05e1b6 | 1622 | return log_unit_error_errno(UNIT(s), r, "Failed to determine SELinux label: %m"); |
049f8642 LP |
1623 | |
1624 | know_label = true; | |
1625 | } | |
1626 | ||
74bb646e | 1627 | /* Apply the socket protocol */ |
d24e561d | 1628 | switch (p->address.type) { |
d2a50e3b | 1629 | |
74bb646e SS |
1630 | case SOCK_STREAM: |
1631 | case SOCK_SEQPACKET: | |
d24e561d LP |
1632 | if (s->socket_protocol == IPPROTO_SCTP) |
1633 | p->address.protocol = s->socket_protocol; | |
74bb646e | 1634 | break; |
d2a50e3b | 1635 | |
74bb646e | 1636 | case SOCK_DGRAM: |
d24e561d LP |
1637 | if (s->socket_protocol == IPPROTO_UDPLITE) |
1638 | p->address.protocol = s->socket_protocol; | |
74bb646e SS |
1639 | break; |
1640 | } | |
1641 | ||
d501e52b YW |
1642 | p->fd = socket_address_listen_in_cgroup(s, &p->address, label); |
1643 | if (p->fd < 0) | |
1644 | return p->fd; | |
83c60c9f | 1645 | |
4fd5948e | 1646 | socket_apply_socket_options(s, p->fd); |
811ba7a0 | 1647 | socket_symlink(s); |
00411a13 | 1648 | break; |
4fd5948e | 1649 | |
00411a13 | 1650 | case SOCKET_SPECIAL: |
b0a3f2bc | 1651 | |
d501e52b YW |
1652 | p->fd = special_address_create(p->path, s->writable); |
1653 | if (p->fd < 0) | |
1654 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open special file %s: %m", p->path); | |
00411a13 | 1655 | break; |
b0a3f2bc | 1656 | |
00411a13 | 1657 | case SOCKET_FIFO: |
83c60c9f | 1658 | |
d501e52b | 1659 | p->fd = fifo_address_create( |
175a3d25 LP |
1660 | p->path, |
1661 | s->directory_mode, | |
e8da24a6 | 1662 | s->socket_mode); |
d501e52b YW |
1663 | if (p->fd < 0) |
1664 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open FIFO %s: %m", p->path); | |
83c60c9f | 1665 | |
b15bdda8 | 1666 | socket_apply_fifo_options(s, p->fd); |
811ba7a0 | 1667 | socket_symlink(s); |
00411a13 | 1668 | break; |
811ba7a0 | 1669 | |
00411a13 | 1670 | case SOCKET_MQUEUE: |
83c60c9f | 1671 | |
d501e52b | 1672 | p->fd = mq_address_create( |
175a3d25 LP |
1673 | p->path, |
1674 | s->socket_mode, | |
1675 | s->mq_maxmsg, | |
e8da24a6 | 1676 | s->mq_msgsize); |
d501e52b YW |
1677 | if (p->fd < 0) |
1678 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open message queue %s: %m", p->path); | |
00411a13 | 1679 | break; |
e8da24a6 | 1680 | |
d2a50e3b | 1681 | case SOCKET_USB_FUNCTION: { |
27a6ea91 | 1682 | _cleanup_free_ char *ep = NULL; |
60252446 | 1683 | |
27a6ea91 GB |
1684 | ep = path_make_absolute("ep0", p->path); |
1685 | ||
d501e52b YW |
1686 | p->fd = usbffs_address_create(ep); |
1687 | if (p->fd < 0) | |
1688 | return p->fd; | |
60252446 | 1689 | |
36078102 | 1690 | r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service))); |
6b7e5923 | 1691 | if (r < 0) |
e5417345 | 1692 | return r; |
6b7e5923 | 1693 | |
36078102 | 1694 | r = usbffs_dispatch_eps(p); |
60252446 | 1695 | if (r < 0) |
e5417345 | 1696 | return r; |
00411a13 LP |
1697 | |
1698 | break; | |
27a6ea91 | 1699 | } |
00411a13 | 1700 | default: |
b15bdda8 | 1701 | assert_not_reached("Unknown port type"); |
00411a13 | 1702 | } |
034c6ed7 LP |
1703 | } |
1704 | ||
e5417345 | 1705 | s = NULL; |
034c6ed7 | 1706 | return 0; |
034c6ed7 LP |
1707 | } |
1708 | ||
1709 | static void socket_unwatch_fds(Socket *s) { | |
1710 | SocketPort *p; | |
718db961 | 1711 | int r; |
9152c765 | 1712 | |
034c6ed7 LP |
1713 | assert(s); |
1714 | ||
1715 | LIST_FOREACH(port, p, s->ports) { | |
1716 | if (p->fd < 0) | |
1717 | continue; | |
1718 | ||
a4152e3f LP |
1719 | if (!p->event_source) |
1720 | continue; | |
1721 | ||
1722 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF); | |
1723 | if (r < 0) | |
f2341e0a | 1724 | log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m"); |
83c60c9f | 1725 | } |
034c6ed7 LP |
1726 | } |
1727 | ||
1728 | static int socket_watch_fds(Socket *s) { | |
1729 | SocketPort *p; | |
1730 | int r; | |
1731 | ||
1732 | assert(s); | |
83c60c9f | 1733 | |
034c6ed7 LP |
1734 | LIST_FOREACH(port, p, s->ports) { |
1735 | if (p->fd < 0) | |
1736 | continue; | |
1737 | ||
cbf60d0a | 1738 | if (p->event_source) { |
718db961 | 1739 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON); |
cbf60d0a LP |
1740 | if (r < 0) |
1741 | goto fail; | |
1742 | } else { | |
151b9b96 | 1743 | r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p); |
cbf60d0a LP |
1744 | if (r < 0) |
1745 | goto fail; | |
4f2d528d | 1746 | |
cbf60d0a | 1747 | (void) sd_event_source_set_description(p->event_source, "socket-port-io"); |
718db961 | 1748 | } |
034c6ed7 | 1749 | } |
83c60c9f | 1750 | |
542563ba | 1751 | return 0; |
83c60c9f | 1752 | |
034c6ed7 | 1753 | fail: |
cbf60d0a | 1754 | log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m"); |
034c6ed7 LP |
1755 | socket_unwatch_fds(s); |
1756 | return r; | |
1757 | } | |
1758 | ||
01a8b467 LP |
1759 | enum { |
1760 | SOCKET_OPEN_NONE, | |
1761 | SOCKET_OPEN_SOME, | |
1762 | SOCKET_OPEN_ALL, | |
1763 | }; | |
1764 | ||
1765 | static int socket_check_open(Socket *s) { | |
1766 | bool have_open = false, have_closed = false; | |
1767 | SocketPort *p; | |
1768 | ||
1769 | assert(s); | |
1770 | ||
1771 | LIST_FOREACH(port, p, s->ports) { | |
1772 | if (p->fd < 0) | |
1773 | have_closed = true; | |
1774 | else | |
1775 | have_open = true; | |
1776 | ||
1777 | if (have_open && have_closed) | |
1778 | return SOCKET_OPEN_SOME; | |
1779 | } | |
1780 | ||
1781 | if (have_open) | |
1782 | return SOCKET_OPEN_ALL; | |
1783 | ||
1784 | return SOCKET_OPEN_NONE; | |
1785 | } | |
1786 | ||
034c6ed7 LP |
1787 | static void socket_set_state(Socket *s, SocketState state) { |
1788 | SocketState old_state; | |
1789 | assert(s); | |
1790 | ||
6fcbec6f LP |
1791 | if (s->state != state) |
1792 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
1793 | ||
034c6ed7 LP |
1794 | old_state = s->state; |
1795 | s->state = state; | |
1796 | ||
bd1fe7c7 LP |
1797 | if (!IN_SET(state, |
1798 | SOCKET_START_PRE, | |
3900e5fd | 1799 | SOCKET_START_CHOWN, |
bd1fe7c7 LP |
1800 | SOCKET_START_POST, |
1801 | SOCKET_STOP_PRE, | |
1802 | SOCKET_STOP_PRE_SIGTERM, | |
1803 | SOCKET_STOP_PRE_SIGKILL, | |
1804 | SOCKET_STOP_POST, | |
1805 | SOCKET_FINAL_SIGTERM, | |
c968d76a YW |
1806 | SOCKET_FINAL_SIGKILL, |
1807 | SOCKET_CLEANING)) { | |
718db961 LP |
1808 | |
1809 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
5e94833f | 1810 | socket_unwatch_control_pid(s); |
034c6ed7 | 1811 | s->control_command = NULL; |
a16e1123 | 1812 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; |
e537352b | 1813 | } |
034c6ed7 | 1814 | |
a16e1123 LP |
1815 | if (state != SOCKET_LISTENING) |
1816 | socket_unwatch_fds(s); | |
1817 | ||
bd1fe7c7 | 1818 | if (!IN_SET(state, |
3900e5fd | 1819 | SOCKET_START_CHOWN, |
bd1fe7c7 LP |
1820 | SOCKET_START_POST, |
1821 | SOCKET_LISTENING, | |
1822 | SOCKET_RUNNING, | |
1823 | SOCKET_STOP_PRE, | |
1824 | SOCKET_STOP_PRE_SIGTERM, | |
c968d76a YW |
1825 | SOCKET_STOP_PRE_SIGKILL, |
1826 | SOCKET_CLEANING)) | |
034c6ed7 LP |
1827 | socket_close_fds(s); |
1828 | ||
e537352b | 1829 | if (state != old_state) |
f2341e0a | 1830 | log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state)); |
acbb0225 | 1831 | |
2ad2e41a | 1832 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0); |
034c6ed7 LP |
1833 | } |
1834 | ||
be847e82 | 1835 | static int socket_coldplug(Unit *u) { |
a16e1123 LP |
1836 | Socket *s = SOCKET(u); |
1837 | int r; | |
1838 | ||
1839 | assert(s); | |
1840 | assert(s->state == SOCKET_DEAD); | |
1841 | ||
e821075a LP |
1842 | if (s->deserialized_state == s->state) |
1843 | return 0; | |
a16e1123 | 1844 | |
c386f588 LP |
1845 | if (s->control_pid > 0 && |
1846 | pid_is_unwaited(s->control_pid) && | |
1847 | IN_SET(s->deserialized_state, | |
3900e5fd LP |
1848 | SOCKET_START_PRE, |
1849 | SOCKET_START_CHOWN, | |
1850 | SOCKET_START_POST, | |
1851 | SOCKET_STOP_PRE, | |
1852 | SOCKET_STOP_PRE_SIGTERM, | |
1853 | SOCKET_STOP_PRE_SIGKILL, | |
1854 | SOCKET_STOP_POST, | |
1855 | SOCKET_FINAL_SIGTERM, | |
c968d76a YW |
1856 | SOCKET_FINAL_SIGKILL, |
1857 | SOCKET_CLEANING)) { | |
a16e1123 | 1858 | |
f75f613d | 1859 | r = unit_watch_pid(UNIT(s), s->control_pid, false); |
e821075a LP |
1860 | if (r < 0) |
1861 | return r; | |
a16e1123 | 1862 | |
36c16a7c | 1863 | r = socket_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec)); |
e821075a LP |
1864 | if (r < 0) |
1865 | return r; | |
1866 | } | |
a16e1123 | 1867 | |
3900e5fd LP |
1868 | if (IN_SET(s->deserialized_state, |
1869 | SOCKET_START_CHOWN, | |
1870 | SOCKET_START_POST, | |
1871 | SOCKET_LISTENING, | |
01a8b467 LP |
1872 | SOCKET_RUNNING)) { |
1873 | ||
1874 | /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors | |
5238e957 | 1875 | * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for |
01a8b467 LP |
1876 | * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open, |
1877 | * and if there's a mismatch, warn loudly. */ | |
1878 | ||
1879 | r = socket_check_open(s); | |
1880 | if (r == SOCKET_OPEN_NONE) | |
1881 | log_unit_warning(UNIT(s), | |
1882 | "Socket unit configuration has changed while unit has been running, " | |
1883 | "no open socket file descriptor left. " | |
1884 | "The socket unit is not functional until restarted."); | |
1885 | else if (r == SOCKET_OPEN_SOME) | |
1886 | log_unit_warning(UNIT(s), | |
1887 | "Socket unit configuration has changed while unit has been running, " | |
1888 | "and some socket file descriptors have not been opened yet. " | |
1889 | "The socket unit is not fully functional until restarted."); | |
e821075a | 1890 | } |
a16e1123 | 1891 | |
e821075a LP |
1892 | if (s->deserialized_state == SOCKET_LISTENING) { |
1893 | r = socket_watch_fds(s); | |
1894 | if (r < 0) | |
1895 | return r; | |
a16e1123 LP |
1896 | } |
1897 | ||
c968d76a | 1898 | if (!IN_SET(s->deserialized_state, SOCKET_DEAD, SOCKET_FAILED, SOCKET_CLEANING)) { |
29206d46 | 1899 | (void) unit_setup_dynamic_creds(u); |
e8a565cb YW |
1900 | (void) unit_setup_exec_runtime(u); |
1901 | } | |
29206d46 | 1902 | |
e821075a | 1903 | socket_set_state(s, s->deserialized_state); |
a16e1123 LP |
1904 | return 0; |
1905 | } | |
1906 | ||
e537352b | 1907 | static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { |
3c7416b6 | 1908 | |
b9c04eaf | 1909 | _cleanup_(exec_params_clear) ExecParameters exec_params = { |
5686391b LP |
1910 | .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN, |
1911 | .stdin_fd = -1, | |
1912 | .stdout_fd = -1, | |
1913 | .stderr_fd = -1, | |
1914 | .exec_fd = -1, | |
9fa95f85 | 1915 | }; |
3c7416b6 LP |
1916 | pid_t pid; |
1917 | int r; | |
034c6ed7 LP |
1918 | |
1919 | assert(s); | |
1920 | assert(c); | |
1921 | assert(_pid); | |
1922 | ||
3c7416b6 | 1923 | r = unit_prepare_exec(UNIT(s)); |
29206d46 LP |
1924 | if (r < 0) |
1925 | return r; | |
1926 | ||
36c16a7c | 1927 | r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec)); |
36697dc0 | 1928 | if (r < 0) |
36c16a7c | 1929 | return r; |
034c6ed7 | 1930 | |
1ad6e8b3 LP |
1931 | r = unit_set_exec_params(UNIT(s), &exec_params); |
1932 | if (r < 0) | |
1933 | return r; | |
3536f49e | 1934 | |
f2341e0a LP |
1935 | r = exec_spawn(UNIT(s), |
1936 | c, | |
9e2f7c11 | 1937 | &s->exec_context, |
9fa95f85 | 1938 | &exec_params, |
613b411c | 1939 | s->exec_runtime, |
29206d46 | 1940 | &s->dynamic_creds, |
9e2f7c11 | 1941 | &pid); |
cee288ad | 1942 | if (r < 0) |
36c16a7c | 1943 | return r; |
9e2f7c11 | 1944 | |
f75f613d | 1945 | r = unit_watch_pid(UNIT(s), pid, true); |
9e2f7c11 | 1946 | if (r < 0) |
36c16a7c | 1947 | return r; |
034c6ed7 | 1948 | |
3900e5fd | 1949 | *_pid = pid; |
3536f49e | 1950 | |
3900e5fd | 1951 | return 0; |
3900e5fd LP |
1952 | } |
1953 | ||
1954 | static int socket_chown(Socket *s, pid_t *_pid) { | |
1955 | pid_t pid; | |
1956 | int r; | |
1957 | ||
36c16a7c | 1958 | r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec)); |
3900e5fd LP |
1959 | if (r < 0) |
1960 | goto fail; | |
1961 | ||
1962 | /* We have to resolve the user names out-of-process, hence | |
1963 | * let's fork here. It's messy, but well, what can we do? */ | |
1964 | ||
4c253ed1 | 1965 | r = unit_fork_helper_process(UNIT(s), "(sd-chown)", &pid); |
a79279c7 LP |
1966 | if (r < 0) |
1967 | return r; | |
1968 | if (r == 0) { | |
fed1e721 LP |
1969 | uid_t uid = UID_INVALID; |
1970 | gid_t gid = GID_INVALID; | |
a79279c7 | 1971 | SocketPort *p; |
3900e5fd | 1972 | |
a79279c7 | 1973 | /* Child */ |
3900e5fd LP |
1974 | |
1975 | if (!isempty(s->user)) { | |
1976 | const char *user = s->user; | |
1977 | ||
fafff8f1 | 1978 | r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); |
3900e5fd | 1979 | if (r < 0) { |
a79279c7 LP |
1980 | log_unit_error_errno(UNIT(s), r, "Failed to resolve user %s: %m", user); |
1981 | _exit(EXIT_USER); | |
3900e5fd LP |
1982 | } |
1983 | } | |
1984 | ||
1985 | if (!isempty(s->group)) { | |
1986 | const char *group = s->group; | |
1987 | ||
fafff8f1 | 1988 | r = get_group_creds(&group, &gid, 0); |
3900e5fd | 1989 | if (r < 0) { |
a79279c7 LP |
1990 | log_unit_error_errno(UNIT(s), r, "Failed to resolve group %s: %m", group); |
1991 | _exit(EXIT_GROUP); | |
3900e5fd LP |
1992 | } |
1993 | } | |
1994 | ||
1995 | LIST_FOREACH(port, p, s->ports) { | |
e5a1c18d | 1996 | const char *path = NULL; |
3900e5fd LP |
1997 | |
1998 | if (p->type == SOCKET_SOCKET) | |
1999 | path = socket_address_get_path(&p->address); | |
2000 | else if (p->type == SOCKET_FIFO) | |
2001 | path = p->path; | |
2002 | ||
2003 | if (!path) | |
2004 | continue; | |
2005 | ||
2006 | if (chown(path, uid, gid) < 0) { | |
a79279c7 LP |
2007 | log_unit_error_errno(UNIT(s), errno, "Failed to chown(): %m"); |
2008 | _exit(EXIT_CHOWN); | |
3900e5fd LP |
2009 | } |
2010 | } | |
2011 | ||
a79279c7 | 2012 | _exit(EXIT_SUCCESS); |
3900e5fd LP |
2013 | } |
2014 | ||
f75f613d | 2015 | r = unit_watch_pid(UNIT(s), pid, true); |
718db961 | 2016 | if (r < 0) |
034c6ed7 | 2017 | goto fail; |
83c60c9f | 2018 | |
034c6ed7 | 2019 | *_pid = pid; |
034c6ed7 LP |
2020 | return 0; |
2021 | ||
2022 | fail: | |
718db961 | 2023 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
83c60c9f | 2024 | return r; |
542563ba LP |
2025 | } |
2026 | ||
cfc4eb4c | 2027 | static void socket_enter_dead(Socket *s, SocketResult f) { |
034c6ed7 LP |
2028 | assert(s); |
2029 | ||
a0fef983 | 2030 | if (s->result == SOCKET_SUCCESS) |
cfc4eb4c | 2031 | s->result = f; |
034c6ed7 | 2032 | |
523ee2d4 LP |
2033 | if (s->result == SOCKET_SUCCESS) |
2034 | unit_log_success(UNIT(s)); | |
2035 | else | |
7c047d74 | 2036 | unit_log_failure(UNIT(s), socket_result_to_string(s->result)); |
ed77d407 | 2037 | |
4c425434 LP |
2038 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop); |
2039 | ||
29206d46 LP |
2040 | socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD); |
2041 | ||
e8a565cb | 2042 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); |
613b411c | 2043 | |
52a12341 | 2044 | unit_destroy_runtime_directory(UNIT(s), &s->exec_context); |
e66cf1a3 | 2045 | |
00d9ef85 LP |
2046 | unit_unref_uid_gid(UNIT(s), true); |
2047 | ||
29206d46 | 2048 | dynamic_creds_destroy(&s->dynamic_creds); |
034c6ed7 LP |
2049 | } |
2050 | ||
cfc4eb4c | 2051 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f); |
80876c20 | 2052 | |
cfc4eb4c | 2053 | static void socket_enter_stop_post(Socket *s, SocketResult f) { |
034c6ed7 LP |
2054 | int r; |
2055 | assert(s); | |
2056 | ||
a0fef983 | 2057 | if (s->result == SOCKET_SUCCESS) |
cfc4eb4c | 2058 | s->result = f; |
034c6ed7 | 2059 | |
5e94833f | 2060 | socket_unwatch_control_pid(s); |
a16e1123 | 2061 | s->control_command_id = SOCKET_EXEC_STOP_POST; |
3900e5fd | 2062 | s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]; |
a16e1123 | 2063 | |
3900e5fd LP |
2064 | if (s->control_command) { |
2065 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2066 | if (r < 0) | |
034c6ed7 LP |
2067 | goto fail; |
2068 | ||
80876c20 LP |
2069 | socket_set_state(s, SOCKET_STOP_POST); |
2070 | } else | |
cfc4eb4c | 2071 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS); |
034c6ed7 LP |
2072 | |
2073 | return; | |
2074 | ||
2075 | fail: | |
f2341e0a | 2076 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); |
cfc4eb4c | 2077 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2078 | } |
2079 | ||
a232ebcc ZJS |
2080 | static int state_to_kill_operation(Socket *s, SocketState state) { |
2081 | if (state == SOCKET_STOP_PRE_SIGTERM && unit_has_job_type(UNIT(s), JOB_RESTART)) | |
2082 | return KILL_RESTART; | |
2083 | ||
2084 | if (state == SOCKET_FINAL_SIGTERM) | |
2085 | return KILL_TERMINATE; | |
2086 | ||
2087 | return KILL_KILL; | |
2088 | } | |
2089 | ||
cfc4eb4c | 2090 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { |
034c6ed7 LP |
2091 | int r; |
2092 | ||
2093 | assert(s); | |
2094 | ||
a0fef983 | 2095 | if (s->result == SOCKET_SUCCESS) |
cfc4eb4c | 2096 | s->result = f; |
034c6ed7 | 2097 | |
cd2086fe LP |
2098 | r = unit_kill_context( |
2099 | UNIT(s), | |
2100 | &s->kill_context, | |
a232ebcc | 2101 | state_to_kill_operation(s, state), |
cd2086fe LP |
2102 | -1, |
2103 | s->control_pid, | |
2104 | false); | |
2105 | if (r < 0) | |
2106 | goto fail; | |
034c6ed7 | 2107 | |
cd2086fe | 2108 | if (r > 0) { |
36c16a7c | 2109 | r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec)); |
36697dc0 | 2110 | if (r < 0) |
80876c20 | 2111 | goto fail; |
d6ea93e3 | 2112 | |
80876c20 | 2113 | socket_set_state(s, state); |
ac84d1fb LP |
2114 | } else if (state == SOCKET_STOP_PRE_SIGTERM) |
2115 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS); | |
2116 | else if (state == SOCKET_STOP_PRE_SIGKILL) | |
cfc4eb4c | 2117 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
ac84d1fb LP |
2118 | else if (state == SOCKET_FINAL_SIGTERM) |
2119 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); | |
80876c20 | 2120 | else |
cfc4eb4c | 2121 | socket_enter_dead(s, SOCKET_SUCCESS); |
034c6ed7 LP |
2122 | |
2123 | return; | |
2124 | ||
2125 | fail: | |
f2341e0a | 2126 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
034c6ed7 | 2127 | |
3742095b | 2128 | if (IN_SET(state, SOCKET_STOP_PRE_SIGTERM, SOCKET_STOP_PRE_SIGKILL)) |
cfc4eb4c | 2129 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 2130 | else |
cfc4eb4c | 2131 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2132 | } |
2133 | ||
cfc4eb4c | 2134 | static void socket_enter_stop_pre(Socket *s, SocketResult f) { |
034c6ed7 LP |
2135 | int r; |
2136 | assert(s); | |
2137 | ||
a0fef983 | 2138 | if (s->result == SOCKET_SUCCESS) |
cfc4eb4c | 2139 | s->result = f; |
034c6ed7 | 2140 | |
5e94833f | 2141 | socket_unwatch_control_pid(s); |
a16e1123 | 2142 | s->control_command_id = SOCKET_EXEC_STOP_PRE; |
3900e5fd | 2143 | s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]; |
a16e1123 | 2144 | |
3900e5fd LP |
2145 | if (s->control_command) { |
2146 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2147 | if (r < 0) | |
034c6ed7 LP |
2148 | goto fail; |
2149 | ||
80876c20 LP |
2150 | socket_set_state(s, SOCKET_STOP_PRE); |
2151 | } else | |
cfc4eb4c | 2152 | socket_enter_stop_post(s, SOCKET_SUCCESS); |
034c6ed7 LP |
2153 | |
2154 | return; | |
2155 | ||
2156 | fail: | |
f2341e0a | 2157 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-pre' task: %m"); |
cfc4eb4c | 2158 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2159 | } |
2160 | ||
e9af15c3 LP |
2161 | static void socket_enter_listening(Socket *s) { |
2162 | int r; | |
2163 | assert(s); | |
2164 | ||
cfc4eb4c LP |
2165 | r = socket_watch_fds(s); |
2166 | if (r < 0) { | |
f2341e0a | 2167 | log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m"); |
e9af15c3 LP |
2168 | goto fail; |
2169 | } | |
2170 | ||
2171 | socket_set_state(s, SOCKET_LISTENING); | |
2172 | return; | |
2173 | ||
2174 | fail: | |
cfc4eb4c | 2175 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
e9af15c3 LP |
2176 | } |
2177 | ||
034c6ed7 LP |
2178 | static void socket_enter_start_post(Socket *s) { |
2179 | int r; | |
2180 | assert(s); | |
2181 | ||
3900e5fd LP |
2182 | socket_unwatch_control_pid(s); |
2183 | s->control_command_id = SOCKET_EXEC_START_POST; | |
2184 | s->control_command = s->exec_command[SOCKET_EXEC_START_POST]; | |
2185 | ||
2186 | if (s->control_command) { | |
2187 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2188 | if (r < 0) { | |
f2341e0a | 2189 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); |
3900e5fd LP |
2190 | goto fail; |
2191 | } | |
2192 | ||
2193 | socket_set_state(s, SOCKET_START_POST); | |
2194 | } else | |
2195 | socket_enter_listening(s); | |
2196 | ||
2197 | return; | |
2198 | ||
2199 | fail: | |
2200 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2201 | } | |
2202 | ||
2203 | static void socket_enter_start_chown(Socket *s) { | |
2204 | int r; | |
2205 | ||
2206 | assert(s); | |
2207 | ||
cfc4eb4c LP |
2208 | r = socket_open_fds(s); |
2209 | if (r < 0) { | |
f2341e0a | 2210 | log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m"); |
034c6ed7 LP |
2211 | goto fail; |
2212 | } | |
2213 | ||
3900e5fd | 2214 | if (!isempty(s->user) || !isempty(s->group)) { |
5e94833f | 2215 | |
3900e5fd LP |
2216 | socket_unwatch_control_pid(s); |
2217 | s->control_command_id = SOCKET_EXEC_START_CHOWN; | |
2218 | s->control_command = NULL; | |
a16e1123 | 2219 | |
3900e5fd | 2220 | r = socket_chown(s, &s->control_pid); |
cfc4eb4c | 2221 | if (r < 0) { |
f2341e0a | 2222 | log_unit_warning_errno(UNIT(s), r, "Failed to fork 'start-chown' task: %m"); |
034c6ed7 LP |
2223 | goto fail; |
2224 | } | |
2225 | ||
3900e5fd | 2226 | socket_set_state(s, SOCKET_START_CHOWN); |
80876c20 | 2227 | } else |
3900e5fd | 2228 | socket_enter_start_post(s); |
034c6ed7 LP |
2229 | |
2230 | return; | |
2231 | ||
2232 | fail: | |
cfc4eb4c | 2233 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2234 | } |
2235 | ||
2236 | static void socket_enter_start_pre(Socket *s) { | |
2237 | int r; | |
2238 | assert(s); | |
2239 | ||
5e94833f | 2240 | socket_unwatch_control_pid(s); |
a4634b21 | 2241 | |
4c425434 | 2242 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start); |
a4634b21 | 2243 | |
a16e1123 | 2244 | s->control_command_id = SOCKET_EXEC_START_PRE; |
3900e5fd | 2245 | s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]; |
a16e1123 | 2246 | |
3900e5fd | 2247 | if (s->control_command) { |
e821075a | 2248 | r = socket_spawn(s, s->control_command, &s->control_pid); |
3900e5fd | 2249 | if (r < 0) { |
f2341e0a | 2250 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); |
034c6ed7 | 2251 | goto fail; |
3900e5fd | 2252 | } |
034c6ed7 | 2253 | |
80876c20 LP |
2254 | socket_set_state(s, SOCKET_START_PRE); |
2255 | } else | |
3900e5fd | 2256 | socket_enter_start_chown(s); |
034c6ed7 LP |
2257 | |
2258 | return; | |
2259 | ||
2260 | fail: | |
cfc4eb4c | 2261 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2262 | } |
2263 | ||
60d9771c LP |
2264 | static void flush_ports(Socket *s) { |
2265 | SocketPort *p; | |
2266 | ||
2267 | /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy | |
2268 | * anymore */ | |
2269 | ||
2270 | LIST_FOREACH(port, p, s->ports) { | |
2271 | if (p->fd < 0) | |
2272 | continue; | |
2273 | ||
2274 | (void) flush_accept(p->fd); | |
2275 | (void) flush_fd(p->fd); | |
2276 | } | |
2277 | } | |
2278 | ||
4f2d528d | 2279 | static void socket_enter_running(Socket *s, int cfd) { |
4afd3348 | 2280 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 LP |
2281 | int r; |
2282 | ||
8b26cdbd LP |
2283 | /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or |
2284 | * close it. */ | |
2285 | ||
034c6ed7 LP |
2286 | assert(s); |
2287 | ||
60d9771c | 2288 | /* We don't take connections anymore if we are supposed to shut down anyway */ |
31afa0a4 | 2289 | if (unit_stop_pending(UNIT(s))) { |
e821075a | 2290 | |
f2341e0a | 2291 | log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled."); |
5d909e3e | 2292 | |
7c610628 | 2293 | if (cfd >= 0) |
a98f7575 | 2294 | goto refuse; |
60d9771c LP |
2295 | else |
2296 | flush_ports(s); | |
7c610628 | 2297 | |
ba3e67a7 LP |
2298 | return; |
2299 | } | |
2300 | ||
7994ac1d | 2301 | if (!ratelimit_below(&s->trigger_limit)) { |
8b26cdbd LP |
2302 | log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation."); |
2303 | socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT); | |
a98f7575 | 2304 | goto refuse; |
8b26cdbd LP |
2305 | } |
2306 | ||
4f2d528d | 2307 | if (cfd < 0) { |
f976f3f6 | 2308 | bool pending = false; |
eef85c4a LP |
2309 | Unit *other; |
2310 | Iterator i; | |
2311 | void *v; | |
f976f3f6 LP |
2312 | |
2313 | /* If there's already a start pending don't bother to | |
2314 | * do anything */ | |
eef85c4a | 2315 | HASHMAP_FOREACH_KEY(v, other, UNIT(s)->dependencies[UNIT_TRIGGERS], i) |
e821075a | 2316 | if (unit_active_or_pending(other)) { |
57020a3a LP |
2317 | pending = true; |
2318 | break; | |
2319 | } | |
f976f3f6 | 2320 | |
1a710b43 | 2321 | if (!pending) { |
640ace4a | 2322 | if (!UNIT_ISSET(s->service)) { |
f2341e0a | 2323 | log_unit_error(UNIT(s), "Service to activate vanished, refusing activation."); |
640ace4a LP |
2324 | r = -ENOENT; |
2325 | goto fail; | |
2326 | } | |
2327 | ||
50cbaba4 | 2328 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, NULL, &error, NULL); |
1a710b43 | 2329 | if (r < 0) |
f976f3f6 | 2330 | goto fail; |
1a710b43 | 2331 | } |
4f2d528d LP |
2332 | |
2333 | socket_set_state(s, SOCKET_RUNNING); | |
2334 | } else { | |
e55001eb | 2335 | _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL; |
9d565427 | 2336 | _cleanup_(socket_peer_unrefp) SocketPeer *p = NULL; |
b15bdda8 | 2337 | Service *service; |
4f2d528d | 2338 | |
6cf6bbc2 | 2339 | if (s->n_connections >= s->max_connections) { |
ea8f50f8 | 2340 | log_unit_warning(UNIT(s), "Too many incoming connections (%u), dropping connection.", |
166cf510 | 2341 | s->n_connections); |
a98f7575 | 2342 | goto refuse; |
6cf6bbc2 LP |
2343 | } |
2344 | ||
9d565427 | 2345 | if (s->max_connections_per_source > 0) { |
166cf510 | 2346 | r = socket_acquire_peer(s, cfd, &p); |
9d565427 | 2347 | if (r < 0) { |
a98f7575 | 2348 | goto refuse; |
166cf510 | 2349 | } else if (r > 0 && p->n_ref > s->max_connections_per_source) { |
ea8f50f8 ZJS |
2350 | _cleanup_free_ char *t = NULL; |
2351 | ||
41733ae1 | 2352 | (void) sockaddr_pretty(&p->peer.sa, p->peer_salen, true, false, &t); |
ea8f50f8 | 2353 | |
166cf510 | 2354 | log_unit_warning(UNIT(s), |
ea8f50f8 ZJS |
2355 | "Too many incoming connections (%u) from source %s, dropping connection.", |
2356 | p->n_ref, strnull(t)); | |
a98f7575 | 2357 | goto refuse; |
9d565427 SS |
2358 | } |
2359 | } | |
2360 | ||
1a710b43 MS |
2361 | r = socket_instantiate_service(s); |
2362 | if (r < 0) | |
b15bdda8 LP |
2363 | goto fail; |
2364 | ||
1a710b43 MS |
2365 | r = instance_from_socket(cfd, s->n_accepted, &instance); |
2366 | if (r < 0) { | |
2367 | if (r != -ENOTCONN) | |
2368 | goto fail; | |
2369 | ||
2370 | /* ENOTCONN is legitimate if TCP RST was received. | |
2371 | * This connection is over, but the socket unit lives on. */ | |
8b26cdbd | 2372 | log_unit_debug(UNIT(s), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring."); |
a98f7575 | 2373 | goto refuse; |
1a710b43 | 2374 | } |
4f2d528d | 2375 | |
7410616c LP |
2376 | r = unit_name_to_prefix(UNIT(s)->id, &prefix); |
2377 | if (r < 0) | |
4f2d528d | 2378 | goto fail; |
4f2d528d | 2379 | |
7410616c LP |
2380 | r = unit_name_build(prefix, instance, ".service", &name); |
2381 | if (r < 0) | |
b6dbbe1c | 2382 | goto fail; |
4f2d528d | 2383 | |
1a710b43 | 2384 | r = unit_add_name(UNIT_DEREF(s->service), name); |
e55001eb | 2385 | if (r < 0) |
4f2d528d | 2386 | goto fail; |
b15bdda8 | 2387 | |
57020a3a LP |
2388 | service = SERVICE(UNIT_DEREF(s->service)); |
2389 | unit_ref_unset(&s->service); | |
6c073082 | 2390 | |
e4f67317 | 2391 | s->n_accepted++; |
b15bdda8 | 2392 | unit_choose_id(UNIT(service), name); |
b15bdda8 | 2393 | |
16115b0a | 2394 | r = service_set_socket_fd(service, cfd, s, s->selinux_context_from_net); |
1a710b43 | 2395 | if (r < 0) |
4f2d528d LP |
2396 | goto fail; |
2397 | ||
8b26cdbd | 2398 | cfd = -1; /* We passed ownership of the fd to the service now. Forget it here. */ |
313cefa1 | 2399 | s->n_connections++; |
6cf6bbc2 | 2400 | |
1cc6c93a | 2401 | service->peer = TAKE_PTR(p); /* Pass ownership of the peer reference */ |
9d565427 | 2402 | |
50cbaba4 | 2403 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, NULL, &error, NULL); |
3e7a1f50 LP |
2404 | if (r < 0) { |
2405 | /* We failed to activate the new service, but it still exists. Let's make sure the service | |
2406 | * closes and forgets the connection fd again, immediately. */ | |
2407 | service_close_socket_fd(service); | |
4f2d528d | 2408 | goto fail; |
3e7a1f50 | 2409 | } |
c4e2ceae LP |
2410 | |
2411 | /* Notify clients about changed counters */ | |
2412 | unit_add_to_dbus_queue(UNIT(s)); | |
4f2d528d | 2413 | } |
034c6ed7 | 2414 | |
034c6ed7 LP |
2415 | return; |
2416 | ||
a98f7575 MM |
2417 | refuse: |
2418 | s->n_refused++; | |
2419 | safe_close(cfd); | |
2420 | return; | |
2421 | ||
034c6ed7 | 2422 | fail: |
f2341e0a LP |
2423 | log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s", |
2424 | cfd >= 0 ? "template" : "non-template", | |
718db961 | 2425 | bus_error_message(&error, r)); |
e821075a | 2426 | |
60089004 | 2427 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
03e334a1 | 2428 | safe_close(cfd); |
034c6ed7 LP |
2429 | } |
2430 | ||
cfc4eb4c | 2431 | static void socket_run_next(Socket *s) { |
034c6ed7 LP |
2432 | int r; |
2433 | ||
2434 | assert(s); | |
2435 | assert(s->control_command); | |
2436 | assert(s->control_command->command_next); | |
2437 | ||
5e94833f LP |
2438 | socket_unwatch_control_pid(s); |
2439 | ||
034c6ed7 LP |
2440 | s->control_command = s->control_command->command_next; |
2441 | ||
e821075a LP |
2442 | r = socket_spawn(s, s->control_command, &s->control_pid); |
2443 | if (r < 0) | |
034c6ed7 LP |
2444 | goto fail; |
2445 | ||
2446 | return; | |
2447 | ||
2448 | fail: | |
f2341e0a | 2449 | log_unit_warning_errno(UNIT(s), r, "Failed to run next task: %m"); |
80876c20 LP |
2450 | |
2451 | if (s->state == SOCKET_START_POST) | |
cfc4eb4c | 2452 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 2453 | else if (s->state == SOCKET_STOP_POST) |
cfc4eb4c | 2454 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); |
034c6ed7 | 2455 | else |
cfc4eb4c | 2456 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); |
034c6ed7 LP |
2457 | } |
2458 | ||
87f0e418 LP |
2459 | static int socket_start(Unit *u) { |
2460 | Socket *s = SOCKET(u); | |
07299350 | 2461 | int r; |
83c60c9f LP |
2462 | |
2463 | assert(s); | |
2464 | ||
034c6ed7 LP |
2465 | /* We cannot fulfill this request right now, try again later |
2466 | * please! */ | |
3900e5fd LP |
2467 | if (IN_SET(s->state, |
2468 | SOCKET_STOP_PRE, | |
2469 | SOCKET_STOP_PRE_SIGKILL, | |
2470 | SOCKET_STOP_PRE_SIGTERM, | |
2471 | SOCKET_STOP_POST, | |
2472 | SOCKET_FINAL_SIGTERM, | |
c968d76a YW |
2473 | SOCKET_FINAL_SIGKILL, |
2474 | SOCKET_CLEANING)) | |
034c6ed7 LP |
2475 | return -EAGAIN; |
2476 | ||
a4152e3f | 2477 | /* Already on it! */ |
3900e5fd LP |
2478 | if (IN_SET(s->state, |
2479 | SOCKET_START_PRE, | |
2480 | SOCKET_START_CHOWN, | |
2481 | SOCKET_START_POST)) | |
034c6ed7 | 2482 | return 0; |
83c60c9f | 2483 | |
034c6ed7 | 2484 | /* Cannot run this without the service being around */ |
9444b1f2 | 2485 | if (UNIT_ISSET(s->service)) { |
57020a3a LP |
2486 | Service *service; |
2487 | ||
2488 | service = SERVICE(UNIT_DEREF(s->service)); | |
2489 | ||
1124fe6f | 2490 | if (UNIT(service)->load_state != UNIT_LOADED) { |
f2341e0a | 2491 | log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id); |
4f2d528d | 2492 | return -ENOENT; |
4ac9236f | 2493 | } |
4f2d528d | 2494 | |
35b8ca3a | 2495 | /* If the service is already active we cannot start the |
4f2d528d | 2496 | * socket */ |
ec2ce0c5 | 2497 | if (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) { |
f2341e0a | 2498 | log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id); |
4f2d528d | 2499 | return -EBUSY; |
4ac9236f | 2500 | } |
4f2d528d | 2501 | } |
e537352b | 2502 | |
3742095b | 2503 | assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED)); |
83c60c9f | 2504 | |
97a3f4ee | 2505 | r = unit_test_start_limit(u); |
07299350 LP |
2506 | if (r < 0) { |
2507 | socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT); | |
2508 | return r; | |
2509 | } | |
2510 | ||
4b58153d LP |
2511 | r = unit_acquire_invocation_id(u); |
2512 | if (r < 0) | |
2513 | return r; | |
2514 | ||
cfc4eb4c | 2515 | s->result = SOCKET_SUCCESS; |
6a1d4d9f | 2516 | exec_command_reset_status_list_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); |
3c7416b6 LP |
2517 | |
2518 | u->reset_accounting = true; | |
5ad096b3 | 2519 | |
034c6ed7 | 2520 | socket_enter_start_pre(s); |
82a2b6bb | 2521 | return 1; |
034c6ed7 | 2522 | } |
83c60c9f | 2523 | |
87f0e418 LP |
2524 | static int socket_stop(Unit *u) { |
2525 | Socket *s = SOCKET(u); | |
034c6ed7 LP |
2526 | |
2527 | assert(s); | |
2528 | ||
e537352b | 2529 | /* Already on it */ |
3900e5fd LP |
2530 | if (IN_SET(s->state, |
2531 | SOCKET_STOP_PRE, | |
2532 | SOCKET_STOP_PRE_SIGTERM, | |
2533 | SOCKET_STOP_PRE_SIGKILL, | |
2534 | SOCKET_STOP_POST, | |
2535 | SOCKET_FINAL_SIGTERM, | |
2536 | SOCKET_FINAL_SIGKILL)) | |
e537352b LP |
2537 | return 0; |
2538 | ||
3f6c78dc LP |
2539 | /* If there's already something running we go directly into |
2540 | * kill mode. */ | |
3900e5fd LP |
2541 | if (IN_SET(s->state, |
2542 | SOCKET_START_PRE, | |
2543 | SOCKET_START_CHOWN, | |
2544 | SOCKET_START_POST)) { | |
cfc4eb4c | 2545 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS); |
3f6c78dc LP |
2546 | return -EAGAIN; |
2547 | } | |
2548 | ||
c968d76a YW |
2549 | /* If we are currently cleaning, then abort it, brutally. */ |
2550 | if (s->state == SOCKET_CLEANING) { | |
2551 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); | |
2552 | return 0; | |
2553 | } | |
2554 | ||
3742095b | 2555 | assert(IN_SET(s->state, SOCKET_LISTENING, SOCKET_RUNNING)); |
83c60c9f | 2556 | |
cfc4eb4c | 2557 | socket_enter_stop_pre(s, SOCKET_SUCCESS); |
82a2b6bb | 2558 | return 1; |
542563ba LP |
2559 | } |
2560 | ||
a16e1123 LP |
2561 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { |
2562 | Socket *s = SOCKET(u); | |
2563 | SocketPort *p; | |
2564 | int r; | |
2565 | ||
2566 | assert(u); | |
2567 | assert(f); | |
2568 | assert(fds); | |
2569 | ||
d68c645b LP |
2570 | (void) serialize_item(f, "state", socket_state_to_string(s->state)); |
2571 | (void) serialize_item(f, "result", socket_result_to_string(s->result)); | |
2572 | (void) serialize_item_format(f, "n-accepted", "%u", s->n_accepted); | |
2573 | (void) serialize_item_format(f, "n-refused", "%u", s->n_refused); | |
a16e1123 LP |
2574 | |
2575 | if (s->control_pid > 0) | |
d68c645b | 2576 | (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid); |
a16e1123 LP |
2577 | |
2578 | if (s->control_command_id >= 0) | |
d68c645b | 2579 | (void) serialize_item(f, "control-command", socket_exec_command_to_string(s->control_command_id)); |
a16e1123 LP |
2580 | |
2581 | LIST_FOREACH(port, p, s->ports) { | |
2582 | int copy; | |
2583 | ||
2584 | if (p->fd < 0) | |
2585 | continue; | |
2586 | ||
613b411c LP |
2587 | copy = fdset_put_dup(fds, p->fd); |
2588 | if (copy < 0) | |
fc2d74ab | 2589 | return log_unit_warning_errno(u, copy, "Failed to serialize socket fd: %m"); |
a16e1123 LP |
2590 | |
2591 | if (p->type == SOCKET_SOCKET) { | |
613b411c | 2592 | _cleanup_free_ char *t = NULL; |
a16e1123 | 2593 | |
ee092817 LP |
2594 | r = socket_address_print(&p->address, &t); |
2595 | if (r < 0) | |
fc2d74ab | 2596 | return log_unit_error_errno(u, r, "Failed to format socket address: %m"); |
a16e1123 | 2597 | |
7a22745a | 2598 | if (socket_address_family(&p->address) == AF_NETLINK) |
d68c645b | 2599 | (void) serialize_item_format(f, "netlink", "%i %s", copy, t); |
7a22745a | 2600 | else |
d68c645b | 2601 | (void) serialize_item_format(f, "socket", "%i %i %s", copy, p->address.type, t); |
b0a3f2bc | 2602 | } else if (p->type == SOCKET_SPECIAL) |
d68c645b | 2603 | (void) serialize_item_format(f, "special", "%i %s", copy, p->path); |
ee092817 | 2604 | else if (p->type == SOCKET_MQUEUE) |
d68c645b | 2605 | (void) serialize_item_format(f, "mqueue", "%i %s", copy, p->path); |
60252446 | 2606 | else if (p->type == SOCKET_USB_FUNCTION) |
d68c645b | 2607 | (void) serialize_item_format(f, "ffs", "%i %s", copy, p->path); |
b0a3f2bc | 2608 | else { |
a16e1123 | 2609 | assert(p->type == SOCKET_FIFO); |
d68c645b | 2610 | (void) serialize_item_format(f, "fifo", "%i %s", copy, p->path); |
a16e1123 LP |
2611 | } |
2612 | } | |
2613 | ||
2614 | return 0; | |
2615 | } | |
2616 | ||
80a58668 | 2617 | static void socket_port_take_fd(SocketPort *p, FDSet *fds, int fd) { |
d68c645b LP |
2618 | assert(p); |
2619 | ||
80a58668 ZJS |
2620 | safe_close(p->fd); |
2621 | p->fd = fdset_remove(fds, fd); | |
2622 | } | |
2623 | ||
a16e1123 LP |
2624 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
2625 | Socket *s = SOCKET(u); | |
a16e1123 LP |
2626 | |
2627 | assert(u); | |
2628 | assert(key); | |
2629 | assert(value); | |
a16e1123 LP |
2630 | |
2631 | if (streq(key, "state")) { | |
2632 | SocketState state; | |
2633 | ||
ee092817 LP |
2634 | state = socket_state_from_string(value); |
2635 | if (state < 0) | |
f2341e0a | 2636 | log_unit_debug(u, "Failed to parse state value: %s", value); |
a16e1123 LP |
2637 | else |
2638 | s->deserialized_state = state; | |
cfc4eb4c LP |
2639 | } else if (streq(key, "result")) { |
2640 | SocketResult f; | |
a16e1123 | 2641 | |
cfc4eb4c LP |
2642 | f = socket_result_from_string(value); |
2643 | if (f < 0) | |
f2341e0a | 2644 | log_unit_debug(u, "Failed to parse result value: %s", value); |
cfc4eb4c LP |
2645 | else if (f != SOCKET_SUCCESS) |
2646 | s->result = f; | |
a16e1123 LP |
2647 | |
2648 | } else if (streq(key, "n-accepted")) { | |
2649 | unsigned k; | |
2650 | ||
e364ad06 | 2651 | if (safe_atou(value, &k) < 0) |
f2341e0a | 2652 | log_unit_debug(u, "Failed to parse n-accepted value: %s", value); |
a16e1123 LP |
2653 | else |
2654 | s->n_accepted += k; | |
a98f7575 MM |
2655 | } else if (streq(key, "n-refused")) { |
2656 | unsigned k; | |
2657 | ||
2658 | if (safe_atou(value, &k) < 0) | |
2659 | log_unit_debug(u, "Failed to parse n-refused value: %s", value); | |
2660 | else | |
2661 | s->n_refused += k; | |
a16e1123 | 2662 | } else if (streq(key, "control-pid")) { |
5925dd3c | 2663 | pid_t pid; |
a16e1123 | 2664 | |
e364ad06 | 2665 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2666 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
a16e1123 | 2667 | else |
5925dd3c | 2668 | s->control_pid = pid; |
a16e1123 LP |
2669 | } else if (streq(key, "control-command")) { |
2670 | SocketExecCommand id; | |
2671 | ||
66870f90 ZJS |
2672 | id = socket_exec_command_from_string(value); |
2673 | if (id < 0) | |
f2341e0a | 2674 | log_unit_debug(u, "Failed to parse exec-command value: %s", value); |
a16e1123 LP |
2675 | else { |
2676 | s->control_command_id = id; | |
2677 | s->control_command = s->exec_command[id]; | |
2678 | } | |
2679 | } else if (streq(key, "fifo")) { | |
2680 | int fd, skip = 0; | |
2681 | SocketPort *p; | |
2682 | ||
2683 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2684 | log_unit_debug(u, "Failed to parse fifo value: %s", value); |
80a58668 | 2685 | else |
a16e1123 | 2686 | LIST_FOREACH(port, p, s->ports) |
b0a3f2bc | 2687 | if (p->type == SOCKET_FIFO && |
e3f791a2 | 2688 | path_equal_or_files_same(p->path, value+skip, 0)) { |
80a58668 | 2689 | socket_port_take_fd(p, fds, fd); |
b0a3f2bc | 2690 | break; |
80a58668 | 2691 | } |
b0a3f2bc LP |
2692 | |
2693 | } else if (streq(key, "special")) { | |
2694 | int fd, skip = 0; | |
2695 | SocketPort *p; | |
2696 | ||
2697 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2698 | log_unit_debug(u, "Failed to parse special value: %s", value); |
80a58668 | 2699 | else |
b0a3f2bc LP |
2700 | LIST_FOREACH(port, p, s->ports) |
2701 | if (p->type == SOCKET_SPECIAL && | |
e3f791a2 | 2702 | path_equal_or_files_same(p->path, value+skip, 0)) { |
80a58668 | 2703 | socket_port_take_fd(p, fds, fd); |
a16e1123 | 2704 | break; |
80a58668 | 2705 | } |
a16e1123 | 2706 | |
ee092817 LP |
2707 | } else if (streq(key, "mqueue")) { |
2708 | int fd, skip = 0; | |
2709 | SocketPort *p; | |
2710 | ||
2711 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2712 | log_unit_debug(u, "Failed to parse mqueue value: %s", value); |
80a58668 | 2713 | else |
ee092817 LP |
2714 | LIST_FOREACH(port, p, s->ports) |
2715 | if (p->type == SOCKET_MQUEUE && | |
80a58668 ZJS |
2716 | streq(p->path, value+skip)) { |
2717 | socket_port_take_fd(p, fds, fd); | |
ee092817 | 2718 | break; |
80a58668 | 2719 | } |
ee092817 | 2720 | |
a16e1123 | 2721 | } else if (streq(key, "socket")) { |
27ca8d7a | 2722 | int fd, type, skip = 0; |
a16e1123 LP |
2723 | SocketPort *p; |
2724 | ||
27ca8d7a | 2725 | if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) |
f2341e0a | 2726 | log_unit_debug(u, "Failed to parse socket value: %s", value); |
80a58668 | 2727 | else |
a16e1123 | 2728 | LIST_FOREACH(port, p, s->ports) |
80a58668 ZJS |
2729 | if (socket_address_is(&p->address, value+skip, type)) { |
2730 | socket_port_take_fd(p, fds, fd); | |
a16e1123 | 2731 | break; |
80a58668 | 2732 | } |
a16e1123 | 2733 | |
7a22745a LP |
2734 | } else if (streq(key, "netlink")) { |
2735 | int fd, skip = 0; | |
2736 | SocketPort *p; | |
2737 | ||
2738 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2739 | log_unit_debug(u, "Failed to parse socket value: %s", value); |
80a58668 | 2740 | else |
7a22745a | 2741 | LIST_FOREACH(port, p, s->ports) |
80a58668 ZJS |
2742 | if (socket_address_is_netlink(&p->address, value+skip)) { |
2743 | socket_port_take_fd(p, fds, fd); | |
7a22745a | 2744 | break; |
80a58668 | 2745 | } |
60252446 PS |
2746 | |
2747 | } else if (streq(key, "ffs")) { | |
2748 | int fd, skip = 0; | |
2749 | SocketPort *p; | |
2750 | ||
2751 | if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) | |
2752 | log_unit_debug(u, "Failed to parse ffs value: %s", value); | |
80a58668 | 2753 | else |
60252446 PS |
2754 | LIST_FOREACH(port, p, s->ports) |
2755 | if (p->type == SOCKET_USB_FUNCTION && | |
e3f791a2 | 2756 | path_equal_or_files_same(p->path, value+skip, 0)) { |
80a58668 | 2757 | socket_port_take_fd(p, fds, fd); |
60252446 | 2758 | break; |
80a58668 | 2759 | } |
60252446 | 2760 | |
a16e1123 | 2761 | } else |
f2341e0a | 2762 | log_unit_debug(UNIT(s), "Unknown serialization key: %s", key); |
a16e1123 LP |
2763 | |
2764 | return 0; | |
2765 | } | |
2766 | ||
9ff1a6f1 | 2767 | static void socket_distribute_fds(Unit *u, FDSet *fds) { |
01e10de3 LP |
2768 | Socket *s = SOCKET(u); |
2769 | SocketPort *p; | |
2770 | ||
2771 | assert(u); | |
2772 | ||
2773 | LIST_FOREACH(port, p, s->ports) { | |
2774 | Iterator i; | |
2775 | int fd; | |
2776 | ||
2777 | if (p->type != SOCKET_SOCKET) | |
2778 | continue; | |
2779 | ||
2780 | if (p->fd >= 0) | |
2781 | continue; | |
2782 | ||
2783 | FDSET_FOREACH(fd, fds, i) { | |
2784 | if (socket_address_matches_fd(&p->address, fd)) { | |
2785 | p->fd = fdset_remove(fds, fd); | |
2786 | s->deserialized_state = SOCKET_LISTENING; | |
2787 | break; | |
2788 | } | |
2789 | } | |
2790 | } | |
01e10de3 LP |
2791 | } |
2792 | ||
44a6b1b6 | 2793 | _pure_ static UnitActiveState socket_active_state(Unit *u) { |
87f0e418 | 2794 | assert(u); |
5cb5a6ff | 2795 | |
acbb0225 | 2796 | return state_translation_table[SOCKET(u)->state]; |
5cb5a6ff LP |
2797 | } |
2798 | ||
44a6b1b6 | 2799 | _pure_ static const char *socket_sub_state_to_string(Unit *u) { |
10a94420 LP |
2800 | assert(u); |
2801 | ||
a16e1123 | 2802 | return socket_state_to_string(SOCKET(u)->state); |
10a94420 LP |
2803 | } |
2804 | ||
67419600 OS |
2805 | const char* socket_port_type_to_string(SocketPort *p) { |
2806 | ||
2807 | assert(p); | |
2808 | ||
2809 | switch (p->type) { | |
718db961 LP |
2810 | |
2811 | case SOCKET_SOCKET: | |
2812 | ||
2813 | switch (p->address.type) { | |
2814 | ||
2815 | case SOCK_STREAM: | |
2816 | return "Stream"; | |
2817 | ||
2818 | case SOCK_DGRAM: | |
2819 | return "Datagram"; | |
2820 | ||
2821 | case SOCK_SEQPACKET: | |
2822 | return "SequentialPacket"; | |
2823 | ||
2824 | case SOCK_RAW: | |
2825 | if (socket_address_family(&p->address) == AF_NETLINK) | |
2826 | return "Netlink"; | |
2827 | ||
4831981d | 2828 | _fallthrough_; |
718db961 LP |
2829 | default: |
2830 | return NULL; | |
2831 | } | |
2832 | ||
2833 | case SOCKET_SPECIAL: | |
2834 | return "Special"; | |
2835 | ||
2836 | case SOCKET_MQUEUE: | |
2837 | return "MessageQueue"; | |
2838 | ||
2839 | case SOCKET_FIFO: | |
2840 | return "FIFO"; | |
2841 | ||
60252446 PS |
2842 | case SOCKET_USB_FUNCTION: |
2843 | return "USBFunction"; | |
2844 | ||
718db961 LP |
2845 | default: |
2846 | return NULL; | |
67419600 OS |
2847 | } |
2848 | } | |
2849 | ||
038ed5a4 YW |
2850 | SocketType socket_port_type_from_string(const char *s) { |
2851 | assert(s); | |
2852 | ||
e045e325 YW |
2853 | if (STR_IN_SET(s, "Stream", "Datagram", "SequentialPacket", "Netlink")) |
2854 | return SOCKET_SOCKET; | |
2855 | else if (streq(s, "Special")) | |
2856 | return SOCKET_SPECIAL; | |
2857 | else if (streq(s, "MessageQueue")) | |
2858 | return SOCKET_MQUEUE; | |
2859 | else if (streq(s, "FIFO")) | |
2860 | return SOCKET_FIFO; | |
2861 | else if (streq(s, "USBFunction")) | |
2862 | return SOCKET_USB_FUNCTION; | |
038ed5a4 YW |
2863 | else |
2864 | return _SOCKET_TYPE_INVALID; | |
2865 | } | |
2866 | ||
f2f725e5 | 2867 | _pure_ static bool socket_may_gc(Unit *u) { |
6cf6bbc2 LP |
2868 | Socket *s = SOCKET(u); |
2869 | ||
2870 | assert(u); | |
2871 | ||
f2f725e5 | 2872 | return s->n_connections == 0; |
6cf6bbc2 LP |
2873 | } |
2874 | ||
a79279c7 LP |
2875 | static int socket_accept_do(Socket *s, int fd) { |
2876 | int cfd; | |
2877 | ||
2878 | assert(s); | |
2879 | assert(fd >= 0); | |
2880 | ||
4ff9bc2e LP |
2881 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); |
2882 | if (cfd < 0) | |
2883 | /* Convert transient network errors into clean and well-defined EAGAIN */ | |
2884 | return ERRNO_IS_ACCEPT_AGAIN(errno) ? -EAGAIN : -errno; | |
a79279c7 LP |
2885 | |
2886 | return cfd; | |
2887 | } | |
2888 | ||
2889 | static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) { | |
2890 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; | |
2891 | int cfd, r; | |
2892 | pid_t pid; | |
2893 | ||
2894 | assert(s); | |
2895 | assert(p); | |
2896 | assert(fd >= 0); | |
2897 | ||
5238e957 | 2898 | /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any |
a79279c7 LP |
2899 | * connection socket is also properly associated with the cgroup. */ |
2900 | ||
2901 | if (!IN_SET(p->address.sockaddr.sa.sa_family, AF_INET, AF_INET6)) | |
2902 | goto shortcut; | |
2903 | ||
2904 | r = bpf_firewall_supported(); | |
2905 | if (r < 0) | |
2906 | return r; | |
2ae7ee58 | 2907 | if (r == BPF_FIREWALL_UNSUPPORTED) |
a79279c7 LP |
2908 | goto shortcut; |
2909 | ||
2910 | if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) | |
2911 | return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); | |
2912 | ||
4c253ed1 | 2913 | r = unit_fork_helper_process(UNIT(s), "(sd-accept)", &pid); |
a79279c7 LP |
2914 | if (r < 0) |
2915 | return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m"); | |
2916 | if (r == 0) { | |
2917 | /* Child */ | |
2918 | ||
2919 | pair[0] = safe_close(pair[0]); | |
2920 | ||
2921 | cfd = socket_accept_do(s, fd); | |
4ff9bc2e LP |
2922 | if (cfd == -EAGAIN) /* spurious accept() */ |
2923 | _exit(EXIT_SUCCESS); | |
a79279c7 LP |
2924 | if (cfd < 0) { |
2925 | log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m"); | |
2926 | _exit(EXIT_FAILURE); | |
2927 | } | |
2928 | ||
2929 | r = send_one_fd(pair[1], cfd, 0); | |
2930 | if (r < 0) { | |
2931 | log_unit_error_errno(UNIT(s), r, "Failed to send connection socket to parent: %m"); | |
2932 | _exit(EXIT_FAILURE); | |
2933 | } | |
2934 | ||
2935 | _exit(EXIT_SUCCESS); | |
2936 | } | |
2937 | ||
2938 | pair[1] = safe_close(pair[1]); | |
2939 | cfd = receive_one_fd(pair[0], 0); | |
2940 | ||
2941 | /* We synchronously wait for the helper, as it shouldn't be slow */ | |
d2e0ac3d | 2942 | r = wait_for_terminate_and_check("(sd-accept)", pid, WAIT_LOG_ABNORMAL); |
a79279c7 LP |
2943 | if (r < 0) { |
2944 | safe_close(cfd); | |
2945 | return r; | |
2946 | } | |
2947 | ||
4ff9bc2e LP |
2948 | /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS |
2949 | * this is a spurious accept(), let's convert that back to EAGAIN here. */ | |
2950 | if (cfd == -EIO) | |
2951 | return -EAGAIN; | |
a79279c7 LP |
2952 | if (cfd < 0) |
2953 | return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m"); | |
2954 | ||
2955 | return cfd; | |
2956 | ||
2957 | shortcut: | |
2958 | cfd = socket_accept_do(s, fd); | |
4ff9bc2e LP |
2959 | if (cfd == -EAGAIN) /* spurious accept(), skip it silently */ |
2960 | return -EAGAIN; | |
a79279c7 LP |
2961 | if (cfd < 0) |
2962 | return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m"); | |
2963 | ||
2964 | return cfd; | |
2965 | } | |
2966 | ||
718db961 LP |
2967 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2968 | SocketPort *p = userdata; | |
4f2d528d | 2969 | int cfd = -1; |
9152c765 | 2970 | |
718db961 | 2971 | assert(p); |
8d567588 | 2972 | assert(fd >= 0); |
9152c765 | 2973 | |
718db961 LP |
2974 | if (p->socket->state != SOCKET_LISTENING) |
2975 | return 0; | |
871d7de4 | 2976 | |
f2341e0a | 2977 | log_unit_debug(UNIT(p->socket), "Incoming traffic"); |
9152c765 | 2978 | |
718db961 | 2979 | if (revents != EPOLLIN) { |
718db961 | 2980 | if (revents & EPOLLHUP) |
f2341e0a | 2981 | log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that."); |
641e01dc | 2982 | else |
f2341e0a | 2983 | log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents); |
8d567588 | 2984 | goto fail; |
4f2d528d LP |
2985 | } |
2986 | ||
718db961 LP |
2987 | if (p->socket->accept && |
2988 | p->type == SOCKET_SOCKET && | |
2989 | socket_address_can_accept(&p->address)) { | |
2990 | ||
a79279c7 | 2991 | cfd = socket_accept_in_cgroup(p->socket, p, fd); |
4ff9bc2e LP |
2992 | if (cfd == -EAGAIN) /* Spurious accept() */ |
2993 | return 0; | |
a79279c7 LP |
2994 | if (cfd < 0) |
2995 | goto fail; | |
4fd5948e | 2996 | |
718db961 | 2997 | socket_apply_socket_options(p->socket, cfd); |
4f2d528d | 2998 | } |
9152c765 | 2999 | |
718db961 LP |
3000 | socket_enter_running(p->socket, cfd); |
3001 | return 0; | |
8d567588 LP |
3002 | |
3003 | fail: | |
718db961 LP |
3004 | socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES); |
3005 | return 0; | |
9152c765 LP |
3006 | } |
3007 | ||
87f0e418 LP |
3008 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
3009 | Socket *s = SOCKET(u); | |
cfc4eb4c | 3010 | SocketResult f; |
5cb5a6ff LP |
3011 | |
3012 | assert(s); | |
034c6ed7 | 3013 | assert(pid >= 0); |
5cb5a6ff | 3014 | |
8c47c732 LP |
3015 | if (pid != s->control_pid) |
3016 | return; | |
542563ba | 3017 | |
034c6ed7 LP |
3018 | s->control_pid = 0; |
3019 | ||
1f0958f6 | 3020 | if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) |
cfc4eb4c LP |
3021 | f = SOCKET_SUCCESS; |
3022 | else if (code == CLD_EXITED) | |
3023 | f = SOCKET_FAILURE_EXIT_CODE; | |
3024 | else if (code == CLD_KILLED) | |
3025 | f = SOCKET_FAILURE_SIGNAL; | |
3026 | else if (code == CLD_DUMPED) | |
3027 | f = SOCKET_FAILURE_CORE_DUMP; | |
3028 | else | |
a4152e3f | 3029 | assert_not_reached("Unknown sigchld code"); |
8c47c732 | 3030 | |
b708e7ce | 3031 | if (s->control_command) { |
6ea832a2 | 3032 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 3033 | |
3ed0cd26 | 3034 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
cfc4eb4c | 3035 | f = SOCKET_SUCCESS; |
b708e7ce LP |
3036 | } |
3037 | ||
91bbd9b7 | 3038 | unit_log_process_exit( |
5cc2cd1c | 3039 | u, |
91bbd9b7 LP |
3040 | "Control process", |
3041 | socket_exec_command_to_string(s->control_command_id), | |
5cc2cd1c | 3042 | f == SOCKET_SUCCESS, |
91bbd9b7 | 3043 | code, status); |
034c6ed7 | 3044 | |
a0fef983 | 3045 | if (s->result == SOCKET_SUCCESS) |
cfc4eb4c LP |
3046 | s->result = f; |
3047 | ||
3048 | if (s->control_command && | |
3049 | s->control_command->command_next && | |
3050 | f == SOCKET_SUCCESS) { | |
3051 | ||
f2341e0a | 3052 | log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state)); |
cfc4eb4c | 3053 | socket_run_next(s); |
acbb0225 | 3054 | } else { |
a16e1123 LP |
3055 | s->control_command = NULL; |
3056 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
3057 | ||
034c6ed7 LP |
3058 | /* No further commands for this step, so let's figure |
3059 | * out what to do next */ | |
5cb5a6ff | 3060 | |
f2341e0a | 3061 | log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state)); |
acbb0225 | 3062 | |
034c6ed7 LP |
3063 | switch (s->state) { |
3064 | ||
3065 | case SOCKET_START_PRE: | |
cfc4eb4c | 3066 | if (f == SOCKET_SUCCESS) |
3900e5fd | 3067 | socket_enter_start_chown(s); |
034c6ed7 | 3068 | else |
cfc4eb4c | 3069 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f); |
034c6ed7 LP |
3070 | break; |
3071 | ||
3900e5fd LP |
3072 | case SOCKET_START_CHOWN: |
3073 | if (f == SOCKET_SUCCESS) | |
3074 | socket_enter_start_post(s); | |
3075 | else | |
3076 | socket_enter_stop_pre(s, f); | |
3077 | break; | |
3078 | ||
034c6ed7 | 3079 | case SOCKET_START_POST: |
cfc4eb4c | 3080 | if (f == SOCKET_SUCCESS) |
e9af15c3 | 3081 | socket_enter_listening(s); |
034c6ed7 | 3082 | else |
cfc4eb4c | 3083 | socket_enter_stop_pre(s, f); |
034c6ed7 LP |
3084 | break; |
3085 | ||
3086 | case SOCKET_STOP_PRE: | |
3087 | case SOCKET_STOP_PRE_SIGTERM: | |
3088 | case SOCKET_STOP_PRE_SIGKILL: | |
cfc4eb4c | 3089 | socket_enter_stop_post(s, f); |
034c6ed7 LP |
3090 | break; |
3091 | ||
3092 | case SOCKET_STOP_POST: | |
80876c20 LP |
3093 | case SOCKET_FINAL_SIGTERM: |
3094 | case SOCKET_FINAL_SIGKILL: | |
cfc4eb4c | 3095 | socket_enter_dead(s, f); |
034c6ed7 LP |
3096 | break; |
3097 | ||
c968d76a YW |
3098 | case SOCKET_CLEANING: |
3099 | ||
3100 | if (s->clean_result == SOCKET_SUCCESS) | |
3101 | s->clean_result = f; | |
3102 | ||
3103 | socket_enter_dead(s, SOCKET_SUCCESS); | |
3104 | break; | |
3105 | ||
034c6ed7 LP |
3106 | default: |
3107 | assert_not_reached("Uh, control process died at wrong time."); | |
3108 | } | |
3109 | } | |
c4e2ceae LP |
3110 | |
3111 | /* Notify clients about changed exit status */ | |
3112 | unit_add_to_dbus_queue(u); | |
034c6ed7 | 3113 | } |
5cb5a6ff | 3114 | |
718db961 LP |
3115 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
3116 | Socket *s = SOCKET(userdata); | |
5cb5a6ff | 3117 | |
034c6ed7 | 3118 | assert(s); |
718db961 | 3119 | assert(s->timer_event_source == source); |
034c6ed7 LP |
3120 | |
3121 | switch (s->state) { | |
3122 | ||
3123 | case SOCKET_START_PRE: | |
f2341e0a | 3124 | log_unit_warning(UNIT(s), "Starting timed out. Terminating."); |
cfc4eb4c | 3125 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
da19d5c1 | 3126 | break; |
80876c20 | 3127 | |
3900e5fd | 3128 | case SOCKET_START_CHOWN: |
034c6ed7 | 3129 | case SOCKET_START_POST: |
f2341e0a | 3130 | log_unit_warning(UNIT(s), "Starting timed out. Stopping."); |
cfc4eb4c | 3131 | socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
3132 | break; |
3133 | ||
3134 | case SOCKET_STOP_PRE: | |
f2341e0a | 3135 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); |
cfc4eb4c | 3136 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
3137 | break; |
3138 | ||
3139 | case SOCKET_STOP_PRE_SIGTERM: | |
4819ff03 | 3140 | if (s->kill_context.send_sigkill) { |
f2341e0a | 3141 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
cfc4eb4c | 3142 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 3143 | } else { |
f2341e0a | 3144 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring."); |
cfc4eb4c | 3145 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 3146 | } |
034c6ed7 LP |
3147 | break; |
3148 | ||
3149 | case SOCKET_STOP_PRE_SIGKILL: | |
f2341e0a | 3150 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
cfc4eb4c | 3151 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
3152 | break; |
3153 | ||
3154 | case SOCKET_STOP_POST: | |
f2341e0a | 3155 | log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating."); |
cfc4eb4c | 3156 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
3157 | break; |
3158 | ||
80876c20 | 3159 | case SOCKET_FINAL_SIGTERM: |
4819ff03 | 3160 | if (s->kill_context.send_sigkill) { |
f2341e0a | 3161 | log_unit_warning(UNIT(s), "Stopping timed out (2). Killing."); |
cfc4eb4c | 3162 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 3163 | } else { |
f2341e0a | 3164 | log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring."); |
cfc4eb4c | 3165 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
ba035df2 | 3166 | } |
034c6ed7 LP |
3167 | break; |
3168 | ||
80876c20 | 3169 | case SOCKET_FINAL_SIGKILL: |
f2341e0a | 3170 | log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode."); |
cfc4eb4c | 3171 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); |
034c6ed7 LP |
3172 | break; |
3173 | ||
c968d76a YW |
3174 | case SOCKET_CLEANING: |
3175 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
3176 | ||
3177 | if (s->clean_result == SOCKET_SUCCESS) | |
3178 | s->clean_result = SOCKET_FAILURE_TIMEOUT; | |
3179 | ||
3180 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, 0); | |
3181 | break; | |
3182 | ||
034c6ed7 LP |
3183 | default: |
3184 | assert_not_reached("Timeout at wrong time."); | |
3185 | } | |
718db961 LP |
3186 | |
3187 | return 0; | |
5cb5a6ff LP |
3188 | } |
3189 | ||
79c7626d | 3190 | int socket_collect_fds(Socket *s, int **fds) { |
da6053d0 | 3191 | size_t k = 0, n = 0; |
44d8db9e | 3192 | SocketPort *p; |
da6053d0 | 3193 | int *rfds; |
44d8db9e LP |
3194 | |
3195 | assert(s); | |
3196 | assert(fds); | |
44d8db9e LP |
3197 | |
3198 | /* Called from the service code for requesting our fds */ | |
3199 | ||
15087cdb | 3200 | LIST_FOREACH(port, p, s->ports) { |
44d8db9e | 3201 | if (p->fd >= 0) |
79c7626d LP |
3202 | n++; |
3203 | n += p->n_auxiliary_fds; | |
15087cdb | 3204 | } |
44d8db9e | 3205 | |
79c7626d | 3206 | if (n <= 0) { |
de3756ab | 3207 | *fds = NULL; |
de3756ab LP |
3208 | return 0; |
3209 | } | |
3210 | ||
79c7626d | 3211 | rfds = new(int, n); |
e5403f09 | 3212 | if (!rfds) |
44d8db9e LP |
3213 | return -ENOMEM; |
3214 | ||
15087cdb | 3215 | LIST_FOREACH(port, p, s->ports) { |
da6053d0 | 3216 | size_t i; |
79c7626d | 3217 | |
44d8db9e LP |
3218 | if (p->fd >= 0) |
3219 | rfds[k++] = p->fd; | |
15087cdb PS |
3220 | for (i = 0; i < p->n_auxiliary_fds; ++i) |
3221 | rfds[k++] = p->auxiliary_fds[i]; | |
3222 | } | |
44d8db9e | 3223 | |
79c7626d | 3224 | assert(k == n); |
44d8db9e LP |
3225 | |
3226 | *fds = rfds; | |
da6053d0 | 3227 | return (int) n; |
44d8db9e LP |
3228 | } |
3229 | ||
e821075a LP |
3230 | static void socket_reset_failed(Unit *u) { |
3231 | Socket *s = SOCKET(u); | |
3232 | ||
3233 | assert(s); | |
3234 | ||
3235 | if (s->state == SOCKET_FAILED) | |
3236 | socket_set_state(s, SOCKET_DEAD); | |
3237 | ||
3238 | s->result = SOCKET_SUCCESS; | |
c968d76a | 3239 | s->clean_result = SOCKET_SUCCESS; |
e821075a LP |
3240 | } |
3241 | ||
6cf6bbc2 LP |
3242 | void socket_connection_unref(Socket *s) { |
3243 | assert(s); | |
3244 | ||
3245 | /* The service is dead. Yay! | |
3246 | * | |
35b8ca3a | 3247 | * This is strictly for one-instance-per-connection |
6cf6bbc2 LP |
3248 | * services. */ |
3249 | ||
3250 | assert(s->n_connections > 0); | |
3251 | s->n_connections--; | |
3252 | ||
f2341e0a | 3253 | log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections); |
5632e374 LP |
3254 | } |
3255 | ||
d137a488 UTL |
3256 | static void socket_trigger_notify(Unit *u, Unit *other) { |
3257 | Socket *s = SOCKET(u); | |
d137a488 UTL |
3258 | |
3259 | assert(u); | |
3260 | assert(other); | |
3261 | ||
d14e3a0d | 3262 | /* Filter out invocations with bogus state */ |
4c2ef327 LP |
3263 | if (!IN_SET(other->load_state, |
3264 | UNIT_LOADED, | |
3265 | UNIT_NOT_FOUND, | |
3266 | UNIT_BAD_SETTING, | |
3267 | UNIT_ERROR, | |
3268 | UNIT_MASKED) || other->type != UNIT_SERVICE) | |
d14e3a0d LP |
3269 | return; |
3270 | ||
3271 | /* Don't propagate state changes from the service if we are already down */ | |
3272 | if (!IN_SET(s->state, SOCKET_RUNNING, SOCKET_LISTENING)) | |
d137a488 UTL |
3273 | return; |
3274 | ||
d14e3a0d LP |
3275 | /* We don't care for the service state if we are in Accept=yes mode */ |
3276 | if (s->accept) | |
3277 | return; | |
3278 | ||
3279 | /* Propagate start limit hit state */ | |
6bf0f408 LP |
3280 | if (other->start_limit_hit) { |
3281 | socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT); | |
d137a488 | 3282 | return; |
6bf0f408 | 3283 | } |
d137a488 | 3284 | |
d14e3a0d LP |
3285 | /* Don't propagate anything if there's still a job queued */ |
3286 | if (other->job) | |
6bf0f408 | 3287 | return; |
d137a488 | 3288 | |
6bf0f408 LP |
3289 | if (IN_SET(SERVICE(other)->state, |
3290 | SERVICE_DEAD, SERVICE_FAILED, | |
3291 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
3292 | SERVICE_AUTO_RESTART)) | |
3293 | socket_enter_listening(s); | |
d137a488 | 3294 | |
6bf0f408 | 3295 | if (SERVICE(other)->state == SERVICE_RUNNING) |
d137a488 UTL |
3296 | socket_set_state(s, SOCKET_RUNNING); |
3297 | } | |
3298 | ||
718db961 | 3299 | static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
814cc562 | 3300 | return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error); |
8a0867d6 LP |
3301 | } |
3302 | ||
7a7821c8 | 3303 | static int socket_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 3304 | Socket *s = SOCKET(u); |
7a7821c8 | 3305 | usec_t t; |
68db7a3b ZJS |
3306 | int r; |
3307 | ||
3308 | if (!s->timer_event_source) | |
3309 | return 0; | |
3310 | ||
7a7821c8 | 3311 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
3312 | if (r < 0) |
3313 | return r; | |
7a7821c8 LP |
3314 | if (t == USEC_INFINITY) |
3315 | return 0; | |
68db7a3b | 3316 | |
7a7821c8 | 3317 | *timeout = t; |
68db7a3b ZJS |
3318 | return 1; |
3319 | } | |
3320 | ||
8dd4c05b LP |
3321 | char *socket_fdname(Socket *s) { |
3322 | assert(s); | |
3323 | ||
3324 | /* Returns the name to use for $LISTEN_NAMES. If the user | |
3325 | * didn't specify anything specifically, use the socket unit's | |
3326 | * name as fallback. */ | |
3327 | ||
84500122 | 3328 | return s->fdname ?: UNIT(s)->id; |
8dd4c05b LP |
3329 | } |
3330 | ||
291d565a LP |
3331 | static int socket_control_pid(Unit *u) { |
3332 | Socket *s = SOCKET(u); | |
3333 | ||
3334 | assert(s); | |
3335 | ||
3336 | return s->control_pid; | |
3337 | } | |
3338 | ||
c968d76a YW |
3339 | static int socket_clean(Unit *u, ExecCleanMask mask) { |
3340 | _cleanup_strv_free_ char **l = NULL; | |
3341 | Socket *s = SOCKET(u); | |
3342 | int r; | |
3343 | ||
3344 | assert(s); | |
3345 | assert(mask != 0); | |
3346 | ||
3347 | if (s->state != SOCKET_DEAD) | |
3348 | return -EBUSY; | |
3349 | ||
3350 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
3351 | if (r < 0) | |
3352 | return r; | |
3353 | ||
3354 | if (strv_isempty(l)) | |
3355 | return -EUNATCH; | |
3356 | ||
3357 | socket_unwatch_control_pid(s); | |
3358 | s->clean_result = SOCKET_SUCCESS; | |
3359 | s->control_command = NULL; | |
3360 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
3361 | ||
3362 | r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec)); | |
3363 | if (r < 0) | |
3364 | goto fail; | |
3365 | ||
3366 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); | |
3367 | if (r < 0) | |
3368 | goto fail; | |
3369 | ||
3370 | socket_set_state(s, SOCKET_CLEANING); | |
3371 | ||
3372 | return 0; | |
3373 | ||
3374 | fail: | |
3375 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); | |
3376 | s->clean_result = SOCKET_FAILURE_RESOURCES; | |
3377 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
3378 | return r; | |
3379 | } | |
3380 | ||
3381 | static int socket_can_clean(Unit *u, ExecCleanMask *ret) { | |
3382 | Socket *s = SOCKET(u); | |
3383 | ||
3384 | assert(s); | |
3385 | ||
3386 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
3387 | } | |
3388 | ||
a16e1123 | 3389 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { |
836bb1cd YW |
3390 | [SOCKET_EXEC_START_PRE] = "ExecStartPre", |
3391 | [SOCKET_EXEC_START_CHOWN] = "ExecStartChown", | |
3392 | [SOCKET_EXEC_START_POST] = "ExecStartPost", | |
3393 | [SOCKET_EXEC_STOP_PRE] = "ExecStopPre", | |
3394 | [SOCKET_EXEC_STOP_POST] = "ExecStopPost" | |
a16e1123 LP |
3395 | }; |
3396 | ||
3397 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
3398 | ||
cfc4eb4c LP |
3399 | static const char* const socket_result_table[_SOCKET_RESULT_MAX] = { |
3400 | [SOCKET_SUCCESS] = "success", | |
3401 | [SOCKET_FAILURE_RESOURCES] = "resources", | |
3402 | [SOCKET_FAILURE_TIMEOUT] = "timeout", | |
3403 | [SOCKET_FAILURE_EXIT_CODE] = "exit-code", | |
3404 | [SOCKET_FAILURE_SIGNAL] = "signal", | |
c2f34808 | 3405 | [SOCKET_FAILURE_CORE_DUMP] = "core-dump", |
07299350 | 3406 | [SOCKET_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
8b26cdbd | 3407 | [SOCKET_FAILURE_TRIGGER_LIMIT_HIT] = "trigger-limit-hit", |
6bf0f408 | 3408 | [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit" |
cfc4eb4c LP |
3409 | }; |
3410 | ||
3411 | DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult); | |
3412 | ||
87f0e418 | 3413 | const UnitVTable socket_vtable = { |
7d17cfbc | 3414 | .object_size = sizeof(Socket), |
718db961 LP |
3415 | .exec_context_offset = offsetof(Socket, exec_context), |
3416 | .cgroup_context_offset = offsetof(Socket, cgroup_context), | |
3417 | .kill_context_offset = offsetof(Socket, kill_context), | |
613b411c | 3418 | .exec_runtime_offset = offsetof(Socket, exec_runtime), |
29206d46 | 3419 | .dynamic_creds_offset = offsetof(Socket, dynamic_creds), |
3ef63c31 | 3420 | |
f975e971 LP |
3421 | .sections = |
3422 | "Unit\0" | |
3423 | "Socket\0" | |
3424 | "Install\0", | |
4ad49000 | 3425 | .private_section = "Socket", |
71645aca | 3426 | |
9c0320e7 | 3427 | .can_transient = true, |
c80a9a33 LP |
3428 | .can_trigger = true, |
3429 | .can_fail = true, | |
9c0320e7 | 3430 | |
034c6ed7 LP |
3431 | .init = socket_init, |
3432 | .done = socket_done, | |
a16e1123 LP |
3433 | .load = socket_load, |
3434 | ||
3435 | .coldplug = socket_coldplug, | |
034c6ed7 | 3436 | |
5cb5a6ff LP |
3437 | .dump = socket_dump, |
3438 | ||
542563ba LP |
3439 | .start = socket_start, |
3440 | .stop = socket_stop, | |
5cb5a6ff | 3441 | |
718db961 | 3442 | .kill = socket_kill, |
c968d76a YW |
3443 | .clean = socket_clean, |
3444 | .can_clean = socket_can_clean, | |
718db961 | 3445 | |
68db7a3b ZJS |
3446 | .get_timeout = socket_get_timeout, |
3447 | ||
a16e1123 LP |
3448 | .serialize = socket_serialize, |
3449 | .deserialize_item = socket_deserialize_item, | |
01e10de3 | 3450 | .distribute_fds = socket_distribute_fds, |
a16e1123 | 3451 | |
5cb5a6ff | 3452 | .active_state = socket_active_state, |
10a94420 | 3453 | .sub_state_to_string = socket_sub_state_to_string, |
5cb5a6ff | 3454 | |
52a12341 YW |
3455 | .will_restart = unit_will_restart_default, |
3456 | ||
f2f725e5 | 3457 | .may_gc = socket_may_gc, |
6cf6bbc2 | 3458 | |
034c6ed7 | 3459 | .sigchld_event = socket_sigchld_event, |
4139c1b2 | 3460 | |
d137a488 UTL |
3461 | .trigger_notify = socket_trigger_notify, |
3462 | ||
fdf20a31 | 3463 | .reset_failed = socket_reset_failed, |
5632e374 | 3464 | |
291d565a LP |
3465 | .control_pid = socket_control_pid, |
3466 | ||
74c964d3 LP |
3467 | .bus_set_property = bus_socket_set_property, |
3468 | .bus_commit_properties = bus_socket_commit_properties, | |
c6918296 MS |
3469 | |
3470 | .status_message_formats = { | |
3471 | /*.starting_stopping = { | |
3472 | [0] = "Starting socket %s...", | |
3473 | [1] = "Stopping socket %s...", | |
3474 | },*/ | |
3475 | .finished_start_job = { | |
3476 | [JOB_DONE] = "Listening on %s.", | |
3477 | [JOB_FAILED] = "Failed to listen on %s.", | |
c6918296 MS |
3478 | [JOB_TIMEOUT] = "Timed out starting %s.", |
3479 | }, | |
3480 | .finished_stop_job = { | |
3481 | [JOB_DONE] = "Closed %s.", | |
3482 | [JOB_FAILED] = "Failed stopping %s.", | |
3483 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
3484 | }, | |
3485 | }, | |
5cb5a6ff | 3486 | }; |