xref: /titanic_41/usr/src/lib/libproc/common/pr_getitimer.c (revision ed5289f91b9bf164dccd6c75398362be77a4478d)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/isa_defs.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include "libproc.h"
34 
35 /*
36  * getitimer() system call -- executed by victim process.
37  */
38 int
39 pr_getitimer(struct ps_prochandle *Pr, int which, struct itimerval *itv)
40 {
41 	sysret_t rval;		/* return value from getitimer() */
42 	argdes_t argd[2];	/* arg descriptors for getitimer() */
43 	argdes_t *adp;
44 	int error;
45 #ifdef _LP64
46 	int victim32 = (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32);
47 	struct itimerval32 itimerval32;
48 #endif
49 
50 	if (Pr == NULL)		/* no victim process */
51 		return (getitimer(which, itv));
52 
53 	adp = &argd[0];		/* which argument */
54 	adp->arg_value = which;
55 	adp->arg_type = AT_BYVAL;
56 	adp->arg_inout = AI_INPUT;
57 	adp->arg_object = NULL;
58 	adp->arg_size = 0;
59 
60 	adp++;			/* itv argument */
61 	adp->arg_value = 0;
62 	adp->arg_type = AT_BYREF;
63 	adp->arg_inout = AI_OUTPUT;
64 #ifdef _LP64
65 	if (victim32) {
66 		adp->arg_object = &itimerval32;
67 		adp->arg_size = sizeof (itimerval32);
68 	} else {
69 		adp->arg_object = itv;
70 		adp->arg_size = sizeof (*itv);
71 	}
72 #else	/* _LP64 */
73 	adp->arg_object = itv;
74 	adp->arg_size = sizeof (*itv);
75 #endif	/* _LP64 */
76 
77 	error = Psyscall(Pr, &rval, SYS_getitimer, 2, &argd[0]);
78 
79 	if (error) {
80 		errno = (error > 0)? error : ENOSYS;
81 		return (-1);
82 	}
83 #ifdef _LP64
84 	if (victim32) {
85 		ITIMERVAL32_TO_ITIMERVAL(itv, &itimerval32);
86 	}
87 #endif	/* _LP64 */
88 	return (rval.sys_rval1);
89 }
90 
91 /*
92  * setitimer() system call -- executed by victim process.
93  */
94 int
95 pr_setitimer(struct ps_prochandle *Pr,
96 	int which, const struct itimerval *itv, struct itimerval *oitv)
97 {
98 	sysret_t rval;		/* return value from setitimer() */
99 	argdes_t argd[3];	/* arg descriptors for setitimer() */
100 	argdes_t *adp;
101 	int error;
102 #ifdef _LP64
103 	int victim32 = (Pstatus(Pr)->pr_dmodel == PR_MODEL_ILP32);
104 	struct itimerval32 itimerval32;
105 	struct itimerval32 oitimerval32;
106 #endif	/* _LP64 */
107 
108 	if (Pr == NULL)		/* no victim process */
109 		return (setitimer(which, (struct itimerval *)itv, oitv));
110 
111 	adp = &argd[0];		/* which argument */
112 	adp->arg_value = which;
113 	adp->arg_type = AT_BYVAL;
114 	adp->arg_inout = AI_INPUT;
115 	adp->arg_object = NULL;
116 	adp->arg_size = 0;
117 
118 	adp++;			/* itv argument */
119 	adp->arg_value = 0;
120 	adp->arg_type = AT_BYREF;
121 	adp->arg_inout = AI_INPUT;
122 #ifdef _LP64
123 	if (victim32) {
124 		ITIMERVAL_TO_ITIMERVAL32(&itimerval32, itv);
125 		adp->arg_object = (void *)&itimerval32;
126 		adp->arg_size = sizeof (itimerval32);
127 	} else {
128 		adp->arg_object = (void *)itv;
129 		adp->arg_size = sizeof (*itv);
130 	}
131 #else	/* _LP64 */
132 	adp->arg_object = (void *)itv;
133 	adp->arg_size = sizeof (*itv);
134 #endif	/* _LP64 */
135 
136 	adp++;			/* oitv argument */
137 	adp->arg_value = 0;
138 	if (oitv == NULL) {
139 		adp->arg_type = AT_BYVAL;
140 		adp->arg_inout = AI_INPUT;
141 		adp->arg_object = NULL;
142 		adp->arg_size = 0;
143 	} else {
144 		adp->arg_type = AT_BYREF;
145 		adp->arg_inout = AI_OUTPUT;
146 #ifdef _LP64
147 		if (victim32) {
148 			adp->arg_object = (void *)&oitimerval32;
149 			adp->arg_size = sizeof (oitimerval32);
150 		} else {
151 			adp->arg_object = oitv;
152 			adp->arg_size = sizeof (*oitv);
153 		}
154 #else	/* _LP64 */
155 		adp->arg_object = oitv;
156 		adp->arg_size = sizeof (*oitv);
157 #endif	/* _LP64 */
158 	}
159 
160 	error = Psyscall(Pr, &rval, SYS_setitimer, 3, &argd[0]);
161 
162 	if (error) {
163 		errno = (error > 0)? error : ENOSYS;
164 		return (-1);
165 	}
166 #ifdef _LP64
167 	if (victim32 && oitv != NULL) {
168 		ITIMERVAL32_TO_ITIMERVAL(oitv, &oitimerval32);
169 	}
170 #endif	/* _LP64 */
171 	return (rval.sys_rval1);
172 }
173