sysv_sem.c revision 1.6 1 1.1 cgd /*
2 1.1 cgd * Implementation of SVID semaphores
3 1.1 cgd *
4 1.1 cgd * Author: Daniel Boulet
5 1.1 cgd *
6 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
7 1.4 mycroft *
8 1.6 mycroft * $Id: sysv_sem.c,v 1.6 1994/02/13 11:31:16 mycroft Exp $
9 1.1 cgd */
10 1.1 cgd
11 1.3 mycroft #include <sys/param.h>
12 1.3 mycroft #include <sys/systm.h>
13 1.3 mycroft #include <sys/kernel.h>
14 1.3 mycroft #include <sys/proc.h>
15 1.3 mycroft #include <sys/sem.h>
16 1.3 mycroft #include <sys/malloc.h>
17 1.1 cgd
18 1.1 cgd static int semctl(), semget(), semop(), semconfig();
19 1.1 cgd int (*semcalls[])() = { semctl, semget, semop, semconfig };
20 1.1 cgd int semtot = 0;
21 1.1 cgd
22 1.1 cgd static struct proc *semlock_holder = NULL;
23 1.1 cgd
24 1.1 cgd int
25 1.1 cgd seminit()
26 1.1 cgd {
27 1.5 mycroft register int i;
28 1.5 mycroft vm_offset_t whocares1, whocares2;
29 1.1 cgd
30 1.5 mycroft if (sema == NULL)
31 1.5 mycroft panic("sema is NULL");
32 1.5 mycroft if (semu == NULL)
33 1.5 mycroft panic("semu is NULL");
34 1.5 mycroft
35 1.5 mycroft for (i = 0; i < seminfo.semmni; i++) {
36 1.5 mycroft sema[i].sem_base = 0;
37 1.5 mycroft sema[i].sem_perm.mode = 0;
38 1.5 mycroft }
39 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
40 1.5 mycroft register struct sem_undo *suptr = SEMU(i);
41 1.5 mycroft suptr->un_proc = NULL;
42 1.5 mycroft }
43 1.5 mycroft semu_list = NULL;
44 1.1 cgd }
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * Entry point for all SEM calls
48 1.1 cgd */
49 1.1 cgd
50 1.1 cgd struct semsys_args {
51 1.1 cgd u_int which;
52 1.1 cgd };
53 1.1 cgd
54 1.1 cgd int
55 1.1 cgd semsys(p, uap, retval)
56 1.1 cgd struct proc *p;
57 1.1 cgd struct semsys_args *uap;
58 1.1 cgd int *retval;
59 1.1 cgd {
60 1.5 mycroft
61 1.4 mycroft while (semlock_holder != NULL && semlock_holder != p)
62 1.5 mycroft sleep((caddr_t)&semlock_holder, (PZERO - 4));
63 1.1 cgd
64 1.1 cgd if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
65 1.1 cgd return (EINVAL);
66 1.1 cgd return ((*semcalls[uap->which])(p, &uap[1], retval));
67 1.1 cgd }
68 1.1 cgd
69 1.1 cgd /*
70 1.1 cgd * Lock or unlock the entire semaphore facility.
71 1.1 cgd *
72 1.1 cgd * This will probably eventually evolve into a general purpose semaphore
73 1.1 cgd * facility status enquiry mechanism (I don't like the "read /dev/kmem"
74 1.1 cgd * approach currently taken by ipcs and the amount of info that we want
75 1.1 cgd * to be able to extract for ipcs is probably beyond what the capability
76 1.1 cgd * of the getkerninfo facility.
77 1.1 cgd *
78 1.1 cgd * At the time that the current version of semconfig was written, ipcs is
79 1.1 cgd * the only user of the semconfig facility. It uses it to ensure that the
80 1.1 cgd * semaphore facility data structures remain static while it fishes around
81 1.1 cgd * in /dev/kmem.
82 1.1 cgd */
83 1.1 cgd
84 1.1 cgd struct semconfig_args {
85 1.1 cgd semconfig_ctl_t flag;
86 1.1 cgd };
87 1.1 cgd
88 1.1 cgd int
89 1.1 cgd semconfig(p, uap, retval)
90 1.1 cgd struct proc *p;
91 1.1 cgd struct semconfig_args *uap;
92 1.1 cgd int *retval;
93 1.1 cgd {
94 1.5 mycroft int eval = 0;
95 1.1 cgd
96 1.5 mycroft switch (uap->flag) {
97 1.5 mycroft case SEM_CONFIG_FREEZE:
98 1.5 mycroft semlock_holder = p;
99 1.5 mycroft break;
100 1.6 mycroft
101 1.5 mycroft case SEM_CONFIG_THAW:
102 1.5 mycroft semlock_holder = NULL;
103 1.5 mycroft wakeup((caddr_t)&semlock_holder);
104 1.5 mycroft break;
105 1.6 mycroft
106 1.5 mycroft default:
107 1.5 mycroft printf("semconfig: unknown flag parameter value (%d) - ignored\n",
108 1.5 mycroft uap->flag);
109 1.5 mycroft eval = EINVAL;
110 1.5 mycroft break;
111 1.5 mycroft }
112 1.1 cgd
113 1.5 mycroft *retval = 0;
114 1.5 mycroft return(eval);
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * Allocate a new sem_undo structure for a process
119 1.1 cgd * (returns ptr to structure or NULL if no more room)
120 1.1 cgd */
121 1.1 cgd
122 1.1 cgd struct sem_undo *
123 1.5 mycroft semu_alloc(p)
124 1.5 mycroft struct proc *p;
125 1.1 cgd {
126 1.5 mycroft register int i;
127 1.5 mycroft register struct sem_undo *suptr;
128 1.5 mycroft register struct sem_undo **supptr;
129 1.5 mycroft int attempt;
130 1.1 cgd
131 1.1 cgd /*
132 1.5 mycroft * Try twice to allocate something.
133 1.5 mycroft * (we'll purge any empty structures after the first pass so
134 1.5 mycroft * two passes are always enough)
135 1.1 cgd */
136 1.1 cgd
137 1.5 mycroft for (attempt = 0; attempt < 2; attempt++) {
138 1.5 mycroft /*
139 1.5 mycroft * Look for a free structure.
140 1.5 mycroft * Fill it in and return it if we find one.
141 1.5 mycroft */
142 1.5 mycroft
143 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
144 1.5 mycroft suptr = SEMU(i);
145 1.5 mycroft if (suptr->un_proc == NULL) {
146 1.5 mycroft suptr->un_next = semu_list;
147 1.5 mycroft semu_list = suptr;
148 1.5 mycroft suptr->un_cnt = 0;
149 1.5 mycroft suptr->un_proc = p;
150 1.5 mycroft return(suptr);
151 1.5 mycroft }
152 1.5 mycroft }
153 1.1 cgd
154 1.5 mycroft /*
155 1.5 mycroft * We didn't find a free one, if this is the first attempt
156 1.5 mycroft * then try to free some structures.
157 1.5 mycroft */
158 1.5 mycroft
159 1.5 mycroft if (attempt == 0) {
160 1.5 mycroft /* All the structures are in use - try to free some */
161 1.5 mycroft int did_something = 0;
162 1.5 mycroft
163 1.5 mycroft supptr = &semu_list;
164 1.5 mycroft while ((suptr = *supptr) != NULL) {
165 1.5 mycroft if (suptr->un_cnt == 0) {
166 1.5 mycroft suptr->un_proc = NULL;
167 1.5 mycroft *supptr = suptr->un_next;
168 1.5 mycroft did_something = 1;
169 1.5 mycroft } else
170 1.5 mycroft supptr = &(suptr->un_next);
171 1.5 mycroft }
172 1.5 mycroft
173 1.5 mycroft /* If we didn't free anything then just give-up */
174 1.5 mycroft if (!did_something)
175 1.5 mycroft return(NULL);
176 1.5 mycroft } else {
177 1.5 mycroft /*
178 1.5 mycroft * The second pass failed even though we freed
179 1.5 mycroft * something after the first pass!
180 1.5 mycroft * This is IMPOSSIBLE!
181 1.5 mycroft */
182 1.5 mycroft panic("semu_alloc - second attempt failed");
183 1.5 mycroft }
184 1.1 cgd }
185 1.1 cgd }
186 1.1 cgd
187 1.1 cgd /*
188 1.1 cgd * Adjust a particular entry for a particular proc
189 1.1 cgd */
190 1.1 cgd
191 1.1 cgd int
192 1.5 mycroft semundo_adjust(p, supptr, semid, semnum, adjval)
193 1.5 mycroft register struct proc *p;
194 1.5 mycroft struct sem_undo **supptr;
195 1.5 mycroft int semid, semnum;
196 1.5 mycroft int adjval;
197 1.1 cgd {
198 1.5 mycroft register struct sem_undo *suptr;
199 1.5 mycroft register struct undo *sunptr;
200 1.5 mycroft int i;
201 1.1 cgd
202 1.5 mycroft /* Look for and remember the sem_undo if the caller doesn't provide
203 1.5 mycroft it */
204 1.1 cgd
205 1.5 mycroft suptr = *supptr;
206 1.4 mycroft if (suptr == NULL) {
207 1.5 mycroft for (suptr = semu_list; suptr != NULL;
208 1.5 mycroft suptr = suptr->un_next) {
209 1.5 mycroft if (suptr->un_proc == p) {
210 1.5 mycroft *supptr = suptr;
211 1.5 mycroft break;
212 1.5 mycroft }
213 1.5 mycroft }
214 1.5 mycroft if (suptr == NULL) {
215 1.5 mycroft if (adjval == 0)
216 1.5 mycroft return(0);
217 1.5 mycroft suptr = semu_alloc(p);
218 1.5 mycroft if (suptr == NULL)
219 1.5 mycroft return(ENOSPC);
220 1.5 mycroft *supptr = suptr;
221 1.5 mycroft }
222 1.1 cgd }
223 1.1 cgd
224 1.6 mycroft /*
225 1.6 mycroft * Look for the requested entry and adjust it (delete if adjval becomes
226 1.6 mycroft * 0).
227 1.6 mycroft */
228 1.6 mycroft sunptr = &suptr->un_ent[0];
229 1.5 mycroft for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
230 1.6 mycroft if (sunptr->un_id != semid || sunptr->un_num != semnum)
231 1.6 mycroft continue;
232 1.6 mycroft if (adjval == 0)
233 1.6 mycroft sunptr->un_adjval = 0;
234 1.6 mycroft else
235 1.6 mycroft sunptr->un_adjval += adjval;
236 1.6 mycroft if (sunptr->un_adjval == 0) {
237 1.6 mycroft suptr->un_cnt--;
238 1.6 mycroft if (i < suptr->un_cnt)
239 1.6 mycroft suptr->un_ent[i] =
240 1.6 mycroft suptr->un_ent[suptr->un_cnt];
241 1.5 mycroft }
242 1.6 mycroft return(0);
243 1.1 cgd }
244 1.1 cgd
245 1.5 mycroft /* Didn't find the right entry - create it */
246 1.5 mycroft if (adjval == 0)
247 1.5 mycroft return(0);
248 1.5 mycroft if (suptr->un_cnt != SEMUME) {
249 1.6 mycroft sunptr = &suptr->un_ent[suptr->un_cnt];
250 1.5 mycroft suptr->un_cnt++;
251 1.5 mycroft sunptr->un_adjval = adjval;
252 1.5 mycroft sunptr->un_id = semid; sunptr->un_num = semnum;
253 1.5 mycroft } else
254 1.5 mycroft return(EINVAL);
255 1.1 cgd return(0);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd void
259 1.5 mycroft semundo_clear(semid, semnum)
260 1.5 mycroft int semid, semnum;
261 1.1 cgd {
262 1.6 mycroft register struct sem_undo *suptr;
263 1.1 cgd
264 1.6 mycroft for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
265 1.6 mycroft register struct undo *sunptr = &suptr->un_ent[0];
266 1.6 mycroft register int i = 0;
267 1.6 mycroft
268 1.6 mycroft while (i < suptr->un_cnt) {
269 1.6 mycroft if (sunptr->un_id == semid) {
270 1.6 mycroft if (semnum == -1 || sunptr->un_num == semnum) {
271 1.6 mycroft suptr->un_cnt--;
272 1.6 mycroft if (i < suptr->un_cnt) {
273 1.6 mycroft suptr->un_ent[i] =
274 1.6 mycroft suptr->un_ent[suptr->un_cnt];
275 1.6 mycroft continue;
276 1.6 mycroft }
277 1.6 mycroft }
278 1.6 mycroft if (semnum != -1)
279 1.6 mycroft break;
280 1.6 mycroft }
281 1.6 mycroft i++, sunptr++;
282 1.6 mycroft }
283 1.1 cgd }
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd struct semctl_args {
287 1.1 cgd int semid;
288 1.1 cgd int semnum;
289 1.1 cgd int cmd;
290 1.1 cgd union semun *arg;
291 1.1 cgd };
292 1.1 cgd
293 1.1 cgd int
294 1.1 cgd semctl(p, uap, retval)
295 1.1 cgd struct proc *p;
296 1.1 cgd register struct semctl_args *uap;
297 1.1 cgd int *retval;
298 1.1 cgd {
299 1.6 mycroft int semid = uap->semid;
300 1.6 mycroft int semnum = uap->semnum;
301 1.6 mycroft int cmd = uap->cmd;
302 1.6 mycroft union semun *arg = uap->arg;
303 1.6 mycroft union semun real_arg;
304 1.6 mycroft struct ucred *cred = p->p_ucred;
305 1.6 mycroft int i, rval, eval;
306 1.6 mycroft struct semid_ds sbuf;
307 1.6 mycroft register struct semid_ds *semaptr;
308 1.1 cgd
309 1.1 cgd #ifdef SEM_DEBUG
310 1.6 mycroft printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
311 1.1 cgd #endif
312 1.1 cgd
313 1.6 mycroft semid = IPCID_TO_IX(semid);
314 1.6 mycroft if (semid < 0 || semid >= seminfo.semmsl)
315 1.6 mycroft return(EINVAL);
316 1.6 mycroft
317 1.6 mycroft semaptr = &sema[semid];
318 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
319 1.6 mycroft semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
320 1.6 mycroft return(EINVAL);
321 1.6 mycroft
322 1.6 mycroft eval = 0;
323 1.6 mycroft rval = 0;
324 1.1 cgd
325 1.6 mycroft switch (cmd) {
326 1.6 mycroft case IPC_RMID:
327 1.6 mycroft if (cred->cr_uid != 0 &&
328 1.6 mycroft semaptr->sem_perm.cuid != cred->cr_uid &&
329 1.6 mycroft semaptr->sem_perm.uid != cred->cr_uid)
330 1.6 mycroft return(EPERM);
331 1.6 mycroft semaptr->sem_perm.cuid = cred->cr_uid;
332 1.6 mycroft semaptr->sem_perm.uid = cred->cr_uid;
333 1.6 mycroft semtot -= semaptr->sem_nsems;
334 1.6 mycroft for (i = semaptr->sem_base - sem; i < semtot; i++)
335 1.6 mycroft sem[i] = sem[i + semaptr->sem_nsems];
336 1.6 mycroft for (i = 0; i < seminfo.semmni; i++) {
337 1.6 mycroft if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
338 1.6 mycroft sema[i].sem_base > semaptr->sem_base)
339 1.6 mycroft sema[i].sem_base -= semaptr->sem_nsems;
340 1.6 mycroft }
341 1.6 mycroft semaptr->sem_perm.mode = 0;
342 1.6 mycroft semundo_clear(semid, -1);
343 1.6 mycroft wakeup((caddr_t)semaptr);
344 1.6 mycroft break;
345 1.1 cgd
346 1.6 mycroft case IPC_SET:
347 1.6 mycroft if (cred->cr_uid != 0 &&
348 1.6 mycroft semaptr->sem_perm.cuid != cred->cr_uid &&
349 1.6 mycroft semaptr->sem_perm.uid != cred->cr_uid)
350 1.6 mycroft return(EPERM);
351 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
352 1.6 mycroft return(eval);
353 1.6 mycroft if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
354 1.6 mycroft sizeof(sbuf))) != 0)
355 1.6 mycroft return(eval);
356 1.6 mycroft semaptr->sem_perm.uid = sbuf.sem_perm.uid;
357 1.6 mycroft semaptr->sem_perm.gid = sbuf.sem_perm.gid;
358 1.6 mycroft semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
359 1.6 mycroft (sbuf.sem_perm.mode & 0777);
360 1.6 mycroft semaptr->sem_ctime = time.tv_sec;
361 1.6 mycroft break;
362 1.1 cgd
363 1.6 mycroft case IPC_STAT:
364 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
365 1.6 mycroft return(eval);
366 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
367 1.6 mycroft return(eval);
368 1.6 mycroft eval = copyout((caddr_t)semaptr, real_arg.buf,
369 1.6 mycroft sizeof(struct semid_ds));
370 1.6 mycroft break;
371 1.1 cgd
372 1.6 mycroft case GETNCNT:
373 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
374 1.6 mycroft return(eval);
375 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
376 1.6 mycroft return(EINVAL);
377 1.6 mycroft rval = semaptr->sem_base[semnum].semncnt;
378 1.6 mycroft break;
379 1.1 cgd
380 1.6 mycroft case GETPID:
381 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
382 1.6 mycroft return(eval);
383 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
384 1.6 mycroft return(EINVAL);
385 1.6 mycroft rval = semaptr->sem_base[semnum].sempid;
386 1.6 mycroft break;
387 1.1 cgd
388 1.6 mycroft case GETVAL:
389 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
390 1.6 mycroft return(eval);
391 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
392 1.6 mycroft return(EINVAL);
393 1.6 mycroft rval = semaptr->sem_base[semnum].semval;
394 1.6 mycroft break;
395 1.1 cgd
396 1.6 mycroft case GETALL:
397 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
398 1.6 mycroft return(eval);
399 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
400 1.6 mycroft return(eval);
401 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
402 1.6 mycroft eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
403 1.6 mycroft &real_arg.array[i], sizeof(real_arg.array[0]));
404 1.6 mycroft if (eval != 0)
405 1.6 mycroft break;
406 1.6 mycroft }
407 1.6 mycroft break;
408 1.1 cgd
409 1.6 mycroft case GETZCNT:
410 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_R, cred)))
411 1.6 mycroft return(eval);
412 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
413 1.6 mycroft return(EINVAL);
414 1.6 mycroft rval = semaptr->sem_base[semnum].semzcnt;
415 1.6 mycroft break;
416 1.1 cgd
417 1.6 mycroft case SETVAL:
418 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_W, cred)))
419 1.6 mycroft return(eval);
420 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
421 1.6 mycroft return(EINVAL);
422 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
423 1.6 mycroft return(eval);
424 1.6 mycroft semaptr->sem_base[semnum].semval = real_arg.val;
425 1.6 mycroft semundo_clear(semid, semnum);
426 1.6 mycroft wakeup((caddr_t)semaptr);
427 1.6 mycroft break;
428 1.1 cgd
429 1.6 mycroft case SETALL:
430 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_W, cred)))
431 1.6 mycroft return(eval);
432 1.6 mycroft if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
433 1.6 mycroft return(eval);
434 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
435 1.6 mycroft eval = copyin(&real_arg.array[i],
436 1.6 mycroft (caddr_t)&semaptr->sem_base[i].semval,
437 1.6 mycroft sizeof(real_arg.array[0]));
438 1.6 mycroft if (eval != 0)
439 1.6 mycroft break;
440 1.6 mycroft }
441 1.6 mycroft semundo_clear(semid, -1);
442 1.6 mycroft wakeup((caddr_t)semaptr);
443 1.6 mycroft break;
444 1.1 cgd
445 1.6 mycroft default:
446 1.6 mycroft return(EINVAL);
447 1.6 mycroft }
448 1.4 mycroft
449 1.6 mycroft if (eval == 0)
450 1.6 mycroft *retval = rval;
451 1.6 mycroft return(eval);
452 1.1 cgd }
453 1.1 cgd
454 1.1 cgd struct semget_args {
455 1.1 cgd key_t key;
456 1.1 cgd int nsems;
457 1.1 cgd int semflg;
458 1.1 cgd };
459 1.1 cgd
460 1.1 cgd int
461 1.1 cgd semget(p, uap, retval)
462 1.1 cgd struct proc *p;
463 1.1 cgd register struct semget_args *uap;
464 1.1 cgd int *retval;
465 1.1 cgd {
466 1.6 mycroft int semid, eval;
467 1.6 mycroft int key = uap->key;
468 1.6 mycroft int nsems = uap->nsems;
469 1.6 mycroft int semflg = uap->semflg;
470 1.6 mycroft struct ucred *cred = p->p_ucred;
471 1.1 cgd
472 1.1 cgd #ifdef SEM_DEBUG
473 1.6 mycroft printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
474 1.1 cgd #endif
475 1.1 cgd
476 1.6 mycroft if (key != IPC_PRIVATE) {
477 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
478 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
479 1.6 mycroft sema[semid].sem_perm.key == key)
480 1.6 mycroft break;
481 1.6 mycroft }
482 1.6 mycroft if (semid < seminfo.semmni) {
483 1.1 cgd #ifdef SEM_DEBUG
484 1.6 mycroft printf("found public key\n");
485 1.1 cgd #endif
486 1.6 mycroft if ((eval = ipcaccess(&sema[semid].sem_perm,
487 1.6 mycroft semflg & 0700, cred)))
488 1.6 mycroft return(eval);
489 1.6 mycroft if (nsems > 0 && sema[semid].sem_nsems < nsems) {
490 1.1 cgd #ifdef SEM_DEBUG
491 1.6 mycroft printf("too small\n");
492 1.1 cgd #endif
493 1.6 mycroft return(EINVAL);
494 1.6 mycroft }
495 1.6 mycroft if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
496 1.1 cgd #ifdef SEM_DEBUG
497 1.6 mycroft printf("not exclusive\n");
498 1.1 cgd #endif
499 1.6 mycroft return(EEXIST);
500 1.6 mycroft }
501 1.6 mycroft goto found;
502 1.6 mycroft }
503 1.6 mycroft }
504 1.6 mycroft
505 1.1 cgd #ifdef SEM_DEBUG
506 1.6 mycroft printf("need to allocate the semid_ds\n");
507 1.1 cgd #endif
508 1.6 mycroft if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
509 1.6 mycroft if (nsems <= 0 || nsems > seminfo.semmsl) {
510 1.1 cgd #ifdef SEM_DEBUG
511 1.6 mycroft printf("nsems out of range (0<%d<=%d)\n", nsems,
512 1.6 mycroft seminfo.semmsl);
513 1.1 cgd #endif
514 1.6 mycroft return(EINVAL);
515 1.6 mycroft }
516 1.6 mycroft if (nsems > seminfo.semmns - semtot) {
517 1.1 cgd #ifdef SEM_DEBUG
518 1.6 mycroft printf("not enough semaphores left (need %d, got %d)\n",
519 1.6 mycroft nsems, seminfo.semmns - semtot);
520 1.1 cgd #endif
521 1.6 mycroft return(ENOSPC);
522 1.6 mycroft }
523 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
524 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
525 1.6 mycroft break;
526 1.6 mycroft }
527 1.6 mycroft if (semid == seminfo.semmni) {
528 1.1 cgd #ifdef SEM_DEBUG
529 1.6 mycroft printf("no more semid_ds's available\n");
530 1.1 cgd #endif
531 1.6 mycroft return(ENOSPC);
532 1.6 mycroft }
533 1.1 cgd #ifdef SEM_DEBUG
534 1.6 mycroft printf("semid %d is available\n", semid);
535 1.1 cgd #endif
536 1.6 mycroft sema[semid].sem_perm.key = key;
537 1.6 mycroft sema[semid].sem_perm.cuid = cred->cr_uid;
538 1.6 mycroft sema[semid].sem_perm.uid = cred->cr_uid;
539 1.6 mycroft sema[semid].sem_perm.cgid = cred->cr_gid;
540 1.6 mycroft sema[semid].sem_perm.gid = cred->cr_gid;
541 1.6 mycroft sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
542 1.6 mycroft sema[semid].sem_perm.seq =
543 1.6 mycroft (sema[semid].sem_perm.seq + 1) & 0x7fff;
544 1.6 mycroft sema[semid].sem_nsems = nsems;
545 1.6 mycroft sema[semid].sem_otime = 0;
546 1.6 mycroft sema[semid].sem_ctime = time.tv_sec;
547 1.6 mycroft sema[semid].sem_base = &sem[semtot];
548 1.6 mycroft semtot += nsems;
549 1.6 mycroft bzero(sema[semid].sem_base,
550 1.6 mycroft sizeof(sema[semid].sem_base[0])*nsems);
551 1.1 cgd #ifdef SEM_DEBUG
552 1.6 mycroft printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
553 1.6 mycroft &sem[semtot]);
554 1.1 cgd #endif
555 1.1 cgd } else {
556 1.1 cgd #ifdef SEM_DEBUG
557 1.6 mycroft printf("didn't find it and wasn't asked to create it\n");
558 1.1 cgd #endif
559 1.6 mycroft return(ENOENT);
560 1.1 cgd }
561 1.1 cgd
562 1.6 mycroft found:
563 1.6 mycroft *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
564 1.6 mycroft return(0);
565 1.1 cgd }
566 1.1 cgd
567 1.1 cgd struct semop_args {
568 1.1 cgd int semid;
569 1.1 cgd struct sembuf *sops;
570 1.1 cgd int nsops;
571 1.1 cgd };
572 1.1 cgd
573 1.1 cgd int
574 1.1 cgd semop(p, uap, retval)
575 1.1 cgd struct proc *p;
576 1.1 cgd register struct semop_args *uap;
577 1.1 cgd int *retval;
578 1.1 cgd {
579 1.6 mycroft int semid = uap->semid;
580 1.6 mycroft int nsops = uap->nsops;
581 1.6 mycroft struct sembuf sops[MAX_SOPS];
582 1.6 mycroft register struct semid_ds *semaptr;
583 1.6 mycroft register struct sembuf *sopptr;
584 1.6 mycroft register struct sem *semptr;
585 1.6 mycroft struct sem_undo *suptr = NULL;
586 1.6 mycroft struct ucred *cred = p->p_ucred;
587 1.6 mycroft int i, j, eval;
588 1.6 mycroft int all_ok, do_wakeup, do_undos;
589 1.1 cgd
590 1.1 cgd #ifdef SEM_DEBUG
591 1.6 mycroft printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
592 1.1 cgd #endif
593 1.1 cgd
594 1.6 mycroft semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
595 1.6 mycroft
596 1.6 mycroft if (semid < 0 || semid >= seminfo.semmsl)
597 1.6 mycroft return(EINVAL);
598 1.6 mycroft
599 1.6 mycroft semaptr = &sema[semid];
600 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
601 1.6 mycroft return(EINVAL);
602 1.6 mycroft if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
603 1.6 mycroft return(EINVAL);
604 1.6 mycroft
605 1.6 mycroft if ((eval = ipcaccess(&semaptr->sem_perm, IPC_W, cred))) {
606 1.1 cgd #ifdef SEM_DEBUG
607 1.6 mycroft printf("eval = %d from ipaccess\n", eval);
608 1.1 cgd #endif
609 1.6 mycroft return(eval);
610 1.6 mycroft }
611 1.1 cgd
612 1.6 mycroft if (nsops > MAX_SOPS) {
613 1.1 cgd #ifdef SEM_DEBUG
614 1.6 mycroft printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
615 1.1 cgd #endif
616 1.6 mycroft return(E2BIG);
617 1.6 mycroft }
618 1.1 cgd
619 1.6 mycroft if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
620 1.6 mycroft #ifdef SEM_DEBUG
621 1.6 mycroft printf("eval = %d from copyin(%08x, %08x, %d)\n", eval,
622 1.6 mycroft uap->sops, &sops, nsops * sizeof(sops[0]));
623 1.6 mycroft #endif
624 1.6 mycroft return(eval);
625 1.6 mycroft }
626 1.1 cgd
627 1.6 mycroft /*
628 1.6 mycroft * Loop trying to satisfy the vector of requests.
629 1.6 mycroft * If we reach a point where we must wait, any requests already
630 1.6 mycroft * performed are rolled back and we go to sleep until some other
631 1.6 mycroft * process wakes us up. At this point, we start all over again.
632 1.6 mycroft *
633 1.6 mycroft * This ensures that from the perspective of other tasks, a set
634 1.6 mycroft * of requests is atomic (never partially satisfied).
635 1.6 mycroft */
636 1.6 mycroft do_undos = 0;
637 1.1 cgd
638 1.6 mycroft for (;;) {
639 1.6 mycroft do_wakeup = 0;
640 1.1 cgd
641 1.6 mycroft for (i = 0; i < nsops; i++) {
642 1.6 mycroft sopptr = &sops[i];
643 1.1 cgd
644 1.6 mycroft if (sopptr->sem_num >= semaptr->sem_nsems)
645 1.6 mycroft return(EFBIG);
646 1.1 cgd
647 1.6 mycroft semptr = &semaptr->sem_base[sopptr->sem_num];
648 1.1 cgd
649 1.1 cgd #ifdef SEM_DEBUG
650 1.6 mycroft printf("semop: semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
651 1.6 mycroft semaptr, semaptr->sem_base, semptr,
652 1.6 mycroft sopptr->sem_num, semptr->semval, sopptr->sem_op,
653 1.6 mycroft (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
654 1.1 cgd #endif
655 1.1 cgd
656 1.6 mycroft if (sopptr->sem_op < 0) {
657 1.6 mycroft if (semptr->semval + sopptr->sem_op < 0) {
658 1.1 cgd #ifdef SEM_DEBUG
659 1.6 mycroft printf("semop: can't do it now\n");
660 1.1 cgd #endif
661 1.6 mycroft break;
662 1.6 mycroft } else {
663 1.6 mycroft semptr->semval += sopptr->sem_op;
664 1.6 mycroft if (semptr->semval == 0 &&
665 1.6 mycroft semptr->semzcnt > 0)
666 1.6 mycroft do_wakeup = 1;
667 1.6 mycroft }
668 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
669 1.6 mycroft do_undos = 1;
670 1.6 mycroft } else if (sopptr->sem_op == 0) {
671 1.6 mycroft if (semptr->semval > 0) {
672 1.6 mycroft #ifdef SEM_DEBUG
673 1.6 mycroft printf("semop: not zero now\n");
674 1.6 mycroft #endif
675 1.6 mycroft break;
676 1.6 mycroft }
677 1.6 mycroft } else {
678 1.6 mycroft if (semptr->semncnt > 0)
679 1.6 mycroft do_wakeup = 1;
680 1.6 mycroft semptr->semval += sopptr->sem_op;
681 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
682 1.6 mycroft do_undos = 1;
683 1.6 mycroft }
684 1.6 mycroft }
685 1.1 cgd
686 1.6 mycroft /*
687 1.6 mycroft * Did we get through the entire vector?
688 1.6 mycroft */
689 1.6 mycroft if (i >= nsops)
690 1.6 mycroft goto done;
691 1.1 cgd
692 1.6 mycroft /*
693 1.6 mycroft * No ... rollback anything that we've already done
694 1.6 mycroft */
695 1.1 cgd #ifdef SEM_DEBUG
696 1.6 mycroft printf("semop: rollback 0 through %d\n", i-1);
697 1.1 cgd #endif
698 1.6 mycroft for (j = 0; j < i; j++)
699 1.6 mycroft semaptr->sem_base[sops[j].sem_num].semval -=
700 1.6 mycroft sops[j].sem_op;
701 1.1 cgd
702 1.6 mycroft /*
703 1.6 mycroft * If the request that we couldn't satisfy has the
704 1.6 mycroft * NOWAIT flag set then return with EAGAIN.
705 1.6 mycroft */
706 1.6 mycroft if (sopptr->sem_flg & IPC_NOWAIT)
707 1.6 mycroft return(EAGAIN);
708 1.1 cgd
709 1.6 mycroft if (sopptr->sem_op == 0)
710 1.6 mycroft semptr->semzcnt++;
711 1.6 mycroft else
712 1.6 mycroft semptr->semncnt++;
713 1.1 cgd
714 1.1 cgd #ifdef SEM_DEBUG
715 1.6 mycroft printf("semop: good night!\n");
716 1.1 cgd #endif
717 1.6 mycroft eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
718 1.6 mycroft "semwait", 0);
719 1.1 cgd #ifdef SEM_DEBUG
720 1.6 mycroft printf("semop: good morning (eval=%d)!\n", eval);
721 1.1 cgd #endif
722 1.1 cgd
723 1.6 mycroft suptr = NULL; /* sem_undo may have been reallocated */
724 1.1 cgd
725 1.6 mycroft if (eval != 0)
726 1.6 mycroft return(EINTR);
727 1.1 cgd #ifdef SEM_DEBUG
728 1.6 mycroft printf("semop: good morning!\n");
729 1.1 cgd #endif
730 1.1 cgd
731 1.6 mycroft /*
732 1.6 mycroft * Make sure that the semaphore still exists
733 1.6 mycroft */
734 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
735 1.6 mycroft semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
736 1.6 mycroft /* The man page says to return EIDRM. */
737 1.6 mycroft /* Unfortunately, BSD doesn't define that code! */
738 1.1 cgd #ifdef EIDRM
739 1.6 mycroft return(EIDRM);
740 1.1 cgd #else
741 1.6 mycroft return(EINVAL);
742 1.1 cgd #endif
743 1.6 mycroft }
744 1.1 cgd
745 1.6 mycroft /*
746 1.6 mycroft * The semaphore is still alive. Readjust the count of
747 1.6 mycroft * waiting processes.
748 1.6 mycroft */
749 1.6 mycroft if (sopptr->sem_op == 0)
750 1.6 mycroft semptr->semzcnt--;
751 1.6 mycroft else
752 1.6 mycroft semptr->semncnt--;
753 1.6 mycroft }
754 1.1 cgd
755 1.6 mycroft done:
756 1.6 mycroft /*
757 1.6 mycroft * Process any SEM_UNDO requests.
758 1.6 mycroft */
759 1.6 mycroft if (do_undos) {
760 1.5 mycroft for (i = 0; i < nsops; i++) {
761 1.6 mycroft /*
762 1.6 mycroft * We only need to deal with SEM_UNDO's for non-zero
763 1.6 mycroft * op's.
764 1.6 mycroft */
765 1.6 mycroft int adjval;
766 1.1 cgd
767 1.6 mycroft if ((sops[i].sem_flg & SEM_UNDO) == 0)
768 1.6 mycroft continue;
769 1.6 mycroft adjval = sops[i].sem_op;
770 1.6 mycroft if (adjval == 0)
771 1.6 mycroft continue;
772 1.6 mycroft eval = semundo_adjust(p, &suptr, semid,
773 1.6 mycroft sops[i].sem_num, -adjval);
774 1.6 mycroft if (eval == 0)
775 1.6 mycroft continue;
776 1.1 cgd
777 1.6 mycroft /*
778 1.6 mycroft * Oh-Oh! We ran out of either sem_undo's or undo's.
779 1.6 mycroft * Rollback the adjustments to this point and then
780 1.6 mycroft * rollback the semaphore ups and down so we can return
781 1.6 mycroft * with an error with all structures restored. We
782 1.6 mycroft * rollback the undo's in the exact reverse order that
783 1.6 mycroft * we applied them. This guarantees that we won't run
784 1.6 mycroft * out of space as we roll things back out.
785 1.6 mycroft */
786 1.6 mycroft for (j = i - 1; j >= 0; j--) {
787 1.6 mycroft if ((sops[j].sem_flg & SEM_UNDO) == 0)
788 1.6 mycroft continue;
789 1.6 mycroft adjval = sops[j].sem_op;
790 1.6 mycroft if (adjval == 0)
791 1.6 mycroft continue;
792 1.6 mycroft if (semundo_adjust(p, &suptr, semid,
793 1.6 mycroft sops[j].sem_num, adjval) != 0)
794 1.1 cgd panic("semop - can't undo undos");
795 1.6 mycroft }
796 1.1 cgd
797 1.6 mycroft for (j = 0; j < nsops; j++)
798 1.6 mycroft semaptr->sem_base[sops[j].sem_num].semval -=
799 1.6 mycroft sops[j].sem_op;
800 1.1 cgd
801 1.1 cgd #ifdef SEM_DEBUG
802 1.6 mycroft printf("eval = %d from semundo_adjust\n", eval);
803 1.1 cgd #endif
804 1.6 mycroft return(eval);
805 1.1 cgd } /* loop through the sops */
806 1.6 mycroft } /* if (do_undos) */
807 1.1 cgd
808 1.6 mycroft /* We're definitely done - set the sempid's */
809 1.6 mycroft for (i = 0; i < nsops; i++) {
810 1.1 cgd sopptr = &sops[i];
811 1.1 cgd semptr = &semaptr->sem_base[sopptr->sem_num];
812 1.1 cgd semptr->sempid = p->p_pid;
813 1.6 mycroft }
814 1.1 cgd
815 1.6 mycroft /* Do a wakeup if any semaphore was up'd. */
816 1.6 mycroft if (do_wakeup) {
817 1.1 cgd #ifdef SEM_DEBUG
818 1.1 cgd printf("semop: doing wakeup\n");
819 1.1 cgd #ifdef SEM_WAKEUP
820 1.4 mycroft sem_wakeup((caddr_t)semaptr);
821 1.1 cgd #else
822 1.4 mycroft wakeup((caddr_t)semaptr);
823 1.1 cgd #endif
824 1.1 cgd printf("semop: back from wakeup\n");
825 1.1 cgd #else
826 1.4 mycroft wakeup((caddr_t)semaptr);
827 1.1 cgd #endif
828 1.6 mycroft }
829 1.1 cgd #ifdef SEM_DEBUG
830 1.6 mycroft printf("semop: done\n");
831 1.1 cgd #endif
832 1.6 mycroft *retval = 0;
833 1.6 mycroft return(0);
834 1.1 cgd }
835 1.1 cgd
836 1.1 cgd /*
837 1.6 mycroft * Go through the undo structures for this process and apply the adjustments to
838 1.6 mycroft * semaphores.
839 1.1 cgd */
840 1.1 cgd semexit(p)
841 1.6 mycroft struct proc *p;
842 1.1 cgd {
843 1.6 mycroft register struct sem_undo *suptr;
844 1.6 mycroft register struct sem_undo **supptr;
845 1.6 mycroft int did_something;
846 1.1 cgd
847 1.6 mycroft /*
848 1.6 mycroft * If somebody else is holding the global semaphore facility lock
849 1.6 mycroft * then sleep until it is released.
850 1.6 mycroft */
851 1.6 mycroft while (semlock_holder != NULL && semlock_holder != p) {
852 1.1 cgd #ifdef SEM_DEBUG
853 1.6 mycroft printf("semaphore facility locked - sleeping ...\n");
854 1.1 cgd #endif
855 1.6 mycroft sleep((caddr_t)&semlock_holder, (PZERO - 4));
856 1.6 mycroft }
857 1.1 cgd
858 1.6 mycroft did_something = 0;
859 1.1 cgd
860 1.6 mycroft /*
861 1.6 mycroft * Go through the chain of undo vectors looking for one
862 1.6 mycroft * associated with this process.
863 1.6 mycroft */
864 1.6 mycroft
865 1.6 mycroft for (supptr = &semu_list; (suptr = *supptr) != NULL;
866 1.6 mycroft supptr = &suptr->un_next) {
867 1.6 mycroft if (suptr->un_proc == p)
868 1.6 mycroft break;
869 1.6 mycroft }
870 1.1 cgd
871 1.6 mycroft if (suptr == NULL)
872 1.6 mycroft goto unlock;
873 1.1 cgd
874 1.1 cgd #ifdef SEM_DEBUG
875 1.6 mycroft printf("proc @%08x has undo structure with %d entries\n", p,
876 1.6 mycroft suptr->un_cnt);
877 1.1 cgd #endif
878 1.1 cgd
879 1.5 mycroft /*
880 1.5 mycroft * If there are any active undo elements then process them.
881 1.5 mycroft */
882 1.5 mycroft if (suptr->un_cnt > 0) {
883 1.6 mycroft int ix;
884 1.1 cgd
885 1.6 mycroft for (ix = 0; ix < suptr->un_cnt; ix++) {
886 1.6 mycroft int semid = suptr->un_ent[ix].un_id;
887 1.6 mycroft int semnum = suptr->un_ent[ix].un_num;
888 1.6 mycroft int adjval = suptr->un_ent[ix].un_adjval;
889 1.6 mycroft struct semid_ds *semaptr;
890 1.6 mycroft
891 1.6 mycroft semaptr = &sema[semid];
892 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
893 1.6 mycroft panic("semexit - semid not allocated");
894 1.6 mycroft if (semnum >= semaptr->sem_nsems)
895 1.6 mycroft panic("semexit - semnum out of range");
896 1.6 mycroft
897 1.6 mycroft #ifdef SEM_DEBUG
898 1.6 mycroft printf("semexit: %08x id=%d num=%d(adj=%d) ; sem=%d\n",
899 1.6 mycroft suptr->un_proc, suptr->un_ent[ix].un_id,
900 1.6 mycroft suptr->un_ent[ix].un_num,
901 1.6 mycroft suptr->un_ent[ix].un_adjval,
902 1.6 mycroft semaptr->sem_base[semnum].semval);
903 1.6 mycroft #endif
904 1.6 mycroft
905 1.6 mycroft if (adjval < 0) {
906 1.6 mycroft if (semaptr->sem_base[semnum].semval < -adjval)
907 1.6 mycroft semaptr->sem_base[semnum].semval = 0;
908 1.6 mycroft else
909 1.6 mycroft semaptr->sem_base[semnum].semval +=
910 1.6 mycroft adjval;
911 1.6 mycroft } else
912 1.6 mycroft semaptr->sem_base[semnum].semval += adjval;
913 1.1 cgd
914 1.1 cgd #ifdef SEM_WAKEUP
915 1.6 mycroft sem_wakeup((caddr_t)semaptr);
916 1.1 cgd #else
917 1.6 mycroft wakeup((caddr_t)semaptr);
918 1.1 cgd #endif
919 1.1 cgd #ifdef SEM_DEBUG
920 1.6 mycroft printf("semexit: back from wakeup\n");
921 1.1 cgd #endif
922 1.6 mycroft }
923 1.5 mycroft }
924 1.1 cgd
925 1.5 mycroft /*
926 1.5 mycroft * Deallocate the undo vector.
927 1.5 mycroft */
928 1.1 cgd #ifdef SEM_DEBUG
929 1.5 mycroft printf("removing vector\n");
930 1.1 cgd #endif
931 1.5 mycroft suptr->un_proc = NULL;
932 1.5 mycroft *supptr = suptr->un_next;
933 1.1 cgd
934 1.6 mycroft unlock:
935 1.6 mycroft /*
936 1.6 mycroft * If the exiting process is holding the global semaphore facility
937 1.6 mycroft * lock then release it.
938 1.6 mycroft */
939 1.6 mycroft if (semlock_holder == p) {
940 1.6 mycroft semlock_holder = NULL;
941 1.6 mycroft wakeup((caddr_t)&semlock_holder);
942 1.6 mycroft }
943 1.1 cgd }
944