socket.c revision 2b3e18de74ca89b374dd4f7a2c30e5731d347841
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2010 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek [SOCKET_START_PRE] = UNIT_ACTIVATING,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->timeout_usec = DEFAULT_TIMEOUT_USEC;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo s->exec_context.std_output = u->manager->default_std_output;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo s->exec_context.std_error = u->manager->default_std_error;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_unwatch_control_pid(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid socket_free_ports(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering while ((p = s->ports)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_REMOVE(SocketPort, port, s->ports, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_done(Unit *u) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_unwatch_timer(u, &s->timer_watch);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_instantiate_service(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* This fills in s->service if it isn't filled in yet. For
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek * Accept=yes sockets we create the next connection service
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * here. For Accept=no this is mostly a NOP since the service
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * is figured out at load time anyway. */
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (!(prefix = unit_name_to_prefix(UNIT(s)->id)))
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek r = asprintf(&name, "%s@%u.service", prefix, s->n_accepted);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek log_error("Using SysV services for socket activation is not supported. Refusing.");
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic bool have_non_accept_socket(Socket *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!socket_address_can_accept(&p->address))
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (UNIT(s)->load_state != UNIT_LOADED)
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek "%s lacks Listen setting. Refusing.", UNIT(s)->id);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->accept && have_non_accept_socket(s)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%s configured for accepting sockets, but sockets are non-accepting. Refusing.",
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek if (s->accept && s->max_connections <= 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo "Explicit service configuration for accepting sockets not supported on %s. Refusing.",
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.",
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputostatic bool socket_needs_mount(Socket *s, const char *prefix) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (socket_address_needs_mount(&p->address, prefix))
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return true;
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering } else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint socket_add_one_mount_link(Socket *s, Mount *m) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (UNIT(s)->load_state != UNIT_LOADED ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek UNIT(m)->load_state != UNIT_LOADED)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!socket_needs_mount(s, m->where))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_add_mount_links(Socket *s) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_MOUNT]) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_add_one_mount_link(s, MOUNT(other));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_add_device_link(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = unit_add_node_link(UNIT(s), t, false);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poetteringstatic int socket_add_default_dependencies(Socket *s) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek_pure_ static bool socket_has_exec(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(u->load_state == UNIT_STUB);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if ((r = unit_load_fragment_and_dropin(u)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* This is a new unit? Then let's add in some extras */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (u->load_state == UNIT_LOADED) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = unit_load_related_unit(u, ".service", &x);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if ((r = socket_add_device_link(s)) < 0)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if ((r = unit_add_default_cgroups(u)) < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((r = socket_add_default_dependencies(s)) < 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = unit_exec_context_defaults(u, &s->exec_context);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering_const_ static const char* listen_lookup(int family, int type) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return "ListenNetlink";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "ListenStream";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "ListenDatagram";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "ListenSequentialPacket";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert_not_reached("Unknown socket type");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic void socket_dump(Unit *u, FILE *f, const char *prefix) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSocket State: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sResult: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sBindIPv6Only: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sBacklog: %u\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sSocketMode: %04o\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sDirectoryMode: %04o\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sKeepAlive: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sFreeBind: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sTransparent: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sBroadcast: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sPassCredentials: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sPassSecurity: %s\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sTCPCongestion: %s\n",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering prefix, socket_state_to_string(s->state),
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering prefix, socket_result_to_string(s->result),
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sControl PID: %lu\n",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sBindToDevice: %s\n",
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sAccepted: %u\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sNConnections: %u\n"
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "%sMaxConnections: %u\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sPriority: %i\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sReceiveBuffer: %zu\n",
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering "%sSendBuffer: %zu\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sIPTOS: %i\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sIPTTL: %i\n",
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering "%sPipeSize: %zu\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sMessageQueueMaxMessages: %li\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sMessageQueueMessageSize: %li\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSmackLabel: %s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSmackLabelIPIn: %s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%sSmackLabelIPOut: %s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = socket_address_print(&p->address, &k)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (p->type == SOCKET_SPECIAL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (p->type == SOCKET_MQUEUE)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek exec_context_dump(&s->exec_context, f, prefix);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek kill_context_dump(&s->kill_context, f, prefix);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek prefix, socket_exec_command_to_string(c));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek exec_command_dump_list(s->exec_command[c], f, prefix2);
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischlerstatic int instance_from_socket(int fd, unsigned nr, char **instance) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (getsockname(fd, &local.sa, &l) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek a = ntohl(local.in.sin_addr.s_addr),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek b = ntohl(remote.in.sin_addr.s_addr);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek static const unsigned char ipv4_prefix[] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *a = local.in6.sin6_addr.s6_addr+12,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *b = remote.in6.sin6_addr.s6_addr+12;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%u-%s:%u-%s:%u",
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek ntohs(remote.in6.sin6_port)) < 0)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_not_reached("Unhandled socket type.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* One little note: we should never delete any sockets
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering * in the file system here! After all some other
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering * process we spawned might still have a reference of
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering * this fd and wants to continue to use it. Therefore
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering * we delete sockets in the file system before we
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * create a new one, not after we stopped using
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void socket_apply_socket_options(Socket *s, int fd) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) < 0)
6355e75610a8d47fc3ba5ab8bd442172a2cfe574Lennart Poettering log_warning_unit(UNIT(s)->id, "SO_KEEPALIVE failed: %m");
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_warning_unit(UNIT(s)->id, "SO_BROADCAST failed: %m");
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_warning_unit(UNIT(s)->id, "SO_PASSCRED failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "SO_PASSSEC failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "SO_PRIORITY failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int value = (int) s->receive_buffer;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "SO_RCVBUF failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "SO_SNDBUF failed: %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (setsockopt(fd, SOL_SOCKET, SO_MARK, &s->mark, sizeof(s->mark)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "SO_MARK failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IP, IP_TOS, &s->ip_tos, sizeof(s->ip_tos)) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_warning_unit(UNIT(s)->id, "IP_TOS failed: %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (s->ip_ttl >= 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_IP, IP_TTL, &s->ip_ttl, sizeof(s->ip_ttl));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek x = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &s->ip_ttl, sizeof(s->ip_ttl));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0 && x < 0)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering log_warning_unit(UNIT(s)->id, "TCP_CONGESTION failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (fsetxattr(fd, "security.SMACK64IPIN", s->smack_ip_in, strlen(s->smack_ip_in), 0) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "fsetxattr(\"security.SMACK64IPIN\"): %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (fsetxattr(fd, "security.SMACK64IPOUT", s->smack_ip_out, strlen(s->smack_ip_out), 0) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "fsetxattr(\"security.SMACK64IPOUT\"): %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic void socket_apply_fifo_options(Socket *s, int fd) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering "F_SETPIPE_SZ: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (fsetxattr(fd, "security.SMACK64", s->smack, strlen(s->smack), 0) < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "fsetxattr(\"security.SMACK64\"): %m");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering mkdir_parents_label(path, directory_mode);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = label_context_set(path, S_IFIFO);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Enforce the right access mode for the fifo */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek old_mask = umask(~ socket_mode);
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering /* Include the original umask in our mask */
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters if ((fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmekstatic int special_address_create(
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek if ((fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
32917e33882778cf2ec6dd54b1e1082266fb072eZbigniew Jędrzejewski-Szmek /* Check whether this is a /proc, /sys or /dev file or char device */
32917e33882778cf2ec6dd54b1e1082266fb072eZbigniew Jędrzejewski-Szmek if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct mq_attr _attr, *attr = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Enforce the right access mode for the mq */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Include the original umask in our mask */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int socket_open_fds(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = socket_instantiate_service(s)) < 0)
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label);
433dd100442e8197868def975c6fd38b48dc6439Lukas Nykryn if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_apply_socket_options(s, p->fd);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (p->type == SOCKET_SPECIAL) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = special_address_create(
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek } else if (p->type == SOCKET_MQUEUE) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_not_reached("Unknown port type");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_unwatch_fds(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_unwatch_fd(UNIT(s), &p->fd_watch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LIST_FOREACH(port, p, s->ports) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_address_can_accept(&p->address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_set_state(Socket *s, SocketState state) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (state != SOCKET_START_PRE &&
253f59dff9c93ee1d2c33444b5715e42bc1c6889Lennart Poettering unit_unwatch_timer(UNIT(s), &s->timer_watch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek state != SOCKET_STOP_PRE_SIGTERM &&
763c7aa288485cf5ab627fe1d25ff58e76f9dacbZbigniew Jędrzejewski-Szmek "%s changed %s -> %s", UNIT(s)->id,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_state_to_string(old_state),
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (s->deserialized_state == SOCKET_START_PRE ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_START_POST ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE_SIGKILL ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_POST ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_FINAL_SIGTERM ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_FINAL_SIGKILL) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = unit_watch_pid(UNIT(s), s->control_pid);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (s->deserialized_state == SOCKET_START_POST ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_LISTENING ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_RUNNING ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE_SIGTERM ||
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->deserialized_state == SOCKET_STOP_PRE_SIGKILL)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if ((r = socket_open_fds(s)) < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if (s->deserialized_state == SOCKET_LISTENING)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if ((r = socket_watch_fds(s)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_set_state(s, s->deserialized_state);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering argv = unit_full_printf_strv(UNIT(s), c->argv);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering /* FIXME: we need to do something here */
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering unit_unwatch_timer(UNIT(s), &s->timer_watch);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic void socket_enter_dead(Socket *s, SocketResult f) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering exec_context_tmp_dirs_done(&s->exec_context);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic void socket_enter_signal(Socket *s, SocketState state, SocketResult f);
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poetteringstatic void socket_enter_stop_post(Socket *s, SocketResult f) {
a315ac4e076c4ce7ce3e5c95792cf916d5e918c5Lennart Poettering s->control_command_id = SOCKET_EXEC_STOP_POST;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_set_state(s, SOCKET_STOP_POST);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%s failed to run 'stop-post' task: %s",
33d52ab92f2f0bfd706e6f343d172618d1e03f3dLennart Poettering socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poetteringstatic void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering state != SOCKET_STOP_PRE_SIGTERM && state != SOCKET_FINAL_SIGTERM,
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_enter_stop_post(s, SOCKET_SUCCESS);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_enter_dead(s, SOCKET_SUCCESS);
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering "%s failed to kill processes: %s",
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poetteringstatic void socket_enter_stop_pre(Socket *s, SocketResult f) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering s->control_command_id = SOCKET_EXEC_STOP_PRE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering socket_enter_stop_post(s, SOCKET_SUCCESS);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering "%s failed to run 'stop-pre' task: %s",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
b374689c02c681671a3c3c0b0fd3add32386b442Lennart Poetteringstatic void socket_enter_listening(Socket *s) {
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering "%s failed to watch sockets: %s",
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poetteringstatic void socket_enter_start_post(Socket *s) {
94b6551662e0db8eb09768ed70f77759f322b4c6Lennart Poettering "%s failed to listen on sockets: %s",
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani s->control_command_id = SOCKET_EXEC_START_POST;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering r = socket_spawn(s, s->control_command, &s->control_pid);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "%s failed to run 'start-post' task: %s",
ff82c36c792a23a03994af2ae40cbd441e128bb4Zbigniew Jędrzejewski-Szmek socket_set_state(s, SOCKET_START_POST);
d581d9d91fa5c42ce7828a7d0b1334d370cf1670Susant Sahani socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_enter_start_pre(Socket *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_command_id = SOCKET_EXEC_START_PRE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socket_set_state(s, SOCKET_START_PRE);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen "%s failed to run 'start-pre' task: %s",
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void socket_enter_running(Socket *s, int cfd) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* We don't take connections anymore if we are supposed to
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * shut down anyway */
a9edaeff8481573764288ccf7e433a95b6dc7c03Josh Triplett "Suppressing connection request on %s since unit stop is scheduled.",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /* Flush all sockets by closing and reopening them */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "%s failed to watch sockets: %s",
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* If there's already a start pending don't bother to
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * do anything */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, true, &error, NULL);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering if (s->n_connections >= s->max_connections) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering "%s: Too many incoming connections (%u)",
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = instance_from_socket(cfd, s->n_accepted, &instance);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* ENOTCONN is legitimate if TCP RST was received.
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * This connection is over, but the socket unit lives on. */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering prefix = unit_name_to_prefix(UNIT(s)->id);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering name = unit_name_build(prefix, instance, ".service");
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner r = unit_add_name(UNIT_DEREF(s->service), name);
28def94cc8fd4394b20e2155d7130166662343c4Dave Reisner if (r < 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = service_set_socket_fd(service, cfd, s);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, true, &error, NULL);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering /* Notify clients about changed counters */
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering assert(s->control_command->command_next);
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering s->control_command = s->control_command->command_next;
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering "%s failed to run next task: %s",
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering /* We cannot fulfill this request right now, try again later
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering /* Cannot run this without the service being around */
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering service = SERVICE(UNIT_DEREF(s->service));
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering if (UNIT(service)->load_state != UNIT_LOADED) {
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering "Socket service %s not loaded, refusing.",
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering /* If the service is already active we cannot start the
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering service->state != SERVICE_AUTO_RESTART) {
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering "Socket service %s already active, refusing.",
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering "Using SysV services for socket activation is not supported. Refusing.");
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering assert(s->state == SOCKET_DEAD || s->state == SOCKET_FAILED);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering /* Already on it */
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering /* If there's already something running we go directly into
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering * kill mode. */
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering socket_enter_stop_pre(s, SOCKET_SUCCESS);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poetteringstatic int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
4de2402b603ea2f518f451d06f09e15aeae54fabLennart Poettering unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering unit_serialize_item(u, f, "result", socket_result_to_string(s->result));
119e9655dc36f18ed74f9a256d5c693b5aeb43abLennart Poettering unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if ((copy = fdset_put_dup(fds, p->fd)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = socket_address_print(&p->address, &t);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (socket_address_family(&p->address) == AF_NETLINK)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz unit_serialize_item_format(u, f, "mqueue", "%i %s", copy, p->path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering exec_context_serialize(&s->exec_context, UNIT(s), f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek state = socket_state_from_string(value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Failed to parse state value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = socket_result_from_string(value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "n-accepted")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Failed to parse n-accepted value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "control-pid")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Failed to parse control-pid value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "control-command")) {
03ee5c38cb0da193dd08733fb4c0c2809cee6a99Lennart Poettering id = socket_exec_command_from_string(value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Failed to parse exec-command value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->control_command = s->exec_command[id];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "fifo")) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
15d91bff36c61d38df8edff258d1702a017a0e66Zbigniew Jędrzejewski-Szmek if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
13790add4bf648fed816361794d8277a75253410Lennart Poettering "Failed to parse special value %s", value);
37b7affefde5443680d73642a990ce86776e28afZbigniew Jędrzejewski-Szmek if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Failed to parse mqueue value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "socket")) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "Failed to parse socket value %s", value);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (socket_address_is(&p->address, value+skip, type))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering "Failed to parse socket value %s", value);
804ae586d475d77946debb22c1bc9ee049d4750cLennart Poettering if (socket_address_is_netlink(&p->address, value+skip))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "tmp-dir")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (streq(key, "var-tmp-dir")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s->exec_context.var_tmp_dir = t;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Unknown serialization key '%s'", key);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidtstatic int socket_distribute_fds(Unit *u, FDSet *fds) {
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering if (socket_address_matches_fd(&p->address, fd)) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering s->deserialized_state = SOCKET_LISTENING;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering_pure_ static UnitActiveState socket_active_state(Unit *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return state_translation_table[SOCKET(u)->state];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek_pure_ static const char *socket_sub_state_to_string(Unit *u) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return socket_state_to_string(SOCKET(u)->state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekconst char* socket_port_type_to_string(SocketPort *p) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case SOCK_SEQPACKET: return "SequentialPacket";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (socket_address_family(&p->address) == AF_NETLINK)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return "Netlink";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering default: return "Invalid";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case SOCKET_MQUEUE: return "MessageQueue";
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering default: return NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering_pure_ static bool socket_check_gc(Unit *u) {
assert(u);
return s->n_connections > 0;
assert(s);
"%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.",
u->id);
goto fail;
if (w->socket_accept) {
if (cfd < 0) {
goto fail;
fail:
SocketResult f;
assert(s);
s->control_pid = 0;
f = SOCKET_SUCCESS;
if (s->control_command) {
f = SOCKET_SUCCESS;
u->id,
if (f != SOCKET_SUCCESS)
s->result = f;
if (s->control_command &&
f == SOCKET_SUCCESS) {
socket_run_next(s);
switch (s->state) {
case SOCKET_START_PRE:
if (f == SOCKET_SUCCESS)
case SOCKET_START_POST:
if (f == SOCKET_SUCCESS)
socket_enter_stop_pre(s, f);
case SOCKET_STOP_PRE:
case SOCKET_STOP_PRE_SIGTERM:
case SOCKET_STOP_PRE_SIGKILL:
socket_enter_stop_post(s, f);
case SOCKET_STOP_POST:
case SOCKET_FINAL_SIGTERM:
case SOCKET_FINAL_SIGKILL:
socket_enter_dead(s, f);
assert(s);
switch (s->state) {
case SOCKET_START_PRE:
case SOCKET_START_POST:
case SOCKET_STOP_PRE:
case SOCKET_STOP_PRE_SIGTERM:
u->id);
case SOCKET_STOP_PRE_SIGKILL:
case SOCKET_STOP_POST:
case SOCKET_FINAL_SIGTERM:
u->id);
case SOCKET_FINAL_SIGKILL:
u->id);
int *rfds;
unsigned rn_fds, k;
SocketPort *p;
assert(s);
rn_fds = 0;
if (p->fd >= 0)
rn_fds++;
if (rn_fds <= 0) {
*n_fds = 0;
return -ENOMEM;
if (p->fd >= 0)
assert(s);
if (failed_permanent)
assert(s);
s->n_connections--;
assert(s);
.sections =
.finished_start_job = {
.finished_stop_job = {