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