]> git.proxmox.com Git - qemu-server.git/commitdiff
qmeventd: extract vmid from cgroup file instead of cmdline
authorFiona Ebner <f.ebner@proxmox.com>
Wed, 24 May 2023 13:56:50 +0000 (15:56 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 7 Jun 2023 17:37:09 +0000 (19:37 +0200)
This is the single remaining user of the id argument. The id argument
is a Proxmox-specific extension to QEMU, which we'd like to drop to
reduce our differences with upstream QEMU.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
qmeventd/qmeventd.c

index a843da5f73ce68058dd7019ec7bd12641b6c8656..921c0dca1607b5e9d974a039516c555ac26e7c8a 100644 (file)
@@ -75,14 +75,13 @@ get_pid_from_fd(int fd)
 }
 
 /*
- * reads the vmid from /proc/<pid>/cmdline
- * after the '-id' argument
+ * parses the vmid from the qemu.slice entry of /proc/<pid>/cgroup
  */
 static unsigned long
 get_vmid_from_pid(pid_t pid)
 {
     char filename[32] = { 0 };
-    int len = snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
+    int len = snprintf(filename, sizeof(filename), "/proc/%d/cgroup", pid);
     if (len < 0) {
        fprintf(stderr, "error during snprintf for %d: %s\n", pid,
                strerror(errno));
@@ -99,41 +98,52 @@ get_vmid_from_pid(pid_t pid)
     }
 
     unsigned long vmid = 0;
-    ssize_t rc = 0;
     char *buf = NULL;
     size_t buflen = 0;
-    while ((rc = getdelim(&buf, &buflen, '\0', fp)) >= 0) {
-       if (!strcmp(buf, "-id")) {
-           break;
+
+    while (getline(&buf, &buflen, fp) >= 0) {
+       char *cgroup_path = strrchr(buf, ':');
+       if (!cgroup_path) {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
+           goto ret;
        }
-    }
+       cgroup_path++;
 
-    if (rc < 0) {
-       goto err;
-    }
+       if (strncmp(cgroup_path, "/qemu.slice", 11)) {
+           continue;
+       }
+
+       char *vmid_start = strrchr(buf, '/');
+       if (!vmid_start) {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
+           goto ret;
+       }
+       vmid_start++;
 
-    if (getdelim(&buf, &buflen, '\0', fp) >= 0) {
-       if (buf[0] == '-' || buf[0] == '\0') {
-           fprintf(stderr, "invalid vmid %s\n", buf);
+       if (vmid_start[0] == '-' || vmid_start[0] == '\0') {
+           fprintf(stderr, "invalid vmid in cgroup entry %s\n", buf);
            goto ret;
        }
 
        errno = 0;
        char *endptr = NULL;
-       vmid = strtoul(buf, &endptr, 10);
+       vmid = strtoul(vmid_start, &endptr, 10);
        if (errno != 0) {
+           fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
            vmid = 0;
-           goto err;
-       } else if (*endptr != '\0') {
-           fprintf(stderr, "invalid vmid %s\n", buf);
+       } else if (*endptr != '.') {
+           fprintf(stderr, "unexpected cgroup entry %s\n", buf);
            vmid = 0;
        }
 
        goto ret;
     }
 
-err:
-    fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
+    if (errno) {
+       fprintf(stderr, "error parsing vmid for %d: %s\n", pid, strerror(errno));
+    } else {
+       fprintf(stderr, "error parsing vmid for %d: no qemu.slice cgroup entry\n", pid);
+    }
 
 ret:
     free(buf);