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