From nobody Tue Apr 23 13:03:29 2024 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=1637449489; cv=none; d=zohomail.com; s=zohoarc; b=hIHP6NDlfNBKA0MdzPIZmD6a8w2reoD8P9xLCHT908//9YiSAck0d0O3sECiC2W8spYZSJmNVYprZO2tUZnIkLZS5UHAHpIHzniPxGkC/UyyUWhPYIzD0vefxbWfN5IIn7KgHKsKaTWTLOKRC40i9H8f9M759hCMvKrWaCnmvRo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1637449489; 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=b1Tft28MtnGKC0lK2EXEG7SwwL9rp5S5etBWnYx1JdI=; b=YVo3p5q/pIi5QT1Yh1dWGYbvq/kfIiHuigmtqwbk477PaZylBkNws+yDYF6gB5IFaXZqxFn1E6koZ2tNMkdlgSRbWpUF/hLUxqH5yjqxEzwzwbEq+C55hfb6V7vYQQ1wGtAtgYv+dNThVptnaZ+VHKIGt4p7/jJWNTa1tb5LQow= 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 1637449489426360.1427461835523; 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-207-5FJy-dBYNz-bkhDDC1W5Ww-1; Sat, 20 Nov 2021 18:04:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 06C0F806690; Sat, 20 Nov 2021 23:04:40 +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 DB8FD5E26A; Sat, 20 Nov 2021 23:04:39 +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 B1A741832E85; Sat, 20 Nov 2021 23:04:39 +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 1AKN4X9Z001132 for ; Sat, 20 Nov 2021 18:04:33 -0500 Received: by smtp.corp.redhat.com (Postfix) id E52C451E2; Sat, 20 Nov 2021 23:04:32 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DF61751E1 for ; Sat, 20 Nov 2021 23:04:32 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.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 C52AE80122B for ; Sat, 20 Nov 2021 23:04:32 +0000 (UTC) Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-447-wE0TbyLRPr-y3yJ3YWkI4w-1; Sat, 20 Nov 2021 18:04:31 -0500 Received: by mail-ed1-f71.google.com with SMTP id d11-20020a50cd4b000000b003da63711a8aso11439247edj.20 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 d18sm1759266edj.23.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 57E8FC20040 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=b1Tft28MtnGKC0lK2EXEG7SwwL9rp5S5etBWnYx1JdI=; b=DGjKShH0XFHeizeSgbHwWdwtHNKUZhiwEgq5ra8UoT7WqskgfI6mKsJULKqSO5uJ+snmlP Mc3QES/hPYQBi6axifPfwQLfhnDWwyWmxXNPHpfy9Js4I4Zi1Y9n/47UUf7Ovqc7B3GAl2 kZx/y0qdGx/HC0CNY0sF6Beyt5mBBM8= X-MC-Unique: 5FJy-dBYNz-bkhDDC1W5Ww-1 X-MC-Unique: wE0TbyLRPr-y3yJ3YWkI4w-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=b1Tft28MtnGKC0lK2EXEG7SwwL9rp5S5etBWnYx1JdI=; b=BiF3Q9iRFgCL/D7Op0BeltBtTJVcVLLrq86yhLyiGBrI6P7pkveiPpIq3TSeuHCVN5 q1M8GDsI4LnLtgqg9P1aPWaNXOY7rcAigA4zv0MV61P6+od8bzXOdNtlmyLWxufsvTbl 9tR9ANjdLg4EDnxkP5Y++yxnLhYXTfvOkPApNZRCH6wgYWK7+aYuNqi5aE8Sr3vFmcZl dy5ljfR4NwiHnlC5VNJoMjvPreSnHwK8loy4IUZjv1BxeY8+kB4y3lhHUE4KabD6tijl xe7RAIr6tZwenW7E+E/CHhl1ZuoGcHIr9mxWstDGIgJdyF729RQpdRW4kpneh9ehbLuA h8Kg== X-Gm-Message-State: AOAM532CxkKJJSuMBwFyLLEJWYLrDl0+ouA+fra99oewXe/7tFRRQOxI GxIWDy1GCQ0R+QMqIVFsoYOcl4WLIncfg3gDNIe7Ou/cI5+BBsMk7pk8JEFBi3dxlj77h7CvZWg Z+Fq4ZAZzEMMzxOIQL0L2wpH/wWB22zNhRJ03FAd6hdfzIlmrqoyQbLCUoJIAEBN3of2poQY= X-Received: by 2002:a17:906:314e:: with SMTP id e14mr23845710eje.165.1637449470351; Sat, 20 Nov 2021 15:04:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwDT2IfRlkntnfP5JuElTdUITF7GMzYCB4F+hBSLiXgVUmEuS6odz1JeWghs7R9dA3aC/mmXA== X-Received: by 2002:a17:906:314e:: with SMTP id e14mr23845659eje.165.1637449470052; Sat, 20 Nov 2021 15:04:30 -0800 (PST) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH v2 1/2] util: Add virProcessGetStat Date: Sun, 21 Nov 2021 00:04:25 +0100 Message-Id: <97c4ed003e4fe0585108c933a082f562d25dbcac.1637449173.git.mkletzan@redhat.com> 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.14 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: 1637450393614100001 Content-Type: text/plain; charset="utf-8" This reads and separates all fields from /proc//stat or /proc//task//stat as there are easy mistakes to be done in the implementation. Some tests are added to show it works correctly. No number parsing is done as it would be unused for most of the fields most, if not a= ll, of the time. No struct is used for the result as the length can vary (new fields can be added in the future). Signed-off-by: Martin Kletzander --- src/libvirt_linux.syms | 3 + src/util/virprocess.c | 78 +++++++++++++++++++++++++ src/util/virprocess.h | 4 ++ tests/meson.build | 1 + tests/virprocessstatdata/complex/stat | 2 + tests/virprocessstatdata/simple/stat | 1 + tests/virprocessstattest.c | 84 +++++++++++++++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 tests/virprocessstatdata/complex/stat create mode 100644 tests/virprocessstatdata/simple/stat create mode 100644 tests/virprocessstattest.c diff --git a/src/libvirt_linux.syms b/src/libvirt_linux.syms index 55649ae39cec..14422fae7286 100644 --- a/src/libvirt_linux.syms +++ b/src/libvirt_linux.syms @@ -10,6 +10,9 @@ virHostCPUGetSiblingsList; virHostCPUGetSocket; virHostCPUGetStatsLinux; =20 +# util/virprocess.h +virProcessGetStat; + # Let emacs know we want case-insensitive sorting # Local Variables: # sort-fold-case: t diff --git a/src/util/virprocess.c b/src/util/virprocess.c index 6de3f36f529c..73ebcaae422f 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -1721,3 +1721,81 @@ virProcessSetScheduler(pid_t pid G_GNUC_UNUSED, } =20 #endif /* !WITH_SCHED_SETSCHEDULER */ + +#ifdef __linux__ +/* + * Get all stat fields for a process based on pid and tid: + * - pid =3D=3D 0 && tid =3D=3D 0 =3D> /proc/self/stat + * - pid !=3D 0 && tid =3D=3D 0 =3D> /proc//stat + * - pid =3D=3D 0 && tid !=3D 0 =3D> /proc/self/task//stat + * - pid !=3D 0 && tid !=3D 0 =3D> /proc//task//stat + * and return them as array of strings. + */ +GStrv +virProcessGetStat(pid_t pid, + pid_t tid) +{ + size_t buflen =3D 0; + g_autofree char *buf =3D NULL; + g_autofree char *path =3D NULL; + GStrv rest =3D NULL; + GStrv ret =3D NULL; + char *comm =3D NULL; + char *rparen =3D NULL; + size_t nrest =3D 0; + + if (pid) { + if (tid) + path =3D g_strdup_printf("/proc/%d/task/%d/stat", (int)pid, (i= nt)tid); + else + path =3D g_strdup_printf("/proc/%d/stat", (int)pid); + } else { + if (tid) + path =3D g_strdup_printf("/proc/self/task/%d/stat", (int)tid); + else + path =3D g_strdup("/proc/self/stat"); + } + + if (virFileReadAllQuiet(path, 1024, &buf) < 0) + return NULL; + + /* eliminate trailing spaces */ + while (g_ascii_isspace(buf[--buflen])) + buf[buflen] =3D '\0'; + + /* Find end of the first field */ + if (!(comm =3D strchr(buf, ' '))) + return NULL; + *comm =3D '\0'; + + /* Check start of the second field (filename of the executable, in + * parentheses) */ + comm++; + if (*comm !=3D '(') + return NULL; + comm++; + + /* Check end of the second field (last closing parenthesis) */ + rparen =3D strrchr(comm, ')'); + if (!rparen) + return NULL; + *rparen =3D '\0'; + + /* We need to check that the next char is not '\0', but why not just o= pt in + * for the safer way of checking whether it is ' ' (space) instead */ + if (rparen[1] !=3D ' ') + return NULL; + + rest =3D g_strsplit(rparen + 2, " ", 0); + nrest =3D g_strv_length(rest); + ret =3D g_new0(char *, nrest + 3); + ret[0] =3D g_strdup(buf); + ret[1] =3D g_strdup(comm); + memcpy(ret + 2, rest, nrest * sizeof(char *)); + + /* Do not use g_strfreev() as individual elements they were moved to @= ret. */ + VIR_FREE(rest); + + return ret; +} +#endif diff --git a/src/util/virprocess.h b/src/util/virprocess.h index 9910331a0caa..74dad1f3b15e 100644 --- a/src/util/virprocess.h +++ b/src/util/virprocess.h @@ -117,6 +117,10 @@ int virProcessSetupPrivateMountNS(void); int virProcessSetScheduler(pid_t pid, virProcessSchedPolicy policy, int priority); +#ifdef __linux__ +GStrv virProcessGetStat(pid_t pid, pid_t tid); +#endif + typedef enum { VIR_PROCESS_NAMESPACE_MNT =3D (1 << 1), VIR_PROCESS_NAMESPACE_IPC =3D (1 << 2), diff --git a/tests/meson.build b/tests/meson.build index 1948c07ae385..f75c24872086 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -347,6 +347,7 @@ if host_machine.system() =3D=3D 'linux' { 'name': 'scsihosttest' }, { 'name': 'vircaps2xmltest', 'link_whole': [ test_file_wrapper_lib ] }, { 'name': 'virnetdevbandwidthtest' }, + { 'name': 'virprocessstattest', 'link_whole': [ test_file_wrapper_lib = ] }, { 'name': 'virresctrltest', 'link_whole': [ test_file_wrapper_lib ] }, { 'name': 'virscsitest' }, { 'name': 'virusbtest' }, diff --git a/tests/virprocessstatdata/complex/stat b/tests/virprocessstatda= ta/complex/stat new file mode 100644 index 000000000000..df0ecc149c6f --- /dev/null +++ b/tests/virprocessstatdata/complex/stat @@ -0,0 +1,2 @@ +1 (this) is ( a weird ) +)( (command ( ) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24= 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49= 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74= 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99= 100 diff --git a/tests/virprocessstatdata/simple/stat b/tests/virprocessstatdat= a/simple/stat new file mode 100644 index 000000000000..97d68735370d --- /dev/null +++ b/tests/virprocessstatdata/simple/stat @@ -0,0 +1 @@ +1 (command) 3 4 5 diff --git a/tests/virprocessstattest.c b/tests/virprocessstattest.c new file mode 100644 index 000000000000..c973dbc629d7 --- /dev/null +++ b/tests/virprocessstattest.c @@ -0,0 +1,84 @@ +#include + +#include "testutils.h" +#include "virfilewrapper.h" +#include "virprocess.h" + + +struct testData { + const char *filename; + const char *command; + size_t count; + bool self; +}; + + +static int +test_virProcessGetStat(const void *opaque) +{ + struct testData *data =3D (struct testData *) opaque; + g_autofree char *data_dir =3D NULL; + g_auto(GStrv) proc_stat =3D NULL; + size_t len =3D 0; + id_t id =3D data->self ? 0 : -1; + + data_dir =3D g_strdup_printf("%s/virprocessstatdata/%s/", + abs_srcdir, data->filename); + + if (data->self) + virFileWrapperAddPrefix("/proc/self/", data_dir); + else + virFileWrapperAddPrefix("/proc/-1/task/-1/", data_dir); + + proc_stat =3D virProcessGetStat(id, id); + + virFileWrapperClearPrefixes(); + + if (!proc_stat) { + fprintf(stderr, "Could not get process stats\n"); + return -1; + } + + len =3D g_strv_length(proc_stat); + if (data->count !=3D len) { + fprintf(stderr, "Count incorrect, expected %zu, got %zu\n", + data->count, len); + return -1; + } + + if (!STREQ_NULLABLE(data->command, proc_stat[1])) { + fprintf(stderr, "Command incorrect, expected %s, got %s\n", + data->command, proc_stat[1]); + return -1; + } + + return 0; +} + + +static int +mymain(void) +{ + struct testData data =3D {0}; + int ret =3D 0; + +#define DO_TEST(_filename, _command, _count, _self) \ + do { \ + data =3D (struct testData){ \ + .filename =3D _filename, \ + .command =3D _command, \ + .count =3D _count, \ + .self =3D _self, \ + }; \ + if (virTestRun("Reading process stat: " _filename, \ + test_virProcessGetStat, &data) < 0) \ + ret =3D -1; \ + } while (0) + + DO_TEST("simple", "command", 5, true); + DO_TEST("complex", "this) is ( a \t weird )\n)( (command ( ", 100, fal= se); + + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) --=20 2.34.0 From nobody Tue Apr 23 13:03:29 2024 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