From nobody Mon Feb 9 20:09:43 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1614971772; cv=none; d=zohomail.com; s=zohoarc; b=GONxzWxcZq7uLs4DD/RPJvJHRQtT3bTeKvzPfXNIoNQCF3HcjbUKKzIC2Saif8kLjsFDHLkjWqAxTmIvk4EJh3tLbPgw53mIKdGAre4hHmgN/ljxiFRIgpdQpQ9SsTZhGfEPqVkY+aTJeTLEK+c5kUgOeIFhm7ZZSpzNxA9Upfk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614971772; 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=sVWULxKy+Fq8rTwVRzuz20BXCQsDfXNbGMi6HbOmncU=; b=Spa4EFKpHk+bEj8FzXKRvK2QZ2a7e3eYN5GxgUhSJWxRYU+dPcJh2g6heIaGo+pUyuXyOA35EepmvLDx1TWGC+AwZTFm1PykGk18qgFJC25UjYtq43ubLaHrtDEa2KrxUJY9nXneE1NeY6b0IAEc7L/wQjk/FT4ohUZ1osoA2YI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1614971772966609.5559017959415; Fri, 5 Mar 2021 11:16:12 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-227-V4136_38MjCI_frqXm3klw-1; Fri, 05 Mar 2021 14:15:36 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BD33E1005D58; Fri, 5 Mar 2021 19:15:23 +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 95A6A37DD; Fri, 5 Mar 2021 19:15:23 +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 5E91B57DC6; Fri, 5 Mar 2021 19:15:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 125JEOgA001848 for ; Fri, 5 Mar 2021 14:14:24 -0500 Received: by smtp.corp.redhat.com (Postfix) id C15105D6DC; Fri, 5 Mar 2021 19:14:24 +0000 (UTC) Received: from harajuku.usersys.redhat.com (unknown [10.40.194.220]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 216F95D6B1 for ; Fri, 5 Mar 2021 19:14:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614971771; 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=sVWULxKy+Fq8rTwVRzuz20BXCQsDfXNbGMi6HbOmncU=; b=JH67GmqiVasjkIqMmNPSX7kRyGNSrzqePImLrvz9qT3lQMHETyPi2FLl+hRBgIEafmSz8q 2d5yW9Ve8qOCQulPdj+K0zrgyBDqd2o1YZ/g7wZomOVu7roclbq9mjxFTUjey/W+cYfn7o j8lTXPkvm+1QGYkD6ZLxImjvxjadkaY= X-MC-Unique: V4136_38MjCI_frqXm3klw-1 From: Andrea Bolognani To: libvir-list@redhat.com Subject: [libvirt PATCH 12/17] util: Try to get limits from /proc Date: Fri, 5 Mar 2021 20:13:59 +0100 Message-Id: <20210305191404.529903-13-abologna@redhat.com> In-Reply-To: <20210305191404.529903-1-abologna@redhat.com> References: <20210305191404.529903-1-abologna@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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.12 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) Content-Type: text/plain; charset="utf-8" Calling prlimit() requires elevated privileges, specifically CAP_SYS_RESOURCE, and getrlimit() only works for the current process which is too limiting for our needs; /proc/$pid/limits, on the other hand, can be read by any process, so implement parsing that file as a fallback for when prlimit() fails. This is useful in containerized environments. Signed-off-by: Andrea Bolognani --- src/util/virprocess.c | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/util/virprocess.c b/src/util/virprocess.c index abce522bf3..e62ec379a6 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -757,6 +757,95 @@ virProcessSetRLimit(int resource, #endif /* WITH_SETRLIMIT */ =20 #if WITH_GETRLIMIT +static const char* +virProcessLimitResourceToLabel(int resource) +{ + switch (resource) { +# if defined(RLIMIT_MEMLOCK) + case RLIMIT_MEMLOCK: + return "Max locked memory"; +# endif /* defined(RLIMIT_MEMLOCK) */ + +# if defined(RLIMIT_NPROC) + case RLIMIT_NPROC: + return "Max processes"; +# endif /* defined(RLIMIT_NPROC) */ + +# if defined(RLIMIT_NOFILE) + case RLIMIT_NOFILE: + return "Max open files"; +# endif /* defined(RLIMIT_NOFILE) */ + +# if defined(RLIMIT_CORE) + case RLIMIT_CORE: + return "Max core file size"; +# endif /* defined(RLIMIT_CORE) */ + + default: + return NULL; + } +} + +static int +virProcessGetLimitFromProc(pid_t pid, + int resource, + struct rlimit *limit) +{ + g_autofree char *procfile =3D NULL; + g_autofree char *buf =3D NULL; + g_auto(GStrv) lines =3D NULL; + const char *label; + size_t len; + size_t i; + + if (!(label =3D virProcessLimitResourceToLabel(resource))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown resource %d requested for process %lld"), + resource, (long long)pid); + return -1; + } + + procfile =3D g_strdup_printf("/proc/%lld/limits", (long long)pid); + + if (!g_file_get_contents(procfile, &buf, &len, NULL)) + return -1; + + if (!(lines =3D g_strsplit(buf, "\n", 0))) + return -1; + + for (i =3D 0; lines[i]; i++) { + g_autofree char *softLimit =3D NULL; + g_autofree char *hardLimit =3D NULL; + char *line =3D lines[i]; + unsigned long long tmp; + + if (!STRPREFIX(line, label)) + continue; + + line +=3D strlen(label); + + if (sscanf(line, "%ms %ms %*s", &softLimit, &hardLimit) < 2) + return -1; + + if (STREQ(softLimit, "unlimited")) { + limit->rlim_cur =3D RLIM_INFINITY; + } else { + if (virStrToLong_ull(softLimit, NULL, 10, &tmp) < 0) + return -1; + limit->rlim_cur =3D tmp; + } + if (STREQ(hardLimit, "unlimited")) { + limit->rlim_max =3D RLIM_INFINITY; + } else { + if (virStrToLong_ull(hardLimit, NULL, 10, &tmp) < 0) + return -1; + limit->rlim_max =3D tmp; + } + } + + return 0; +} + static int virProcessGetLimit(pid_t pid, int resource, @@ -768,6 +857,15 @@ virProcessGetLimit(pid_t pid, if (virProcessPrLimit(pid, resource, NULL, old_limit) =3D=3D 0) return 0; =20 + /* For whatever reason, using prlimit() on another process - even + * when it's just to obtain the current limit rather than changing + * it - requires CAP_SYS_RESOURCE, which we might not have in a + * containerized environment; on the other hand, no particular + * permission is needed to poke around /proc, so try that if going + * through the syscall didn't work */ + if (virProcessGetLimitFromProc(pid, resource, old_limit) =3D=3D 0) + return 0; + if (same_process && virProcessGetRLimit(resource, old_limit) =3D=3D 0) return 0; =20 --=20 2.26.2