xref: /illumos-gate/usr/src/lib/libproc/common/pr_open.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include "libproc.h"
35 
36 /*
37  * open() system call -- executed by subject process.
38  */
39 int
40 pr_open(struct ps_prochandle *Pr, const char *filename, int flags, mode_t mode)
41 {
42 	sysret_t rval;			/* return value from open() */
43 	argdes_t argd[3];		/* arg descriptors for open() */
44 	argdes_t *adp;
45 	int error;
46 
47 	if (Pr == NULL)		/* no subject process */
48 		return (open(filename, flags, mode));
49 
50 	adp = &argd[0];		/* filename argument */
51 	adp->arg_value = 0;
52 	adp->arg_object = (void *)filename;
53 	adp->arg_type = AT_BYREF;
54 	adp->arg_inout = AI_INPUT;
55 	adp->arg_size = strlen(filename)+1;
56 
57 	adp++;			/* flags argument */
58 	adp->arg_value = (long)flags;
59 	adp->arg_object = NULL;
60 	adp->arg_type = AT_BYVAL;
61 	adp->arg_inout = AI_INPUT;
62 	adp->arg_size = 0;
63 
64 	adp++;			/* mode argument */
65 	adp->arg_value = (long)mode;
66 	adp->arg_object = NULL;
67 	adp->arg_type = AT_BYVAL;
68 	adp->arg_inout = AI_INPUT;
69 	adp->arg_size = 0;
70 
71 	error = Psyscall(Pr, &rval, SYS_open, 3, &argd[0]);
72 
73 	if (error) {
74 		errno = (error > 0)? error : ENOSYS;
75 		return (-1);
76 	}
77 	return (rval.sys_rval1);
78 }
79 
80 /*
81  * creat() system call -- executed by subject process.
82  */
83 int
84 pr_creat(struct ps_prochandle *Pr, const char *filename, mode_t mode)
85 {
86 	sysret_t rval;			/* return value from creat() */
87 	argdes_t argd[2];		/* arg descriptors for creat() */
88 	argdes_t *adp;
89 	int error;
90 
91 	if (Pr == NULL)		/* no subject process */
92 		return (creat(filename, mode));
93 
94 	adp = &argd[0];		/* filename argument */
95 	adp->arg_value = 0;
96 	adp->arg_object = (void *)filename;
97 	adp->arg_type = AT_BYREF;
98 	adp->arg_inout = AI_INPUT;
99 	adp->arg_size = strlen(filename)+1;
100 
101 	adp++;			/* mode argument */
102 	adp->arg_value = (long)mode;
103 	adp->arg_object = NULL;
104 	adp->arg_type = AT_BYVAL;
105 	adp->arg_inout = AI_INPUT;
106 	adp->arg_size = 0;
107 
108 	error = Psyscall(Pr, &rval, SYS_creat, 2, &argd[0]);
109 
110 	if (error) {
111 		errno = (error > 0)? error : ENOSYS;
112 		return (-1);
113 	}
114 	return (rval.sys_rval1);
115 }
116 
117 /*
118  * close() system call -- executed by subject process.
119  */
120 int
121 pr_close(struct ps_prochandle *Pr, int fd)
122 {
123 	sysret_t rval;			/* return value from close() */
124 	argdes_t argd[1];		/* arg descriptors for close() */
125 	argdes_t *adp;
126 	int error;
127 
128 	if (Pr == NULL)		/* no subject process */
129 		return (close(fd));
130 
131 	adp = &argd[0];		/* fd argument */
132 	adp->arg_value = (int)fd;
133 	adp->arg_object = NULL;
134 	adp->arg_type = AT_BYVAL;
135 	adp->arg_inout = AI_INPUT;
136 	adp->arg_size = 0;
137 
138 	error = Psyscall(Pr, &rval, SYS_close, 1, &argd[0]);
139 
140 	if (error) {
141 		errno = (error > 0)? error : ENOSYS;
142 		return (-1);
143 	}
144 	return (rval.sys_rval1);
145 }
146 
147 /*
148  * access() system call -- executed by subject process.
149  */
150 int
151 pr_access(struct ps_prochandle *Pr, const char *path, int amode)
152 {
153 	sysret_t rval;			/* return from access() */
154 	argdes_t argd[2];		/* arg descriptors for access() */
155 	argdes_t *adp;
156 	int err;
157 
158 	if (Pr == NULL)		/* no subject process */
159 		return (access(path, amode));
160 
161 	adp = &argd[0];		/* path argument */
162 	adp->arg_value = 0;
163 	adp->arg_object = (void *)path;
164 	adp->arg_type = AT_BYREF;
165 	adp->arg_inout = AI_INPUT;
166 	adp->arg_size = strlen(path) + 1;
167 
168 	adp++;			/* amode argument */
169 	adp->arg_value = (long)amode;
170 	adp->arg_object = NULL;
171 	adp->arg_type = AT_BYVAL;
172 	adp->arg_inout = AI_INPUT;
173 	adp->arg_size = 0;
174 
175 	err = Psyscall(Pr, &rval, SYS_access, 2, &argd[0]);
176 
177 	if (err) {
178 		errno = (err > 0) ? err : ENOSYS;
179 		return (-1);
180 	}
181 
182 	return (rval.sys_rval1);
183 }
184