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