2012-07-18 21:07:51 +04:00
# pragma once
2010-01-23 03:52:57 +03:00
2010-02-03 15:03:47 +03:00
/***
This file is part of systemd .
Copyright 2010 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
2012-04-12 02:20:58 +04:00
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
2010-02-03 15:03:47 +03:00
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2012-04-12 02:20:58 +04:00
Lesser General Public License for more details .
2010-02-03 15:03:47 +03:00
2012-04-12 02:20:58 +04:00
You should have received a copy of the GNU Lesser General Public License
2010-02-03 15:03:47 +03:00
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
2010-01-23 03:52:57 +03:00
typedef struct Service Service ;
2015-01-06 02:26:25 +03:00
typedef struct ServiceFDStore ServiceFDStore ;
2010-01-23 03:52:57 +03:00
2015-11-19 00:46:33 +03:00
# include "exit-status.h"
# include "kill.h"
2012-01-05 03:56:21 +04:00
# include "path.h"
2010-01-29 06:42:57 +03:00
# include "ratelimit.h"
2010-01-23 03:52:57 +03:00
2010-01-26 06:18:44 +03:00
typedef enum ServiceRestart {
2010-10-05 22:30:44 +04:00
SERVICE_RESTART_NO ,
2010-01-26 06:18:44 +03:00
SERVICE_RESTART_ON_SUCCESS ,
2010-10-08 20:34:54 +04:00
SERVICE_RESTART_ON_FAILURE ,
2014-06-05 20:42:52 +04:00
SERVICE_RESTART_ON_ABNORMAL ,
2013-07-25 16:07:59 +04:00
SERVICE_RESTART_ON_WATCHDOG ,
2010-10-08 20:34:54 +04:00
SERVICE_RESTART_ON_ABORT ,
2010-01-30 03:55:42 +03:00
SERVICE_RESTART_ALWAYS ,
_SERVICE_RESTART_MAX ,
_SERVICE_RESTART_INVALID = - 1
2010-01-26 06:18:44 +03:00
} ServiceRestart ;
typedef enum ServiceType {
2010-04-16 01:16:16 +04:00
SERVICE_SIMPLE , /* we fork and go on right-away (i.e. modern socket activated daemons) */
2010-06-02 21:15:42 +04:00
SERVICE_FORKING , /* forks by itself (i.e. traditional daemons) */
2010-08-13 20:23:01 +04:00
SERVICE_ONESHOT , /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
2010-04-16 01:16:16 +04:00
SERVICE_DBUS , /* we fork and wait until a specific D-Bus name appears on the bus */
2010-06-16 07:10:31 +04:00
SERVICE_NOTIFY , /* we fork and wait until a daemon sends us a ready message with sd_notify() */
2012-04-24 16:28:00 +04:00
SERVICE_IDLE , /* much like simple, but delay exec() until all jobs are dispatched. */
2010-01-30 03:55:42 +03:00
_SERVICE_TYPE_MAX ,
_SERVICE_TYPE_INVALID = - 1
2010-01-26 06:18:44 +03:00
} ServiceType ;
2010-01-23 03:52:57 +03:00
typedef enum ServiceExecCommand {
SERVICE_EXEC_START_PRE ,
SERVICE_EXEC_START ,
SERVICE_EXEC_START_POST ,
SERVICE_EXEC_RELOAD ,
SERVICE_EXEC_STOP ,
SERVICE_EXEC_STOP_POST ,
2010-04-10 19:53:17 +04:00
_SERVICE_EXEC_COMMAND_MAX ,
_SERVICE_EXEC_COMMAND_INVALID = - 1
2010-01-23 03:52:57 +03:00
} ServiceExecCommand ;
2010-06-19 01:12:48 +04:00
typedef enum NotifyAccess {
NOTIFY_NONE ,
NOTIFY_ALL ,
NOTIFY_MAIN ,
_NOTIFY_ACCESS_MAX ,
_NOTIFY_ACCESS_INVALID = - 1
} NotifyAccess ;
2014-08-21 19:03:15 +04:00
typedef enum NotifyState {
NOTIFY_UNKNOWN ,
NOTIFY_READY ,
NOTIFY_RELOADING ,
NOTIFY_STOPPING ,
_NOTIFY_STATE_MAX ,
_NOTIFY_STATE_INVALID = - 1
} NotifyState ;
2012-02-03 05:01:35 +04:00
typedef enum ServiceResult {
SERVICE_SUCCESS ,
2016-04-25 22:36:25 +03:00
SERVICE_FAILURE_RESOURCES , /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
2012-02-03 05:01:35 +04:00
SERVICE_FAILURE_TIMEOUT ,
SERVICE_FAILURE_EXIT_CODE ,
SERVICE_FAILURE_SIGNAL ,
SERVICE_FAILURE_CORE_DUMP ,
2012-02-08 13:10:34 +04:00
SERVICE_FAILURE_WATCHDOG ,
core: move enforcement of the start limit into per-unit-type code again
Let's move the enforcement of the per-unit start limit from unit.c into the
type-specific files again. For unit types that know a concept of "result" codes
this allows us to hook up the start limit condition to it with an explicit
result code. Also, this makes sure that the state checks in clal like
service_start() may be done before the start limit is checked, as the start
limit really should be checked last, right before everything has been verified
to be in order.
The generic start limit logic is left in unit.c, but the invocation of it is
moved into the per-type files, in the various xyz_start() functions, so that
they may place the check at the right location.
Note that this change drops the enforcement entirely from device, slice, target
and scope units, since these unit types generally may not fail activation, or
may only be activated a single time. This is also documented now.
Note that restores the "start-limit-hit" result code that existed before
6bf0f408e4833152197fb38fb10a9989c89f3a59 already in the service code. However,
it's not introduced for all units that have a result code concept.
Fixes #3166.
2016-05-02 14:01:26 +03:00
SERVICE_FAILURE_START_LIMIT_HIT ,
2012-02-03 05:01:35 +04:00
_SERVICE_RESULT_MAX ,
_SERVICE_RESULT_INVALID = - 1
} ServiceResult ;
2015-01-06 02:26:25 +03:00
struct ServiceFDStore {
Service * service ;
int fd ;
2015-10-04 18:36:19 +03:00
char * fdname ;
2015-01-06 02:26:25 +03:00
sd_event_source * event_source ;
LIST_FIELDS ( ServiceFDStore , fd_store ) ;
} ;
2010-01-23 03:52:57 +03:00
struct Service {
2012-01-15 15:04:08 +04:00
Unit meta ;
2010-01-23 03:52:57 +03:00
2010-01-26 06:18:44 +03:00
ServiceType type ;
ServiceRestart restart ;
2014-07-03 14:47:40 +04:00
ExitStatusSet restart_prevent_status ;
ExitStatusSet restart_force_status ;
2012-08-13 15:58:01 +04:00
ExitStatusSet success_status ;
2010-01-26 06:18:44 +03:00
/* If set we'll read the main daemon PID from this file */
char * pid_file ;
usec_t restart_usec ;
2012-08-07 16:41:48 +04:00
usec_t timeout_start_usec ;
usec_t timeout_stop_usec ;
core: rework unit timeout handling, and add new setting RuntimeMaxSec=
This clean-ups timeout handling in PID 1. Specifically, instead of storing 0 in internal timeout variables as
indication for a disabled timeout, use USEC_INFINITY which is in-line with how we do this in the rest of our code
(following the logic that 0 means "no", and USEC_INFINITY means "never").
This also replace all usec_t additions with invocations to usec_add(), so that USEC_INFINITY is properly propagated,
and sd-event considers it has indication for turning off the event source.
This also alters the deserialization of the units to restart timeouts from the time they were originally started from.
Before this patch timeouts would be restarted beginning with the time of the deserialization, which could lead to
artificially prolonged timeouts if a daemon reload took place.
Finally, a new RuntimeMaxSec= setting is introduced for service units, that specifies a maximum runtime after which a
specific service is forcibly terminated. This is useful to put time limits on time-intensive processing jobs.
This also simplifies the various xyz_spawn() calls of the various types in that explicit distruction of the timers is
removed, as that is done anyway by the state change handlers, and a state change is always done when the xyz_spawn()
calls fail.
Fixes: #2249
2016-02-01 23:48:10 +03:00
usec_t runtime_max_usec ;
2010-01-23 03:52:57 +03:00
2012-02-01 20:17:12 +04:00
dual_timestamp watchdog_timestamp ;
2012-02-08 13:10:34 +04:00
usec_t watchdog_usec ;
2013-11-20 00:12:59 +04:00
sd_event_source * watchdog_event_source ;
2012-02-01 20:17:12 +04:00
2010-04-10 19:53:17 +04:00
ExecCommand * exec_command [ _SERVICE_EXEC_COMMAND_MAX ] ;
2012-07-20 01:47:10 +04:00
2010-01-23 03:52:57 +03:00
ExecContext exec_context ;
2012-07-20 01:47:10 +04:00
KillContext kill_context ;
2013-06-27 06:14:27 +04:00
CGroupContext cgroup_context ;
2010-01-23 03:52:57 +03:00
2010-04-21 05:27:44 +04:00
ServiceState state , deserialized_state ;
2010-01-26 06:18:44 +03:00
2011-01-20 20:46:38 +03:00
/* The exit status of the real main process */
2010-01-26 06:18:44 +03:00
ExecStatus main_exec_status ;
2011-01-20 20:46:38 +03:00
/* The currently executed control process */
2010-01-26 06:18:44 +03:00
ExecCommand * control_command ;
2011-01-20 20:46:38 +03:00
/* The currently executed main process, which may be NULL if
* the main process got started via forking mode and not by
* us */
ExecCommand * main_command ;
/* The ID of the control command currently being executed */
2010-04-21 05:27:44 +04:00
ServiceExecCommand control_command_id ;
2011-01-20 20:46:38 +03:00
2013-11-27 23:23:18 +04:00
/* Runtime data of the execution context */
ExecRuntime * exec_runtime ;
2010-01-26 06:18:44 +03:00
pid_t main_pid , control_pid ;
2010-09-21 07:23:12 +04:00
int socket_fd ;
2014-07-24 12:40:28 +04:00
bool socket_fd_selinux_context_net ;
2010-07-20 22:33:19 +04:00
bool permissions_start_only ;
bool root_directory_start_only ;
2010-08-17 21:37:36 +04:00
bool remain_after_exit ;
2011-02-13 20:51:30 +03:00
bool guess_main_pid ;
2010-07-20 22:33:19 +04:00
2010-04-16 01:16:16 +04:00
/* If we shut down, remember why */
2012-02-03 05:01:35 +04:00
ServiceResult result ;
ServiceResult reload_result ;
2011-01-20 15:17:22 +03:00
2010-08-09 19:12:25 +04:00
bool main_pid_known : 1 ;
2011-04-28 06:56:53 +04:00
bool main_pid_alien : 1 ;
2010-04-16 01:16:16 +04:00
bool bus_name_good : 1 ;
2010-08-10 01:33:48 +04:00
bool forbid_restart : 1 ;
2012-08-07 16:41:48 +04:00
bool start_timeout_defined : 1 ;
2010-02-14 03:09:01 +03:00
2015-03-01 18:24:19 +03:00
bool reset_cpu_usage : 1 ;
2010-04-16 01:16:16 +04:00
char * bus_name ;
core: fix bus name synchronization after daemon-reload
During daemon-reload, PID1 temporarly loses its DBus connection, so there's
a small window in which all signals sent by dbus-daemon are lost.
This is a problem, since we rely on the NameOwnerChanged signals in order to
consider a service with Type=dbus fully started or terminated, respectively.
In order to fix this, a rewrite of bus_list_names() is necessary. We used
to walk the current list of names on the bus, and blindly triggered the
bus_name_owner_change() callback on each service, providing the actual name
as current owner. This implementation has a number of problems:
* We cannot detect if the the name was moved from one owner to the other
while we were reloading
* We don't notify services which missed the name loss signal
* Providing the actual name as current owner is a hack, as the comment also
admits.
To fix this, this patch carries the following changes:
* Track the name of the current bus name owner, and (de-)serialize it
during reload. This way, we can detect changes.
* In bus_list_names(), walk the list of bus names we're interested in
first, and then see if the name is active on the bus. If it is,
check it it's still the same as it used to be, and synthesize
NameOwnerChanged signals for the name add and/or loss.
This should fully synchronize the current name list with the internal
state of all services.
2015-12-18 19:28:15 +03:00
char * bus_name_owner ; /* unique name of the current owner */
2010-04-16 01:16:16 +04:00
2010-06-16 07:10:31 +04:00
char * status_text ;
2014-07-07 16:20:36 +04:00
int status_errno ;
2010-06-16 07:10:31 +04:00
2014-08-22 18:36:38 +04:00
FailureAction failure_action ;
2012-02-09 16:05:23 +04:00
2012-01-07 02:08:54 +04:00
UnitRef accept_socket ;
2010-04-15 08:19:54 +04:00
2013-11-20 00:12:59 +04:00
sd_event_source * timer_event_source ;
2011-12-03 05:13:30 +04:00
PathSpec * pid_file_pathspec ;
2010-07-20 22:33:19 +04:00
NotifyAccess notify_access ;
2014-08-21 19:03:15 +04:00
NotifyState notify_state ;
2015-01-06 02:26:25 +03:00
ServiceFDStore * fd_store ;
unsigned n_fd_store ;
unsigned n_fd_store_max ;
2015-09-04 13:23:51 +03:00
char * usb_function_descriptors ;
char * usb_function_strings ;
2015-10-08 00:07:39 +03:00
int stdin_fd ;
int stdout_fd ;
int stderr_fd ;
2010-01-23 03:52:57 +03:00
} ;
2010-02-03 16:21:48 +03:00
extern const UnitVTable service_vtable ;
2010-01-23 03:52:57 +03:00
2014-07-24 12:40:28 +04:00
int service_set_socket_fd ( Service * s , int fd , struct Socket * socket , bool selinux_context_net ) ;
2016-04-28 18:09:50 +03:00
void service_close_socket_fd ( Service * s ) ;
2010-04-15 08:19:54 +04:00
2013-05-03 06:51:50 +04:00
const char * service_restart_to_string ( ServiceRestart i ) _const_ ;
ServiceRestart service_restart_from_string ( const char * s ) _pure_ ;
2010-01-30 03:55:42 +03:00
2013-05-03 06:51:50 +04:00
const char * service_type_to_string ( ServiceType i ) _const_ ;
ServiceType service_type_from_string ( const char * s ) _pure_ ;
2010-01-30 03:55:42 +03:00
2013-05-03 06:51:50 +04:00
const char * service_exec_command_to_string ( ServiceExecCommand i ) _const_ ;
ServiceExecCommand service_exec_command_from_string ( const char * s ) _pure_ ;
2010-01-30 03:55:42 +03:00
2013-05-03 06:51:50 +04:00
const char * notify_access_to_string ( NotifyAccess i ) _const_ ;
NotifyAccess notify_access_from_string ( const char * s ) _pure_ ;
2010-06-19 01:12:48 +04:00
2014-08-21 19:03:15 +04:00
const char * notify_state_to_string ( NotifyState i ) _const_ ;
NotifyState notify_state_from_string ( const char * s ) _pure_ ;
2013-05-03 06:51:50 +04:00
const char * service_result_to_string ( ServiceResult i ) _const_ ;
ServiceResult service_result_from_string ( const char * s ) _pure_ ;