chiark / gitweb /
Introduce _cleanup_fdset_free_
[elogind.git] / src / core / manager.c
index ad1a8d61797b708d01ec40fd4633a0865d853b57..b45a2e11a5a1cde016bd0b529e9468fc33346a10 100644 (file)
@@ -55,6 +55,7 @@
 #include "util.h"
 #include "mkdir.h"
 #include "ratelimit.h"
+#include "locale-setup.h"
 #include "mount-setup.h"
 #include "unit-name.h"
 #include "dbus-unit.h"
@@ -69,7 +70,7 @@
 #include "cgroup-util.h"
 #include "path-util.h"
 #include "audit-fd.h"
-#include "efivars.h"
+#include "boot-timestamps.h"
 #include "env-util.h"
 
 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
@@ -235,7 +236,7 @@ static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned po
                 *p++ = '*';
                 if (pos < width-1)
                         p = mempset(p, ' ', width-1-pos);
-                p = stpcpy(p, ANSI_HIGHLIGHT_OFF);
+                strcpy(p, ANSI_HIGHLIGHT_OFF);
         }
 }
 
@@ -256,6 +257,7 @@ static void manager_print_jobs_in_progress(Manager *m) {
         /* m->n_running_jobs must be consistent with the contents of m->jobs,
          * so the above loop must have succeeded in finding j. */
         assert(counter == print_nr + 1);
+        assert(j);
 
         cylon_pos = m->jobs_in_progress_iteration % 14;
         if (cylon_pos >= 8)
@@ -301,8 +303,6 @@ static int manager_watch_idle_pipe(Manager *m) {
         }
 
         log_debug("Set up idle_pipe watch.");
-        log_debug("m->epoll_fd=%d m->idle_pipe_watch.fd=%d",
-                  m->epoll_fd, m->idle_pipe_watch.fd);
 
         return 0;
 
@@ -317,8 +317,6 @@ static void manager_unwatch_idle_pipe(Manager *m) {
         if (m->idle_pipe_watch.type != WATCH_IDLE_PIPE)
                 return;
 
-        log_debug("m->epoll_fd=%d m->idle_pipe_watch.fd=%d",
-                  m->epoll_fd, m->idle_pipe_watch.fd);
         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, m->idle_pipe_watch.fd, NULL) >= 0);
         watch_init(&m->idle_pipe_watch);
 
@@ -458,22 +456,31 @@ static int manager_setup_signals(Manager *m) {
         return 0;
 }
 
