From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 5FAB29AF3A
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 12:54:09 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 4120D1D23C
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 12:54:09 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 12:54:08 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DB14346DA0
 for <pve-devel@lists.proxmox.com>; Wed, 24 May 2023 12:54:07 +0200 (CEST)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 24 May 2023 12:54:00 +0200
Message-Id: <20230524105403.98049-2-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20230524105403.98049-1-f.ebner@proxmox.com>
References: <20230524105403.98049-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.049 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pve-devel] [RFC qemu-server 1/2] qmeventd: extract vmid from
 cgroup file instead of cmdline
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 24 May 2023 10:54:09 -0000

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