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