chiark / gitweb /
catalog: open up catalog internals
[elogind.git] / src / shared / util.c
index 03d6f00622862aee394edaa45b79592116062251..2241b79859ff16233f4a33e7f66aea13efb11464 100644 (file)
@@ -59,6 +59,7 @@
 #include <limits.h>
 #include <langinfo.h>
 #include <locale.h>
+#include <libgen.h>
 
 #include "macro.h"
 #include "util.h"
@@ -2356,6 +2357,24 @@ int dir_is_empty(const char *path) {
         }
 }
 
+char* dirname_malloc(const char *path) {
+        char *d, *dir, *dir2;
+
+        d = strdup(path);
+        if (!d)
+                return NULL;
+        dir = dirname(d);
+        assert(dir);
+
+        if (dir != d) {
+                dir2 = strdup(dir);
+                free(d);
+                return dir2;
+        }
+
+        return dir;
+}
+
 unsigned long long random_ull(void) {
         _cleanup_close_ int fd;
         uint64_t ull;
@@ -5246,53 +5265,6 @@ int get_home_dir(char **_h) {
         return 0;
 }
 
-int get_shell(char **_sh) {
-        char *sh;
-        const char *e;
-        uid_t u;
-        struct passwd *p;
-
-        assert(_sh);
-
-        /* Take the user specified one */
-        e = getenv("SHELL");
-        if (e) {
-                sh = strdup(e);
-                if (!sh)
-                        return -ENOMEM;
-
-                *_sh = sh;
-                return 0;
-        }
-
-        /* Hardcode home directory for root to avoid NSS */
-        u = getuid();
-        if (u == 0) {
-                sh = strdup("/bin/sh");
-                if (!sh)
-                        return -ENOMEM;
-
-                *_sh = sh;
-                return 0;
-        }
-
-        /* Check the database... */
-        errno = 0;
-        p = getpwuid(u);
-        if (!p)
-                return errno ? -errno : -ESRCH;
-
-        if (!path_is_absolute(p->pw_shell))
-                return -EINVAL;
-
-        sh = strdup(p->pw_shell);
-        if (!sh)
-                return -ENOMEM;
-
-        *_sh = sh;
-        return 0;
-}
-
 void fclosep(FILE **f) {
         if (*f)
                 fclose(*f);
@@ -5755,7 +5727,7 @@ int create_tmp_dir(char template[], char** dir_name) {
         dt = strjoin(d, "/tmp", NULL);
         if (!dt) {
                 r = log_oom();
-                goto fail2;
+                goto fail3;
         }
 
         umask(0000);
@@ -5763,7 +5735,7 @@ int create_tmp_dir(char template[], char** dir_name) {
         if (r) {
                 log_error("Can't create directory %s: %m", dt);
                 r = -errno;
-                goto fail1;
+                goto fail2;
         }
         log_debug("Created temporary directory %s", dt);
 
@@ -5781,6 +5753,8 @@ int create_tmp_dir(char template[], char** dir_name) {
 fail1:
         rmdir(dt);
 fail2:
+        free(dt);
+fail3:
         rmdir(template);
         return r;
 }