From nobody Sun Feb 8 14:30:29 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=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