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