xref: /illumos-gate/usr/src/lib/libc/port/gen/priocntl.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "lint.h"
31 #include <sys/types.h>
32 #include <sys/procset.h>
33 #include <sys/priocntl.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 
37 /*
38  * The declarations of __priocntlset() and __priocntl() were in prior releases
39  * in <sys/priocntl.h>.  They are used to define PC_VERSION at compile time,
40  * based on the contents of the header file.  This behavior is now changed.
41  * Old binaries call __priocntl() and __priocntlset() instead of priocntl()
42  * and priocntlset().  New binaries call priocntl() and priocntlset().
43  */
44 
45 /*
46  * defined in priocntlset.s
47  */
48 extern long __priocntlset(int, procset_t *, int, caddr_t, ...);
49 
50 static int pc_vaargs2parms(va_list valist, pc_vaparms_t *vp);
51 
52 long
__priocntl(int pc_version,idtype_t idtype,id_t id,int cmd,caddr_t arg)53 __priocntl(int pc_version, idtype_t idtype, id_t id, int cmd, caddr_t arg)
54 {
55 	procset_t	procset;
56 
57 	setprocset(&procset, POP_AND, idtype, id, P_ALL, 0);
58 
59 	return (__priocntlset(pc_version, &procset, cmd, arg, 0));
60 }
61 
62 
63 /*VARARGS3*/
64 long
priocntl(idtype_t idtype,id_t id,int cmd,...)65 priocntl(idtype_t idtype, id_t id, int cmd, ...)
66 {
67 	procset_t	procset;
68 	va_list		valist;
69 	pc_vaparms_t	varparms;
70 	caddr_t		arg;
71 	int		error;
72 
73 	setprocset(&procset, POP_AND, idtype, id, P_ALL, 0);
74 
75 	va_start(valist, cmd);
76 	arg = va_arg(valist, caddr_t);
77 
78 	if (cmd != PC_GETXPARMS && cmd != PC_SETXPARMS) {
79 		va_end(valist);
80 		return (__priocntlset(PC_VERSION, &procset, cmd, arg, 0));
81 	}
82 
83 	error = pc_vaargs2parms(valist, &varparms);
84 	va_end(valist);
85 
86 	if (error) {
87 		errno = error;
88 		return (-1);
89 	}
90 
91 	return (__priocntlset(PC_VERSION, &procset, cmd, arg, &varparms));
92 }
93 
94 
95 /*VARARGS2*/
96 long
priocntlset(procset_t * procsetp,int cmd,...)97 priocntlset(procset_t *procsetp, int cmd, ...)
98 {
99 	va_list		valist;
100 	pc_vaparms_t	varparms;
101 	caddr_t		arg;
102 	int		error;
103 
104 	va_start(valist, cmd);
105 	arg = va_arg(valist, caddr_t);
106 
107 	if (cmd != PC_GETXPARMS && cmd != PC_SETXPARMS) {
108 		va_end(valist);
109 		return (__priocntlset(PC_VERSION, procsetp, cmd, arg, 0));
110 	}
111 
112 	error = pc_vaargs2parms(valist, &varparms);
113 	va_end(valist);
114 
115 	if (error) {
116 		errno = error;
117 		return (-1);
118 	}
119 
120 	return (__priocntlset(PC_VERSION, procsetp, cmd, arg, &varparms));
121 }
122 
123 
124 static int
pc_vaargs2parms(va_list valist,pc_vaparms_t * vp)125 pc_vaargs2parms(va_list valist, pc_vaparms_t *vp)
126 {
127 	pc_vaparm_t	*vpp = &vp->pc_parms[0];
128 	int		key;
129 
130 	for (vp->pc_vaparmscnt = 0;
131 	    (key = va_arg(valist, int)) != PC_KY_NULL; vpp++) {
132 		if (++vp->pc_vaparmscnt > PC_VAPARMCNT)
133 			return (EINVAL);
134 
135 		vpp->pc_key = key;
136 		vpp->pc_parm = va_arg(valist, uintptr_t);
137 	}
138 
139 	return (0);
140 }
141