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