public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC v2 qemu-server 2/3] qmeventd: extract vmid from cgroup file instead of cmdline
Date: Wed, 24 May 2023 15:56:50 +0200	[thread overview]
Message-ID: <20230524135654.214008-3-f.ebner@proxmox.com> (raw)
In-Reply-To: <20230524135654.214008-1-f.ebner@proxmox.com>

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>
---

No changes in v2.

 qmeventd/qmeventd.c | 50 +++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 20 deletions(-)

diff --git a/qmeventd/qmeventd.c b/qmeventd/qmeventd.c
index a843da5f..921c0dca 100644
--- a/qmeventd/qmeventd.c
+++ b/qmeventd/qmeventd.c
@@ -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;
+	}
 
-    if (getdelim(&buf, &buflen, '\0', fp) >= 0) {
-	if (buf[0] == '-' || buf[0] == '\0') {
-	    fprintf(stderr, "invalid vmid %s\n", buf);
+	char *vmid_start = strrchr(buf, '/');
+	if (!vmid_start) {
+	    fprintf(stderr, "unexpected cgroup entry %s\n", buf);
+	    goto ret;
+	}
+	vmid_start++;
+
+	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);
-- 
2.39.2





  parent reply	other threads:[~2023-05-24 13:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24 13:56 [pve-devel] [PATCH-SERIES v2 qemu-server/qemu] Some breaking QEMU changes Fiona Ebner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 qemu-server 1/3] remove left-over mentions of to-be-dropped, outdated QMP commands Fiona Ebner
2023-05-24 13:56 ` Fiona Ebner [this message]
2023-05-24 13:56 ` [pve-devel] [RFC v2 qemu-server 3/3] cfg2cmd: drop custom id parameter Fiona Ebner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 qemu 1/3] drop patch for custom get_link_status QMP command Fiona Ebner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 qemu 2/3] drop deprecated custom drive snapshot QMP commands Fiona Ebner
2023-05-24 13:56 ` [pve-devel] [RFC v2 qemu 3/3] drop patch for custom dummy id CLI argument Fiona Ebner
2023-06-07 17:39 ` [pve-devel] partially-applied-series: [PATCH-SERIES v2 qemu-server/qemu] Some breaking QEMU changes Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230524135654.214008-3-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal