]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/utils.c
tree-wide: make files cloexec whenever possible
[mirror_lxc.git] / src / lxc / utils.c
index 8397d54aef97077638df9492ac0d0ff4b2ace4f2..a3495635af4bdd3b79cad81acf8ed68301c0bfc1 100644 (file)
@@ -514,19 +514,17 @@ int lxc_pclose(struct lxc_popen_FILE *fp)
 
 int randseed(bool srand_it)
 {
-       FILE *f;
+       __do_fclose FILE *f = NULL;
        /*
         * srand pre-seed function based on /dev/urandom
         */
        unsigned int seed = time(NULL) + getpid();
 
-       f = fopen("/dev/urandom", "r");
+       f = fopen("/dev/urandom", "re");
        if (f) {
                int ret = fread(&seed, sizeof(seed), 1, f);
                if (ret != 1)
                        SYSDEBUG("Unable to fread /dev/urandom, fallback to time+pid rand seed");
-
-               fclose(f);
        }
 
        if (srand_it)
@@ -537,12 +535,12 @@ int randseed(bool srand_it)
 
 uid_t get_ns_uid(uid_t orig)
 {
-       char *line = NULL;
+       __do_free char *line = NULL;
+       __do_fclose FILE *f = NULL;
        size_t sz = 0;
        uid_t nsid, hostid, range;
-       FILE *f;
 
-       f = fopen("/proc/self/uid_map", "r");
+       f = fopen("/proc/self/uid_map", "re");
        if (!f) {
                SYSERROR("Failed to open uid_map");
                return 0;
@@ -552,28 +550,21 @@ uid_t get_ns_uid(uid_t orig)
                if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
                        continue;
 
-               if (hostid <= orig && hostid + range > orig) {
-                       nsid += orig - hostid;
-                       goto found;
-               }
+               if (hostid <= orig && hostid + range > orig)
+                       return nsid += orig - hostid;
        }
 
-       nsid = LXC_INVALID_UID;
-
-found:
-       fclose(f);
-       free(line);
-       return nsid;
+       return LXC_INVALID_UID;
 }
 
 gid_t get_ns_gid(gid_t orig)
 {
-       char *line = NULL;
+       __do_free char *line = NULL;
+       __do_fclose FILE *f = NULL;
        size_t sz = 0;
        gid_t nsid, hostid, range;
-       FILE *f;
 
-       f = fopen("/proc/self/gid_map", "r");
+       f = fopen("/proc/self/gid_map", "re");
        if (!f) {
                SYSERROR("Failed to open gid_map");
                return 0;
@@ -583,18 +574,11 @@ gid_t get_ns_gid(gid_t orig)
                if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
                        continue;
 
-               if (hostid <= orig && hostid + range > orig) {
-                       nsid += orig - hostid;
-                       goto found;
-               }
+               if (hostid <= orig && hostid + range > orig)
+                       return nsid += orig - hostid;
        }
 
-       nsid = LXC_INVALID_GID;
-
-found:
-       fclose(f);
-       free(line);
-       return nsid;
+       return LXC_INVALID_GID;
 }
 
 bool dir_exists(const char *path)
@@ -640,7 +624,7 @@ bool is_shared_mountpoint(const char *path)
        int i;
        size_t len = 0;
 
-       f = fopen("/proc/self/mountinfo", "r");
+       f = fopen("/proc/self/mountinfo", "re");
        if (!f)
                return 0;
 
@@ -722,19 +706,19 @@ bool switch_to_ns(pid_t pid, const char *ns)
  */
 bool detect_ramfs_rootfs(void)
 {
-       FILE *f;
-       char *p, *p2;
-       char *line = NULL;
+       __do_free char *line = NULL;
+       __do_free void *fopen_cache = NULL;
+       __do_fclose FILE *f = NULL;
        size_t len = 0;
-       int i;
 
-       f = fopen("/proc/self/mountinfo", "r");
-       if (!f) {
-               SYSERROR("Failed to open mountinfo");
+       f = fopen_cached("/proc/self/mountinfo", "re", &fopen_cache);
+       if (!f)
                return false;
-       }
 
        while (getline(&line, &len, f) != -1) {
+               int i;
+               char *p, *p2;
+
                for (p = line, i = 0; p && i < 4; i++)
                        p = strchr(p + 1, ' ');
                if (!p)
@@ -743,22 +727,15 @@ bool detect_ramfs_rootfs(void)
                p2 = strchr(p + 1, ' ');
                if (!p2)
                        continue;
-
                *p2 = '\0';
                if (strcmp(p + 1, "/") == 0) {
                        /* This is '/'. Is it the ramfs? */
                        p = strchr(p2 + 1, '-');
-                       if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) {
-                               free(line);
-                               fclose(f);
-                               INFO("Rootfs is located on ramfs");
+                       if (p && strncmp(p, "- rootfs rootfs ", 16) == 0)
                                return true;
-                       }
                }
        }
 
-       free(line);
-       fclose(f);
        return false;
 }
 
@@ -1317,21 +1294,21 @@ int null_stdfds(void)
 #define __PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
 bool task_blocks_signal(pid_t pid, int signal)
 {
+       __do_free char *line = NULL;
+       __do_fclose FILE *f = NULL;
        int ret;
        char status[__PROC_STATUS_LEN] = {0};
-       FILE *f;
        uint64_t sigblk = 0, one = 1;
        size_t n = 0;
        bool bret = false;
-       char *line = NULL;
 
        ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid);
        if (ret < 0 || ret >= __PROC_STATUS_LEN)
                return bret;
 
-       f = fopen(status, "r");
+       f = fopen(status, "re");
        if (!f)
-               return bret;
+               return false;
 
        while (getline(&line, &n, f) != -1) {
                char *numstr;
@@ -1342,7 +1319,7 @@ bool task_blocks_signal(pid_t pid, int signal)
                numstr = lxc_trim_whitespace_in_place(line + 7);
                ret = lxc_safe_uint64(numstr, &sigblk, 16);
                if (ret < 0)
-                       goto out;
+                       return false;
 
                break;
        }
@@ -1350,9 +1327,6 @@ bool task_blocks_signal(pid_t pid, int signal)
        if (sigblk & (one << (signal - 1)))
                bret = true;
 
-out:
-       free(line);
-       fclose(f);
        return bret;
 }
 
@@ -1723,10 +1697,11 @@ static int process_dead(/* takes */ int status_fd)
        if (fd_cloexec(dupfd, true) < 0)
                return -1;
 
-       /* transfer ownership of fd */
        f = fdopen(dupfd, "re");
        if (!f)
                return -1;
+
+       /* Transfer ownership of fd. */
        move_fd(dupfd);
 
        ret = 0;