From nobody Thu Apr 25 23:11:09 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=1637343126; cv=none; d=zohomail.com; s=zohoarc; b=ZRJdtAJgnvuBt4tdCQW6w1obfo0pWZoxi3P+hzVM6fim4w8J+f5Pz0MTcEu/iA4qi5RGCZkvy96i3CjuZEW3CHn3yJJX2VIRvUGjp5v6syhR4VQEoLcuIoH+Bl1iFPa5cbZaTrOzvI0hc5eaYdTLVG1EVoLcQNJzH9UcGAPFzDU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1637343126; 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=lf5IOwOhYwSZTtUD/5gwOlr58IdDGI7Qrtl2I4w+iE0=; b=XloolP10HRgfqXjwtuDLJ56IT51DdgqseyENi3sl0gg6PwjtcY0DBMKm1/GeI2C9OwCZz543IE9FqxjGiIu3LO+TjaVOAK9hUHn0dGhFM+T+Zc7a5e5MW5nird+rPGz31E1gcS0mppXym0J969r4EkUAVXP+ZuX9mWtGMS2yAO4= 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 1637343126220630.5099102859695; Fri, 19 Nov 2021 09:32:06 -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-199-HhLy2K1-MyaljAfjIZaNiA-1; Fri, 19 Nov 2021 12:32:01 -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 30E8D1DDE1; Fri, 19 Nov 2021 17:31:52 +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 BD5395E275; Fri, 19 Nov 2021 17:31:50 +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 40A941832E80; Fri, 19 Nov 2021 17:31:47 +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 1AJHVktt030375 for ; Fri, 19 Nov 2021 12:31:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id E138040CFD15; Fri, 19 Nov 2021 17:31:45 +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 DC60C40CFD14 for ; Fri, 19 Nov 2021 17:31:45 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (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 C21B880B715 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-434-hA4HHGpEPoC7q3tbgbbuIg-1; Fri, 19 Nov 2021 12:31:43 -0500 Received: by mail-ed1-f69.google.com with SMTP id v22-20020a50a456000000b003e7cbfe3dfeso9013621edb.11 for ; Fri, 19 Nov 2021 09:31:42 -0800 (PST) Received: from wheatley.localdomain (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id e13sm201592eje.95.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 064EC1E02D12 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=1637343125; 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=lf5IOwOhYwSZTtUD/5gwOlr58IdDGI7Qrtl2I4w+iE0=; b=IQg0Wp7nDKAjHX97ZRlzZqjXyV1/IECmjDybGBfYme2rxeu4Ym0cfsMQKs6ayh2akOyPPz A3DkmdBmsMpQ3aEpxoIIh7JNraPMgTLVe2tRd2x29LtvT3VubRojlHwLIOHJcxBWYot9hI J2xFMKVj112mcm2/XjLRIL5OubdLl8s= X-MC-Unique: HhLy2K1-MyaljAfjIZaNiA-1 X-MC-Unique: hA4HHGpEPoC7q3tbgbbuIg-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=lf5IOwOhYwSZTtUD/5gwOlr58IdDGI7Qrtl2I4w+iE0=; b=OyzFNHqyiTGNMK1EaKaID7bI6vs5vaFr0/suY8DYcWXcxd6CU6aHQ+6dq/UTJ3JTvf AO/oNnZF/Wjk/959aQ+kreBHIOrB3IWe2NkZheUypVkMUOzp/TCWpuulV89mmy29/+G4 oaIW6JWJwvVJ6RWfxakEt2wAo4TwW9iUuvztrGiotMMJmD2xflIclCUECx1kAjtzpiow hU6hXxNupAW2MOrKvMftdXEAcMDR29IulSCD13S06PNIQzkypKg4XN3fgyMfdoiFR5BD 8cU6cLNlm9HQXLp0aPxJs4zU5obH/ssVkHg63xfJlmyQJfFYTRlMlNC4gE9/BnIr9YcS uRJg== X-Gm-Message-State: AOAM532ki2mK+PfUDnN2+16bY7UnziOPtKNpLLwBdVseHnQkIsrVBDd4 otDhrowrvhPu742/fvDU72ddVRnZFsJtIJgZ1mxB/6Kx1LsrT3LbAxpJH5uvUi+JhJYWDdKspc/ mcWkD8fiPBwsseNTcLcgFtV+hIXmygrjBr4N7g7fOs+AGQdxcuEPm3uch2m1574L3MtoodGg= X-Received: by 2002:a17:907:168f:: with SMTP id hc15mr10004400ejc.115.1637343101832; Fri, 19 Nov 2021 09:31:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJzSPR+5tOPOmhIElCnw0Y3BxJw8rZGYfPpy/FI60wA4D9TvjZSr4OxBnZi5uAxk5HxcFScxlg== X-Received: by 2002:a17:907:168f:: with SMTP id hc15mr10004362ejc.115.1637343101586; Fri, 19 Nov 2021 09:31:41 -0800 (PST) From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH 1/2] util: Add virProcessGetStat Date: Fri, 19 Nov 2021 18:31:35 +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.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: 1637343126887100001 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..4325f5116e8d 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 (!g_file_get_contents(path, &buf, &buflen, NULL)) + 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 Thu Apr 25 23:11:09 2024 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