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
.data
.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
))) {
583 UNIT(s
)->meta
.manager
->confirm_spawn
,
584 UNIT(s
)->meta
.cgroup_bondings
,
591 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
592 /* FIXME: we need to do something here */
600 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
605 static void socket_enter_dead(Socket
*s
, bool success
) {
611 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
614 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
);
616 static void socket_enter_stop_post(Socket
*s
, bool success
) {
623 socket_unwatch_control_pid(s
);
625 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
627 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
628 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
631 socket_set_state(s
, SOCKET_STOP_POST
);
633 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, true);
638 log_warning("%s failed to run stop-post executable: %s", s
->meta
.id
, strerror(-r
));
639 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
642 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
651 if (s
->kill_mode
!= KILL_NONE
) {
652 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
654 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
656 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
657 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
663 if (!sent
&& s
->control_pid
> 0)
664 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
671 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
674 socket_set_state(s
, state
);
675 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
676 socket_enter_stop_post(s
, true);
678 socket_enter_dead(s
, true);
683 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
685 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
686 socket_enter_stop_post(s
, false);
688 socket_enter_dead(s
, false);
691 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
698 socket_unwatch_control_pid(s
);
700 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
702 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
703 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
706 socket_set_state(s
, SOCKET_STOP_PRE
);
708 socket_enter_stop_post(s
, true);
713 log_warning("%s failed to run stop-pre executable: %s", s
->meta
.id
, strerror(-r
));
714 socket_enter_stop_post(s
, false);
717 static void socket_enter_listening(Socket
*s
) {
721 if ((r
= socket_watch_fds(s
)) < 0) {
722 log_warning("%s failed to watch sockets: %s", s
->meta
.id
, strerror(-r
));
726 socket_set_state(s
, SOCKET_LISTENING
);
730 socket_enter_stop_pre(s
, false);
733 static void socket_enter_start_post(Socket
*s
) {
737 if ((r
= socket_open_fds(s
)) < 0) {
738 log_warning("%s failed to listen on sockets: %s", s
->meta
.id
, strerror(-r
));
742 socket_unwatch_control_pid(s
);
744 s
->control_command_id
= SOCKET_EXEC_START_POST
;
746 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
747 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0) {
748 log_warning("%s failed to run start-post executable: %s", s
->meta
.id
, strerror(-r
));
752 socket_set_state(s
, SOCKET_START_POST
);
754 socket_enter_listening(s
);
759 socket_enter_stop_pre(s
, false);
762 static void socket_enter_start_pre(Socket
*s
) {
766 socket_unwatch_control_pid(s
);
768 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
770 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
771 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
774 socket_set_state(s
, SOCKET_START_PRE
);
776 socket_enter_start_post(s
);
781 log_warning("%s failed to run start-pre exectuable: %s", s
->meta
.id
, strerror(-r
));
782 socket_enter_dead(s
, false);
785 static void socket_enter_running(Socket
*s
, int cfd
) {
791 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
794 socket_set_state(s
, SOCKET_RUNNING
);
797 char *prefix
, *instance
, *name
;
799 if ((r
= instance_from_socket(cfd
, s
->n_accepted
++, &instance
)))
802 if (!(prefix
= unit_name_to_prefix(UNIT(s
)->meta
.id
))) {
808 name
= unit_name_build(prefix
, instance
, ".service");
815 r
= manager_load_unit(UNIT(s
)->meta
.manager
, name
, NULL
, &u
);
821 if ((r
= service_set_socket_fd(SERVICE(u
), cfd
) < 0))
826 if ((r
= manager_add_job(u
->meta
.manager
, JOB_START
, u
, JOB_REPLACE
, true, NULL
)) < 0)
833 log_warning("%s failed to queue socket startup job: %s", s
->meta
.id
, strerror(-r
));
834 socket_enter_stop_pre(s
, false);
837 close_nointr_nofail(cfd
);
840 static void socket_run_next(Socket
*s
, bool success
) {
844 assert(s
->control_command
);
845 assert(s
->control_command
->command_next
);
850 socket_unwatch_control_pid(s
);
852 s
->control_command
= s
->control_command
->command_next
;
854 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
860 log_warning("%s failed to run spawn next executable: %s", s
->meta
.id
, strerror(-r
));
862 if (s
->state
== SOCKET_START_POST
)
863 socket_enter_stop_pre(s
, false);
864 else if (s
->state
== SOCKET_STOP_POST
)
865 socket_enter_dead(s
, false);
867 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
870 static int socket_start(Unit
*u
) {
871 Socket
*s
= SOCKET(u
);
875 /* We cannot fulfill this request right now, try again later
877 if (s
->state
== SOCKET_STOP_PRE
||
878 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
879 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
880 s
->state
== SOCKET_STOP_POST
||
881 s
->state
== SOCKET_FINAL_SIGTERM
||
882 s
->state
== SOCKET_FINAL_SIGKILL
)
885 if (s
->state
== SOCKET_START_PRE
||
886 s
->state
== SOCKET_START_POST
)
889 /* Cannot run this without the service being around */
891 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
894 /* If the service is alredy actvie we cannot start the
896 if (s
->service
->state
!= SERVICE_DEAD
&&
897 s
->service
->state
!= SERVICE_MAINTAINANCE
&&
898 s
->service
->state
!= SERVICE_AUTO_RESTART
)
902 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
905 socket_enter_start_pre(s
);
909 static int socket_stop(Unit
*u
) {
910 Socket
*s
= SOCKET(u
);
914 /* We cannot fulfill this request right now, try again later
916 if (s
->state
== SOCKET_START_PRE
||
917 s
->state
== SOCKET_START_POST
)
921 if (s
->state
== SOCKET_STOP_PRE
||
922 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
923 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
924 s
->state
== SOCKET_STOP_POST
||
925 s
->state
== SOCKET_FINAL_SIGTERM
||
926 s
->state
== SOCKET_FINAL_SIGTERM
)
929 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
931 socket_enter_stop_pre(s
, true);
935 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
936 Socket
*s
= SOCKET(u
);
944 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
945 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
946 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
948 if (s
->control_pid
> 0)
949 unit_serialize_item_format(u
, f
, "control-pid", "%u", (unsigned) s
->control_pid
);
951 if (s
->control_command_id
>= 0)
952 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
954 LIST_FOREACH(port
, p
, s
->ports
) {
960 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
963 if (p
->type
== SOCKET_SOCKET
) {
966 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
969 unit_serialize_item_format(u
, f
, "socket", "%i %s", copy
, t
);
972 assert(p
->type
== SOCKET_FIFO
);
973 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
980 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
981 Socket
*s
= SOCKET(u
);
989 if (streq(key
, "state")) {
992 if ((state
= socket_state_from_string(value
)) < 0)
993 log_debug("Failed to parse state value %s", value
);
995 s
->deserialized_state
= state
;
996 } else if (streq(key
, "failure")) {
999 if ((b
= parse_boolean(value
)) < 0)
1000 log_debug("Failed to parse failure value %s", value
);
1002 s
->failure
= b
|| s
->failure
;
1004 } else if (streq(key
, "n-accepted")) {
1007 if ((r
= safe_atou(value
, &k
)) < 0)
1008 log_debug("Failed to parse n-accepted value %s", value
);
1011 } else if (streq(key
, "control-pid")) {
1014 if ((r
= safe_atou(value
, &pid
)) < 0 || pid
<= 0)
1015 log_debug("Failed to parse control-pid value %s", value
);
1017 s
->control_pid
= (pid_t
) pid
;
1018 } else if (streq(key
, "control-command")) {
1019 SocketExecCommand id
;
1021 if ((id
= socket_exec_command_from_string(value
)) < 0)
1022 log_debug("Failed to parse exec-command value %s", value
);
1024 s
->control_command_id
= id
;
1025 s
->control_command
= s
->exec_command
[id
];
1027 } else if (streq(key
, "fifo")) {
1031 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1032 log_debug("Failed to parse fifo value %s", value
);
1035 LIST_FOREACH(port
, p
, s
->ports
)
1036 if (streq(p
->path
, value
+skip
))
1041 close_nointr_nofail(p
->fd
);
1042 p
->fd
= fdset_remove(fds
, fd
);
1046 } else if (streq(key
, "socket")) {
1050 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1051 log_debug("Failed to parse socket value %s", value
);
1054 LIST_FOREACH(port
, p
, s
->ports
)
1055 if (socket_address_is(&p
->address
, value
+skip
))
1060 close_nointr_nofail(p
->fd
);
1061 p
->fd
= fdset_remove(fds
, fd
);
1066 log_debug("Unknown serialization key '%s'", key
);
1071 static UnitActiveState
socket_active_state(Unit
*u
) {
1074 return state_translation_table
[SOCKET(u
)->state
];
1077 static const char *socket_sub_state_to_string(Unit
*u
) {
1080 return socket_state_to_string(SOCKET(u
)->state
);
1083 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1084 Socket
*s
= SOCKET(u
);
1090 log_debug("Incoming traffic on %s", u
->meta
.id
);
1092 if (events
!= EPOLLIN
) {
1093 log_error("Got invalid poll event on socket.");
1097 if (w
->data
.socket_accept
) {
1100 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1105 log_error("Failed to accept socket: %m");
1113 log_debug("cfd=%i", cfd
);
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