xref: /titanic_50/usr/src/lib/libc/port/gen/priocntl.c (revision 6dd841390b98241b22857b3e09e7720492884621)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 #include "synonyms.h"
33 #include <sys/types.h>
34 #include <sys/procset.h>
35 #include <sys/priocntl.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 
39 /*
40  * The declarations of __priocntlset() and __priocntl() were in prior releases
41  * in <sys/priocntl.h>.  They are used to define PC_VERSION at compile time,
42  * based on the contents of the header file.  This behavior is now changed.
43  * Old binaries call __priocntl() and __priocntlset() instead of priocntl()
44  * and priocntlset().  New binaries call priocntl() and priocntlset().
45  */
46 
47 /*
48  * defined in priocntlset.s
49  */
50 extern long __priocntlset(int, procset_t *, int, caddr_t, ...);
51 
52 static int pc_vaargs2parms(va_list valist, pc_vaparms_t *vp);
53 
54 long
55 __priocntl(int pc_version, idtype_t idtype, id_t id, int cmd, caddr_t arg)
56 {
57 	procset_t	procset;
58 
59 	setprocset(&procset, POP_AND, idtype, id, P_ALL, 0);
60 
61 	return (__priocntlset(pc_version, &procset, cmd, arg, 0));
62 }
63 
64 /*
65  * Internally to libc, we call this function rather than priocntl()
66  * when the cmd is not PC_GETXPARMS or PC_SETXPARMS.  We do this
67  * for the sake of calling common code in various places.  One of
68  * these places is in spawn() and spawnp(), where we must not call
69  * any function that is exported from libc while in the child of vfork().
70  */
71 long
72 _private_priocntl(idtype_t idtype, id_t id, int cmd, void *arg)
73 {
74 	extern long _private__priocntlset(int, procset_t *, int, caddr_t, ...);
75 	procset_t procset;
76 
77 	setprocset(&procset, POP_AND, idtype, id, P_ALL, 0);
78 	return (_private__priocntlset(PC_VERSION, &procset, cmd, arg, 0));
79 }
80 
81 
82 /*VARARGS3*/
83 long
84 priocntl(idtype_t idtype, id_t id, int cmd, ...)
85 {
86 	procset_t	procset;
87 	va_list		valist;
88 	pc_vaparms_t	varparms;
89 	caddr_t		arg;
90 	int		error;
91 
92 	setprocset(&procset, POP_AND, idtype, id, P_ALL, 0);
93 
94 	va_start(valist, cmd);
95 	arg = va_arg(valist, caddr_t);
96 
97 	if (cmd != PC_GETXPARMS && cmd != PC_SETXPARMS) {
98 		va_end(valist);
99 		return (__priocntlset(PC_VERSION, &procset, cmd, arg, 0));
100 	}
101 
102 	error = pc_vaargs2parms(valist, &varparms);
103 	va_end(valist);
104 
105 	if (error) {
106 		errno = error;
107 		return (-1);
108 	}
109 
110 	return (__priocntlset(PC_VERSION, &procset, cmd, arg, &varparms));
111 }
112 
113 
114 /*VARARGS2*/
115 long
116 priocntlset(procset_t *procsetp, int cmd, ...)
117 {
118 	va_list		valist;
119 	pc_vaparms_t	varparms;
120 	caddr_t		arg;
121 	int		error;
122 
123 	va_start(valist, cmd);
124 	arg = va_arg(valist, caddr_t);
125 
126 	if (cmd != PC_GETXPARMS && cmd != PC_SETXPARMS) {
127 		va_end(valist);
128 		return (__priocntlset(PC_VERSION, procsetp, cmd, arg, 0));
129 	}
130 
131 	error = pc_vaargs2parms(valist, &varparms);
132 	va_end(valist);
133 
134 	if (error) {
135 		errno = error;
136 		return (-1);
137 	}
138 
139 	return (__priocntlset(PC_VERSION, procsetp, cmd, arg, &varparms));
140 }
141 
142 
143 static int
144 pc_vaargs2parms(va_list valist, pc_vaparms_t *vp)
145 {
146 	pc_vaparm_t	*vpp = &vp->pc_parms[0];
147 	int		key;
148 
149 	for (vp->pc_vaparmscnt = 0;
150 	    (key = va_arg(valist, int)) != PC_KY_NULL; vpp++) {
151 		if (++vp->pc_vaparmscnt > PC_VAPARMCNT)
152 			return (EINVAL);
153 
154 		vpp->pc_key = key;
155 		vpp->pc_parm = va_arg(valist, uintptr_t);
156 	}
157 
158 	return (0);
159 }
160