xref: /onnv-gate/usr/src/lib/libproc/common/pr_open.c (revision 11798:1e7f1f154004)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11798SRoger.Faulkner@Sun.COM  * Common Development and Distribution License (the "License").
6*11798SRoger.Faulkner@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*11798SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
23*11798SRoger.Faulkner@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24*11798SRoger.Faulkner@Sun.COM  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include "libproc.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * open() system call -- executed by subject process.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate int
pr_open(struct ps_prochandle * Pr,const char * filename,int flags,mode_t mode)380Sstevel@tonic-gate pr_open(struct ps_prochandle *Pr, const char *filename, int flags, mode_t mode)
390Sstevel@tonic-gate {
40*11798SRoger.Faulkner@Sun.COM 	sysret_t rval;			/* return value from openat() */
41*11798SRoger.Faulkner@Sun.COM 	argdes_t argd[4];		/* arg descriptors for openat() */
420Sstevel@tonic-gate 	argdes_t *adp;
430Sstevel@tonic-gate 	int error;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	if (Pr == NULL)		/* no subject process */
460Sstevel@tonic-gate 		return (open(filename, flags, mode));
470Sstevel@tonic-gate 
48*11798SRoger.Faulkner@Sun.COM 	adp = &argd[0];		/* AT_FDCWD argument */
49*11798SRoger.Faulkner@Sun.COM 	adp->arg_value = AT_FDCWD;
50*11798SRoger.Faulkner@Sun.COM 	adp->arg_object = NULL;
51*11798SRoger.Faulkner@Sun.COM 	adp->arg_type = AT_BYVAL;
52*11798SRoger.Faulkner@Sun.COM 	adp->arg_inout = AI_INPUT;
53*11798SRoger.Faulkner@Sun.COM 	adp->arg_size = 0;
54*11798SRoger.Faulkner@Sun.COM 
55*11798SRoger.Faulkner@Sun.COM 	adp++;			/* filename argument */
560Sstevel@tonic-gate 	adp->arg_value = 0;
570Sstevel@tonic-gate 	adp->arg_object = (void *)filename;
580Sstevel@tonic-gate 	adp->arg_type = AT_BYREF;
590Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
600Sstevel@tonic-gate 	adp->arg_size = strlen(filename)+1;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	adp++;			/* flags argument */
630Sstevel@tonic-gate 	adp->arg_value = (long)flags;
640Sstevel@tonic-gate 	adp->arg_object = NULL;
650Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
660Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
670Sstevel@tonic-gate 	adp->arg_size = 0;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	adp++;			/* mode argument */
700Sstevel@tonic-gate 	adp->arg_value = (long)mode;
710Sstevel@tonic-gate 	adp->arg_object = NULL;
720Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
730Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
740Sstevel@tonic-gate 	adp->arg_size = 0;
750Sstevel@tonic-gate 
76*11798SRoger.Faulkner@Sun.COM 	error = Psyscall(Pr, &rval, SYS_openat, 4, &argd[0]);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (error) {
790Sstevel@tonic-gate 		errno = (error > 0)? error : ENOSYS;
800Sstevel@tonic-gate 		return (-1);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	return (rval.sys_rval1);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * creat() system call -- executed by subject process.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate int
pr_creat(struct ps_prochandle * Pr,const char * filename,mode_t mode)890Sstevel@tonic-gate pr_creat(struct ps_prochandle *Pr, const char *filename, mode_t mode)
900Sstevel@tonic-gate {
91*11798SRoger.Faulkner@Sun.COM 	sysret_t rval;			/* return value from openat() */
92*11798SRoger.Faulkner@Sun.COM 	argdes_t argd[4];		/* arg descriptors for openat() */
930Sstevel@tonic-gate 	argdes_t *adp;
940Sstevel@tonic-gate 	int error;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (Pr == NULL)		/* no subject process */
970Sstevel@tonic-gate 		return (creat(filename, mode));
980Sstevel@tonic-gate 
99*11798SRoger.Faulkner@Sun.COM 	adp = &argd[0];		/* AT_FDCWD argument */
100*11798SRoger.Faulkner@Sun.COM 	adp->arg_value = AT_FDCWD;
101*11798SRoger.Faulkner@Sun.COM 	adp->arg_object = NULL;
102*11798SRoger.Faulkner@Sun.COM 	adp->arg_type = AT_BYVAL;
103*11798SRoger.Faulkner@Sun.COM 	adp->arg_inout = AI_INPUT;
104*11798SRoger.Faulkner@Sun.COM 	adp->arg_size = 0;
105*11798SRoger.Faulkner@Sun.COM 
106*11798SRoger.Faulkner@Sun.COM 	adp++;			/* filename argument */
1070Sstevel@tonic-gate 	adp->arg_value = 0;
1080Sstevel@tonic-gate 	adp->arg_object = (void *)filename;
1090Sstevel@tonic-gate 	adp->arg_type = AT_BYREF;
1100Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
1110Sstevel@tonic-gate 	adp->arg_size = strlen(filename)+1;
1120Sstevel@tonic-gate 
113*11798SRoger.Faulkner@Sun.COM 	adp++;			/* flags argument */
114*11798SRoger.Faulkner@Sun.COM 	adp->arg_value = (O_WRONLY | O_CREAT | O_TRUNC);
115*11798SRoger.Faulkner@Sun.COM 	adp->arg_object = NULL;
116*11798SRoger.Faulkner@Sun.COM 	adp->arg_type = AT_BYVAL;
117*11798SRoger.Faulkner@Sun.COM 	adp->arg_inout = AI_INPUT;
118*11798SRoger.Faulkner@Sun.COM 	adp->arg_size = 0;
119*11798SRoger.Faulkner@Sun.COM 
1200Sstevel@tonic-gate 	adp++;			/* mode argument */
1210Sstevel@tonic-gate 	adp->arg_value = (long)mode;
1220Sstevel@tonic-gate 	adp->arg_object = NULL;
1230Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
1240Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
1250Sstevel@tonic-gate 	adp->arg_size = 0;
1260Sstevel@tonic-gate 
127*11798SRoger.Faulkner@Sun.COM 	error = Psyscall(Pr, &rval, SYS_openat, 4, &argd[0]);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (error) {
1300Sstevel@tonic-gate 		errno = (error > 0)? error : ENOSYS;
1310Sstevel@tonic-gate 		return (-1);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	return (rval.sys_rval1);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * close() system call -- executed by subject process.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate int
pr_close(struct ps_prochandle * Pr,int fd)1400Sstevel@tonic-gate pr_close(struct ps_prochandle *Pr, int fd)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	sysret_t rval;			/* return value from close() */
1430Sstevel@tonic-gate 	argdes_t argd[1];		/* arg descriptors for close() */
1440Sstevel@tonic-gate 	argdes_t *adp;
1450Sstevel@tonic-gate 	int error;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (Pr == NULL)		/* no subject process */
1480Sstevel@tonic-gate 		return (close(fd));
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	adp = &argd[0];		/* fd argument */
1510Sstevel@tonic-gate 	adp->arg_value = (int)fd;
1520Sstevel@tonic-gate 	adp->arg_object = NULL;
1530Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
1540Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
1550Sstevel@tonic-gate 	adp->arg_size = 0;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	error = Psyscall(Pr, &rval, SYS_close, 1, &argd[0]);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (error) {
1600Sstevel@tonic-gate 		errno = (error > 0)? error : ENOSYS;
1610Sstevel@tonic-gate 		return (-1);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	return (rval.sys_rval1);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * access() system call -- executed by subject process.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate int
pr_access(struct ps_prochandle * Pr,const char * path,int amode)1700Sstevel@tonic-gate pr_access(struct ps_prochandle *Pr, const char *path, int amode)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	sysret_t rval;			/* return from access() */
173*11798SRoger.Faulkner@Sun.COM 	argdes_t argd[4];		/* arg descriptors for access() */
1740Sstevel@tonic-gate 	argdes_t *adp;
1750Sstevel@tonic-gate 	int err;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (Pr == NULL)		/* no subject process */
1780Sstevel@tonic-gate 		return (access(path, amode));
1790Sstevel@tonic-gate 
180*11798SRoger.Faulkner@Sun.COM 	adp = &argd[0];		/* directory fd argument */
181*11798SRoger.Faulkner@Sun.COM 	adp->arg_value = AT_FDCWD;
182*11798SRoger.Faulkner@Sun.COM 	adp->arg_object = NULL;
183*11798SRoger.Faulkner@Sun.COM 	adp->arg_type = AT_BYVAL;
184*11798SRoger.Faulkner@Sun.COM 	adp->arg_inout = AI_INPUT;
185*11798SRoger.Faulkner@Sun.COM 	adp->arg_size = 0;
186*11798SRoger.Faulkner@Sun.COM 
187*11798SRoger.Faulkner@Sun.COM 	adp++;			/* path argument */
1880Sstevel@tonic-gate 	adp->arg_value = 0;
1890Sstevel@tonic-gate 	adp->arg_object = (void *)path;
1900Sstevel@tonic-gate 	adp->arg_type = AT_BYREF;
1910Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
1920Sstevel@tonic-gate 	adp->arg_size = strlen(path) + 1;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	adp++;			/* amode argument */
1950Sstevel@tonic-gate 	adp->arg_value = (long)amode;
1960Sstevel@tonic-gate 	adp->arg_object = NULL;
1970Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
1980Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
1990Sstevel@tonic-gate 	adp->arg_size = 0;
2000Sstevel@tonic-gate 
201*11798SRoger.Faulkner@Sun.COM 	adp++;			/* flag argument */
202*11798SRoger.Faulkner@Sun.COM 	adp->arg_value = 0;
203*11798SRoger.Faulkner@Sun.COM 	adp->arg_object = NULL;
204*11798SRoger.Faulkner@Sun.COM 	adp->arg_type = AT_BYVAL;
205*11798SRoger.Faulkner@Sun.COM 	adp->arg_inout = AI_INPUT;
206*11798SRoger.Faulkner@Sun.COM 	adp->arg_size = 0;
207*11798SRoger.Faulkner@Sun.COM 
208*11798SRoger.Faulkner@Sun.COM 	err = Psyscall(Pr, &rval, SYS_faccessat, 4, &argd[0]);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (err) {
2110Sstevel@tonic-gate 		errno = (err > 0) ? err : ENOSYS;
2120Sstevel@tonic-gate 		return (-1);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	return (rval.sys_rval1);
2160Sstevel@tonic-gate }
217