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