xref: /freebsd/lib/libutil/cpuset.c (revision 04eeb364d439bec0931f7e73627f59a88bb474f3)
1*04eeb364SBaptiste Daroussin /*
2*04eeb364SBaptiste Daroussin  * Copyright (c) 2007, 2008 	Jeffrey Roberson <jeff@freebsd.org>
3*04eeb364SBaptiste Daroussin  * All rights reserved.
4*04eeb364SBaptiste Daroussin  *
5*04eeb364SBaptiste Daroussin  * Copyright (c) 2008 Nokia Corporation
6*04eeb364SBaptiste Daroussin  * All rights reserved.
7*04eeb364SBaptiste Daroussin  *
8*04eeb364SBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
9*04eeb364SBaptiste Daroussin  * modification, are permitted provided that the following conditions
10*04eeb364SBaptiste Daroussin  * are met:
11*04eeb364SBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
12*04eeb364SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
13*04eeb364SBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
14*04eeb364SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
15*04eeb364SBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
16*04eeb364SBaptiste Daroussin  *
17*04eeb364SBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*04eeb364SBaptiste Daroussin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*04eeb364SBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*04eeb364SBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*04eeb364SBaptiste Daroussin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*04eeb364SBaptiste Daroussin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*04eeb364SBaptiste Daroussin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*04eeb364SBaptiste Daroussin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*04eeb364SBaptiste Daroussin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*04eeb364SBaptiste Daroussin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*04eeb364SBaptiste Daroussin  * SUCH DAMAGE.
28*04eeb364SBaptiste Daroussin  */
29*04eeb364SBaptiste Daroussin 
30*04eeb364SBaptiste Daroussin #include <sys/types.h>
31*04eeb364SBaptiste Daroussin #include <sys/cpuset.h>
32*04eeb364SBaptiste Daroussin 
33*04eeb364SBaptiste Daroussin #include <stdlib.h>
34*04eeb364SBaptiste Daroussin #include <string.h>
35*04eeb364SBaptiste Daroussin #include <libutil.h>
36*04eeb364SBaptiste Daroussin #include <ctype.h>
37*04eeb364SBaptiste Daroussin 
38*04eeb364SBaptiste Daroussin int
cpuset_parselist(const char * list,cpuset_t * mask)39*04eeb364SBaptiste Daroussin cpuset_parselist(const char *list, cpuset_t *mask)
40*04eeb364SBaptiste Daroussin {
41*04eeb364SBaptiste Daroussin 	enum { NONE, NUM, DASH } state;
42*04eeb364SBaptiste Daroussin 	int lastnum;
43*04eeb364SBaptiste Daroussin 	int curnum;
44*04eeb364SBaptiste Daroussin 	const char *l;
45*04eeb364SBaptiste Daroussin 
46*04eeb364SBaptiste Daroussin 	if (strcasecmp(list, "all") == 0) {
47*04eeb364SBaptiste Daroussin 		if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
48*04eeb364SBaptiste Daroussin 		    sizeof(*mask), mask) != 0)
49*04eeb364SBaptiste Daroussin 			return (CPUSET_PARSE_GETAFFINITY);
50*04eeb364SBaptiste Daroussin 		return (CPUSET_PARSE_OK);
51*04eeb364SBaptiste Daroussin 	}
52*04eeb364SBaptiste Daroussin 	state = NONE;
53*04eeb364SBaptiste Daroussin 	curnum = lastnum = 0;
54*04eeb364SBaptiste Daroussin 	for (l = list; *l != '\0';) {
55*04eeb364SBaptiste Daroussin 		if (isdigit(*l)) {
56*04eeb364SBaptiste Daroussin 			curnum = atoi(l);
57*04eeb364SBaptiste Daroussin 			if (curnum > CPU_SETSIZE)
58*04eeb364SBaptiste Daroussin 				return (CPUSET_PARSE_INVALID_CPU);
59*04eeb364SBaptiste Daroussin 			while (isdigit(*l))
60*04eeb364SBaptiste Daroussin 				l++;
61*04eeb364SBaptiste Daroussin 			switch (state) {
62*04eeb364SBaptiste Daroussin 			case NONE:
63*04eeb364SBaptiste Daroussin 				lastnum = curnum;
64*04eeb364SBaptiste Daroussin 				state = NUM;
65*04eeb364SBaptiste Daroussin 				break;
66*04eeb364SBaptiste Daroussin 			case DASH:
67*04eeb364SBaptiste Daroussin 				for (; lastnum <= curnum; lastnum++)
68*04eeb364SBaptiste Daroussin 					CPU_SET(lastnum, mask);
69*04eeb364SBaptiste Daroussin 				state = NONE;
70*04eeb364SBaptiste Daroussin 				break;
71*04eeb364SBaptiste Daroussin 			case NUM:
72*04eeb364SBaptiste Daroussin 			default:
73*04eeb364SBaptiste Daroussin 				goto parserr;
74*04eeb364SBaptiste Daroussin 			}
75*04eeb364SBaptiste Daroussin 			continue;
76*04eeb364SBaptiste Daroussin 		}
77*04eeb364SBaptiste Daroussin 		switch (*l) {
78*04eeb364SBaptiste Daroussin 		case ',':
79*04eeb364SBaptiste Daroussin 			switch (state) {
80*04eeb364SBaptiste Daroussin 			case NONE:
81*04eeb364SBaptiste Daroussin 				break;
82*04eeb364SBaptiste Daroussin 			case NUM:
83*04eeb364SBaptiste Daroussin 				CPU_SET(curnum, mask);
84*04eeb364SBaptiste Daroussin 				state = NONE;
85*04eeb364SBaptiste Daroussin 				break;
86*04eeb364SBaptiste Daroussin 			case DASH:
87*04eeb364SBaptiste Daroussin 				goto parserr;
88*04eeb364SBaptiste Daroussin 				break;
89*04eeb364SBaptiste Daroussin 			}
90*04eeb364SBaptiste Daroussin 			break;
91*04eeb364SBaptiste Daroussin 		case '-':
92*04eeb364SBaptiste Daroussin 			if (state != NUM)
93*04eeb364SBaptiste Daroussin 				goto parserr;
94*04eeb364SBaptiste Daroussin 			state = DASH;
95*04eeb364SBaptiste Daroussin 			break;
96*04eeb364SBaptiste Daroussin 		default:
97*04eeb364SBaptiste Daroussin 			goto parserr;
98*04eeb364SBaptiste Daroussin 		}
99*04eeb364SBaptiste Daroussin 		l++;
100*04eeb364SBaptiste Daroussin 	}
101*04eeb364SBaptiste Daroussin 	switch (state) {
102*04eeb364SBaptiste Daroussin 		case NONE:
103*04eeb364SBaptiste Daroussin 			break;
104*04eeb364SBaptiste Daroussin 		case NUM:
105*04eeb364SBaptiste Daroussin 			CPU_SET(curnum, mask);
106*04eeb364SBaptiste Daroussin 			break;
107*04eeb364SBaptiste Daroussin 		case DASH:
108*04eeb364SBaptiste Daroussin 			goto parserr;
109*04eeb364SBaptiste Daroussin 	}
110*04eeb364SBaptiste Daroussin 	return (CPUSET_PARSE_OK);
111*04eeb364SBaptiste Daroussin parserr:
112*04eeb364SBaptiste Daroussin 	return (CPUSET_PARSE_ERROR);
113*04eeb364SBaptiste Daroussin }
114