From nobody Sun Feb 8 14:59:25 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=1637343139; cv=none; d=zohomail.com; s=zohoarc; b=ksArmkTf31FrneE1uSr2JcKtJ7NeDy+/+XOFdTg+flJhh40x+QoYl48Kd3T6a2deEE3GP/EaJJS9JRRgfXwIObSD0+I1x1rptMkH8mjAut/puT05PRfndVtPaiHNqp//+tANizqpMCzMZHKmlPABDHXbFEl1Nfeqt3LSUlFSALU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1637343139; 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=x3AhYgwQUbagMKR7nsIPW82aj39k2KgeP2cv+OwpsRc=; b=lv/ve5wKHG1gj0YMYXkL5LnjHTATiPg9u00kZs3LnqPfHza//yYH3iREJZsDk1Mzd4uQX/Weke6+6WcEn2m+9sImYs9+vV0/VDpuaFz4/x+BSE+DPMkkAHPp4NbIB4653xEWFWMBKaswQB4fpOOYza3N8XcOW1NIC54P+aW+l2Y= 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 1637343139254671.5888209010918; Fri, 19 Nov 2021 09:32:19 -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-12-KbE1bT-ONDu6xBT52LXf8g-1; Fri, 19 Nov 2021 12:32:15 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B594C875122; Fri, 19 Nov 2021 17:32:10 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9283B18512; Fri, 19 Nov 2021 17:32:10 +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 5EFFE1832E85; Fri, 19 Nov 2021 17:32:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1AJHVpm4030390 for ; Fri, 19 Nov 2021 12:31:51 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4501E1121318; Fri, 19 Nov 2021 17:31:51 +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 402FD1121315 for ; Fri, 19 Nov 2021 17:31:45 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (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 CAEE185A5AA for ; Fri, 19 Nov 2021 17:31:45 +0000 (UTC) Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-303-n9cAzAhzPSuy8E1WZonVlw-1; Fri, 19 Nov 2021 12:31:43 -0500 Received: by mail-ed1-f69.google.com with SMTP id b15-20020aa7c6cf000000b003e7cf0f73daso8970213eds.22 for ; Fri, 19 Nov 2021 09:31:43 -0800 (PST) Received: from wheatley.localdomain (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id hd15sm221747ejc.69.2021.11.19.09.31.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Nov 2021 09:31:40 -0800 (PST) Received: from wheatley.redhat.com (wheatley.k8r.cz [127.0.0.1]) by wheatley.localdomain (Postfix) with ESMTP id 087CB1E02D13 for ; Fri, 19 Nov 2021 18:31:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637343137; 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=x3AhYgwQUbagMKR7nsIPW82aj39k2KgeP2cv+OwpsRc=; b=NtUMhD5Y8hruP235EwmK5rg3RnV3wYJaUFlBkWWvVR3slyxFIEGcOhONCT3FeJKNoN6Kw3 OYv9tBxsBnUfLpLhnt+nFaPa9ilqF1NSnkWGDq8oJqXl5nw8FzNcv3zR774A2TdBimlIDe qxzsLhYz3MA6KPFd/KSr9Aazj0YXyIA= X-MC-Unique: KbE1bT-ONDu6xBT52LXf8g-1 X-MC-Unique: n9cAzAhzPSuy8E1WZonVlw-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=x3AhYgwQUbagMKR7nsIPW82aj39k2KgeP2cv+OwpsRc=; b=w/QxirTsGUfvzwTJfdQWkK7Dlbx12tlWIzeDS1sn0gtiLpCekQFATbQBl7hj4PBXeY tMHWVtDJIQGJIddYrPiePq2vbhqRmOTuUc9/cbwTTqg22r8oUyq2lJCRD4qZJ4CiHeZK DGUul2QXjeH1MmMOogJdnU2WIudiSEMz1lj0Weatjz1T1WAlX5D+UEyRadItlmO1r6RF V4hHtRLaQAMAQOfArW84rALPCIOdJDY0xWHR2cjFV/Lm0u6uNBPEn3eac3gu4tbZcfbK KETSkXCqN8IUNHpNGK8OisBW19vTwP4iqCTE1RY+4Yl9g69nNDx/ZMtCO6I4naGDY0nh 9dpQ== X-Gm-Message-State: AOAM530oXhNXmJlFXNweoHbEb4c4mId/I8Qk2ajCR4PhAe76BIDgpKXd 0YY/SXYQzo5sIleq+jSYaib+ZnlPNWitV7flD+JagF7lqXcVNqPFTL07VrVrSPo+FFNtxaqcZG4 MR3G9PPs4160o/uPYO+cpe2VTZWf3FTWKrAqZmN5FfIXT+WVOHY4Jd0oKzl79RzuIR3owl2k= X-Received: by 2002:a05:6402:d05:: with SMTP id eb5mr26942658edb.345.1637343102314; Fri, 19 Nov 2021 09:31:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLdLSC9i1nIZO/Bb5HTWaaGdEk29AeBwrpYZdGT+IB20F/NNBkDnRpgoHT3+SncTgR/sueqQ== X-Received: by 2002:a05:6402:d05:: with SMTP id eb5mr26942607edb.345.1637343102034; Fri, 19 Nov 2021 09:31:42 -0800 (PST) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH 2/2] Use virProcessGetStat Date: Fri, 19 Nov 2021 18:31:36 +0100 Message-Id: <8fdeeb8caa8f8422716590539239bd9e049ef704.1637340500.git.mkletzan@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 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.84 on 10.5.11.23 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: 1637343140527100001 Content-Type: text/plain; charset="utf-8" This eliminates one incorrect parsing implementation. Signed-off-by: Martin Kletzander --- src/qemu/qemu_driver.c | 29 ++++++------------------- src/util/virprocess.c | 48 ++++++------------------------------------ 2 files changed, 12 insertions(+), 65 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d954635dde2a..511ff078ce98 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1399,36 +1399,19 @@ 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; unsigned long long usertime =3D 0, systime =3D 0; long rss =3D 0; int cpu =3D 0; + g_auto(GStrv) proc_stat =3D virProcessGetStat(pid, tid); =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 diff --git a/src/util/virprocess.c b/src/util/virprocess.c index 4325f5116e8d..6b60b979d2d5 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