merge with calder
[unix-history] / usr / src / sys / kern / kern_prot.c
CommitLineData
4147b3f6 1/* kern_prot.c 5.3 82/07/24 */
a05af100
BJ
2
3/*
4147b3f6 4 * System calls related to processes and protection
a05af100
BJ
5 */
6
4147b3f6
BJ
7/* NEED ALLOCATION AND PROTECTION MECHANISM FOR PROCESS GROUPS */
8
a05af100
BJ
9#include "../h/param.h"
10#include "../h/systm.h"
11#include "../h/dir.h"
12#include "../h/user.h"
13#include "../h/reg.h"
14#include "../h/inode.h"
15#include "../h/proc.h"
16#include "../h/clock.h"
17#include "../h/mtpr.h"
18#include "../h/timeb.h"
19#include "../h/times.h"
20#include "../h/reboot.h"
21#include "../h/fs.h"
22#include "../h/conf.h"
23#include "../h/buf.h"
24#include "../h/mount.h"
57e12479 25#include "../h/quota.h"
a05af100 26
4147b3f6
BJ
27getpid()
28{
29
30 u.u_r.r_val1 = u.u_procp->p_pid;
31 u.u_r.r_val2 = u.u_procp->p_ppid;
32}
33
34getpgrp()
35{
36 register struct a {
37 int pid;
38 } *uap = (struct a *)u.u_ap;
39 register struct proc *p;
40
41 if (uap->pid == 0)
42 uap->pid = u.u_procp->p_pid;
43 p = pfind(uap->pid);
44 if (p == 0) {
45 u.u_error = ESRCH;
46 return;
47 }
48 u.u_r.r_val1 = p->p_pgrp;
49}
50
a05af100
BJ
51getuid()
52{
53
54 u.u_r.r_val1 = u.u_ruid;
55 u.u_r.r_val2 = u.u_uid;
56}
57
4147b3f6
BJ
58getgid()
59{
60
61 u.u_r.r_val1 = u.u_rgid;
62 u.u_r.r_val2 = u.u_gid;
63}
64
65getgrp()
66{
67 register struct a {
68 int *gidset;
69 } *uap = (struct a *)u.u_ap;
70
71 if (copyout((caddr_t)u.u_grps, (caddr_t)uap->gidset,
72 sizeof (u.u_grps))) {
73 u.u_error = EFAULT;
74 return;
75 }
76}
77
78setpgrp()
79{
80 register struct proc *p;
81 register struct a {
82 int pid;
83 int pgrp;
84 } *uap = (struct a *)u.u_ap;
85
86 if (uap->pid == 0)
87 uap->pid = u.u_procp->p_pid;
88 p = pfind(uap->pid);
89 if (p == 0) {
90 u.u_error = ESRCH;
91 return;
92 }
93 if (p->p_uid != u.u_uid && u.u_uid && !inferior(p)) {
94 u.u_error = EPERM;
95 return;
96 }
97 p->p_pgrp = uap->pgrp;
98}
99
a05af100
BJ
100setuid()
101{
102 register uid;
103 register struct a {
104 int uid;
105 } *uap;
106
107 uap = (struct a *)u.u_ap;
108 uid = uap->uid;
109 if (u.u_ruid == uid || u.u_uid == uid || suser()) {
57e12479
RE
110#ifdef QUOTA
111 if (u.u_quota->q_uid != uid) {
112 qclean();
113 qstart(getquota(uid, 0, 0));
114 }
115#endif
a05af100
BJ
116 u.u_uid = uid;
117 u.u_procp->p_uid = uid;
118 u.u_ruid = uid;
119 }
120}
121
a05af100
BJ
122setgid()
123{
124 register gid;
125 register struct a {
126 int gid;
127 } *uap;
128
129 uap = (struct a *)u.u_ap;
130 gid = uap->gid;
131 if (u.u_rgid == gid || u.u_gid == gid || suser()) {
132 u.u_gid = gid;
133 u.u_rgid = gid;
134 }
135}
4147b3f6
BJ
136
137setgrp()
138{
139 register struct a {
140 int *gidset;
141 } *uap = (struct a *)u.u_ap;
142
143 if (suser())
144 return;
145 if (copyin((caddr_t)uap->gidset, (caddr_t)u.u_grps,
146 sizeof (u.u_grps))) {
147 u.u_error = EFAULT;
148 return;
149 }
150}
151
152/* BEGIN DEFUNCT */
153osetgrp()
154{
155 register struct a {
156 int *ngrps;
157 int *ogrps;
158 } *uap = (struct a *)u.u_ap;
159 int thegroups[NGRPS/(sizeof(int)*8)];
160
161 if (uap->ogrps && copyout((caddr_t)u.u_grps, (caddr_t)uap->ogrps,
162 sizeof (thegroups))) {
163 u.u_error = EFAULT;
164 return;
165 }
166 if (uap->ngrps == 0)
167 return;
168 if (copyin((caddr_t)uap->ngrps, (caddr_t)thegroups,
169 sizeof (thegroups))) {
170 u.u_error = EFAULT;
171 return;
172 }
173 if (suser())
174 bcopy((caddr_t)thegroups, (caddr_t)u.u_grps, sizeof (u.u_grps));
175}
176
177/*
178 * Pid of zero implies current process.
179 * Pgrp -1 is getpgrp system call returning
180 * current process group.
181 */
182osetpgrp()
183{
184 register struct proc *p;
185 register struct a {
186 int pid;
187 int pgrp;
188 } *uap;
189
190 uap = (struct a *)u.u_ap;
191 if (uap->pid == 0)
192 p = u.u_procp;
193 else {
194 p = pfind(uap->pid);
195 if (p == 0) {
196 u.u_error = ESRCH;
197 return;
198 }
199 }
200 if (uap->pgrp <= 0) {
201 u.u_r.r_val1 = p->p_pgrp;
202 return;
203 }
204 if (p->p_uid != u.u_uid && u.u_uid && !inferior(p)) {
205 u.u_error = EPERM;
206 return;
207 }
208 p->p_pgrp = uap->pgrp;
209}
210/* END DEFUNCT */