chiark / gitweb /
shared: free dt (temporary dir name) on fail
[elogind.git] / src / shared / util.c
index bc6e035c60d756ee7a06095ffe59b259114c4bcb..8fa01fa8a49609baf640ef850da891ac19dfcb85 100644 (file)
@@ -219,6 +219,8 @@ void close_nointr_nofail(int fd) {
 void close_many(const int fds[], unsigned n_fd) {
         unsigned i;
 
+        assert(fds || n_fd <= 0);
+
         for (i = 0; i < n_fd; i++)
                 close_nointr_nofail(fds[i]);
 }
@@ -290,7 +292,7 @@ int safe_atou(const char *s, unsigned *ret_u) {
         l = strtoul(s, &x, 0);
 
         if (!x || x == s || *x || errno)
-                return errno ? -errno : -EINVAL;
+                return errno > 0 ? -errno : -EINVAL;
 
         if ((unsigned long) (unsigned) l != l)
                 return -ERANGE;
@@ -310,7 +312,7 @@ int safe_atoi(const char *s, int *ret_i) {
         l = strtol(s, &x, 0);
 
         if (!x || x == s || *x || errno)
-                return errno ? -errno : -EINVAL;
+                return errno > 0 ? -errno : -EINVAL;
 
         if ((long) (int) l != l)
                 return -ERANGE;
@@ -5244,53 +5246,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);
@@ -5753,7 +5708,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);
@@ -5761,7 +5716,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);
 
@@ -5779,6 +5734,8 @@ int create_tmp_dir(char template[], char** dir_name) {
 fail1:
         rmdir(dt);
 fail2:
+        free(dt);
+fail3:
         rmdir(template);
         return r;
 }