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