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