1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
27 #include <sys/epoll.h>
29 #include <arpa/inet.h>
34 #include "load-dropin.h"
35 #include "load-fragment.h"
37 #include "unit-name.h"
38 #include "dbus-socket.h"
40 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
41 [SOCKET_DEAD
] = UNIT_INACTIVE
,
42 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
43 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
44 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
45 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
46 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
47 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
48 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
49 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
50 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
51 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
52 [SOCKET_MAINTAINANCE
] = UNIT_INACTIVE
,
55 static void socket_init(Unit
*u
) {
56 Socket
*s
= SOCKET(u
);
59 assert(u
->meta
.load_state
== UNIT_STUB
);
61 s
->timer_watch
.type
= WATCH_INVALID
;
62 s
->backlog
= SOMAXCONN
;
63 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
64 s
->directory_mode
= 0755;
65 s
->socket_mode
= 0666;
67 exec_context_init(&s
->exec_context
);
69 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
72 static void socket_unwatch_control_pid(Socket
*s
) {
75 if (s
->control_pid
<= 0)
78 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
82 static void socket_done(Unit
*u
) {
83 Socket
*s
= SOCKET(u
);
88 while ((p
= s
->ports
)) {
89 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
92 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
93 close_nointr_nofail(p
->fd
);
100 exec_context_done(&s
->exec_context
);
101 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
102 s
->control_command
= NULL
;
104 socket_unwatch_control_pid(s
);
108 free(s
->bind_to_device
);
109 s
->bind_to_device
= NULL
;
111 unit_unwatch_timer(u
, &s
->timer_watch
);
114 static bool have_non_accept_socket(Socket
*s
) {
122 LIST_FOREACH(port
, p
, s
->ports
) {
124 if (p
->type
!= SOCKET_SOCKET
)
127 if (!socket_address_can_accept(&p
->address
))
134 static int socket_verify(Socket
*s
) {
137 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
141 log_error("%s lacks Listen setting. Refusing.", UNIT(s
)->meta
.id
);
148 static int socket_load(Unit
*u
) {
149 Socket
*s
= SOCKET(u
);
153 assert(u
->meta
.load_state
== UNIT_STUB
);
155 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
158 /* This is a new unit? Then let's add in some extras */
159 if (u
->meta
.load_state
== UNIT_LOADED
) {
161 if (have_non_accept_socket(s
)) {
162 if ((r
= unit_load_related_unit(u
, ".service", (Unit
**) &s
->service
)))
165 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
), true)) < 0)
169 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
172 if ((r
= unit_add_default_cgroup(u
)) < 0)
176 return socket_verify(s
);
179 static const char* listen_lookup(int type
) {
181 if (type
== SOCK_STREAM
)
182 return "ListenStream";
183 else if (type
== SOCK_DGRAM
)
184 return "ListenDatagram";
185 else if (type
== SOCK_SEQPACKET
)
186 return "ListenSequentialPacket";
188 assert_not_reached("Unknown socket type");
192 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
195 Socket
*s
= SOCKET(u
);
203 p2
= strappend(prefix
, "\t");
204 prefix2
= p2
? p2
: prefix
;
207 "%sSocket State: %s\n"
208 "%sBindIPv6Only: %s\n"
211 "%sSocketMode: %04o\n"
212 "%sDirectoryMode: %04o\n",
213 prefix
, socket_state_to_string(s
->state
),
214 prefix
, yes_no(s
->bind_ipv6_only
),
216 prefix
, kill_mode_to_string(s
->kill_mode
),
217 prefix
, s
->socket_mode
,
218 prefix
, s
->directory_mode
);
220 if (s
->control_pid
> 0)
222 "%sControl PID: %llu\n",
223 prefix
, (unsigned long long) s
->control_pid
);
225 if (s
->bind_to_device
)
227 "%sBindToDevice: %s\n",
228 prefix
, s
->bind_to_device
);
233 prefix
, s
->n_accepted
);
235 LIST_FOREACH(port
, p
, s
->ports
) {
237 if (p
->type
== SOCKET_SOCKET
) {
242 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
247 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
250 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
253 exec_context_dump(&s
->exec_context
, f
, prefix
);
255 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
256 if (!s
->exec_command
[c
])
259 fprintf(f
, "%s-> %s:\n",
260 prefix
, socket_exec_command_to_string(c
));
262 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
268 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
273 struct sockaddr_un un
;
274 struct sockaddr_in in
;
275 struct sockaddr_in6 in6
;
276 struct sockaddr_storage storage
;
283 if (getsockname(fd
, &local
.sa
, &l
) < 0)
287 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
290 switch (local
.sa
.sa_family
) {
294 a
= ntohl(local
.in
.sin_addr
.s_addr
),
295 b
= ntohl(remote
.in
.sin_addr
.s_addr
);
298 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
300 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
301 ntohs(local
.in
.sin_port
),
302 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
303 ntohs(remote
.in
.sin_port
)) < 0)
310 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
315 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
316 ntohs(local
.in6
.sin6_port
),
317 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
318 ntohs(remote
.in6
.sin6_port
)) < 0)
328 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
334 (unsigned long long) ucred
.pid
,
335 (unsigned long long) ucred
.uid
) < 0)
342 assert_not_reached("Unhandled socket type.");
349 static void socket_close_fds(Socket
*s
) {
354 LIST_FOREACH(port
, p
, s
->ports
) {
358 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
359 close_nointr_nofail(p
->fd
);
361 /* One little note: we should never delete any sockets
362 * in the file system here! After all some other
363 * process we spawned might still have a reference of
364 * this fd and wants to continue to use it. Therefore
365 * we delete sockets in the file system before we
366 * create a new one, not after we stopped using
373 static int socket_open_fds(Socket
*s
) {
379 LIST_FOREACH(port
, p
, s
->ports
) {
384 if (p
->type
== SOCKET_SOCKET
) {
386 if ((r
= socket_address_listen(
398 assert(p
->type
== SOCKET_FIFO
);
400 mkdir_parents(p
->path
, s
->directory_mode
);
402 if (mkfifo(p
->path
, s
->socket_mode
) < 0 && errno
!= EEXIST
) {
407 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
412 if (fstat(p
->fd
, &st
) < 0) {
417 /* FIXME verify user, access mode */
419 if (!S_ISFIFO(st
.st_mode
)) {
433 static void socket_unwatch_fds(Socket
*s
) {
438 LIST_FOREACH(port
, p
, s
->ports
) {
442 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
446 static int socket_watch_fds(Socket
*s
) {
452 LIST_FOREACH(port
, p
, s
->ports
) {
456 p
->fd_watch
.socket_accept
=
458 p
->type
== SOCKET_SOCKET
&&
459 socket_address_can_accept(&p
->address
);
461 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
468 socket_unwatch_fds(s
);
472 static void socket_set_state(Socket
*s
, SocketState state
) {
473 SocketState old_state
;
476 old_state
= s
->state
;
479 if (state
!= SOCKET_START_PRE
&&
480 state
!= SOCKET_START_POST
&&
481 state
!= SOCKET_STOP_PRE
&&
482 state
!= SOCKET_STOP_PRE_SIGTERM
&&
483 state
!= SOCKET_STOP_PRE_SIGKILL
&&
484 state
!= SOCKET_STOP_POST
&&
485 state
!= SOCKET_FINAL_SIGTERM
&&
486 state
!= SOCKET_FINAL_SIGKILL
) {
487 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
488 socket_unwatch_control_pid(s
);
489 s
->control_command
= NULL
;
490 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
493 if (state
!= SOCKET_LISTENING
)
494 socket_unwatch_fds(s
);
496 if (state
!= SOCKET_START_POST
&&
497 state
!= SOCKET_LISTENING
&&
498 state
!= SOCKET_RUNNING
&&
499 state
!= SOCKET_STOP_PRE
&&
500 state
!= SOCKET_STOP_PRE_SIGTERM
&&
501 state
!= SOCKET_STOP_PRE_SIGKILL
)
504 if (state
!= old_state
)
505 log_debug("%s changed %s -> %s",
507 socket_state_to_string(old_state
),
508 socket_state_to_string(state
));
510 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
513 static int socket_coldplug(Unit
*u
) {
514 Socket
*s
= SOCKET(u
);
518 assert(s
->state
== SOCKET_DEAD
);
520 if (s
->deserialized_state
!= s
->state
) {
522 if (s
->deserialized_state
== SOCKET_START_PRE
||
523 s
->deserialized_state
== SOCKET_START_POST
||
524 s
->deserialized_state
== SOCKET_STOP_PRE
||
525 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
526 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
||
527 s
->deserialized_state
== SOCKET_STOP_POST
||
528 s
->deserialized_state
== SOCKET_FINAL_SIGTERM
||
529 s
->deserialized_state
== SOCKET_FINAL_SIGKILL
) {
531 if (s
->control_pid
<= 0)
534 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
537 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
541 if (s
->deserialized_state
== SOCKET_START_POST
||
542 s
->deserialized_state
== SOCKET_LISTENING
||
543 s
->deserialized_state
== SOCKET_RUNNING
||
544 s
->deserialized_state
== SOCKET_STOP_PRE
||
545 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
546 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
)
547 if ((r
= socket_open_fds(s
)) < 0)
550 if (s
->deserialized_state
== SOCKET_LISTENING
)
551 if ((r
= socket_watch_fds(s
)) < 0)
554 socket_set_state(s
, s
->deserialized_state
);
560 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
569 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
572 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
581 s
->meta
.manager
->environment
,
584 UNIT(s
)->meta
.manager
->confirm_spawn
,
585 UNIT(s
)->meta
.cgroup_bondings
,
592 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
593 /* FIXME: we need to do something here */
601 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
606 static void socket_enter_dead(Socket
*s
, bool success
) {
612 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
615 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
);
617 static void socket_enter_stop_post(Socket
*s
, bool success
) {
624 socket_unwatch_control_pid(s
);
626 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
628 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
629 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
632 socket_set_state(s
, SOCKET_STOP_POST
);
634 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, true);
639 log_warning("%s failed to run stop-post executable: %s", s
->meta
.id
, strerror(-r
));
640 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
643 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
652 if (s
->kill_mode
!= KILL_NONE
) {
653 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
655 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
657 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
658 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
664 if (!sent
&& s
->control_pid
> 0)
665 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
671 if (sent
&& s
->control_pid
> 0) {
672 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
675 socket_set_state(s
, state
);
676 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
677 socket_enter_stop_post(s
, true);
679 socket_enter_dead(s
, true);
684 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
686 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
687 socket_enter_stop_post(s
, false);
689 socket_enter_dead(s
, false);
692 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
699 socket_unwatch_control_pid(s
);
701 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
703 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
704 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
707 socket_set_state(s
, SOCKET_STOP_PRE
);
709 socket_enter_stop_post(s
, true);
714 log_warning("%s failed to run stop-pre executable: %s", s
->meta
.id
, strerror(-r
));
715 socket_enter_stop_post(s
, false);
718 static void socket_enter_listening(Socket
*s
) {
722 if ((r
= socket_watch_fds(s
)) < 0) {
723 log_warning("%s failed to watch sockets: %s", s
->meta
.id
, strerror(-r
));
727 socket_set_state(s
, SOCKET_LISTENING
);
731 socket_enter_stop_pre(s
, false);
734 static void socket_enter_start_post(Socket
*s
) {
738 if ((r
= socket_open_fds(s
)) < 0) {
739 log_warning("%s failed to listen on sockets: %s", s
->meta
.id
, strerror(-r
));
743 socket_unwatch_control_pid(s
);
745 s
->control_command_id
= SOCKET_EXEC_START_POST
;
747 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
748 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0) {
749 log_warning("%s failed to run start-post executable: %s", s
->meta
.id
, strerror(-r
));
753 socket_set_state(s
, SOCKET_START_POST
);
755 socket_enter_listening(s
);
760 socket_enter_stop_pre(s
, false);
763 static void socket_enter_start_pre(Socket
*s
) {
767 socket_unwatch_control_pid(s
);
769 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
771 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
772 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
775 socket_set_state(s
, SOCKET_START_PRE
);
777 socket_enter_start_post(s
);
782 log_warning("%s failed to run start-pre exectuable: %s", s
->meta
.id
, strerror(-r
));
783 socket_enter_dead(s
, false);
786 static void socket_enter_running(Socket
*s
, int cfd
) {
792 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
795 socket_set_state(s
, SOCKET_RUNNING
);
798 char *prefix
, *instance
, *name
;
800 if ((r
= instance_from_socket(cfd
, s
->n_accepted
++, &instance
)))
803 if (!(prefix
= unit_name_to_prefix(UNIT(s
)->meta
.id
))) {
809 name
= unit_name_build(prefix
, instance
, ".service");
816 r
= manager_load_unit(UNIT(s
)->meta
.manager
, name
, NULL
, &u
);
822 if ((r
= service_set_socket_fd(SERVICE(u
), cfd
) < 0))
827 if ((r
= manager_add_job(u
->meta
.manager
, JOB_START
, u
, JOB_REPLACE
, true, NULL
)) < 0)
834 log_warning("%s failed to queue socket startup job: %s", s
->meta
.id
, strerror(-r
));
835 socket_enter_stop_pre(s
, false);
838 close_nointr_nofail(cfd
);
841 static void socket_run_next(Socket
*s
, bool success
) {
845 assert(s
->control_command
);
846 assert(s
->control_command
->command_next
);
851 socket_unwatch_control_pid(s
);
853 s
->control_command
= s
->control_command
->command_next
;
855 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
861 log_warning("%s failed to run spawn next executable: %s", s
->meta
.id
, strerror(-r
));
863 if (s
->state
== SOCKET_START_POST
)
864 socket_enter_stop_pre(s
, false);
865 else if (s
->state
== SOCKET_STOP_POST
)
866 socket_enter_dead(s
, false);
868 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
871 static int socket_start(Unit
*u
) {
872 Socket
*s
= SOCKET(u
);
876 /* We cannot fulfill this request right now, try again later
878 if (s
->state
== SOCKET_STOP_PRE
||
879 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
880 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
881 s
->state
== SOCKET_STOP_POST
||
882 s
->state
== SOCKET_FINAL_SIGTERM
||
883 s
->state
== SOCKET_FINAL_SIGKILL
)
886 if (s
->state
== SOCKET_START_PRE
||
887 s
->state
== SOCKET_START_POST
)
890 /* Cannot run this without the service being around */
892 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
895 /* If the service is alredy actvie we cannot start the
897 if (s
->service
->state
!= SERVICE_DEAD
&&
898 s
->service
->state
!= SERVICE_MAINTAINANCE
&&
899 s
->service
->state
!= SERVICE_AUTO_RESTART
)
903 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
906 socket_enter_start_pre(s
);
910 static int socket_stop(Unit
*u
) {
911 Socket
*s
= SOCKET(u
);
915 /* We cannot fulfill this request right now, try again later
917 if (s
->state
== SOCKET_START_PRE
||
918 s
->state
== SOCKET_START_POST
)
922 if (s
->state
== SOCKET_STOP_PRE
||
923 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
924 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
925 s
->state
== SOCKET_STOP_POST
||
926 s
->state
== SOCKET_FINAL_SIGTERM
||
927 s
->state
== SOCKET_FINAL_SIGTERM
)
930 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
932 socket_enter_stop_pre(s
, true);
936 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
937 Socket
*s
= SOCKET(u
);
945 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
946 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
947 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
949 if (s
->control_pid
> 0)
950 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) s
->control_pid
);
952 if (s
->control_command_id
>= 0)
953 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
955 LIST_FOREACH(port
, p
, s
->ports
) {
961 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
964 if (p
->type
== SOCKET_SOCKET
) {
967 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
970 unit_serialize_item_format(u
, f
, "socket", "%i %s", copy
, t
);
973 assert(p
->type
== SOCKET_FIFO
);
974 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
981 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
982 Socket
*s
= SOCKET(u
);
990 if (streq(key
, "state")) {
993 if ((state
= socket_state_from_string(value
)) < 0)
994 log_debug("Failed to parse state value %s", value
);
996 s
->deserialized_state
= state
;
997 } else if (streq(key
, "failure")) {
1000 if ((b
= parse_boolean(value
)) < 0)
1001 log_debug("Failed to parse failure value %s", value
);
1003 s
->failure
= b
|| s
->failure
;
1005 } else if (streq(key
, "n-accepted")) {
1008 if ((r
= safe_atou(value
, &k
)) < 0)
1009 log_debug("Failed to parse n-accepted value %s", value
);
1012 } else if (streq(key
, "control-pid")) {
1015 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1016 log_debug("Failed to parse control-pid value %s", value
);
1018 s
->control_pid
= (pid_t
) pid
;
1019 } else if (streq(key
, "control-command")) {
1020 SocketExecCommand id
;
1022 if ((id
= socket_exec_command_from_string(value
)) < 0)
1023 log_debug("Failed to parse exec-command value %s", value
);
1025 s
->control_command_id
= id
;
1026 s
->control_command
= s
->exec_command
[id
];
1028 } else if (streq(key
, "fifo")) {
1032 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1033 log_debug("Failed to parse fifo value %s", value
);
1036 LIST_FOREACH(port
, p
, s
->ports
)
1037 if (streq(p
->path
, value
+skip
))
1042 close_nointr_nofail(p
->fd
);
1043 p
->fd
= fdset_remove(fds
, fd
);
1047 } else if (streq(key
, "socket")) {
1051 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1052 log_debug("Failed to parse socket value %s", value
);
1055 LIST_FOREACH(port
, p
, s
->ports
)
1056 if (socket_address_is(&p
->address
, value
+skip
))
1061 close_nointr_nofail(p
->fd
);
1062 p
->fd
= fdset_remove(fds
, fd
);
1067 log_debug("Unknown serialization key '%s'", key
);
1072 static UnitActiveState
socket_active_state(Unit
*u
) {
1075 return state_translation_table
[SOCKET(u
)->state
];
1078 static const char *socket_sub_state_to_string(Unit
*u
) {
1081 return socket_state_to_string(SOCKET(u
)->state
);
1084 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1085 Socket
*s
= SOCKET(u
);
1091 log_debug("Incoming traffic on %s", u
->meta
.id
);
1093 if (events
!= EPOLLIN
) {
1094 log_error("Got invalid poll event on socket.");
1098 if (w
->socket_accept
) {
1101 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1106 log_error("Failed to accept socket: %m");
1114 socket_enter_running(s
, cfd
);
1118 socket_enter_stop_pre(s
, false);
1121 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1122 Socket
*s
= SOCKET(u
);
1128 success
= code
== CLD_EXITED
&& status
== 0;
1129 s
->failure
= s
->failure
|| !success
;
1131 assert(s
->control_pid
== pid
);
1134 if (s
->control_command
)
1135 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1137 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1139 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1140 log_debug("%s running next command for state %s", u
->meta
.id
, socket_state_to_string(s
->state
));
1141 socket_run_next(s
, success
);
1143 s
->control_command
= NULL
;
1144 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1146 /* No further commands for this step, so let's figure
1147 * out what to do next */
1149 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, socket_state_to_string(s
->state
));
1153 case SOCKET_START_PRE
:
1155 socket_enter_start_post(s
);
1157 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1160 case SOCKET_START_POST
:
1162 socket_enter_listening(s
);
1164 socket_enter_stop_pre(s
, false);
1167 case SOCKET_STOP_PRE
:
1168 case SOCKET_STOP_PRE_SIGTERM
:
1169 case SOCKET_STOP_PRE_SIGKILL
:
1170 socket_enter_stop_post(s
, success
);
1173 case SOCKET_STOP_POST
:
1174 case SOCKET_FINAL_SIGTERM
:
1175 case SOCKET_FINAL_SIGKILL
:
1176 socket_enter_dead(s
, success
);
1180 assert_not_reached("Uh, control process died at wrong time.");
1185 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
1186 Socket
*s
= SOCKET(u
);
1189 assert(elapsed
== 1);
1190 assert(w
== &s
->timer_watch
);
1194 case SOCKET_START_PRE
:
1195 log_warning("%s starting timed out. Terminating.", u
->meta
.id
);
1196 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1198 case SOCKET_START_POST
:
1199 log_warning("%s starting timed out. Stopping.", u
->meta
.id
);
1200 socket_enter_stop_pre(s
, false);
1203 case SOCKET_STOP_PRE
:
1204 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
1205 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
1208 case SOCKET_STOP_PRE_SIGTERM
:
1209 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
1210 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
1213 case SOCKET_STOP_PRE_SIGKILL
:
1214 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
1215 socket_enter_stop_post(s
, false);
1218 case SOCKET_STOP_POST
:
1219 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
1220 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1223 case SOCKET_FINAL_SIGTERM
:
1224 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
1225 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, false);
1228 case SOCKET_FINAL_SIGKILL
:
1229 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u
->meta
.id
);
1230 socket_enter_dead(s
, false);
1234 assert_not_reached("Timeout at wrong time.");
1238 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
1247 /* Called from the service code for requesting our fds */
1250 LIST_FOREACH(port
, p
, s
->ports
)
1254 if (!(rfds
= new(int, rn_fds
)) < 0)
1258 LIST_FOREACH(port
, p
, s
->ports
)
1262 assert(k
== rn_fds
);
1270 void socket_notify_service_dead(Socket
*s
) {
1273 /* The service is dead. Dang. */
1275 if (s
->state
== SOCKET_RUNNING
) {
1276 log_debug("%s got notified about service death.", s
->meta
.id
);
1277 socket_enter_listening(s
);
1281 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
1282 [SOCKET_DEAD
] = "dead",
1283 [SOCKET_START_PRE
] = "start-pre",
1284 [SOCKET_START_POST
] = "start-post",
1285 [SOCKET_LISTENING
] = "listening",
1286 [SOCKET_RUNNING
] = "running",
1287 [SOCKET_STOP_PRE
] = "stop-pre",
1288 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
1289 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
1290 [SOCKET_STOP_POST
] = "stop-post",
1291 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
1292 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
1293 [SOCKET_MAINTAINANCE
] = "maintainance"
1296 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
1298 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
1299 [SOCKET_EXEC_START_PRE
] = "StartPre",
1300 [SOCKET_EXEC_START_POST
] = "StartPost",
1301 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
1302 [SOCKET_EXEC_STOP_POST
] = "StopPost"
1305 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
1307 const UnitVTable socket_vtable
= {
1308 .suffix
= ".socket",
1310 .init
= socket_init
,
1311 .done
= socket_done
,
1312 .load
= socket_load
,
1314 .coldplug
= socket_coldplug
,
1316 .dump
= socket_dump
,
1318 .start
= socket_start
,
1319 .stop
= socket_stop
,
1321 .serialize
= socket_serialize
,
1322 .deserialize_item
= socket_deserialize_item
,
1324 .active_state
= socket_active_state
,
1325 .sub_state_to_string
= socket_sub_state_to_string
,
1327 .fd_event
= socket_fd_event
,
1328 .sigchld_event
= socket_sigchld_event
,
1329 .timer_event
= socket_timer_event
,
1331 .bus_message_handler
= bus_socket_message_handler