Received: by 2002:a05:7412:e79e:b0:f3:1519:9f41 with SMTP id o30csp257694rdd; Wed, 22 Nov 2023 15:22:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGk2xMm0ZZalOgNT6N3twJol1KgIt1rqGOP9HD7p2t4qyv4La2gA51r2zFNlPwfDjD/nCmS X-Received: by 2002:a05:6a20:560e:b0:188:20e0:2bca with SMTP id ir14-20020a056a20560e00b0018820e02bcamr3062193pzc.13.1700695320508; Wed, 22 Nov 2023 15:22:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700695320; cv=none; d=google.com; s=arc-20160816; b=w5i4aNygvWMseXKeaApteMf28ZwyRA25hBicUClfTI3xinEbRhbPAX63iyHMyIY1A9 LApdcMUY4rmY9ElWNd6y+rpkRu1u9kYckylK1QFBCSXj0jXuFmoAOLXso0j0BXSpjBru fODud1jB23JvKeAWnGKi8k4h8BZsvd72BxYoJ2car7S5GVA+9MJiya9iAAboz7IlLkMA xzVvwtthak1MYbXs2W5Xv34tsJ1VWc3B7bzuFoiI83xH5RyKLr2DjYDnJhqNKsgnnsXF F0U+86nioKifPcM7e6dnPk9uq3+P8cBHAEzYmVkPtlxI1HncW+ufwvqB79toaAuQP5DI Rupw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=5XcgkzDN4xN6ND7d5GdgzgNY/08v0+76TkhtCRqwu9Q=; fh=Qbk0RS4bf6GjbhGkpgFoVgYg5V+1DfWcZ6CAErfO2MQ=; b=G9OZttEZm0v+E/OFLBmLAtY9fnANpyd77Wj0DfeX/Ma9R/JGKoIZxqp8vB9QnO09Ec KE1T/jSYTEVHTw2omhtjY7m/IIK989stfeqjp4d4bh+BEFMbX/Iz1iJnIUhpuTTO1eR/ bJp2MhPNFewWABkZSEHGzMbFYarPqsJtKB3wDMXnzlIqc59byAF8AqZNiM+zoE+PzxVu jFVeCxIMB315pYmA5AH1Q68D9mJCqb7/RlbKqcINYcsgW3yJieJGj55/CGRsR2rN0UYH 7B8tYnXil+7vJRWn+ZYp3PXhZr1Yg/3Q64Pa1la7j7kXgfDlX92i7oRJlIXI+PLy/xkm bNRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=mj3VfFIz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id t7-20020a056a0021c700b006cb70cd9e4dsi53546pfj.143.2023.11.22.15.22.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 15:22:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=mj3VfFIz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A3C848257E28; Wed, 22 Nov 2023 15:21:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344178AbjKVXVi (ORCPT + 99 others); Wed, 22 Nov 2023 18:21:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229576AbjKVXVc (ORCPT ); Wed, 22 Nov 2023 18:21:32 -0500 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AD8C199; Wed, 22 Nov 2023 15:21:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1700695286; bh=+wwLWsOzUhO5DcZAPO5W8gHxG/+RvPZMOuDOlIqHikY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mj3VfFIzMYATJNncnNxf7lCtyL3K6dQoyS9Lbp0oFSdDotwX8gFxppwzH5tB39izR 7KVuXn1tTQLfyQxb2d8h9IgqbnSjjxUB+fv2JK2aQw0BgqyWviBJEqNbp9pM5Ndl5i bbZwcEag6OyEdWBkuZ7XPorboZ5kLmF2XcNlaVYc= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 23 Nov 2023 00:21:26 +0100 Subject: [PATCH 2/3] tools/nolibc: add support for getrlimit/setrlimit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20231123-nolibc-rlimit-v1-2-a428b131de2a@weissschuh.net> References: <20231123-nolibc-rlimit-v1-0-a428b131de2a@weissschuh.net> In-Reply-To: <20231123-nolibc-rlimit-v1-0-a428b131de2a@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1700695285; l=3555; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=+wwLWsOzUhO5DcZAPO5W8gHxG/+RvPZMOuDOlIqHikY=; b=7MH+fFec8xzEZGptGCONnSZDtxVsM+HH4V2Xb+pgNVsICmD0Q+KtMr6Ifj3FNt2kIOQmsu8G0 pjNx/wPYoAiDwH6zFsR6eS08MvGJmNRAl0Bokd1kUCjwBTX07S46Hh8 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 22 Nov 2023 15:21:55 -0800 (PST) The implementation uses the prlimit64 systemcall as that is available on all architectures. Signed-off-by: Thomas Weißschuh --- tools/include/nolibc/sys.h | 38 ++++++++++++++++++++++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 29 +++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 2f359cb03d10..dda9dffd1d74 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -21,6 +21,7 @@ #include /* for O_* and AT_* */ #include /* for statx() */ #include +#include #include "arch.h" #include "errno.h" @@ -898,6 +899,43 @@ int reboot(int cmd) } +/* + * int getrlimit(int resource, struct rlimit *rlim); + * int setrlimit(int resource, const struct rlimit *rlim); + */ + +static __attribute__((unused)) +int sys_prlimit64(pid_t pid, int resource, + const struct rlimit64 *new_limit, struct rlimit64 *old_limit) +{ + return my_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit); +} + +static __attribute__((unused)) +int getrlimit(int resource, struct rlimit *rlim) +{ + struct rlimit64 rlim64; + int ret; + + ret = __sysret(sys_prlimit64(0, resource, NULL, &rlim64)); + rlim->rlim_cur = rlim64.rlim_cur; + rlim->rlim_max = rlim64.rlim_max; + + return ret; +} + +static __attribute__((unused)) +int setrlimit(int resource, const struct rlimit *rlim) +{ + struct rlimit64 rlim64 = { + .rlim_cur = rlim->rlim_cur, + .rlim_max = rlim->rlim_max, + }; + + return __sysret(sys_prlimit64(0, resource, &rlim64, NULL)); +} + + /* * int sched_yield(void); */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 2b71fb5fae4e..d07cebace107 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -839,6 +840,33 @@ int test_pipe(void) return !!memcmp(buf, msg, len); } +int test_rlimit(void) +{ + struct rlimit rlim = { + .rlim_cur = 1 << 20, + .rlim_max = 1 << 20, + }; + int ret; + + ret = setrlimit(RLIMIT_CORE, &rlim); + if (ret) + return -1; + + rlim.rlim_cur = 0; + rlim.rlim_max = 0; + + ret = getrlimit(RLIMIT_CORE, &rlim); + if (ret) + return -1; + + if (rlim.rlim_cur != 1 << 20) + return -1; + if (rlim.rlim_max != 1 << 20) + return -1; + + return 0; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. @@ -929,6 +957,7 @@ int run_syscall(int min, int max) CASE_TEST(poll_fault); EXPECT_SYSER(1, poll(NULL, 1, 0), -1, EFAULT); break; CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break; CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break; + CASE_TEST(rlimit); EXPECT_SYSZR(1, test_rlimit()); break; CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break; CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break; CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; -- 2.43.0