git: 04eeb364d439 - main - cpuset(3): Move cpuset's parselist function into libutil

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Thu, 01 Jun 2023 21:17:16 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=04eeb364d439bec0931f7e73627f59a88bb474f3

commit 04eeb364d439bec0931f7e73627f59a88bb474f3
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2023-06-01 21:16:01 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
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 <bapt@FreeBSD.org>
+.\" 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 <jeff@freebsd.org>
+ * 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 <sys/types.h>
+#include <sys/cpuset.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <libutil.h>
+#include <ctype.h>
+
+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) */