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