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