1 /*
2  * linux/ipc/sem.c
3  * Copyright (C) 1992 Krishna Balasubramanian
4  * Copyright (C) 1995 Eric Schenk, Bruno Haible
5  *
6  * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7  * This code underwent a massive rewrite in order to solve some problems
8  * with the original code. In particular the original code failed to
9  * wake up processes that were waiting for semval to go to 0 if the
10  * value went to 0 and was then incremented rapidly enough. In solving
11  * this problem I have also modified the implementation so that it
12  * processes pending operations in a FIFO manner, thus give a guarantee
13  * that processes waiting for a lock on the semaphore won't starve
14  * unless another locking process fails to unlock.
15  * In addition the following two changes in behavior have been introduced:
16  * - The original implementation of semop returned the value
17  *   last semaphore element examined on success. This does not
18  *   match the manual page specifications, and effectively
19  *   allows the user to read the semaphore even if they do not
20  *   have read permissions. The implementation now returns 0
21  *   on success as stated in the manual page.
22  * - There is some confusion over whether the set of undo adjustments
23  *   to be performed at exit should be done in an atomic manner.
24  *   That is, if we are attempting to decrement the semval should we queue
25  *   up and wait until we can do so legally?
26  *   The original implementation attempted to do this.
27  *   The current implementation does not do so. This is because I don't
28  *   think it is the right thing (TM) to do, and because I couldn't
29  *   see a clean way to get the old behavior with the new design.
30  *   The POSIX standard and SVID should be consulted to determine
31  *   what behavior is mandated.
32  *
33  * Further notes on refinement (Christoph Rohland, December 1998):
34  * - The POSIX standard says, that the undo adjustments simply should
35  *   redo. So the current implementation is o.K.
36  * - The previous code had two flaws:
37  *   1) It actively gave the semaphore to the next waiting process
38  *      sleeping on the semaphore. Since this process did not have the
39  *      cpu this led to many unnecessary context switches and bad
40  *      performance. Now we only check which process should be able to
41  *      get the semaphore and if this process wants to reduce some
42  *      semaphore value we simply wake it up without doing the
43  *      operation. So it has to try to get it later. Thus e.g. the
44  *      running process may reacquire the semaphore during the current
45  *      time slice. If it only waits for zero or increases the semaphore,
46  *      we do the operation in advance and wake it up.
47  *   2) It did not wake up all zero waiting processes. We try to do
48  *      better but only get the semops right which only wait for zero or
49  *      increase. If there are decrement operations in the operations
50  *      array we do the same as before.
51  *
52  * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
53  *
54  * SMP-threaded, sysctl's added
55  * (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
56  * Enforced range limit on SEM_UNDO
57  * (c) 2001 Red Hat Inc <alan@redhat.com>
58  */
59 
60 #include <linux/config.h>
61 #include <linux/slab.h>
62 #include <linux/spinlock.h>
63 #include <linux/init.h>
64 #include <linux/proc_fs.h>
65 #include <linux/time.h>
66 #include <asm/uaccess.h>
67 #include "util.h"
68 
69 
70 #define sem_lock(id)	((struct sem_array*)ipc_lock(&sem_ids,id))
71 #define sem_unlock(id)	ipc_unlock(&sem_ids,id)
72 #define sem_rmid(id)	((struct sem_array*)ipc_rmid(&sem_ids,id))
73 #define sem_checkid(sma, semid)	\
74 	ipc_checkid(&sem_ids,&sma->sem_perm,semid)
75 #define sem_buildid(id, seq) \
76 	ipc_buildid(&sem_ids, id, seq)
77 static struct ipc_ids sem_ids;
78 
79 static int newary (key_t, int, int);
80 static void freeary (int id);
81 #ifdef CONFIG_PROC_FS
82 static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
83 #endif
84 
85 #define SEMMSL_FAST	256 /* 512 bytes on stack */
86 #define SEMOPM_FAST	64  /* ~ 372 bytes on stack */
87 
88 /*
89  * linked list protection:
90  *	sem_undo.id_next,
91  *	sem_array.sem_pending{,last},
92  *	sem_array.sem_undo: sem_lock() for read/write
93  *	sem_undo.proc_next: only "current" is allowed to read/write that field.
94  *
95  */
96 
97 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
98 #define sc_semmsl	(sem_ctls[0])
99 #define sc_semmns	(sem_ctls[1])
100 #define sc_semopm	(sem_ctls[2])
101 #define sc_semmni	(sem_ctls[3])
102 
103 static int used_sems;
104 
sem_init(void)105 void __init sem_init (void)
106 {
107 	used_sems = 0;
108 	ipc_init_ids(&sem_ids,sc_semmni);
109 
110 #ifdef CONFIG_PROC_FS
111 	create_proc_read_entry("sysvipc/sem", 0, 0, sysvipc_sem_read_proc, NULL);
112 #endif
113 }
114 
newary(key_t key,int nsems,int semflg)115 static int newary (key_t key, int nsems, int semflg)
116 {
117 	int id;
118 	struct sem_array *sma;
119 	int size;
120 
121 	if (!nsems)
122 		return -EINVAL;
123 	if (used_sems + nsems > sc_semmns)
124 		return -ENOSPC;
125 
126 	size = sizeof (*sma) + nsems * sizeof (struct sem);
127 	sma = (struct sem_array *) ipc_alloc(size);
128 	if (!sma) {
129 		return -ENOMEM;
130 	}
131 	memset (sma, 0, size);
132 	id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
133 	if(id == -1) {
134 		ipc_free(sma, size);
135 		return -ENOSPC;
136 	}
137 	used_sems += nsems;
138 
139 	sma->sem_perm.mode = (semflg & S_IRWXUGO);
140 	sma->sem_perm.key = key;
141 
142 	sma->sem_base = (struct sem *) &sma[1];
143 	/* sma->sem_pending = NULL; */
144 	sma->sem_pending_last = &sma->sem_pending;
145 	/* sma->undo = NULL; */
146 	sma->sem_nsems = nsems;
147 	sma->sem_ctime = CURRENT_TIME;
148 	sem_unlock(id);
149 
150 	return sem_buildid(id, sma->sem_perm.seq);
151 }
152 
sys_semget(key_t key,int nsems,int semflg)153 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
154 {
155 	int id, err = -EINVAL;
156 	struct sem_array *sma;
157 
158 	if (nsems < 0 || nsems > sc_semmsl)
159 		return -EINVAL;
160 	down(&sem_ids.sem);
161 
162 	if (key == IPC_PRIVATE) {
163 		err = newary(key, nsems, semflg);
164 	} else if ((id = ipc_findkey(&sem_ids, key)) == -1) {  /* key not used */
165 		if (!(semflg & IPC_CREAT))
166 			err = -ENOENT;
167 		else
168 			err = newary(key, nsems, semflg);
169 	} else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
170 		err = -EEXIST;
171 	} else {
172 		sma = sem_lock(id);
173 		if(sma==NULL)
174 			BUG();
175 		if (nsems > sma->sem_nsems)
176 			err = -EINVAL;
177 		else if (ipcperms(&sma->sem_perm, semflg))
178 			err = -EACCES;
179 		else
180 			err = sem_buildid(id, sma->sem_perm.seq);
181 		sem_unlock(id);
182 	}
183 
184 	up(&sem_ids.sem);
185 	return err;
186 }
187 
188 /* doesn't acquire the sem_lock on error! */
sem_revalidate(int semid,struct sem_array * sma,int nsems,short flg)189 static int sem_revalidate(int semid, struct sem_array* sma, int nsems, short flg)
190 {
191 	struct sem_array* smanew;
192 
193 	smanew = sem_lock(semid);
194 	if(smanew==NULL)
195 		return -EIDRM;
196 	if(smanew != sma || sem_checkid(sma,semid) || sma->sem_nsems != nsems) {
197 		sem_unlock(semid);
198 		return -EIDRM;
199 	}
200 
201 	if (ipcperms(&sma->sem_perm, flg)) {
202 		sem_unlock(semid);
203 		return -EACCES;
204 	}
205 	return 0;
206 }
207 /* Manage the doubly linked list sma->sem_pending as a FIFO:
208  * insert new queue elements at the tail sma->sem_pending_last.
209  */
append_to_queue(struct sem_array * sma,struct sem_queue * q)210 static inline void append_to_queue (struct sem_array * sma,
211 				    struct sem_queue * q)
212 {
213 	*(q->prev = sma->sem_pending_last) = q;
214 	*(sma->sem_pending_last = &q->next) = NULL;
215 }
216 
prepend_to_queue(struct sem_array * sma,struct sem_queue * q)217 static inline void prepend_to_queue (struct sem_array * sma,
218 				     struct sem_queue * q)
219 {
220 	q->next = sma->sem_pending;
221 	*(q->prev = &sma->sem_pending) = q;
222 	if (q->next)
223 		q->next->prev = &q->next;
224 	else /* sma->sem_pending_last == &sma->sem_pending */
225 		sma->sem_pending_last = &q->next;
226 }
227 
remove_from_queue(struct sem_array * sma,struct sem_queue * q)228 static inline void remove_from_queue (struct sem_array * sma,
229 				      struct sem_queue * q)
230 {
231 	*(q->prev) = q->next;
232 	if (q->next)
233 		q->next->prev = q->prev;
234 	else /* sma->sem_pending_last == &q->next */
235 		sma->sem_pending_last = q->prev;
236 	q->prev = NULL; /* mark as removed */
237 }
238 
239 /*
240  * Determine whether a sequence of semaphore operations would succeed
241  * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
242  */
243 
try_atomic_semop(struct sem_array * sma,struct sembuf * sops,int nsops,struct sem_undo * un,int pid,int do_undo)244 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
245 			     int nsops, struct sem_undo *un, int pid,
246 			     int do_undo)
247 {
248 	int result, sem_op;
249 	struct sembuf *sop;
250 	struct sem * curr;
251 
252 	for (sop = sops; sop < sops + nsops; sop++) {
253 		curr = sma->sem_base + sop->sem_num;
254 		sem_op = sop->sem_op;
255 		result = curr->semval;
256 
257 		if (!sem_op && result)
258 			goto would_block;
259 
260 		result += sem_op;
261 		if (result < 0)
262 			goto would_block;
263 		if (result > SEMVMX)
264 			goto out_of_range;
265 		if (sop->sem_flg & SEM_UNDO) {
266 			int undo = un->semadj[sop->sem_num] - sem_op;
267 			/*
268 	 		 *	Exceeding the undo range is an error.
269 			 */
270 			if (undo < (-SEMAEM - 1) || undo > SEMAEM)
271 				goto out_of_range;
272 		}
273 		curr->semval = result;
274 	}
275 
276 	if (do_undo) {
277 		result = 0;
278 		goto undo;
279 	}
280 	sop--;
281 	while (sop >= sops) {
282 		sma->sem_base[sop->sem_num].sempid = pid;
283 		if (sop->sem_flg & SEM_UNDO)
284 			un->semadj[sop->sem_num] -= sop->sem_op;
285 		sop--;
286 	}
287 	sma->sem_otime = CURRENT_TIME;
288 	return 0;
289 
290 out_of_range:
291 	result = -ERANGE;
292 	goto undo;
293 
294 would_block:
295 	if (sop->sem_flg & IPC_NOWAIT)
296 		result = -EAGAIN;
297 	else
298 		result = 1;
299 
300 undo:
301 	sop--;
302 	while (sop >= sops) {
303 		sma->sem_base[sop->sem_num].semval -= sop->sem_op;
304 		sop--;
305 	}
306 
307 	return result;
308 }
309 
310 /* Go through the pending queue for the indicated semaphore
311  * looking for tasks that can be completed.
312  */
update_queue(struct sem_array * sma)313 static void update_queue (struct sem_array * sma)
314 {
315 	int error;
316 	struct sem_queue * q;
317 
318 	for (q = sma->sem_pending; q; q = q->next) {
319 
320 		if (q->status == 1)
321 			continue;	/* this one was woken up before */
322 
323 		error = try_atomic_semop(sma, q->sops, q->nsops,
324 					 q->undo, q->pid, q->alter);
325 
326 		/* Does q->sleeper still need to sleep? */
327 		if (error <= 0) {
328 				/* Found one, wake it up */
329 			wake_up_process(q->sleeper);
330 			if (error == 0 && q->alter) {
331 				/* if q-> alter let it self try */
332 				q->status = 1;
333 				return;
334 			}
335 			q->status = error;
336 			remove_from_queue(sma,q);
337 		}
338 	}
339 }
340 
341 /* The following counts are associated to each semaphore:
342  *   semncnt        number of tasks waiting on semval being nonzero
343  *   semzcnt        number of tasks waiting on semval being zero
344  * This model assumes that a task waits on exactly one semaphore.
345  * Since semaphore operations are to be performed atomically, tasks actually
346  * wait on a whole sequence of semaphores simultaneously.
347  * The counts we return here are a rough approximation, but still
348  * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
349  */
count_semncnt(struct sem_array * sma,ushort semnum)350 static int count_semncnt (struct sem_array * sma, ushort semnum)
351 {
352 	int semncnt;
353 	struct sem_queue * q;
354 
355 	semncnt = 0;
356 	for (q = sma->sem_pending; q; q = q->next) {
357 		struct sembuf * sops = q->sops;
358 		int nsops = q->nsops;
359 		int i;
360 		for (i = 0; i < nsops; i++)
361 			if (sops[i].sem_num == semnum
362 			    && (sops[i].sem_op < 0)
363 			    && !(sops[i].sem_flg & IPC_NOWAIT))
364 				semncnt++;
365 	}
366 	return semncnt;
367 }
count_semzcnt(struct sem_array * sma,ushort semnum)368 static int count_semzcnt (struct sem_array * sma, ushort semnum)
369 {
370 	int semzcnt;
371 	struct sem_queue * q;
372 
373 	semzcnt = 0;
374 	for (q = sma->sem_pending; q; q = q->next) {
375 		struct sembuf * sops = q->sops;
376 		int nsops = q->nsops;
377 		int i;
378 		for (i = 0; i < nsops; i++)
379 			if (sops[i].sem_num == semnum
380 			    && (sops[i].sem_op == 0)
381 			    && !(sops[i].sem_flg & IPC_NOWAIT))
382 				semzcnt++;
383 	}
384 	return semzcnt;
385 }
386 
387 /* Free a semaphore set. */
freeary(int id)388 static void freeary (int id)
389 {
390 	struct sem_array *sma;
391 	struct sem_undo *un;
392 	struct sem_queue *q;
393 	int size;
394 
395 	sma = sem_rmid(id);
396 
397 	/* Invalidate the existing undo structures for this semaphore set.
398 	 * (They will be freed without any further action in sem_exit()
399 	 * or during the next semop.)
400 	 */
401 	for (un = sma->undo; un; un = un->id_next)
402 		un->semid = -1;
403 
404 	/* Wake up all pending processes and let them fail with EIDRM. */
405 	for (q = sma->sem_pending; q; q = q->next) {
406 		q->status = -EIDRM;
407 		q->prev = NULL;
408 		wake_up_process(q->sleeper); /* doesn't sleep */
409 	}
410 	sem_unlock(id);
411 
412 	used_sems -= sma->sem_nsems;
413 	size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
414 	ipc_free(sma, size);
415 }
416 
copy_semid_to_user(void * buf,struct semid64_ds * in,int version)417 static unsigned long copy_semid_to_user(void *buf, struct semid64_ds *in, int version)
418 {
419 	switch(version) {
420 	case IPC_64:
421 		return copy_to_user(buf, in, sizeof(*in));
422 	case IPC_OLD:
423 	    {
424 		struct semid_ds out;
425 
426 		ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
427 
428 		out.sem_otime	= in->sem_otime;
429 		out.sem_ctime	= in->sem_ctime;
430 		out.sem_nsems	= in->sem_nsems;
431 
432 		return copy_to_user(buf, &out, sizeof(out));
433 	    }
434 	default:
435 		return -EINVAL;
436 	}
437 }
438 
semctl_nolock(int semid,int semnum,int cmd,int version,union semun arg)439 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
440 {
441 	int err = -EINVAL;
442 
443 	switch(cmd) {
444 	case IPC_INFO:
445 	case SEM_INFO:
446 	{
447 		struct seminfo seminfo;
448 		int max_id;
449 
450 		memset(&seminfo,0,sizeof(seminfo));
451 		seminfo.semmni = sc_semmni;
452 		seminfo.semmns = sc_semmns;
453 		seminfo.semmsl = sc_semmsl;
454 		seminfo.semopm = sc_semopm;
455 		seminfo.semvmx = SEMVMX;
456 		seminfo.semmnu = SEMMNU;
457 		seminfo.semmap = SEMMAP;
458 		seminfo.semume = SEMUME;
459 		down(&sem_ids.sem);
460 		if (cmd == SEM_INFO) {
461 			seminfo.semusz = sem_ids.in_use;
462 			seminfo.semaem = used_sems;
463 		} else {
464 			seminfo.semusz = SEMUSZ;
465 			seminfo.semaem = SEMAEM;
466 		}
467 		max_id = sem_ids.max_id;
468 		up(&sem_ids.sem);
469 		if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
470 			return -EFAULT;
471 		return (max_id < 0) ? 0: max_id;
472 	}
473 	case SEM_STAT:
474 	{
475 		struct sem_array *sma;
476 		struct semid64_ds tbuf;
477 		int id;
478 
479 		if(semid >= sem_ids.size)
480 			return -EINVAL;
481 
482 		memset(&tbuf,0,sizeof(tbuf));
483 
484 		sma = sem_lock(semid);
485 		if(sma == NULL)
486 			return -EINVAL;
487 
488 		err = -EACCES;
489 		if (ipcperms (&sma->sem_perm, S_IRUGO))
490 			goto out_unlock;
491 		id = sem_buildid(semid, sma->sem_perm.seq);
492 
493 		kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
494 		tbuf.sem_otime  = sma->sem_otime;
495 		tbuf.sem_ctime  = sma->sem_ctime;
496 		tbuf.sem_nsems  = sma->sem_nsems;
497 		sem_unlock(semid);
498 		if (copy_semid_to_user (arg.buf, &tbuf, version))
499 			return -EFAULT;
500 		return id;
501 	}
502 	default:
503 		return -EINVAL;
504 	}
505 	return err;
506 out_unlock:
507 	sem_unlock(semid);
508 	return err;
509 }
510 
semctl_main(int semid,int semnum,int cmd,int version,union semun arg)511 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
512 {
513 	struct sem_array *sma;
514 	struct sem* curr;
515 	int err;
516 	ushort fast_sem_io[SEMMSL_FAST];
517 	ushort* sem_io = fast_sem_io;
518 	int nsems;
519 
520 	sma = sem_lock(semid);
521 	if(sma==NULL)
522 		return -EINVAL;
523 
524 	nsems = sma->sem_nsems;
525 
526 	err=-EIDRM;
527 	if (sem_checkid(sma,semid))
528 		goto out_unlock;
529 
530 	err = -EACCES;
531 	if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
532 		goto out_unlock;
533 
534 	switch (cmd) {
535 	case GETALL:
536 	{
537 		ushort *array = arg.array;
538 		int i;
539 
540 		if(nsems > SEMMSL_FAST) {
541 			sem_unlock(semid);
542 			sem_io = ipc_alloc(sizeof(ushort)*nsems);
543 			if(sem_io == NULL)
544 				return -ENOMEM;
545 			err = sem_revalidate(semid, sma, nsems, S_IRUGO);
546 			if(err)
547 				goto out_free;
548 		}
549 
550 		for (i = 0; i < sma->sem_nsems; i++)
551 			sem_io[i] = sma->sem_base[i].semval;
552 		sem_unlock(semid);
553 		err = 0;
554 		if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
555 			err = -EFAULT;
556 		goto out_free;
557 	}
558 	case SETALL:
559 	{
560 		int i;
561 		struct sem_undo *un;
562 
563 		sem_unlock(semid);
564 
565 		if(nsems > SEMMSL_FAST) {
566 			sem_io = ipc_alloc(sizeof(ushort)*nsems);
567 			if(sem_io == NULL)
568 				return -ENOMEM;
569 		}
570 
571 		if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
572 			err = -EFAULT;
573 			goto out_free;
574 		}
575 
576 		for (i = 0; i < nsems; i++) {
577 			if (sem_io[i] > SEMVMX) {
578 				err = -ERANGE;
579 				goto out_free;
580 			}
581 		}
582 		err = sem_revalidate(semid, sma, nsems, S_IWUGO);
583 		if(err)
584 			goto out_free;
585 
586 		for (i = 0; i < nsems; i++)
587 			sma->sem_base[i].semval = sem_io[i];
588 		for (un = sma->undo; un; un = un->id_next)
589 			for (i = 0; i < nsems; i++)
590 				un->semadj[i] = 0;
591 		sma->sem_ctime = CURRENT_TIME;
592 		/* maybe some queued-up processes were waiting for this */
593 		update_queue(sma);
594 		err = 0;
595 		goto out_unlock;
596 	}
597 	case IPC_STAT:
598 	{
599 		struct semid64_ds tbuf;
600 		memset(&tbuf,0,sizeof(tbuf));
601 		kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
602 		tbuf.sem_otime  = sma->sem_otime;
603 		tbuf.sem_ctime  = sma->sem_ctime;
604 		tbuf.sem_nsems  = sma->sem_nsems;
605 		sem_unlock(semid);
606 		if (copy_semid_to_user (arg.buf, &tbuf, version))
607 			return -EFAULT;
608 		return 0;
609 	}
610 	/* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
611 	}
612 	err = -EINVAL;
613 	if(semnum < 0 || semnum >= nsems)
614 		goto out_unlock;
615 
616 	curr = &sma->sem_base[semnum];
617 
618 	switch (cmd) {
619 	case GETVAL:
620 		err = curr->semval;
621 		goto out_unlock;
622 	case GETPID:
623 		err = curr->sempid;
624 		goto out_unlock;
625 	case GETNCNT:
626 		err = count_semncnt(sma,semnum);
627 		goto out_unlock;
628 	case GETZCNT:
629 		err = count_semzcnt(sma,semnum);
630 		goto out_unlock;
631 	case SETVAL:
632 	{
633 		int val = arg.val;
634 		struct sem_undo *un;
635 		err = -ERANGE;
636 		if (val > SEMVMX || val < 0)
637 			goto out_unlock;
638 
639 		for (un = sma->undo; un; un = un->id_next)
640 			un->semadj[semnum] = 0;
641 		curr->semval = val;
642 		curr->sempid = current->pid;
643 		sma->sem_ctime = CURRENT_TIME;
644 		/* maybe some queued-up processes were waiting for this */
645 		update_queue(sma);
646 		err = 0;
647 		goto out_unlock;
648 	}
649 	}
650 out_unlock:
651 	sem_unlock(semid);
652 out_free:
653 	if(sem_io != fast_sem_io)
654 		ipc_free(sem_io, sizeof(ushort)*nsems);
655 	return err;
656 }
657 
658 struct sem_setbuf {
659 	uid_t	uid;
660 	gid_t	gid;
661 	mode_t	mode;
662 };
663 
copy_semid_from_user(struct sem_setbuf * out,void * buf,int version)664 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void *buf, int version)
665 {
666 	switch(version) {
667 	case IPC_64:
668 	    {
669 		struct semid64_ds tbuf;
670 
671 		if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
672 			return -EFAULT;
673 
674 		out->uid	= tbuf.sem_perm.uid;
675 		out->gid	= tbuf.sem_perm.gid;
676 		out->mode	= tbuf.sem_perm.mode;
677 
678 		return 0;
679 	    }
680 	case IPC_OLD:
681 	    {
682 		struct semid_ds tbuf_old;
683 
684 		if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
685 			return -EFAULT;
686 
687 		out->uid	= tbuf_old.sem_perm.uid;
688 		out->gid	= tbuf_old.sem_perm.gid;
689 		out->mode	= tbuf_old.sem_perm.mode;
690 
691 		return 0;
692 	    }
693 	default:
694 		return -EINVAL;
695 	}
696 }
697 
semctl_down(int semid,int semnum,int cmd,int version,union semun arg)698 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
699 {
700 	struct sem_array *sma;
701 	int err;
702 	struct sem_setbuf setbuf;
703 	struct kern_ipc_perm *ipcp;
704 
705 	if(cmd == IPC_SET) {
706 		if(copy_semid_from_user (&setbuf, arg.buf, version))
707 			return -EFAULT;
708 	}
709 	sma = sem_lock(semid);
710 	if(sma==NULL)
711 		return -EINVAL;
712 
713 	if (sem_checkid(sma,semid)) {
714 		err=-EIDRM;
715 		goto out_unlock;
716 	}
717 	ipcp = &sma->sem_perm;
718 
719 	if (current->euid != ipcp->cuid &&
720 	    current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
721 	    	err=-EPERM;
722 		goto out_unlock;
723 	}
724 
725 	switch(cmd){
726 	case IPC_RMID:
727 		freeary(semid);
728 		err = 0;
729 		break;
730 	case IPC_SET:
731 		ipcp->uid = setbuf.uid;
732 		ipcp->gid = setbuf.gid;
733 		ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
734 				| (setbuf.mode & S_IRWXUGO);
735 		sma->sem_ctime = CURRENT_TIME;
736 		sem_unlock(semid);
737 		err = 0;
738 		break;
739 	default:
740 		sem_unlock(semid);
741 		err = -EINVAL;
742 		break;
743 	}
744 	return err;
745 
746 out_unlock:
747 	sem_unlock(semid);
748 	return err;
749 }
750 
sys_semctl(int semid,int semnum,int cmd,union semun arg)751 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
752 {
753 	int err = -EINVAL;
754 	int version;
755 
756 	if (semid < 0)
757 		return -EINVAL;
758 
759 	version = ipc_parse_version(&cmd);
760 
761 	switch(cmd) {
762 	case IPC_INFO:
763 	case SEM_INFO:
764 	case SEM_STAT:
765 		err = semctl_nolock(semid,semnum,cmd,version,arg);
766 		return err;
767 	case GETALL:
768 	case GETVAL:
769 	case GETPID:
770 	case GETNCNT:
771 	case GETZCNT:
772 	case IPC_STAT:
773 	case SETVAL:
774 	case SETALL:
775 		err = semctl_main(semid,semnum,cmd,version,arg);
776 		return err;
777 	case IPC_RMID:
778 	case IPC_SET:
779 		down(&sem_ids.sem);
780 		err = semctl_down(semid,semnum,cmd,version,arg);
781 		up(&sem_ids.sem);
782 		return err;
783 	default:
784 		return -EINVAL;
785 	}
786 }
787 
freeundos(struct sem_array * sma,struct sem_undo * un)788 static struct sem_undo* freeundos(struct sem_array *sma, struct sem_undo* un)
789 {
790 	struct sem_undo* u;
791 	struct sem_undo** up;
792 
793 	for(up = &current->semundo;(u=*up);up=&u->proc_next) {
794 		if(un==u) {
795 			un=u->proc_next;
796 			*up=un;
797 			kfree(u);
798 			return un;
799 		}
800 	}
801 	printk ("freeundos undo list error id=%d\n", un->semid);
802 	return un->proc_next;
803 }
804 
805 /* returns without sem_lock on error! */
alloc_undo(struct sem_array * sma,struct sem_undo ** unp,int semid,int alter)806 static int alloc_undo(struct sem_array *sma, struct sem_undo** unp, int semid, int alter)
807 {
808 	int size, nsems, error;
809 	struct sem_undo *un;
810 
811 	nsems = sma->sem_nsems;
812 	size = sizeof(struct sem_undo) + sizeof(short)*nsems;
813 	sem_unlock(semid);
814 
815 	un = (struct sem_undo *) kmalloc(size, GFP_KERNEL);
816 	if (!un)
817 		return -ENOMEM;
818 
819 	memset(un, 0, size);
820 	error = sem_revalidate(semid, sma, nsems, alter ? S_IWUGO : S_IRUGO);
821 	if(error) {
822 		kfree(un);
823 		return error;
824 	}
825 
826 	un->semadj = (short *) &un[1];
827 	un->semid = semid;
828 	un->proc_next = current->semundo;
829 	current->semundo = un;
830 	un->id_next = sma->undo;
831 	sma->undo = un;
832 	*unp = un;
833 	return 0;
834 }
835 
sys_semop(int semid,struct sembuf * tsops,unsigned nsops)836 asmlinkage long sys_semop (int semid, struct sembuf *tsops, unsigned nsops)
837 {
838 	return sys_semtimedop(semid, tsops, nsops, NULL);
839 }
840 
sys_semtimedop(int semid,struct sembuf * tsops,unsigned nsops,const struct timespec * timeout)841 asmlinkage long sys_semtimedop (int semid, struct sembuf *tsops,
842 			unsigned nsops, const struct timespec *timeout)
843 {
844 	int error = -EINVAL;
845 	struct sem_array *sma;
846 	struct sembuf fast_sops[SEMOPM_FAST];
847 	struct sembuf* sops = fast_sops, *sop;
848 	struct sem_undo *un;
849 	int undos = 0, decrease = 0, alter = 0;
850 	struct sem_queue queue;
851 	unsigned long jiffies_left = 0;
852 
853 	if (nsops < 1 || semid < 0)
854 		return -EINVAL;
855 	if (nsops > sc_semopm)
856 		return -E2BIG;
857 	if(nsops > SEMOPM_FAST) {
858 		sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
859 		if(sops==NULL)
860 			return -ENOMEM;
861 	}
862 	if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
863 		error=-EFAULT;
864 		goto out_free;
865 	}
866 	if (timeout) {
867 		struct timespec _timeout;
868 		if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
869 			error = -EFAULT;
870 			goto out_free;
871 		}
872 		if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
873 		    _timeout.tv_nsec >= 1000000000L) {
874 			error = -EINVAL;
875 			goto out_free;
876 		}
877 		jiffies_left = timespec_to_jiffies(&_timeout);
878 	}
879 	sma = sem_lock(semid);
880 	error=-EINVAL;
881 	if(sma==NULL)
882 		goto out_free;
883 	error = -EIDRM;
884 	if (sem_checkid(sma,semid))
885 		goto out_unlock_free;
886 	error = -EFBIG;
887 	for (sop = sops; sop < sops + nsops; sop++) {
888 		if (sop->sem_num >= sma->sem_nsems)
889 			goto out_unlock_free;
890 		if (sop->sem_flg & SEM_UNDO)
891 			undos++;
892 		if (sop->sem_op < 0)
893 			decrease = 1;
894 		if (sop->sem_op > 0)
895 			alter = 1;
896 	}
897 	alter |= decrease;
898 
899 	error = -EACCES;
900 	if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
901 		goto out_unlock_free;
902 	if (undos) {
903 		/* Make sure we have an undo structure
904 		 * for this process and this semaphore set.
905 		 */
906 		un=current->semundo;
907 		while(un != NULL) {
908 			if(un->semid==semid)
909 				break;
910 			if(un->semid==-1)
911 				un=freeundos(sma,un);
912 			 else
913 				un=un->proc_next;
914 		}
915 		if (!un) {
916 			error = alloc_undo(sma,&un,semid,alter);
917 			if(error)
918 				goto out_free;
919 		}
920 	} else
921 		un = NULL;
922 
923 	error = try_atomic_semop (sma, sops, nsops, un, current->pid, 0);
924 	if (error <= 0)
925 		goto update;
926 
927 	/* We need to sleep on this operation, so we put the current
928 	 * task into the pending queue and go to sleep.
929 	 */
930 
931 	queue.sma = sma;
932 	queue.sops = sops;
933 	queue.nsops = nsops;
934 	queue.undo = un;
935 	queue.pid = current->pid;
936 	queue.alter = decrease;
937 	queue.id = semid;
938 	if (alter)
939 		append_to_queue(sma ,&queue);
940 	else
941 		prepend_to_queue(sma ,&queue);
942 	current->semsleeping = &queue;
943 
944 	for (;;) {
945 		struct sem_array* tmp;
946 		queue.status = -EINTR;
947 		queue.sleeper = current;
948 		current->state = TASK_INTERRUPTIBLE;
949 		sem_unlock(semid);
950 
951 		if (timeout)
952 			jiffies_left = schedule_timeout(jiffies_left);
953 		else
954 			schedule();
955 
956 		tmp = sem_lock(semid);
957 		if(tmp==NULL) {
958 			if(queue.prev != NULL)
959 				BUG();
960 			current->semsleeping = NULL;
961 			error = -EIDRM;
962 			goto out_free;
963 		}
964 		/*
965 		 * If queue.status == 1 we where woken up and
966 		 * have to retry else we simply return.
967 		 * If an interrupt occurred we have to clean up the
968 		 * queue
969 		 *
970 		 */
971 		if (queue.status == 1)
972 		{
973 			error = try_atomic_semop (sma, sops, nsops, un,
974 						  current->pid,0);
975 			if (error <= 0)
976 				break;
977 		} else {
978 			error = queue.status;
979 			if (error == -EINTR && timeout && jiffies_left == 0)
980 				error = -EAGAIN;
981 			if (queue.prev) /* got Interrupt */
982 				break;
983 			/* Everything done by update_queue */
984 			current->semsleeping = NULL;
985 			goto out_unlock_free;
986 		}
987 	}
988 	current->semsleeping = NULL;
989 	remove_from_queue(sma,&queue);
990 update:
991 	if (alter)
992 		update_queue (sma);
993 out_unlock_free:
994 	sem_unlock(semid);
995 out_free:
996 	if(sops != fast_sops)
997 		kfree(sops);
998 	return error;
999 }
1000 
1001 /*
1002  * add semadj values to semaphores, free undo structures.
1003  * undo structures are not freed when semaphore arrays are destroyed
1004  * so some of them may be out of date.
1005  * IMPLEMENTATION NOTE: There is some confusion over whether the
1006  * set of adjustments that needs to be done should be done in an atomic
1007  * manner or not. That is, if we are attempting to decrement the semval
1008  * should we queue up and wait until we can do so legally?
1009  * The original implementation attempted to do this (queue and wait).
1010  * The current implementation does not do so. The POSIX standard
1011  * and SVID should be consulted to determine what behavior is mandated.
1012  */
sem_exit(void)1013 void sem_exit (void)
1014 {
1015 	struct sem_queue *q;
1016 	struct sem_undo *u, *un = NULL, **up, **unp;
1017 	struct sem_array *sma;
1018 	int nsems, i;
1019 
1020 	/* If the current process was sleeping for a semaphore,
1021 	 * remove it from the queue.
1022 	 */
1023 	if ((q = current->semsleeping)) {
1024 		int semid = q->id;
1025 		sma = sem_lock(semid);
1026 		current->semsleeping = NULL;
1027 
1028 		if (q->prev) {
1029 			if(sma==NULL)
1030 				BUG();
1031 			remove_from_queue(q->sma,q);
1032 		}
1033 		if(sma!=NULL)
1034 			sem_unlock(semid);
1035 	}
1036 
1037 	for (up = &current->semundo; (u = *up); *up = u->proc_next, kfree(u)) {
1038 		int semid = u->semid;
1039 		if(semid == -1)
1040 			continue;
1041 		sma = sem_lock(semid);
1042 		if (sma == NULL)
1043 			continue;
1044 
1045 		if (u->semid == -1)
1046 			goto next_entry;
1047 
1048 		if (sem_checkid(sma,u->semid))
1049 			goto next_entry;
1050 
1051 		/* remove u from the sma->undo list */
1052 		for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1053 			if (u == un)
1054 				goto found;
1055 		}
1056 		printk ("sem_exit undo list error id=%d\n", u->semid);
1057 		goto next_entry;
1058 found:
1059 		*unp = un->id_next;
1060 		/* perform adjustments registered in u */
1061 		nsems = sma->sem_nsems;
1062 		for (i = 0; i < nsems; i++) {
1063 			struct sem * sem = &sma->sem_base[i];
1064 			sem->semval += u->semadj[i];
1065 			if (sem->semval < 0)
1066 				sem->semval = 0; /* shouldn't happen */
1067 			sem->sempid = current->pid;
1068 		}
1069 		sma->sem_otime = CURRENT_TIME;
1070 		/* maybe some queued-up processes were waiting for this */
1071 		update_queue(sma);
1072 next_entry:
1073 		sem_unlock(semid);
1074 	}
1075 	current->semundo = NULL;
1076 }
1077 
1078 #ifdef CONFIG_PROC_FS
sysvipc_sem_read_proc(char * buffer,char ** start,off_t offset,int length,int * eof,void * data)1079 static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
1080 {
1081 	off_t pos = 0;
1082 	off_t begin = 0;
1083 	int i, len = 0;
1084 
1085 	len += sprintf(buffer, "       key      semid perms      nsems   uid   gid  cuid  cgid      otime      ctime\n");
1086 	down(&sem_ids.sem);
1087 
1088 	for(i = 0; i <= sem_ids.max_id; i++) {
1089 		struct sem_array *sma;
1090 		sma = sem_lock(i);
1091 		if(sma) {
1092 			len += sprintf(buffer + len, "%10d %10d  %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1093 				sma->sem_perm.key,
1094 				sem_buildid(i,sma->sem_perm.seq),
1095 				sma->sem_perm.mode,
1096 				sma->sem_nsems,
1097 				sma->sem_perm.uid,
1098 				sma->sem_perm.gid,
1099 				sma->sem_perm.cuid,
1100 				sma->sem_perm.cgid,
1101 				sma->sem_otime,
1102 				sma->sem_ctime);
1103 			sem_unlock(i);
1104 
1105 			pos += len;
1106 			if(pos < offset) {
1107 				len = 0;
1108 	    			begin = pos;
1109 			}
1110 			if(pos > offset + length)
1111 				goto done;
1112 		}
1113 	}
1114 	*eof = 1;
1115 done:
1116 	up(&sem_ids.sem);
1117 	*start = buffer + (offset - begin);
1118 	len -= (offset - begin);
1119 	if(len > length)
1120 		len = length;
1121 	if(len < 0)
1122 		len = 0;
1123 	return len;
1124 }
1125 #endif
1126