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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "libproc.h"
32 
33 /*
34  * rename() system call -- executed by subject process.
35  */
36 int
37 pr_rename(struct ps_prochandle *Pr, const char *old, const char *new)
38 {
39 	sysret_t rval;
40 	argdes_t argd[4];
41 	argdes_t *adp;
42 	int error;
43 
44 	if (Pr == NULL)
45 		return (rename(old, new));
46 
47 	adp = &argd[0];		/* old fd argument */
48 	adp->arg_value = AT_FDCWD;
49 	adp->arg_object = NULL;
50 	adp->arg_type = AT_BYVAL;
51 	adp->arg_inout = AI_INPUT;
52 	adp->arg_size = 0;
53 
54 	adp++;			/* move to old argument */
55 	adp->arg_value = 0;
56 	adp->arg_object = (void *)old;
57 	adp->arg_type = AT_BYREF;
58 	adp->arg_inout = AI_INPUT;
59 	adp->arg_size = strlen(old) + 1;
60 
61 	adp++;			/* move to new fd argument */
62 	adp->arg_value = AT_FDCWD;
63 	adp->arg_object = NULL;
64 	adp->arg_type = AT_BYVAL;
65 	adp->arg_inout = AI_INPUT;
66 	adp->arg_size = 0;
67 
68 	adp++;			/* move to new argument */
69 	adp->arg_value = 0;
70 	adp->arg_object = (void *)new;
71 	adp->arg_type = AT_BYREF;
72 	adp->arg_inout = AI_INPUT;
73 	adp->arg_size = strlen(new) + 1;
74 
75 	error = Psyscall(Pr, &rval, SYS_renameat, 4, &argd[0]);
76 
77 	if (error) {
78 		errno = (error > 0) ? error : ENOSYS;
79 		return (-1);
80 	}
81 	return (rval.sys_rval1);
82 }
83 
84 /*
85  * link() system call -- executed by subject process.
86  */
87 int
88 pr_link(struct ps_prochandle *Pr, const char *existing, const char *new)
89 {
90 	sysret_t rval;
91 	argdes_t argd[2];
92 	argdes_t *adp;
93 	int error;
94 
95 	if (Pr == NULL)
96 		return (link(existing, new));
97 
98 	adp = &argd[0];		/* existing argument */
99 	adp->arg_value = 0;
100 	adp->arg_object = (void *)existing;
101 	adp->arg_type = AT_BYREF;
102 	adp->arg_inout = AI_INPUT;
103 	adp->arg_size = strlen(existing) + 1;
104 
105 	adp++;			/* new argument */
106 	adp->arg_value = 0;
107 	adp->arg_object = (void *)new;
108 	adp->arg_type = AT_BYREF;
109 	adp->arg_inout = AI_INPUT;
110 	adp->arg_size = strlen(new) + 1;
111 
112 	error = Psyscall(Pr, &rval, SYS_link, 2, &argd[0]);
113 
114 	if (error) {
115 		errno = (error > 0) ? error : ENOSYS;
116 		return (-1);
117 	}
118 	return (rval.sys_rval1);
119 }
120 
121 /*
122  * unlink() system call -- executed by subject process.
123  */
124 int
125 pr_unlink(struct ps_prochandle *Pr, const char *path)
126 {
127 	sysret_t rval;
128 	argdes_t argd[3];
129 	argdes_t *adp;
130 	int error;
131 
132 	if (Pr == NULL)
133 		return (unlink(path));
134 
135 	adp = &argd[0];		/* directory fd argument */
136 	adp->arg_value = AT_FDCWD;
137 	adp->arg_object = NULL;
138 	adp->arg_type = AT_BYVAL;
139 	adp->arg_inout = AI_INPUT;
140 	adp->arg_size = 0;
141 	adp++;			/* move to path argument */
142 
143 	adp->arg_value = 0;
144 	adp->arg_object = (void *)path;
145 	adp->arg_type = AT_BYREF;
146 	adp->arg_inout = AI_INPUT;
147 	adp->arg_size = strlen(path) + 1;
148 	adp++;			/* move to flags argument */
149 
150 	adp->arg_value = 0;
151 	adp->arg_object = NULL;
152 	adp->arg_type = AT_BYVAL;
153 	adp->arg_inout = AI_INPUT;
154 	adp->arg_size = 0;
155 
156 	error = Psyscall(Pr, &rval, SYS_unlinkat, 3, &argd[0]);
157 
158 	if (error) {
159 		errno = (error > 0) ? error : ENOSYS;
160 		return (-1);
161 	}
162 	return (rval.sys_rval1);
163 }
164