From nobody Sun Feb 8 12:51:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1637449490; cv=none; d=zohomail.com; s=zohoarc; b=lPe97qRVv/ERnmJdJN1lUxUw1DTJ4B5TJmbSuu5G2mGYjq87IGzkMMsCkb7fyDGfGrCQYXtUv0NKn334LF0Nhb6lWlXQVqyHr/6qo8vGCZHHOsUcs7S4gXAXwIgUgILHZE/RCntfLuagzlioy/DIEKSJiRbqanY/EwjVYq5G9uA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1637449490; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lkmHkrYFeTTNZ0MMfsXcSsXWH0+h0aGZNLCcgCT6rAo=; b=cIMTewOCDtuplpnnhsgAr0gcNsEP4OsZOQjXWsovaAfHv2jl2mZjMt0hasMqGy6cbWBiRq3xanOhTxBIJL6+obZ06S1UEKPqcy/qXvZreJUSCCSOVtfrXYPFxp+H9n10a1jSFptM5PtqIzlHypUA7Z2BIBtdSUw2lKQ/OOFELMo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 163744948998170.06077949384769; Sat, 20 Nov 2021 15:04:49 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-101-Yi0BoUyuOvul61xrXI2CYw-1; Sat, 20 Nov 2021 18:04:47 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CBEC1DDE8; Sat, 20 Nov 2021 23:04:42 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 503005F4EA; Sat, 20 Nov 2021 23:04:42 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 23B0B4E9F4; Sat, 20 Nov 2021 23:04:42 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1AKN4XnB001142 for ; Sat, 20 Nov 2021 18:04:33 -0500 Received: by smtp.corp.redhat.com (Postfix) id 7775B51E2; Sat, 20 Nov 2021 23:04:33 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7285C51E1 for ; Sat, 20 Nov 2021 23:04:33 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5A13D8001EA for ; Sat, 20 Nov 2021 23:04:33 +0000 (UTC) Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-158-Vq2J6C5SM8Op5qeWZHFnoQ-1; Sat, 20 Nov 2021 18:04:31 -0500 Received: by mail-ed1-f70.google.com with SMTP id bx28-20020a0564020b5c00b003e7c42443dbso11455413edb.15 for ; Sat, 20 Nov 2021 15:04:31 -0800 (PST) Received: from carol.localdomain (k8r.cz. [95.82.177.71]) by smtp.gmail.com with ESMTPSA id u16sm1692760ejy.16.2021.11.20.15.04.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 15:04:29 -0800 (PST) Received: from carol.redhat.com (carol.k8r.cz [IPv6:::1]) by carol.localdomain (Postfix) with ESMTP id 59796C2004E for ; Sun, 21 Nov 2021 00:04:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637449488; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=lkmHkrYFeTTNZ0MMfsXcSsXWH0+h0aGZNLCcgCT6rAo=; b=hkS0bUgl4EB10cXzdQtrS6oqyUlIPaWnPkN17RCzzT+oa4ZG7QgyX6KGgetYqq7+SAsj2u otiqu8F3kLjUSmOShfFAx2JAJIICOmdbI3mnQtHOlmlOJTH7iycEuh9TuxGUa2oNS14A42 AILn7WXsxLcfMvBUcIkeh16imMwMNj8= X-MC-Unique: Yi0BoUyuOvul61xrXI2CYw-1 X-MC-Unique: Vq2J6C5SM8Op5qeWZHFnoQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lkmHkrYFeTTNZ0MMfsXcSsXWH0+h0aGZNLCcgCT6rAo=; b=4UeyRElYk1ChvMBFO1zkU/toR7bBVo1tGOtMArqvGUQb+Y8kC8BwWUwKcHiDdt+ur7 E8DuJsO0BlcB7ZGAOvh1ceb9D3orVBB0l/eJbhdVU5nNhFdoW5RSOa7w4Ggg/Nutt/lb ksAQT+SIKJQomHdxmjz1IQB5nlKFI5a2raugyPDK7FNm5YPQoV+TiAgS21K70/0tvPht /jH8nq4sGLSJMAqkyhg694+7HbO7QIKb2+JDA0zyEc55EZ2lWPdBcnRDkN1H4XwAO5gn zsgomFKagVkEWR6evaTtQf/QSJViKWdUyqsbn2D4F0lVBynRczswYFUlCQY+rS1HI178 klIQ== X-Gm-Message-State: AOAM533ciUpxXGy6pn+5CJrivZozC9VV41zWxupNNRWWONxgFp2Gki1G RD/2qezRkej68/9/mP8sSUFfiCQjvQSXifBkZrESB7j3cjGdkfJuTge2VYniQJVu/SdL/fr4Zjh TrvRJcb3P/i1m8uL+82NcshRtPWgK/3U7Y7Xf4dH2ltmcP0IBhNWK5X62xNSn+g+BcEGwARs= X-Received: by 2002:a05:6402:1cb2:: with SMTP id cz18mr42414893edb.99.1637449470748; Sat, 20 Nov 2021 15:04:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJyecwKPxrTqZOCl5QXY1fOsM5MlQ/mzyci/4OBKm6bM8J5NTxs/iYFYJ+yKjD2gt1CYrXu1YQ== X-Received: by 2002:a05:6402:1cb2:: with SMTP id cz18mr42414841edb.99.1637449470437; Sat, 20 Nov 2021 15:04:30 -0800 (PST) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH v2 2/2] Use virProcessGetStat Date: Sun, 21 Nov 2021 00:04:26 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1637449491951100001 Content-Type: text/plain; charset="utf-8" This eliminates one incorrect parsing implementation. Signed-off-by: Martin Kletzander --- src/qemu/qemu_driver.c | 33 ++++++----------------------- src/util/virprocess.c | 48 ++++++------------------------------------ 2 files changed, 12 insertions(+), 69 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d954635dde2a..0468d6aaf314 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1399,36 +1399,17 @@ qemuGetSchedInfo(unsigned long long *cpuWait, =20 static int qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss, - pid_t pid, int tid) + pid_t pid, pid_t tid) { - g_autofree char *proc =3D NULL; - FILE *pidinfo; + g_auto(GStrv) proc_stat =3D virProcessGetStat(pid, tid); unsigned long long usertime =3D 0, systime =3D 0; long rss =3D 0; int cpu =3D 0; =20 - /* In general, we cannot assume pid_t fits in int; but /proc parsing - * is specific to Linux where int works fine. */ - if (tid) - proc =3D g_strdup_printf("/proc/%d/task/%d/stat", (int)pid, tid); - else - proc =3D g_strdup_printf("/proc/%d/stat", (int)pid); - if (!proc) - return -1; - - pidinfo =3D fopen(proc, "r"); - - /* See 'man proc' for information about what all these fields are. We'= re - * only interested in a very few of them */ - if (!pidinfo || - fscanf(pidinfo, - /* pid -> stime */ - "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %= llu %llu" - /* cutime -> endcode */ - "%*d %*d %*d %*d %*d %*d %*u %*u %ld %*u %*u %*u" - /* startstack -> processor */ - "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %d", - &usertime, &systime, &rss, &cpu) !=3D 4) { + if (virStrToLong_ullp(proc_stat[13], NULL, 10, &usertime) < 0 || + virStrToLong_ullp(proc_stat[14], NULL, 10, &systime) < 0 || + virStrToLong_l(proc_stat[23], NULL, 10, &rss) < 0 || + virStrToLong_i(proc_stat[38], NULL, 10, &cpu) < 0) { VIR_WARN("cannot parse process status data"); } =20 @@ -1450,8 +1431,6 @@ qemuGetProcessInfo(unsigned long long *cpuTime, int *= lastCpu, long *vm_rss, VIR_DEBUG("Got status for %d/%d user=3D%llu sys=3D%llu cpu=3D%d rss=3D= %ld", (int)pid, tid, usertime, systime, cpu, rss); =20 - VIR_FORCE_FCLOSE(pidinfo); - return 0; } =20 diff --git a/src/util/virprocess.c b/src/util/virprocess.c index 73ebcaae422f..4def5ecf5eb3 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -1153,56 +1153,20 @@ virProcessSetMaxCoreSize(pid_t pid G_GNUC_UNUSED, int virProcessGetStartTime(pid_t pid, unsigned long long *timestamp) { - char *tmp; - int len; - g_autofree char *filename =3D NULL; - g_autofree char *buf =3D NULL; - g_auto(GStrv) tokens =3D NULL; - - filename =3D g_strdup_printf("/proc/%llu/stat", (long long)pid); - - if ((len =3D virFileReadAll(filename, 1024, &buf)) < 0) - return -1; + g_auto(GStrv) proc_stat =3D virProcessGetStat(pid, 0); =20 - /* start time is the token at index 19 after the '(process name)' entr= y - since only this - * field can contain the ')' character, search backwards for this to a= void malicious - * processes trying to fool us - */ - - if (!(tmp =3D strrchr(buf, ')'))) { + if (!proc_stat || g_strv_length(proc_stat) < 22) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot find start time in %s"), - filename); + _("Cannot find start time for pid %d"), (int)pid); return -1; } - tmp +=3D 2; /* skip ') ' */ - if ((tmp - buf) >=3D len) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot find start time in %s"), - filename); - return -1; - } - - tokens =3D g_strsplit(tmp, " ", 0); =20 - if (!tokens || - g_strv_length(tokens) < 20) { + if (virStrToLong_ull(proc_stat[21], NULL, 10, timestamp) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot find start time in %s"), - filename); + _("Cannot parse start time %s for pid %d"), + proc_stat[21], (int)pid); return -1; } - - if (virStrToLong_ull(tokens[19], - NULL, - 10, - timestamp) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot parse start time %s in %s"), - tokens[19], filename); - return -1; - } - return 0; } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) --=20 2.34.0