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