From nobody Thu Jun 1 21:17:16 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QXJqd1hmMz4YGcq; Thu, 1 Jun 2023 21:17:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QXJqd1DBBz4D7Z; Thu, 1 Jun 2023 21:17:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1685654237; h=from:from: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; bh=cXlys4Z0mtHOfzhpZubq1I3ZlWWefWsY/+QY3v4UrfM=; b=GNJD5Fr+FG+G7xdmt2f5V545Gm6b2mxMTffoxjwRJcixLm1S6u9Np3aJVZ4E21IJhry0hF hjoy9od+heqSQUIgtQd2iGfUwiHiz0ChC/OsBDdFAAKOyAtasdHnN8FQrgBEsfTXGLRaeX 5k8/J4XASyC8smoB3phNtCLsnJ94X9WZr2qQ6bEg30ktNijLm6lxG+wzRhEGpsVgDc3/a9 FezUmPCE4c0J9rmM3UaKO9AfGYfqm/ydlCozIXXR4pXYhSak1J+NPNc2PlyDYSmDMDM1CP NJN/uqjpZ2PD3HDPlqWjD1o/oXnFQhvqvYABXVUaY+ZW/KzHe3KJ3lwxAmKOfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1685654237; h=from:from: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; bh=cXlys4Z0mtHOfzhpZubq1I3ZlWWefWsY/+QY3v4UrfM=; b=rC0HruEOXOCMRYUQH2qE5ZnwvNfmce6vzv8G1czb68Ym+LMsMdjjS7JBrPs+Pb+pmxsP3Q eTpXMDBtae5bTTHhOtg+JI4AGrYJdnGvfbMxvOxpQsbrpqn4Pfk3GTCngXZkOYdqKV6t8C QSLU/i57XdAScmj0R2H+b6HJXnR9d9+XyVgMoyH8f7oIEy0yLtLGT0JGRv+FqyXYxMEtqE GCFh9I8EI+lY+yBUN2j6hecxESIOpfYubWUK5D7LTTWT0zd32X//0g/zV4fk9TvtvcUOaq 6jp+8PSjTvxe/zIeyklbvoT6T5CPntPnuMD8SQlRTtTTnUIF+PB5HmMLehsTcQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1685654237; a=rsa-sha256; cv=none; b=hNLz7aTNYQAc+zvgPVSMPgh2GCTVsuarxoqsnUIWoaBmeUc3AjvRBXZr2a79nRQeLlIFW0 IYyx39/myrMDC1qz/GgFOWYEfgPMvSeB7gpwgFXjTjfg5WbNt0cwHDgo5fJfKTvFatqQfp iqT67vXgZ9okj8YgWgwIm6Cb3mHS3WDTuoORXtZ2qNvyx6azTnT6420bCgTv3IdmVrTKqK 6d1+ee68oiD6j86bMs/3bWVM9qTCbzQpotKRYUBwahzRtggWHVYWP6BMzcZtwInc7Vt0Nt BXV3j/NSf8lUNG0WWLAeU6Ja69hxxEBnGuZPmJf0mQGzG9xokTwMnpkVJScFaw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QXJqd06ZgzY21; Thu, 1 Jun 2023 21:17:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 351LHGqf079245; Thu, 1 Jun 2023 21:17:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 351LHGsJ079244; Thu, 1 Jun 2023 21:17:16 GMT (envelope-from git) Date: Thu, 1 Jun 2023 21:17:16 GMT Message-Id: <202306012117.351LHGsJ079244@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 04eeb364d439 - main - cpuset(3): Move cpuset's parselist function into libutil List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 04eeb364d439bec0931f7e73627f59a88bb474f3 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=04eeb364d439bec0931f7e73627f59a88bb474f3 commit 04eeb364d439bec0931f7e73627f59a88bb474f3 Author: Baptiste Daroussin AuthorDate: 2023-06-01 21:16:01 +0000 Commit: Baptiste Daroussin CommitDate: 2023-06-01 21:16:01 +0000 cpuset(3): Move cpuset's parselist function into libutil In order to allow to add cpuset(2) functionality to more utilities than just cpuset(1) move the parselist code into libutil While here, make the code a little more "library" friendly, by returning a range of various errors so that the consumer can check for them and report appropriate error message to the users (One of the planed usage is the jail(8) utility) Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D12873 --- lib/libutil/Makefile | 5 ++- lib/libutil/cpuset.3 | 80 +++++++++++++++++++++++++++++++++++ lib/libutil/cpuset.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libutil/libutil.h | 8 ++++ 4 files changed, 204 insertions(+), 2 deletions(-) diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index df3fb622b374..dd3d03d748de 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -12,7 +12,7 @@ PACKAGE= runtime LIB= util SHLIB_MAJOR= 9 -SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c \ +SRCS= _secure_path.c auth.c cpuset.c expand_number.c flopen.c fparseln.c \ getlocalbase.c gr_util.c \ hexdump.c humanize_number.c kinfo_getfile.c \ kinfo_getallproc.c kinfo_getproc.c kinfo_getvmmap.c \ @@ -31,7 +31,7 @@ CFLAGS+= -DINET6 CFLAGS+= -I${.CURDIR} -I${SRCTOP}/lib/libc/gen/ -MAN+= expand_number.3 flopen.3 fparseln.3 getlocalbase.3 hexdump.3 \ +MAN+= cpuset.3 expand_number.3 flopen.3 fparseln.3 getlocalbase.3 hexdump.3 \ humanize_number.3 kinfo_getallproc.3 kinfo_getfile.3 \ kinfo_getproc.3 kinfo_getvmmap.3 kinfo_getvmobject.3 kld.3 \ login_auth.3 login_cap.3 \ @@ -88,6 +88,7 @@ MLINKS+=pw_util.3 pw_copy.3 \ pw_util.3 pw_scan.3 \ pw_util.3 pw_tempname.3 \ pw_util.3 pw_tmp.3 +MLINKS+=cpuset.3 cpuset_parselist.3 HAS_TESTS= SUBDIR.${MK_TESTS}+= tests diff --git a/lib/libutil/cpuset.3 b/lib/libutil/cpuset.3 new file mode 100644 index 000000000000..db7edf4bbfb9 --- /dev/null +++ b/lib/libutil/cpuset.3 @@ -0,0 +1,80 @@ +.\" Copyright (c) 2017 Baptiste Daroussin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd October 31, 2017 +.Dt CPUSET 3 +.Os +.Sh NAME +.Nm cpuset_parselist +.Nd utility functions for +.Xr cpuset 2 +handling +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/cpuset.h +.In libutil.h +.Ft int +.Fn cpuset_parselist "const char *cpu-list" "cpuset_t *mask" +.Sh DESCRIPTION +The +.Fn cpuset_parselist +function parses a +.Va cpu-list +filling the +.Va mask . +.Pp +The +.Va cpu-list +may include numbers separated by '-' for ranges and commas separating individual +numbers. +A special list of +.Dq all +may be specified in which case the list includes all CPUs from the root set. +.Sh RETURN VALUES +Return values can be the following +.Bl -tag -width Er +.It Dv CPUSET_PARSE_OK +The parsing was successful +.It Dv CPUSET_PARSE_ERROR +The +.Va cpu-list +format is invalid +.It Dv CPUSET_PARSE_GETAFFINITY +The +.Xr cpuset_getaffinity 2 +call has failed +.It Dv CPUSET_PARSE_INVALID_CPU +The number of supported CPUs has been exceeded. +The maximum number being +.Va CPU_SETSIZE . +.El +.Sh SEE ALSO +.Xr cpuset 1 , +.Xr cpuset 2 , +.Xr cpuset 9 +.Sh AUTHORS +.An Jeffrey Roberson Aq Mt jeff@FreeBSD.org diff --git a/lib/libutil/cpuset.c b/lib/libutil/cpuset.c new file mode 100644 index 000000000000..3c374bfa6cac --- /dev/null +++ b/lib/libutil/cpuset.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2007, 2008 Jeffrey Roberson + * All rights reserved. + * + * Copyright (c) 2008 Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#include +#include +#include +#include + +int +cpuset_parselist(const char *list, cpuset_t *mask) +{ + enum { NONE, NUM, DASH } state; + int lastnum; + int curnum; + const char *l; + + if (strcasecmp(list, "all") == 0) { + if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1, + sizeof(*mask), mask) != 0) + return (CPUSET_PARSE_GETAFFINITY); + return (CPUSET_PARSE_OK); + } + state = NONE; + curnum = lastnum = 0; + for (l = list; *l != '\0';) { + if (isdigit(*l)) { + curnum = atoi(l); + if (curnum > CPU_SETSIZE) + return (CPUSET_PARSE_INVALID_CPU); + while (isdigit(*l)) + l++; + switch (state) { + case NONE: + lastnum = curnum; + state = NUM; + break; + case DASH: + for (; lastnum <= curnum; lastnum++) + CPU_SET(lastnum, mask); + state = NONE; + break; + case NUM: + default: + goto parserr; + } + continue; + } + switch (*l) { + case ',': + switch (state) { + case NONE: + break; + case NUM: + CPU_SET(curnum, mask); + state = NONE; + break; + case DASH: + goto parserr; + break; + } + break; + case '-': + if (state != NUM) + goto parserr; + state = DASH; + break; + default: + goto parserr; + } + l++; + } + switch (state) { + case NONE: + break; + case NUM: + CPU_SET(curnum, mask); + break; + case DASH: + goto parserr; + } + return (CPUSET_PARSE_OK); +parserr: + return (CPUSET_PARSE_ERROR); +} diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index 1ca22ce40e95..be2beefd9284 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -210,6 +210,14 @@ int quota_write_limits(struct quotafile *_qf, struct dqblk *_dqb, int _id); int quota_write_usage(struct quotafile *_qf, struct dqblk *_dqb, int _id); #endif +#ifdef _SYS_CPUSET_H_ +int cpuset_parselist(const char *list, cpuset_t *mask); +#define CPUSET_PARSE_OK 0 +#define CPUSET_PARSE_GETAFFINITY -1 +#define CPUSET_PARSE_ERROR -2 +#define CPUSET_PARSE_INVALID_CPU -3 +#endif + __END_DECLS /* fparseln(3) */