xref: /titanic_41/usr/src/lib/libproc/common/pr_lseek.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include "libproc.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate typedef union {
35*7c478bd9Sstevel@tonic-gate 	offset_t	full;		/* full 64 bit offset value */
36*7c478bd9Sstevel@tonic-gate 	uint32_t	half[2];	/* two 32-bit halves */
37*7c478bd9Sstevel@tonic-gate } offsets_t;
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * lseek() system call -- executed by subject process.
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate off_t
pr_lseek(struct ps_prochandle * Pr,int filedes,off_t offset,int whence)43*7c478bd9Sstevel@tonic-gate pr_lseek(struct ps_prochandle *Pr, int filedes, off_t offset, int whence)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	int syscall;		/* SYS_lseek or SYS_llseek */
46*7c478bd9Sstevel@tonic-gate 	int nargs;		/* 3 or 4, depending on syscall */
47*7c478bd9Sstevel@tonic-gate 	offsets_t off;
48*7c478bd9Sstevel@tonic-gate 	sysret_t rval;		/* return value from lseek() */
49*7c478bd9Sstevel@tonic-gate 	argdes_t argd[4];	/* arg descriptors for lseek() */
50*7c478bd9Sstevel@tonic-gate 	argdes_t *adp;
51*7c478bd9Sstevel@tonic-gate 	int error;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	if (Pr == NULL)
54*7c478bd9Sstevel@tonic-gate 		return (lseek(filedes, offset, whence));
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	adp = &argd[0];		/* filedes argument */
57*7c478bd9Sstevel@tonic-gate 	adp->arg_value = filedes;
58*7c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
59*7c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
60*7c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
61*7c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	adp++;			/* offset argument */
64*7c478bd9Sstevel@tonic-gate 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_NATIVE) {
65*7c478bd9Sstevel@tonic-gate 		syscall = SYS_lseek;
66*7c478bd9Sstevel@tonic-gate 		nargs = 3;
67*7c478bd9Sstevel@tonic-gate 		adp->arg_value = offset;
68*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
69*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
70*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
71*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
72*7c478bd9Sstevel@tonic-gate 	} else {
73*7c478bd9Sstevel@tonic-gate 		syscall = SYS_llseek;
74*7c478bd9Sstevel@tonic-gate 		nargs = 4;
75*7c478bd9Sstevel@tonic-gate 		off.full = offset;
76*7c478bd9Sstevel@tonic-gate 		adp->arg_value = off.half[0];	/* first 32 bits */
77*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
78*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
79*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
80*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
81*7c478bd9Sstevel@tonic-gate 		adp++;
82*7c478bd9Sstevel@tonic-gate 		adp->arg_value = off.half[1];	/* second 32 bits */
83*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
84*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
85*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
86*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	adp++;			/* whence argument */
90*7c478bd9Sstevel@tonic-gate 	adp->arg_value = whence;
91*7c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
92*7c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
93*7c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
94*7c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	error = Psyscall(Pr, &rval, syscall, nargs, &argd[0]);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (error) {
99*7c478bd9Sstevel@tonic-gate 		errno = (error > 0)? error : ENOSYS;
100*7c478bd9Sstevel@tonic-gate 		return ((off_t)(-1));
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_NATIVE)
104*7c478bd9Sstevel@tonic-gate 		offset = rval.sys_rval1;
105*7c478bd9Sstevel@tonic-gate 	else {
106*7c478bd9Sstevel@tonic-gate 		off.half[0] = (uint32_t)rval.sys_rval1;
107*7c478bd9Sstevel@tonic-gate 		off.half[1] = (uint32_t)rval.sys_rval2;
108*7c478bd9Sstevel@tonic-gate 		offset = (off_t)off.full;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	return (offset);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * llseek() system call -- executed by subject process.
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate offset_t
pr_llseek(struct ps_prochandle * Pr,int filedes,offset_t offset,int whence)118*7c478bd9Sstevel@tonic-gate pr_llseek(struct ps_prochandle *Pr, int filedes, offset_t offset, int whence)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	int syscall;		/* SYS_lseek or SYS_llseek */
121*7c478bd9Sstevel@tonic-gate 	int nargs;		/* 3 or 4, depending on syscall */
122*7c478bd9Sstevel@tonic-gate 	offsets_t off;
123*7c478bd9Sstevel@tonic-gate 	sysret_t rval;		/* return value from llseek() */
124*7c478bd9Sstevel@tonic-gate 	argdes_t argd[4];	/* arg descriptors for llseek() */
125*7c478bd9Sstevel@tonic-gate 	argdes_t *adp;
126*7c478bd9Sstevel@tonic-gate 	int error;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (Pr == NULL)
129*7c478bd9Sstevel@tonic-gate 		return (llseek(filedes, offset, whence));
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	adp = &argd[0];		/* filedes argument */
132*7c478bd9Sstevel@tonic-gate 	adp->arg_value = filedes;
133*7c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
134*7c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
135*7c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
136*7c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	adp++;			/* offset argument */
139*7c478bd9Sstevel@tonic-gate 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) {
140*7c478bd9Sstevel@tonic-gate 		syscall = SYS_lseek;
141*7c478bd9Sstevel@tonic-gate 		nargs = 3;
142*7c478bd9Sstevel@tonic-gate 		adp->arg_value = offset;
143*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
144*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
145*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
146*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
147*7c478bd9Sstevel@tonic-gate 	} else {
148*7c478bd9Sstevel@tonic-gate 		syscall = SYS_llseek;
149*7c478bd9Sstevel@tonic-gate 		nargs = 4;
150*7c478bd9Sstevel@tonic-gate 		off.full = offset;
151*7c478bd9Sstevel@tonic-gate 		adp->arg_value = off.half[0];	/* first 32 bits */
152*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
153*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
154*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
155*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
156*7c478bd9Sstevel@tonic-gate 		adp++;
157*7c478bd9Sstevel@tonic-gate 		adp->arg_value = off.half[1];	/* second 32 bits */
158*7c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
159*7c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
160*7c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
161*7c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	adp++;			/* whence argument */
165*7c478bd9Sstevel@tonic-gate 	adp->arg_value = whence;
166*7c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
167*7c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
168*7c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
169*7c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	error = Psyscall(Pr, &rval, syscall, nargs, &argd[0]);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (error) {
174*7c478bd9Sstevel@tonic-gate 		errno = (error > 0)? error : ENOSYS;
175*7c478bd9Sstevel@tonic-gate 		return ((offset_t)(-1));
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64)
179*7c478bd9Sstevel@tonic-gate 		offset = rval.sys_rval1;
180*7c478bd9Sstevel@tonic-gate 	else {
181*7c478bd9Sstevel@tonic-gate 		off.half[0] = (uint32_t)rval.sys_rval1;
182*7c478bd9Sstevel@tonic-gate 		off.half[1] = (uint32_t)rval.sys_rval2;
183*7c478bd9Sstevel@tonic-gate 		offset = off.full;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (offset);
187*7c478bd9Sstevel@tonic-gate }
188