chiark / gitweb /
systemctl: require correctly formed unit names when enabling units
[elogind.git] / src / unit.h
index fd0defe2d1666f49165a3c65d6569ed42764cb11..afae580409d90a0bb753b2ad29349cf607a7206c 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 #ifndef foounithfoo
 #define foounithfoo
@@ -39,19 +39,9 @@ typedef enum UnitDependency UnitDependency;
 #include "socket-util.h"
 #include "execute.h"
 
-#define UNIT_NAME_MAX 128
-#define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
+#define DEFAULT_TIMEOUT_USEC (60*USEC_PER_SEC)
 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
 
-typedef enum KillMode {
-        KILL_CONTROL_GROUP = 0,
-        KILL_PROCESS_GROUP,
-        KILL_PROCESS,
-        KILL_NONE,
-        _KILL_MODE_MAX,
-        _KILL_MODE_INVALID = -1
-} KillMode;
-
 enum UnitType {
         UNIT_SERVICE = 0,
         UNIT_SOCKET,
@@ -62,6 +52,7 @@ enum UnitType {
         UNIT_SNAPSHOT,
         UNIT_TIMER,
         UNIT_SWAP,
+        UNIT_PATH,
         _UNIT_TYPE_MAX,
         _UNIT_TYPE_INVALID = -1
 };
@@ -69,7 +60,7 @@ enum UnitType {
 enum UnitLoadState {
         UNIT_STUB,
         UNIT_LOADED,
-        UNIT_FAILED,
+        UNIT_ERROR,
         UNIT_MERGED,
         _UNIT_LOAD_STATE_MAX,
         _UNIT_LOAD_STATE_INVALID = -1
@@ -77,8 +68,9 @@ enum UnitLoadState {
 
 enum UnitActiveState {
         UNIT_ACTIVE,
-        UNIT_ACTIVE_RELOADING,
+        UNIT_RELOADING,
         UNIT_INACTIVE,
+        UNIT_FAILED,
         UNIT_ACTIVATING,
         UNIT_DEACTIVATING,
         _UNIT_ACTIVE_STATE_MAX,
@@ -86,15 +78,19 @@ enum UnitActiveState {
 };
 
 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
-        return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
+        return t == UNIT_ACTIVE || t == UNIT_RELOADING;
 }
 
 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
-        return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
+        return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_RELOADING;
 }
 
 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
-        return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
+        return t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_DEACTIVATING;
+}
+
+static inline bool UNIT_IS_INACTIVE_OR_FAILED(UnitActiveState t) {
+        return t == UNIT_INACTIVE || t == UNIT_FAILED;
 }
 
 enum UnitDependency {
@@ -111,12 +107,16 @@ enum UnitDependency {
         UNIT_WANTED_BY,               /* inverse of 'wants' */
 
         /* Negative dependencies */
-        UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicts' */
+        UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicted_by' */
+        UNIT_CONFLICTED_BY,
 
         /* Order */
         UNIT_BEFORE,                  /* inverse of 'before' is 'after' and vice versa */
         UNIT_AFTER,
 
+        /* On Failure */
+        UNIT_ON_FAILURE,
+
         /* Reference information for GC logic */
         UNIT_REFERENCES,              /* Inverse of 'references' is 'referenced_by' */
         UNIT_REFERENCED_BY,
@@ -136,9 +136,6 @@ struct Meta {
         UnitLoadState load_state;
         Unit *merged_into;
 
-        /* Refuse manual starting, allow starting only indirectly via dependency. */
-        bool only_by_dependency;
-
         char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
         char *instance;
 
@@ -146,16 +143,20 @@ struct Meta {
         Set *dependencies[_UNIT_DEPENDENCY_MAX];
 
         char *description;
+
         char *fragment_path; /* if loaded from a config file this is the primary path to it */
+        usec_t fragment_mtime;
 
         /* If there is something to do with this unit, then this is
          * the job for it */
         Job *job;
 
-        timestamp inactive_exit_timestamp;
-        timestamp active_enter_timestamp;
-        timestamp active_exit_timestamp;
-        timestamp inactive_enter_timestamp;
+        usec_t job_timeout;
+
+        dual_timestamp inactive_exit_timestamp;
+        dual_timestamp active_enter_timestamp;
+        dual_timestamp active_exit_timestamp;
+        dual_timestamp inactive_enter_timestamp;
 
         /* Counterparts in the cgroup filesystem */
         CGroupBonding *cgroup_bondings;
@@ -178,18 +179,44 @@ struct Meta {
         /* Used during GC sweeps */
         unsigned gc_marker;
 
+        /* When deserializing, temporarily store the job type for this
+         * unit here, if there was a job scheduled */
+        int deserialized_job; /* This is actually of type JobType */
+
+        /* Error code when we didn't manage to load the unit (negative) */
+        int load_error;
+
         /* If we go down, pull down everything that depends on us, too */
         bool recursive_stop;
 
         /* Garbage collect us we nobody wants or requires us anymore */
         bool stop_when_unneeded;
 
+        /* Create default depedencies */
+        bool default_dependencies;
+
+        /* Bring up this unit even if a dependency fails to start */
+        bool ignore_dependency_failure;
+
+        /* Refuse manual starting, allow starting only indirectly via dependency. */
+        bool refuse_manual_start;
+
+        /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
+        bool refuse_manual_stop;
+
+        /* Allow isolation requests */
+        bool allow_isolate;
+
         bool in_load_queue:1;
         bool in_dbus_queue:1;
         bool in_cleanup_queue:1;
         bool in_gc_queue:1;
 
         bool sent_dbus_new_signal:1;
+
+        bool no_gc:1;
+
+        bool in_audit:1;
 };
 
 #include "service.h"
@@ -201,6 +228,7 @@ struct Meta {
 #include "automount.h"
 #include "snapshot.h"
 #include "swap.h"
+#include "path.h"
 
 union Unit {
         Meta meta;
@@ -213,6 +241,7 @@ union Unit {
         Automount automount;
         Snapshot snapshot;
         Swap swap;
+        Path path;
 };
 
 struct UnitVTable {
@@ -274,10 +303,16 @@ struct UnitVTable {
         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
         void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
 
+        /* Reset failed state if we are in failed state */
+        void (*reset_failed)(Unit *u);
+
         /* Called whenever any of the cgroups this unit watches for
          * ran empty */
         void (*cgroup_notify_empty)(Unit *u);
 
+        /* Called whenever a process of this unit sends us a message */
+        void (*notify_message)(Unit *u, pid_t pid, char **tags);
+
         /* Called whenever a name thus Unit registered for comes or
          * goes away. */
         void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
@@ -286,7 +321,10 @@ struct UnitVTable {
         void (*bus_query_pid_done)(Unit *u, const char *name, pid_t pid);
 
         /* Called for each message received on the bus */
-        DBusHandlerResult (*bus_message_handler)(Unit *u, DBusMessage *message);
+        DBusHandlerResult (*bus_message_handler)(Unit *u, DBusConnection *c, DBusMessage *message);
+
+        /* Return the unit this unit is following */
+        Unit *(*following)(Unit *u);
 
         /* This is called for each unit type and should be used to
          * enumerate existing devices and load them. However,
@@ -298,6 +336,14 @@ struct UnitVTable {
         /* Type specific cleanups. */
         void (*shutdown)(Manager *m);
 
+        /* When sending out PropertiesChanged signal, which properties
+         * shall be invalidated? This is a NUL separated list of
+         * strings, to minimize relocations a little. */
+        const char *bus_invalidating_properties;
+
+        /* The interface name */
+        const char *bus_interface;
+
         /* Can units of this type have multiple names? */
         bool no_alias:1;
 
@@ -315,8 +361,11 @@ struct UnitVTable {
         /* Exclude from automatic gc */
         bool no_gc:1;
 
-        /* Exclude from isolation requests */
+        /* Exclude from stopping on isolation requests */
         bool no_isolate:1;
+
+        /* Show status updates on the console */
+        bool show_status:1;
 };
 
 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
@@ -326,14 +375,14 @@ extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
 /* For casting a unit into the various unit types */
 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
         static inline MixedCase* UPPERCASE(Unit *u) {                   \
-                if (!u || u->meta.type != UNIT_##UPPERCASE)             \
+                if (_unlikely_(!u || u->meta.type != UNIT_##UPPERCASE)) \
                         return NULL;                                    \
                                                                         \
                 return (MixedCase*) u;                                  \
         }
 
 /* For casting the various unit types into a unit */
-#define UNIT(u) ((Unit*) (u))
+#define UNIT(u) ((Unit*) (&(u)->meta))
 
 DEFINE_CAST(SOCKET, Socket);
 DEFINE_CAST(TIMER, Timer);
@@ -344,6 +393,7 @@ DEFINE_CAST(MOUNT, Mount);
 DEFINE_CAST(AUTOMOUNT, Automount);
 DEFINE_CAST(SNAPSHOT, Snapshot);
 DEFINE_CAST(SWAP, Swap);
+DEFINE_CAST(PATH, Path);
 
 Unit *unit_new(Manager *m);
 void unit_free(Unit *u);
@@ -351,8 +401,13 @@ void unit_free(Unit *u);
 int unit_add_name(Unit *u, const char *name);
 
 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
+int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference);
+
 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
+int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
+
 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
+int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
 
 int unit_add_exec_dependencies(Unit *u, ExecContext *c);
 
@@ -378,7 +433,6 @@ Unit *unit_follow_merge(Unit *u);
 
 int unit_load_fragment_and_dropin(Unit *u);
 int unit_load_fragment_and_dropin_optional(Unit *u);
-int unit_load_nop(Unit *u);
 int unit_load(Unit *unit);
 
 const char *unit_description(Unit *u);
@@ -393,6 +447,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix);
 
 bool unit_can_reload(Unit *u);
 bool unit_can_start(Unit *u);
+bool unit_can_isolate(Unit *u);
 
 int unit_start(Unit *u);
 int unit_stop(Unit *u);
@@ -427,14 +482,29 @@ char **unit_full_printf_strv(Unit *u, char **l);
 
 bool unit_can_serialize(Unit *u);
 int unit_serialize(Unit *u, FILE *f, FDSet *fds);
-void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr(4,5);
+void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr_(4,5);
 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
 int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
 
 int unit_add_node_link(Unit *u, const char *what, bool wants);
 
-const char *unit_type_to_string(UnitType i);
-UnitType unit_type_from_string(const char *s);
+int unit_coldplug(Unit *u);
+
+void unit_status_printf(Unit *u, const char *format, ...);
+
+bool unit_need_daemon_reload(Unit *u);
+
+void unit_reset_failed(Unit *u);
+
+Unit *unit_following(Unit *u);
+
+bool unit_pending_inactive(Unit *u);
+bool unit_pending_active(Unit *u);
+
+int unit_add_default_target_dependency(Unit *u, Unit *target);
+
+UnitType unit_name_to_type(const char *n);
+bool unit_name_is_valid(const char *n);
 
 const char *unit_load_state_to_string(UnitLoadState i);
 UnitLoadState unit_load_state_from_string(const char *s);