xref: /dragonfly/lib/libc/sys/sched_getaffinity.c (revision 35ca622f)
1907281d1SSepherosa Ziehau /*
2907281d1SSepherosa Ziehau  * Copyright (c) 2017 The DragonFly Project.  All rights reserved.
3907281d1SSepherosa Ziehau  *
4907281d1SSepherosa Ziehau  * This code is derived from software contributed to The DragonFly Project
5907281d1SSepherosa Ziehau  * by Sepherosa Ziehau <sepherosa@gmail.com>
6907281d1SSepherosa Ziehau  *
7907281d1SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
8907281d1SSepherosa Ziehau  * modification, are permitted provided that the following conditions
9907281d1SSepherosa Ziehau  * are met:
10907281d1SSepherosa Ziehau  *
11907281d1SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
12907281d1SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
13907281d1SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
14907281d1SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in
15907281d1SSepherosa Ziehau  *    the documentation and/or other materials provided with the
16907281d1SSepherosa Ziehau  *    distribution.
17907281d1SSepherosa Ziehau  * 3. Neither the name of The DragonFly Project nor the names of its
18907281d1SSepherosa Ziehau  *    contributors may be used to endorse or promote products derived
19907281d1SSepherosa Ziehau  *    from this software without specific, prior written permission.
20907281d1SSepherosa Ziehau  *
21907281d1SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22907281d1SSepherosa Ziehau  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23907281d1SSepherosa Ziehau  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24907281d1SSepherosa Ziehau  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25907281d1SSepherosa Ziehau  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26907281d1SSepherosa Ziehau  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27907281d1SSepherosa Ziehau  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28907281d1SSepherosa Ziehau  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29907281d1SSepherosa Ziehau  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30907281d1SSepherosa Ziehau  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31907281d1SSepherosa Ziehau  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32907281d1SSepherosa Ziehau  * SUCH DAMAGE.
33907281d1SSepherosa Ziehau  */
34907281d1SSepherosa Ziehau 
35*0d9899e7SSepherosa Ziehau #include <sys/lwp.h>
36907281d1SSepherosa Ziehau #include <sched.h>
37907281d1SSepherosa Ziehau #include <string.h>
38907281d1SSepherosa Ziehau 
39907281d1SSepherosa Ziehau int
sched_getaffinity(pid_t pid,size_t cpusetsize,cpu_set_t * mask)40907281d1SSepherosa Ziehau sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask)
41907281d1SSepherosa Ziehau {
42178a6112SSepherosa Ziehau 	cpu_set_t mask1;
43178a6112SSepherosa Ziehau 	int ret;
44907281d1SSepherosa Ziehau 
45178a6112SSepherosa Ziehau 	ret = lwp_getaffinity(pid, -1, &mask1);
46178a6112SSepherosa Ziehau 	if (ret < 0)
47178a6112SSepherosa Ziehau 		return (ret);
48178a6112SSepherosa Ziehau 
49178a6112SSepherosa Ziehau 	if (cpusetsize > sizeof(mask1)) {
50907281d1SSepherosa Ziehau 		memset(mask, 0, cpusetsize);
51178a6112SSepherosa Ziehau 		memcpy(mask, &mask1, sizeof(mask1));
52178a6112SSepherosa Ziehau 	} else {
53178a6112SSepherosa Ziehau 		memcpy(mask, &mask1, cpusetsize);
54178a6112SSepherosa Ziehau 	}
55178a6112SSepherosa Ziehau 	return (0);
56907281d1SSepherosa Ziehau }
57