From nobody Sun Feb 8 12:14:05 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1637660934; cv=none; d=zohomail.com; s=zohoarc; b=cYki/7y8/ei5HjDSQsSQMwcQPiJYjQWEsmIGE9bDawXb68esKC7C/IgMF1QncVfBB111/+yFcF2dZ55H7PsFHJOZ3u7PuRDMQIZMCiw+y9qDxWGYyQ6prgg45jn/hMRkHl7PC8q1rCqmlpzDMHKq1jdlxLCZnehO9LNLLpEUFEo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1637660934; 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=37LSyIvuSpc8ljKk0EeyVt5tNcZYRwzSSlFu62Bzv8o=; b=GSl0vRplRcw3OaITz7DpbjKZZpdkpHqPgv3RKPiQ4c/kreSLmsapFwGkz3v/HgCrk2BV1kdrYrppfseGVgipF1VLdcWnm2+0aePeLeaLuI+GNWKtmL0JDuvyLdN3eCG49Y8632XfIpu9YWVu7pXdpJwogaiu1V7r1fCg8aMj+2M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 1637660934587612.6237329184223; Tue, 23 Nov 2021 01:48:54 -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-560-1zS_2qNeNGK0gFAXSwJRNg-1; Tue, 23 Nov 2021 04:48:50 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 93F3118125C0; Tue, 23 Nov 2021 09:48:45 +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 20B151981F; Tue, 23 Nov 2021 09:48:45 +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 720F84A703; Tue, 23 Nov 2021 09:48:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1AN9mhLB007099 for ; Tue, 23 Nov 2021 04:48:43 -0500 Received: by smtp.corp.redhat.com (Postfix) id E294540CFD0D; Tue, 23 Nov 2021 09:48:42 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD74C40CFD04 for ; Tue, 23 Nov 2021 09:48:42 +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 C20CC85A5B9 for ; Tue, 23 Nov 2021 09:48:42 +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-544-N7JKPtSyMU69iR5wTMs2Kg-1; Tue, 23 Nov 2021 04:48:41 -0500 Received: by mail-ed1-f70.google.com with SMTP id k7-20020aa7c387000000b003e7ed87fb31so17430076edq.3 for ; Tue, 23 Nov 2021 01:48:41 -0800 (PST) Received: from wheatley.localdomain (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id z22sm5867702edd.78.2021.11.23.01.48.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Nov 2021 01:48:39 -0800 (PST) Received: from wheatley.redhat.com (wheatley.k8r.cz [127.0.0.1]) by wheatley.localdomain (Postfix) with ESMTP id 1478020BC4F8 for ; Tue, 23 Nov 2021 10:48:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637660933; 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=37LSyIvuSpc8ljKk0EeyVt5tNcZYRwzSSlFu62Bzv8o=; b=MBoZ6nn0tCKPslrLXvvLYM1R+TWtYi3fC9YT0yEakPwCuYewZ2m3Odv0EIDDnGQbbGty5m 3YEHtEOax9v7a0OKqYXJA0/ZkhWn3l1oZkkzS8TDjeSHVfnoblRNsBliI0lrX8GcU6iXjA ngPPrEG1MsruNNx+NiaSeSUIyyh0/TM= X-MC-Unique: 1zS_2qNeNGK0gFAXSwJRNg-1 X-MC-Unique: N7JKPtSyMU69iR5wTMs2Kg-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=37LSyIvuSpc8ljKk0EeyVt5tNcZYRwzSSlFu62Bzv8o=; b=kPDVxyYZLPJrf76TQu0YG/35UNDAurVIJvBT/LPXwlVASU1M5dY9pzQ71HG9DzEkFV 6yf3+OVKlXx3v3E+w4LvODe6bBwlI5PGHgwic3x+aEnAybmNe8KTPVa7T6u05UlKp67q +cG+JvdMUUzZpinhJ55u8fBrHMr+nXM+CmveXNWhd5krZcmdWOOwdERST0kGO8b81y7E wVYb8Ce6h+8s6pPbCoGekWzShi72rlfL8ViMkRQHuEwJb2yD6xb3RUguKUXAPoVMDKMb XBhnkIU0UX/lE2HPpuLTD/7uzMpdwJ7FoAfc8MmQSD1njIm7AeifYdqKlAKP5mZ/pJVR 0bfg== X-Gm-Message-State: AOAM533/inR9ripdAF2ErFHnxtfhW/HvcDw18KcoSDRgfFC0CgwudpkR OzKPBsYvZB46GwtOF+QsL5M1Cb2Ao7HtQFFtfI9B7a9LTI1oaD7nFM19PfFqI8aSIrR06yK8xpZ qgsyUIVzJTyLUHs5gRDvu6HKyW39eYIRtKpEP5hdAJ1QzcND3hkjcWX3uAoHoFOQ8Gwm3dCU= X-Received: by 2002:a17:906:79c8:: with SMTP id m8mr5457607ejo.511.1637660920255; Tue, 23 Nov 2021 01:48:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJzkr4HGlFlZdSDF139lGl/WPnq1iIsYfrHUnnGwBFGkBXFaTt4YXqRcXzDqfYGfZi1exCJzZw== X-Received: by 2002:a17:906:79c8:: with SMTP id m8mr5457586ejo.511.1637660920057; Tue, 23 Nov 2021 01:48:40 -0800 (PST) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH v3 2/2] Use virProcessGetStat Date: Tue, 23 Nov 2021 10:48:06 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 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.11 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: 1637660935065100001 Content-Type: text/plain; charset="utf-8" This eliminates one incorrect parsing implementation which relied on the command field not having a closing bracket. This possibility is already tested against in the virProcessGetStat() tests. Signed-off-by: Martin Kletzander Reviewed-by: J=C3=A1n Tomko --- src/qemu/qemu_driver.c | 34 ++++++------------------------ src/util/virprocess.c | 48 ++++++------------------------------------ 2 files changed, 13 insertions(+), 69 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d954635dde2a..16d449913ca7 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1399,36 +1399,18 @@ 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 (!proc_stat || + virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_UTIME], NULL, 10, &us= ertime) < 0 || + virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_STIME], NULL, 10, &sy= stime) < 0 || + virStrToLong_l(proc_stat[VIR_PROCESS_STAT_RSS], NULL, 10, &rss) < = 0 || + virStrToLong_i(proc_stat[VIR_PROCESS_STAT_PROCESSOR], NULL, 10, &c= pu) < 0) { VIR_WARN("cannot parse process status data"); } =20 @@ -1450,8 +1432,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 aab51962acfc..20d04bad6f26 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