chiark / gitweb /
manager: identify the init/system/user mode we are running it and pick D-Bus bus...
[elogind.git] / util.c
diff --git a/util.c b/util.c
index 6ed87a76262827cbb0e74cfbe370cb3736ae003e..517c99c45ef5cbe9fab4f50a1e53a97527391392 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,5 +1,24 @@
 /*-*- Mode: C; c-basic-offset: 8 -*-*/
 
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (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
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
 #include <assert.h>
 #include <string.h>
 #include <unistd.h>
 #include <syslog.h>
 #include <sched.h>
 #include <sys/resource.h>
+#include <linux/sched.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "macro.h"
 #include "util.h"
 #include "ioprio.h"
 #include "missing.h"
+#include "log.h"
 
-usec_t now(clockid_t clock) {
+usec_t now(clockid_t clock_id) {
         struct timespec ts;
 
-        assert_se(clock_gettime(clock, &ts) == 0);
+        assert_se(clock_gettime(clock_id, &ts) == 0);
 
         return timespec_load(&ts);
 }
@@ -264,6 +287,22 @@ char *split_spaces(const char *c, size_t *l, char **state) {
         return (char*) current;
 }
 
+/* Split a path into filenames. */
+char *split_slash(const char *c, size_t *l, char **state) {
+        char *current;
+
+        current = *state ? *state : (char*) c;
+
+        if (!*current || *c == 0)
+                return NULL;
+
+        current += strspn(current, "/");
+        *l = strcspn(current, "/");
+        *state = current+*l;
+
+        return (char*) current;
+}
+
 /* Split a string into words, but consider strings enclosed in '' and
  * "" as words even if they include spaces. */
 char *split_quoted(const char *c, size_t *l, char **state) {
@@ -554,6 +593,39 @@ char *file_in_same_dir(const char *path, const char *filename) {
         return r;
 }
 
+int mkdir_parents(const char *path, mode_t mode) {
+        const char *p, *e;
+
+        assert(path);
+
+        /* Creates every parent directory in the path except the last
+         * component. */
+
+        p = path + strspn(path, "/");
+        for (;;) {
+                int r;
+                char *t;
+
+                e = p + strcspn(p, "/");
+                p = e + strspn(e, "/");
+
+                /* Is this the last component? If so, then we're
+                 * done */
+                if (*p == 0)
+                        return 0;
+
+                if (!(t = strndup(path, e - path)))
+                        return -ENOMEM;
+
+                r = mkdir(t, mode);
+
+                free(t);
+
+                if (r < 0 && errno != EEXIST)
+                        return -errno;
+        }
+}
+
 char hexchar(int x) {
         static const char table[16] = "0123456789abcdef";
 
@@ -804,11 +876,11 @@ char *xescape(const char *s, const char *bad) {
 }
 
 char *bus_path_escape(const char *s) {
-        assert(s);
-
         char *r, *t;
         const char *f;
 
+        assert(s);
+
         /* Escapes all chars that D-Bus' object path cannot deal
          * with. Can be reverse with bus_path_unescape() */
 
@@ -833,11 +905,11 @@ char *bus_path_escape(const char *s) {
 }
 
 char *bus_path_unescape(const char *s) {
-        assert(s);
-
         char *r, *t;
         const char *f;
 
+        assert(s);
+
         if (!(r = new(char, strlen(s)+1)))
                 return NULL;