]> git.sven.stormbind.net Git - sven/jattach.git/blobdiff - src/posix/psutil.c
releasing package jattach version 2.2-1
[sven/jattach.git] / src / posix / psutil.c
index 847a0609a332f1eff46eebe5515ee76c873b8347..d0287f598e2955c4c418be2a1e3cc24b735d5634 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -93,15 +93,14 @@ static int alt_lookup_nspid(int pid) {
                 // Check if /proc/<container-pid>/sched points back to <host-pid>
                 snprintf(path, sizeof(path), "/proc/%d/root/proc/%s/sched", pid, entry->d_name);
                 if (sched_get_host_pid(path) == pid) {
-                    closedir(dir);
-                    return atoi(entry->d_name);
+                    pid = atoi(entry->d_name);
+                    break;
                 }
             }
         }
         closedir(dir);
     }
 
-    // Could not find container pid; return host pid as the last resort
     return pid;
 }
 
@@ -129,15 +128,18 @@ int get_process_info(int pid, uid_t* uid, gid_t* gid, int* nspid) {
     int nspid_found = 0;
 
     while (getline(&line, &size, status_file) != -1) {
-        if (strncmp(line, "Uid:", 4) == 0) {
+        if (strncmp(line, "Uid:", 4) == 0 && strtok(line + 4, "\t ") != NULL) {
             // Get the effective UID, which is the second value in the line
-            *uid = (uid_t)atoi(strchr(line + 5, '\t'));
-        } else if (strncmp(line, "Gid:", 4) == 0) {
+            *uid = (uid_t)atoi(strtok(NULL, "\t "));
+        } else if (strncmp(line, "Gid:", 4) == 0 && strtok(line + 4, "\t ") != NULL) {
             // Get the effective GID, which is the second value in the line
-            *gid = (gid_t)atoi(strchr(line + 5, '\t'));
+            *gid = (gid_t)atoi(strtok(NULL, "\t "));
         } else if (strncmp(line, "NStgid:", 7) == 0) {
             // PID namespaces can be nested; the last one is the innermost one
-            *nspid = atoi(strrchr(line, '\t'));
+            char* s;
+            for (s = strtok(line + 7, "\t "); s != NULL; s = strtok(NULL, "\t ")) {
+                *nspid = atoi(s);
+            }
             nspid_found = 1;
         }
     }