]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/utils.c
utils: make lxc_setgroups() return bool
[mirror_lxc.git] / src / lxc / utils.c
index 58dd223a976ab793a1c5c7ca4afcb565d5a99701..9c30dc2eac41c0d8c8b5bf4e4cf59f311309284c 100644 (file)
@@ -544,7 +544,34 @@ uid_t get_ns_uid(uid_t orig)
                }
        }
 
-       nsid = 0;
+       nsid = LXC_INVALID_UID;
+
+found:
+       fclose(f);
+       free(line);
+       return nsid;
+}
+
+gid_t get_ns_gid(gid_t orig)
+{
+       char *line = NULL;
+       size_t sz = 0;
+       gid_t nsid, hostid, range;
+       FILE *f = fopen("/proc/self/gid_map", "r");
+       if (!f)
+               return 0;
+
+       while (getline(&line, &sz, f) != -1) {
+               if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
+                       continue;
+
+               if (hostid <= orig && hostid + range > orig) {
+                       nsid += orig - hostid;
+                       goto found;
+               }
+       }
+
+       nsid = LXC_INVALID_GID;
 
 found:
        fclose(f);
@@ -898,10 +925,10 @@ static char *get_nextpath(char *path, int *offsetp, int fulllen)
        if (offset >= fulllen)
                return NULL;
 
-       while (path[offset] != '\0' && offset < fulllen)
+       while (offset < fulllen && path[offset] != '\0')
                offset++;
 
-       while (path[offset] == '\0' && offset < fulllen)
+       while (offset < fulllen && path[offset] == '\0')
                offset++;
 
        *offsetp = offset;
@@ -1157,7 +1184,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
 {
        char path[MAXPATHLEN];
        int link_to_pid, linklen, mypid, ret;
-       char link[LXC_NUMSTRLEN64] = {0};
+       char link[INTTYPE_TO_STRLEN(pid_t)] = {0};
 
        ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs);
        if (ret < 0 || ret >= MAXPATHLEN) {
@@ -1165,7 +1192,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
                return -1;
        }
 
-       linklen = readlink(path, link, LXC_NUMSTRLEN64);
+       linklen = readlink(path, link, sizeof(link));
 
        ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
        if (ret < 0 || ret >= MAXPATHLEN) {
@@ -1179,7 +1206,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
                        return -1;
 
                goto domount;
-       } else if (linklen >= LXC_NUMSTRLEN64) {
+       } else if (linklen >= sizeof(link)) {
                link[linklen - 1] = '\0';
                ERROR("readlink returned truncated content: \"%s\"", link);
                return -1;
@@ -1260,7 +1287,7 @@ int null_stdfds(void)
 
 /* Check whether a signal is blocked by a process. */
 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
-#define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1)
+#define __PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
 bool task_blocks_signal(pid_t pid, int signal)
 {
        int ret;
@@ -1326,31 +1353,39 @@ int lxc_preserve_ns(const int pid, const char *ns)
 
 int lxc_switch_uid_gid(uid_t uid, gid_t gid)
 {
-       if (setgid(gid) < 0) {
-               SYSERROR("Failed to switch to gid %d.", gid);
-               return -errno;
+       int ret = 0;
+
+       if (gid != LXC_INVALID_GID) {
+               ret = setgid(gid);
+               if (ret < 0) {
+                       SYSERROR("Failed to switch to gid %d", gid);
+                       return -1;
+               }
+               NOTICE("Switched to gid %d", gid);
        }
-       NOTICE("Switched to gid %d.", gid);
 
-       if (setuid(uid) < 0) {
-               SYSERROR("Failed to switch to uid %d.", uid);
-               return -errno;
+       if (uid != LXC_INVALID_UID) {
+               ret = setuid(uid);
+               if (ret < 0) {
+                       SYSERROR("Failed to switch to uid %d", uid);
+                       return -1;
+               }
+               NOTICE("Switched to uid %d", uid);
        }
-       NOTICE("Switched to uid %d.", uid);
 
-       return 0;
+       return ret;
 }
 
 /* Simple covenience function which enables uniform logging. */
-int lxc_setgroups(int size, gid_t list[])
+bool lxc_setgroups(int size, gid_t list[])
 {
        if (setgroups(size, list) < 0) {
-               SYSERROR("Failed to setgroups().");
-               return -errno;
+               SYSERROR("Failed to setgroups()");
+               return false;
        }
-       NOTICE("Dropped additional groups.");
+       NOTICE("Dropped additional groups");
 
-       return 0;
+       return true;
 }
 
 static int lxc_get_unused_loop_dev_legacy(char *loop_name)