Fix CPU stats percentages formatting
[dragonfly.git] / sys / emulation / linux / linux_uid16.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 2001 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $
90b9818c 27 * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.5 2003/07/26 18:12:40 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_compat.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/proc.h>
35#include <sys/sysproto.h>
36
37#include <machine/../linux/linux.h>
38#include <machine/../linux/linux_proto.h>
39#include <compat/linux/linux_util.h>
40
41DUMMY(setfsuid16);
42DUMMY(setfsgid16);
43DUMMY(getresuid16);
44DUMMY(getresgid16);
45
46#define CAST_NOCHG(x) (x == 0xFFFF) ? -1 : x;
47
48int
41c20dac 49linux_chown16(struct linux_chown16_args *args)
984263bc
MD
50{
51 struct chown_args bsd;
52 caddr_t sg;
90b9818c 53 int error;
984263bc
MD
54
55 sg = stackgap_init();
41c20dac 56 CHECKALTEXIST(&sg, args->path);
984263bc
MD
57
58#ifdef DEBUG
59 if (ldebug(chown16))
60 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid,
61 args->gid);
62#endif
63
64 bsd.path = args->path;
65 bsd.uid = CAST_NOCHG(args->uid);
66 bsd.gid = CAST_NOCHG(args->gid);
90b9818c
MD
67 bsd.lmsg.u.ms_result = 0;
68 error = chown(&bsd);
69 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
70 return(error);
984263bc
MD
71}
72
73int
41c20dac 74linux_lchown16(struct linux_lchown16_args *args)
984263bc
MD
75{
76 struct lchown_args bsd;
77 caddr_t sg;
90b9818c 78 int error;
984263bc
MD
79
80 sg = stackgap_init();
41c20dac 81 CHECKALTEXIST(&sg, args->path);
984263bc
MD
82
83#ifdef DEBUG
84 if (ldebug(lchown16))
85 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid,
86 args->gid);
87#endif
88
89 bsd.path = args->path;
90 bsd.uid = CAST_NOCHG(args->uid);
91 bsd.gid = CAST_NOCHG(args->gid);
90b9818c
MD
92 bsd.lmsg.u.ms_result = 0;
93
94 error = lchown(&bsd);
95 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
96 return(error);
984263bc
MD
97}
98
99int
41c20dac 100linux_setgroups16(struct linux_setgroups16_args *args)
984263bc 101{
41c20dac 102 struct proc *p = curproc;
984263bc
MD
103 struct ucred *newcred, *oldcred;
104 l_gid16_t linux_gidset[NGROUPS];
105 gid_t *bsd_gidset;
106 int ngrp, error;
107
108#ifdef DEBUG
109 if (ldebug(setgroups16))
110 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
111#endif
112
113 ngrp = args->gidsetsize;
114 oldcred = p->p_ucred;
115
116 /*
117 * cr_groups[0] holds egid. Setting the whole set from
118 * the supplied set will cause egid to be changed too.
119 * Keep cr_groups[0] unchanged to prevent that.
120 */
121
dadab5e9 122 if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0)
984263bc
MD
123 return (error);
124
125 if (ngrp >= NGROUPS)
126 return (EINVAL);
127
128 newcred = crdup(oldcred);
129 if (ngrp > 0) {
130 error = copyin((caddr_t)args->gidset, linux_gidset,
131 ngrp * sizeof(l_gid16_t));
132 if (error)
133 return (error);
134
135 newcred->cr_ngroups = ngrp + 1;
136
137 bsd_gidset = newcred->cr_groups;
138 ngrp--;
139 while (ngrp >= 0) {
140 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
141 ngrp--;
142 }
143 }
144 else
145 newcred->cr_ngroups = 1;
146
41c20dac 147 setsugid();
984263bc
MD
148 p->p_ucred = newcred;
149 crfree(oldcred);
150 return (0);
151}
152
153int
41c20dac 154linux_getgroups16(struct linux_getgroups16_args *args)
984263bc 155{
41c20dac 156 struct proc *p = curproc;
984263bc
MD
157 struct ucred *cred;
158 l_gid16_t linux_gidset[NGROUPS];
159 gid_t *bsd_gidset;
160 int bsd_gidsetsz, ngrp, error;
161
162#ifdef DEBUG
163 if (ldebug(getgroups16))
164 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
165#endif
166
167 cred = p->p_ucred;
168 bsd_gidset = cred->cr_groups;
169 bsd_gidsetsz = cred->cr_ngroups - 1;
170
171 /*
172 * cr_groups[0] holds egid. Returning the whole set
173 * here will cause a duplicate. Exclude cr_groups[0]
174 * to prevent that.
175 */
176
177 if ((ngrp = args->gidsetsize) == 0) {
90b9818c 178 args->lmsg.u.ms_result = bsd_gidsetsz;
984263bc
MD
179 return (0);
180 }
181
182 if (ngrp < bsd_gidsetsz)
183 return (EINVAL);
184
185 ngrp = 0;
186 while (ngrp < bsd_gidsetsz) {
187 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
188 ngrp++;
189 }
190
191 error = copyout(linux_gidset, (caddr_t)args->gidset,
192 ngrp * sizeof(l_gid16_t));
193 if (error)
194 return (error);
195
90b9818c 196 args->lmsg.u.ms_result = ngrp;
984263bc
MD
197 return (0);
198}
199
200/*
201 * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
202 * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
203 * are assumed to be preserved. The following lightweight syscalls fixes
204 * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
205 * linux_misc.c
206 *
207 * linux_getgid16() - MP SAFE
208 * linux_getuid16() - MP SAFE
209 */
210
211int
41c20dac 212linux_getgid16(struct linux_getgid16_args *args)
984263bc 213{
41c20dac 214 struct proc *p = curproc;
984263bc 215
90b9818c 216 args->lmsg.u.ms_result = p->p_ucred->cr_rgid;
984263bc
MD
217 return (0);
218}
219
220int
41c20dac 221linux_getuid16(struct linux_getuid16_args *args)
984263bc 222{
41c20dac 223 struct proc *p = curproc;
984263bc 224
90b9818c 225 args->lmsg.u.ms_result = p->p_ucred->cr_ruid;
984263bc
MD
226 return (0);
227}
228
229int
41c20dac 230linux_getegid16(struct linux_getegid16_args *args)
984263bc
MD
231{
232 struct getegid_args bsd;
90b9818c
MD
233 int error;
234
235 bsd.lmsg.u.ms_result = 0;
984263bc 236
90b9818c
MD
237 error = getegid(&bsd);
238 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
239 return(error);
984263bc
MD
240}
241
242int
41c20dac 243linux_geteuid16(struct linux_geteuid16_args *args)
984263bc
MD
244{
245 struct geteuid_args bsd;
90b9818c
MD
246 int error;
247
248 bsd.lmsg.u.ms_result = 0;
984263bc 249
90b9818c
MD
250 error = geteuid(&bsd);
251 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
252 return(error);
984263bc
MD
253}
254
255int
41c20dac 256linux_setgid16(struct linux_setgid16_args *args)
984263bc
MD
257{
258 struct setgid_args bsd;
90b9818c 259 int error;
984263bc
MD
260
261 bsd.gid = args->gid;
90b9818c
MD
262 bsd.lmsg.u.ms_result = 0;
263
264 error = setgid(&bsd);
265 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
266 return(error);
984263bc
MD
267}
268
269int
41c20dac 270linux_setuid16(struct linux_setuid16_args *args)
984263bc
MD
271{
272 struct setuid_args bsd;
90b9818c 273 int error;
984263bc
MD
274
275 bsd.uid = args->uid;
90b9818c
MD
276 bsd.lmsg.u.ms_result = 0;
277
278 error = setuid(&bsd);
279 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
280 return(error);
984263bc
MD
281}
282
283int
41c20dac 284linux_setregid16(struct linux_setregid16_args *args)
984263bc
MD
285{
286 struct setregid_args bsd;
90b9818c 287 int error;
984263bc
MD
288
289 bsd.rgid = CAST_NOCHG(args->rgid);
290 bsd.egid = CAST_NOCHG(args->egid);
90b9818c
MD
291 bsd.lmsg.u.ms_result = 0;
292
293 error = setregid(&bsd);
294 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
295 return(error);
984263bc
MD
296}
297
298int
41c20dac 299linux_setreuid16(struct linux_setreuid16_args *args)
984263bc
MD
300{
301 struct setreuid_args bsd;
90b9818c 302 int error;
984263bc
MD
303
304 bsd.ruid = CAST_NOCHG(args->ruid);
305 bsd.euid = CAST_NOCHG(args->euid);
90b9818c
MD
306 bsd.lmsg.u.ms_result = 0;
307
308 error = setreuid(&bsd);
309 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
310 return(error);
984263bc
MD
311}
312
313int
41c20dac 314linux_setresgid16(struct linux_setresgid16_args *args)
984263bc
MD
315{
316 struct setresgid_args bsd;
90b9818c 317 int error;
984263bc
MD
318
319 bsd.rgid = CAST_NOCHG(args->rgid);
320 bsd.egid = CAST_NOCHG(args->egid);
321 bsd.sgid = CAST_NOCHG(args->sgid);
90b9818c
MD
322 bsd.lmsg.u.ms_result = 0;
323
324 error = setresgid(&bsd);
325 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
326 return(error);
984263bc
MD
327}
328
329int
41c20dac 330linux_setresuid16(struct linux_setresuid16_args *args)
984263bc
MD
331{
332 struct setresuid_args bsd;
90b9818c 333 int error;
984263bc
MD
334
335 bsd.ruid = CAST_NOCHG(args->ruid);
336 bsd.euid = CAST_NOCHG(args->euid);
337 bsd.suid = CAST_NOCHG(args->suid);
90b9818c
MD
338 bsd.lmsg.u.ms_result = 0;
339
340 error = setresuid(&bsd);
341 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
342 return(error);
984263bc 343}
90b9818c 344