-static void manager_strip_environment(Manager *m) {
+static int manager_default_environment(Manager *m) {
         assert(m);
 
-        /* Remove variables from the inherited set that are part of
-         * the container interface:
-         * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */
-        strv_remove_prefix(m->environment, "container=");
-        strv_remove_prefix(m->environment, "container_");
+        if (m->running_as == SYSTEMD_SYSTEM) {
+                /* The system manager always starts with a clean
+                 * environment for its children. It does not import
+                 * the kernel or the parents exported variables.
+                 *
+                 * The initial passed environ is untouched to keep
+                 * /proc/self/environ valid; it is used for tagging
+                 * the init process inside containers. */
+                m->environment = strv_new("PATH=" DEFAULT_PATH,
+                                          NULL);
+
+                /* Import locale variables LC_*= from configuration */
+                locale_setup(&m->environment);
+        } else
+                /* The user manager passes its own environment
+                 * along to its children. */
+                m->environment = strv_copy(environ);
 
-        /* Remove variables from the inherited set that are part of
-         * the initrd interface:
-         * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */
-        strv_remove_prefix(m->environment, "RD_");
+        if (!m->environment)
+                return -ENOMEM;
 
-        /* Drop invalid entries */
-        strv_env_clean(m->environment);
+        return 0;
 }
 
 int manager_new(SystemdRunningAs running_as, bool reexecuting, Manager **_m) {
@@ -490,7 +497,7 @@ int manager_new(SystemdRunningAs running_as, bool reexecuting, Manager **_m) {
 
 #ifdef ENABLE_EFI
         if (detect_container(NULL) <= 0)
-                efi_get_boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
+                boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
 #endif
 
         m->running_as = running_as;
@@ -509,12 +516,10 @@ int manager_new(SystemdRunningAs running_as, bool reexecuting, Manager **_m) {
         m->epoll_fd = m->dev_autofs_fd = -1;
         m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
 
-        m->environment = strv_copy(environ);
-        if (!m->environment)
+        r = manager_default_environment(m);
+        if (r < 0)
                 goto fail;
 
-        manager_strip_environment(m);
-
         if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
                 goto fail;
 
@@ -766,6 +771,9 @@ void manager_free(Manager *m) {
         for (i = 0; i < RLIMIT_NLIMITS; i++)
                 free(m->rlimit[i]);
 
+        assert(hashmap_isempty(m->units_requiring_mounts_for));
+        hashmap_free(m->units_requiring_mounts_for);
+
         free(m);
 }
 
@@ -778,9 +786,11 @@ int manager_enumerate(Manager *m) {
         /* Let's ask every type to load all units from disk/kernel
          * that it might know */
         for (c = 0; c < _UNIT_TYPE_MAX; c++)
-                if (unit_vtable[c]->enumerate)
-                        if ((q = unit_vtable[c]->enumerate(m)) < 0)
+                if (unit_vtable[c]->enumerate) {
+                        q = unit_vtable[c]->enumerate(m);
+                        if (q < 0)
                                 r = q;
+                }
 
         manager_dispatch_load_queue(m);
         return r;
@@ -2308,8 +2318,8 @@ int manager_distribute_fds(Manager *m, FDSet *fds) {
 
 int manager_reload(Manager *m) {
         int r, q;
-        FILE *f;
-        FDSet *fds;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_fdset_free_ FDSet *fds = NULL;
 
         assert(m);
 
@@ -2323,20 +2333,18 @@ int manager_reload(Manager *m) {
         fds = fdset_new();
         if (!fds) {
                 m->n_reloading --;
-                r = -ENOMEM;
-                goto finish;
+                return -ENOMEM;
         }
 
         r = manager_serialize(m, f, fds, false);
         if (r < 0) {
                 m->n_reloading --;
-                goto finish;
+                return r;
         }
 
         if (fseeko(f, 0, SEEK_SET) < 0) {
                 m->n_reloading --;
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         /* From here on there is no way back. */
@@ -2380,13 +2388,6 @@ int manager_reload(Manager *m) {
 
         m->send_reloading_done = true;
 
-finish:
-        if (f)
-                fclose(f);
-
-        if (fds)
-                fdset_free(fds);
-
         return r;
 }
 
@@ -2655,15 +2656,17 @@ void manager_undo_generators(Manager *m) {
         remove_generator_dir(m, &m->generator_unit_path_late);
 }
 
-int manager_set_default_environment(Manager *m, char **environment) {
-
+int manager_environment_add(Manager *m, char **environment) {
         char **e = NULL;
         assert(m);
+
         e = strv_env_merge(2, m->environment, environment);
         if (!e)
                 return -ENOMEM;
+
         strv_free(m->environment);
         m->environment = e;
+
         return 0;
 }
 
@@ -2760,6 +2763,41 @@ void manager_status_printf(Manager *m, bool ephemeral, const char *status, const
         va_end(ap);
 }
 
+int manager_get_unit_by_path(Manager *m, const char *path, const char *suffix, Unit **_found) {
+        _cleanup_free_ char *p = NULL;
+        Unit *found;
+
+        assert(m);
+        assert(path);
+        assert(suffix);
+        assert(_found);
+
+        p = unit_name_from_path(path, suffix);
+        if (!p)
+                return -ENOMEM;
+
+        found = manager_get_unit(m, p);
+        if (!found) {
+                *_found = NULL;
+                return 0;
+        }
+
+        *_found = found;
+        return 1;
+}
+
+Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) {
+        char p[strlen(path)+1];
+
+        assert(m);
+        assert(path);
+
+        strcpy(p, path);
+        path_kill_slashes(p);
+
+        return hashmap_get(m->units_requiring_mounts_for, streq(p, "/") ? "" : p);
+}
+
 void watch_init(Watch *w) {
         assert(w);