]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - ipc/sem.c
IPC/semaphores: consolidate SEM_STAT and IPC_STAT commands
[thirdparty/kernel/stable.git] / ipc / sem.c
CommitLineData
1da177e4
LT
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 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
55 *
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
57 *
58 * SMP-threaded, sysctl's added
624dffcb 59 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
1da177e4
LT
60 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
62 * Lockless wakeup
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
073115d6
SG
64 *
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
e3893534
KK
67 *
68 * namespaces support
69 * OpenVZ, SWsoft Inc.
70 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
71 */
72
1da177e4
LT
73#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/init.h>
76#include <linux/proc_fs.h>
77#include <linux/time.h>
1da177e4
LT
78#include <linux/security.h>
79#include <linux/syscalls.h>
80#include <linux/audit.h>
c59ede7b 81#include <linux/capability.h>
19b4946c 82#include <linux/seq_file.h>
3e148c79 83#include <linux/rwsem.h>
e3893534 84#include <linux/nsproxy.h>
ae5e1b22 85#include <linux/ipc_namespace.h>
5f921ae9 86
1da177e4
LT
87#include <asm/uaccess.h>
88#include "util.h"
89
e3893534
KK
90#define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
91
e3893534 92#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
1b531f21
ND
93#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
94#define sem_buildid(id, seq) ipc_buildid(id, seq)
1da177e4 95
e3893534 96static struct ipc_ids init_sem_ids;
1da177e4 97
7748dbfa 98static int newary(struct ipc_namespace *, struct ipc_params *);
7ca7e564 99static void freeary(struct ipc_namespace *, struct sem_array *);
1da177e4 100#ifdef CONFIG_PROC_FS
19b4946c 101static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
1da177e4
LT
102#endif
103
104#define SEMMSL_FAST 256 /* 512 bytes on stack */
105#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
106
107/*
108 * linked list protection:
109 * sem_undo.id_next,
110 * sem_array.sem_pending{,last},
111 * sem_array.sem_undo: sem_lock() for read/write
112 * sem_undo.proc_next: only "current" is allowed to read/write that field.
113 *
114 */
115
e3893534
KK
116#define sc_semmsl sem_ctls[0]
117#define sc_semmns sem_ctls[1]
118#define sc_semopm sem_ctls[2]
119#define sc_semmni sem_ctls[3]
120
7d69a1f4 121static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
e3893534
KK
122{
123 ns->ids[IPC_SEM_IDS] = ids;
124 ns->sc_semmsl = SEMMSL;
125 ns->sc_semmns = SEMMNS;
126 ns->sc_semopm = SEMOPM;
127 ns->sc_semmni = SEMMNI;
128 ns->used_sems = 0;
7ca7e564 129 ipc_init_ids(ids);
e3893534
KK
130}
131
ae5e1b22 132#ifdef CONFIG_IPC_NS
e3893534
KK
133int sem_init_ns(struct ipc_namespace *ns)
134{
135 struct ipc_ids *ids;
136
137 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
138 if (ids == NULL)
139 return -ENOMEM;
140
141 __sem_init_ns(ns, ids);
142 return 0;
143}
144
145void sem_exit_ns(struct ipc_namespace *ns)
146{
e3893534 147 struct sem_array *sma;
b1ed88b4 148 struct kern_ipc_perm *perm;
7ca7e564
ND
149 int next_id;
150 int total, in_use;
e3893534 151
3e148c79 152 down_write(&sem_ids(ns).rw_mutex);
7ca7e564
ND
153
154 in_use = sem_ids(ns).in_use;
155
156 for (total = 0, next_id = 0; total < in_use; next_id++) {
b1ed88b4
PP
157 perm = idr_find(&sem_ids(ns).ipcs_idr, next_id);
158 if (perm == NULL)
e3893534 159 continue;
b1ed88b4
PP
160 ipc_lock_by_ptr(perm);
161 sma = container_of(perm, struct sem_array, sem_perm);
7ca7e564
ND
162 freeary(ns, sma);
163 total++;
e3893534 164 }
3e148c79 165 up_write(&sem_ids(ns).rw_mutex);
1da177e4 166
e3893534
KK
167 kfree(ns->ids[IPC_SEM_IDS]);
168 ns->ids[IPC_SEM_IDS] = NULL;
169}
ae5e1b22 170#endif
1da177e4
LT
171
172void __init sem_init (void)
173{
e3893534 174 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
19b4946c
MW
175 ipc_init_proc_interface("sysvipc/sem",
176 " key semid perms nsems uid gid cuid cgid otime ctime\n",
e3893534 177 IPC_SEM_IDS, sysvipc_sem_proc_show);
1da177e4
LT
178}
179
3e148c79
ND
180/*
181 * This routine is called in the paths where the rw_mutex is held to protect
182 * access to the idr tree.
183 */
184static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
185 int id)
186{
187 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
188
b1ed88b4
PP
189 if (IS_ERR(ipcp))
190 return (struct sem_array *)ipcp;
191
3e148c79
ND
192 return container_of(ipcp, struct sem_array, sem_perm);
193}
194
195/*
196 * sem_lock_(check_) routines are called in the paths where the rw_mutex
197 * is not held.
198 */
023a5355
ND
199static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
200{
03f02c76
ND
201 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
202
b1ed88b4
PP
203 if (IS_ERR(ipcp))
204 return (struct sem_array *)ipcp;
205
03f02c76 206 return container_of(ipcp, struct sem_array, sem_perm);
023a5355
ND
207}
208
209static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
210 int id)
211{
03f02c76
ND
212 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
213
b1ed88b4
PP
214 if (IS_ERR(ipcp))
215 return (struct sem_array *)ipcp;
216
03f02c76 217 return container_of(ipcp, struct sem_array, sem_perm);
023a5355
ND
218}
219
7ca7e564
ND
220static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
221{
222 ipc_rmid(&sem_ids(ns), &s->sem_perm);
223}
224
1da177e4
LT
225/*
226 * Lockless wakeup algorithm:
227 * Without the check/retry algorithm a lockless wakeup is possible:
228 * - queue.status is initialized to -EINTR before blocking.
229 * - wakeup is performed by
230 * * unlinking the queue entry from sma->sem_pending
231 * * setting queue.status to IN_WAKEUP
232 * This is the notification for the blocked thread that a
233 * result value is imminent.
234 * * call wake_up_process
235 * * set queue.status to the final value.
236 * - the previously blocked thread checks queue.status:
237 * * if it's IN_WAKEUP, then it must wait until the value changes
238 * * if it's not -EINTR, then the operation was completed by
239 * update_queue. semtimedop can return queue.status without
5f921ae9 240 * performing any operation on the sem array.
1da177e4
LT
241 * * otherwise it must acquire the spinlock and check what's up.
242 *
243 * The two-stage algorithm is necessary to protect against the following
244 * races:
245 * - if queue.status is set after wake_up_process, then the woken up idle
246 * thread could race forward and try (and fail) to acquire sma->lock
247 * before update_queue had a chance to set queue.status
248 * - if queue.status is written before wake_up_process and if the
249 * blocked process is woken up by a signal between writing
250 * queue.status and the wake_up_process, then the woken up
251 * process could return from semtimedop and die by calling
252 * sys_exit before wake_up_process is called. Then wake_up_process
253 * will oops, because the task structure is already invalid.
254 * (yes, this happened on s390 with sysv msg).
255 *
256 */
257#define IN_WAKEUP 1
258
f4566f04
ND
259/**
260 * newary - Create a new semaphore set
261 * @ns: namespace
262 * @params: ptr to the structure that contains key, semflg and nsems
263 *
3e148c79 264 * Called with sem_ids.rw_mutex held (as a writer)
f4566f04
ND
265 */
266
7748dbfa 267static int newary(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4
LT
268{
269 int id;
270 int retval;
271 struct sem_array *sma;
272 int size;
7748dbfa
ND
273 key_t key = params->key;
274 int nsems = params->u.nsems;
275 int semflg = params->flg;
1da177e4
LT
276
277 if (!nsems)
278 return -EINVAL;
e3893534 279 if (ns->used_sems + nsems > ns->sc_semmns)
1da177e4
LT
280 return -ENOSPC;
281
282 size = sizeof (*sma) + nsems * sizeof (struct sem);
283 sma = ipc_rcu_alloc(size);
284 if (!sma) {
285 return -ENOMEM;
286 }
287 memset (sma, 0, size);
288
289 sma->sem_perm.mode = (semflg & S_IRWXUGO);
290 sma->sem_perm.key = key;
291
292 sma->sem_perm.security = NULL;
293 retval = security_sem_alloc(sma);
294 if (retval) {
295 ipc_rcu_putref(sma);
296 return retval;
297 }
298
e3893534 299 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
283bb7fa 300 if (id < 0) {
1da177e4
LT
301 security_sem_free(sma);
302 ipc_rcu_putref(sma);
283bb7fa 303 return id;
1da177e4 304 }
e3893534 305 ns->used_sems += nsems;
1da177e4 306
1b531f21 307 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
1da177e4
LT
308 sma->sem_base = (struct sem *) &sma[1];
309 /* sma->sem_pending = NULL; */
310 sma->sem_pending_last = &sma->sem_pending;
311 /* sma->undo = NULL; */
312 sma->sem_nsems = nsems;
313 sma->sem_ctime = get_seconds();
314 sem_unlock(sma);
315
7ca7e564 316 return sma->sem_perm.id;
1da177e4
LT
317}
318
7748dbfa 319
f4566f04 320/*
3e148c79 321 * Called with sem_ids.rw_mutex and ipcp locked.
f4566f04 322 */
03f02c76 323static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
7748dbfa 324{
03f02c76
ND
325 struct sem_array *sma;
326
327 sma = container_of(ipcp, struct sem_array, sem_perm);
328 return security_sem_associate(sma, semflg);
7748dbfa
ND
329}
330
f4566f04 331/*
3e148c79 332 * Called with sem_ids.rw_mutex and ipcp locked.
f4566f04 333 */
03f02c76
ND
334static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
335 struct ipc_params *params)
7748dbfa 336{
03f02c76
ND
337 struct sem_array *sma;
338
339 sma = container_of(ipcp, struct sem_array, sem_perm);
340 if (params->u.nsems > sma->sem_nsems)
7748dbfa
ND
341 return -EINVAL;
342
343 return 0;
344}
345
346asmlinkage long sys_semget(key_t key, int nsems, int semflg)
1da177e4 347{
e3893534 348 struct ipc_namespace *ns;
7748dbfa
ND
349 struct ipc_ops sem_ops;
350 struct ipc_params sem_params;
e3893534
KK
351
352 ns = current->nsproxy->ipc_ns;
1da177e4 353
e3893534 354 if (nsems < 0 || nsems > ns->sc_semmsl)
1da177e4 355 return -EINVAL;
7ca7e564 356
7748dbfa
ND
357 sem_ops.getnew = newary;
358 sem_ops.associate = sem_security;
359 sem_ops.more_checks = sem_more_checks;
360
361 sem_params.key = key;
362 sem_params.flg = semflg;
363 sem_params.u.nsems = nsems;
1da177e4 364
7748dbfa 365 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
1da177e4
LT
366}
367
368/* Manage the doubly linked list sma->sem_pending as a FIFO:
369 * insert new queue elements at the tail sma->sem_pending_last.
370 */
371static inline void append_to_queue (struct sem_array * sma,
372 struct sem_queue * q)
373{
374 *(q->prev = sma->sem_pending_last) = q;
375 *(sma->sem_pending_last = &q->next) = NULL;
376}
377
378static inline void prepend_to_queue (struct sem_array * sma,
379 struct sem_queue * q)
380{
381 q->next = sma->sem_pending;
382 *(q->prev = &sma->sem_pending) = q;
383 if (q->next)
384 q->next->prev = &q->next;
385 else /* sma->sem_pending_last == &sma->sem_pending */
386 sma->sem_pending_last = &q->next;
387}
388
389static inline void remove_from_queue (struct sem_array * sma,
390 struct sem_queue * q)
391{
392 *(q->prev) = q->next;
393 if (q->next)
394 q->next->prev = q->prev;
395 else /* sma->sem_pending_last == &q->next */
396 sma->sem_pending_last = q->prev;
397 q->prev = NULL; /* mark as removed */
398}
399
400/*
401 * Determine whether a sequence of semaphore operations would succeed
402 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
403 */
404
405static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
406 int nsops, struct sem_undo *un, int pid)
407{
408 int result, sem_op;
409 struct sembuf *sop;
410 struct sem * curr;
411
412 for (sop = sops; sop < sops + nsops; sop++) {
413 curr = sma->sem_base + sop->sem_num;
414 sem_op = sop->sem_op;
415 result = curr->semval;
416
417 if (!sem_op && result)
418 goto would_block;
419
420 result += sem_op;
421 if (result < 0)
422 goto would_block;
423 if (result > SEMVMX)
424 goto out_of_range;
425 if (sop->sem_flg & SEM_UNDO) {
426 int undo = un->semadj[sop->sem_num] - sem_op;
427 /*
428 * Exceeding the undo range is an error.
429 */
430 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
431 goto out_of_range;
432 }
433 curr->semval = result;
434 }
435
436 sop--;
437 while (sop >= sops) {
438 sma->sem_base[sop->sem_num].sempid = pid;
439 if (sop->sem_flg & SEM_UNDO)
440 un->semadj[sop->sem_num] -= sop->sem_op;
441 sop--;
442 }
443
444 sma->sem_otime = get_seconds();
445 return 0;
446
447out_of_range:
448 result = -ERANGE;
449 goto undo;
450
451would_block:
452 if (sop->sem_flg & IPC_NOWAIT)
453 result = -EAGAIN;
454 else
455 result = 1;
456
457undo:
458 sop--;
459 while (sop >= sops) {
460 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
461 sop--;
462 }
463
464 return result;
465}
466
467/* Go through the pending queue for the indicated semaphore
468 * looking for tasks that can be completed.
469 */
470static void update_queue (struct sem_array * sma)
471{
472 int error;
473 struct sem_queue * q;
474
475 q = sma->sem_pending;
476 while(q) {
477 error = try_atomic_semop(sma, q->sops, q->nsops,
478 q->undo, q->pid);
479
480 /* Does q->sleeper still need to sleep? */
481 if (error <= 0) {
482 struct sem_queue *n;
483 remove_from_queue(sma,q);
484 q->status = IN_WAKEUP;
485 /*
486 * Continue scanning. The next operation
487 * that must be checked depends on the type of the
488 * completed operation:
489 * - if the operation modified the array, then
490 * restart from the head of the queue and
491 * check for threads that might be waiting
492 * for semaphore values to become 0.
493 * - if the operation didn't modify the array,
494 * then just continue.
495 */
496 if (q->alter)
497 n = sma->sem_pending;
498 else
499 n = q->next;
500 wake_up_process(q->sleeper);
501 /* hands-off: q will disappear immediately after
502 * writing q->status.
503 */
1224b375 504 smp_wmb();
1da177e4
LT
505 q->status = error;
506 q = n;
507 } else {
508 q = q->next;
509 }
510 }
511}
512
513/* The following counts are associated to each semaphore:
514 * semncnt number of tasks waiting on semval being nonzero
515 * semzcnt number of tasks waiting on semval being zero
516 * This model assumes that a task waits on exactly one semaphore.
517 * Since semaphore operations are to be performed atomically, tasks actually
518 * wait on a whole sequence of semaphores simultaneously.
519 * The counts we return here are a rough approximation, but still
520 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
521 */
522static int count_semncnt (struct sem_array * sma, ushort semnum)
523{
524 int semncnt;
525 struct sem_queue * q;
526
527 semncnt = 0;
528 for (q = sma->sem_pending; q; q = q->next) {
529 struct sembuf * sops = q->sops;
530 int nsops = q->nsops;
531 int i;
532 for (i = 0; i < nsops; i++)
533 if (sops[i].sem_num == semnum
534 && (sops[i].sem_op < 0)
535 && !(sops[i].sem_flg & IPC_NOWAIT))
536 semncnt++;
537 }
538 return semncnt;
539}
540static int count_semzcnt (struct sem_array * sma, ushort semnum)
541{
542 int semzcnt;
543 struct sem_queue * q;
544
545 semzcnt = 0;
546 for (q = sma->sem_pending; q; q = q->next) {
547 struct sembuf * sops = q->sops;
548 int nsops = q->nsops;
549 int i;
550 for (i = 0; i < nsops; i++)
551 if (sops[i].sem_num == semnum
552 && (sops[i].sem_op == 0)
553 && !(sops[i].sem_flg & IPC_NOWAIT))
554 semzcnt++;
555 }
556 return semzcnt;
557}
558
3e148c79
ND
559/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
560 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
561 * remains locked on exit.
1da177e4 562 */
7ca7e564 563static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
1da177e4
LT
564{
565 struct sem_undo *un;
566 struct sem_queue *q;
1da177e4
LT
567
568 /* Invalidate the existing undo structures for this semaphore set.
569 * (They will be freed without any further action in exit_sem()
570 * or during the next semop.)
571 */
572 for (un = sma->undo; un; un = un->id_next)
573 un->semid = -1;
574
575 /* Wake up all pending processes and let them fail with EIDRM. */
576 q = sma->sem_pending;
577 while(q) {
578 struct sem_queue *n;
579 /* lazy remove_from_queue: we are killing the whole queue */
580 q->prev = NULL;
581 n = q->next;
582 q->status = IN_WAKEUP;
583 wake_up_process(q->sleeper); /* doesn't sleep */
6003a93e 584 smp_wmb();
1da177e4
LT
585 q->status = -EIDRM; /* hands-off q */
586 q = n;
587 }
588
7ca7e564
ND
589 /* Remove the semaphore set from the IDR */
590 sem_rmid(ns, sma);
1da177e4
LT
591 sem_unlock(sma);
592
e3893534 593 ns->used_sems -= sma->sem_nsems;
1da177e4
LT
594 security_sem_free(sma);
595 ipc_rcu_putref(sma);
596}
597
598static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
599{
600 switch(version) {
601 case IPC_64:
602 return copy_to_user(buf, in, sizeof(*in));
603 case IPC_OLD:
604 {
605 struct semid_ds out;
606
607 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
608
609 out.sem_otime = in->sem_otime;
610 out.sem_ctime = in->sem_ctime;
611 out.sem_nsems = in->sem_nsems;
612
613 return copy_to_user(buf, &out, sizeof(out));
614 }
615 default:
616 return -EINVAL;
617 }
618}
619
4b9fcb0e
PP
620static int semctl_nolock(struct ipc_namespace *ns, int semid,
621 int cmd, int version, union semun arg)
1da177e4
LT
622{
623 int err = -EINVAL;
624 struct sem_array *sma;
625
626 switch(cmd) {
627 case IPC_INFO:
628 case SEM_INFO:
629 {
630 struct seminfo seminfo;
631 int max_id;
632
633 err = security_sem_semctl(NULL, cmd);
634 if (err)
635 return err;
636
637 memset(&seminfo,0,sizeof(seminfo));
e3893534
KK
638 seminfo.semmni = ns->sc_semmni;
639 seminfo.semmns = ns->sc_semmns;
640 seminfo.semmsl = ns->sc_semmsl;
641 seminfo.semopm = ns->sc_semopm;
1da177e4
LT
642 seminfo.semvmx = SEMVMX;
643 seminfo.semmnu = SEMMNU;
644 seminfo.semmap = SEMMAP;
645 seminfo.semume = SEMUME;
3e148c79 646 down_read(&sem_ids(ns).rw_mutex);
1da177e4 647 if (cmd == SEM_INFO) {
e3893534
KK
648 seminfo.semusz = sem_ids(ns).in_use;
649 seminfo.semaem = ns->used_sems;
1da177e4
LT
650 } else {
651 seminfo.semusz = SEMUSZ;
652 seminfo.semaem = SEMAEM;
653 }
7ca7e564 654 max_id = ipc_get_maxid(&sem_ids(ns));
3e148c79 655 up_read(&sem_ids(ns).rw_mutex);
1da177e4
LT
656 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
657 return -EFAULT;
658 return (max_id < 0) ? 0: max_id;
659 }
4b9fcb0e 660 case IPC_STAT:
1da177e4
LT
661 case SEM_STAT:
662 {
663 struct semid64_ds tbuf;
664 int id;
665
4b9fcb0e
PP
666 if (cmd == SEM_STAT) {
667 sma = sem_lock(ns, semid);
668 if (IS_ERR(sma))
669 return PTR_ERR(sma);
670 id = sma->sem_perm.id;
671 } else {
672 sma = sem_lock_check(ns, semid);
673 if (IS_ERR(sma))
674 return PTR_ERR(sma);
675 id = 0;
676 }
1da177e4
LT
677
678 err = -EACCES;
679 if (ipcperms (&sma->sem_perm, S_IRUGO))
680 goto out_unlock;
681
682 err = security_sem_semctl(sma, cmd);
683 if (err)
684 goto out_unlock;
685
023a5355
ND
686 memset(&tbuf, 0, sizeof(tbuf));
687
1da177e4
LT
688 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
689 tbuf.sem_otime = sma->sem_otime;
690 tbuf.sem_ctime = sma->sem_ctime;
691 tbuf.sem_nsems = sma->sem_nsems;
692 sem_unlock(sma);
693 if (copy_semid_to_user (arg.buf, &tbuf, version))
694 return -EFAULT;
695 return id;
696 }
697 default:
698 return -EINVAL;
699 }
700 return err;
701out_unlock:
702 sem_unlock(sma);
703 return err;
704}
705
e3893534
KK
706static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
707 int cmd, int version, union semun arg)
1da177e4
LT
708{
709 struct sem_array *sma;
710 struct sem* curr;
711 int err;
712 ushort fast_sem_io[SEMMSL_FAST];
713 ushort* sem_io = fast_sem_io;
714 int nsems;
715
023a5355
ND
716 sma = sem_lock_check(ns, semid);
717 if (IS_ERR(sma))
718 return PTR_ERR(sma);
1da177e4
LT
719
720 nsems = sma->sem_nsems;
721
1da177e4
LT
722 err = -EACCES;
723 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
724 goto out_unlock;
725
726 err = security_sem_semctl(sma, cmd);
727 if (err)
728 goto out_unlock;
729
730 err = -EACCES;
731 switch (cmd) {
732 case GETALL:
733 {
734 ushort __user *array = arg.array;
735 int i;
736
737 if(nsems > SEMMSL_FAST) {
738 ipc_rcu_getref(sma);
739 sem_unlock(sma);
740
741 sem_io = ipc_alloc(sizeof(ushort)*nsems);
742 if(sem_io == NULL) {
743 ipc_lock_by_ptr(&sma->sem_perm);
744 ipc_rcu_putref(sma);
745 sem_unlock(sma);
746 return -ENOMEM;
747 }
748
749 ipc_lock_by_ptr(&sma->sem_perm);
750 ipc_rcu_putref(sma);
751 if (sma->sem_perm.deleted) {
752 sem_unlock(sma);
753 err = -EIDRM;
754 goto out_free;
755 }
756 }
757
758 for (i = 0; i < sma->sem_nsems; i++)
759 sem_io[i] = sma->sem_base[i].semval;
760 sem_unlock(sma);
761 err = 0;
762 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
763 err = -EFAULT;
764 goto out_free;
765 }
766 case SETALL:
767 {
768 int i;
769 struct sem_undo *un;
770
771 ipc_rcu_getref(sma);
772 sem_unlock(sma);
773
774 if(nsems > SEMMSL_FAST) {
775 sem_io = ipc_alloc(sizeof(ushort)*nsems);
776 if(sem_io == NULL) {
777 ipc_lock_by_ptr(&sma->sem_perm);
778 ipc_rcu_putref(sma);
779 sem_unlock(sma);
780 return -ENOMEM;
781 }
782 }
783
784 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
785 ipc_lock_by_ptr(&sma->sem_perm);
786 ipc_rcu_putref(sma);
787 sem_unlock(sma);
788 err = -EFAULT;
789 goto out_free;
790 }
791
792 for (i = 0; i < nsems; i++) {
793 if (sem_io[i] > SEMVMX) {
794 ipc_lock_by_ptr(&sma->sem_perm);
795 ipc_rcu_putref(sma);
796 sem_unlock(sma);
797 err = -ERANGE;
798 goto out_free;
799 }
800 }
801 ipc_lock_by_ptr(&sma->sem_perm);
802 ipc_rcu_putref(sma);
803 if (sma->sem_perm.deleted) {
804 sem_unlock(sma);
805 err = -EIDRM;
806 goto out_free;
807 }
808
809 for (i = 0; i < nsems; i++)
810 sma->sem_base[i].semval = sem_io[i];
811 for (un = sma->undo; un; un = un->id_next)
812 for (i = 0; i < nsems; i++)
813 un->semadj[i] = 0;
814 sma->sem_ctime = get_seconds();
815 /* maybe some queued-up processes were waiting for this */
816 update_queue(sma);
817 err = 0;
818 goto out_unlock;
819 }
1da177e4
LT
820 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
821 }
822 err = -EINVAL;
823 if(semnum < 0 || semnum >= nsems)
824 goto out_unlock;
825
826 curr = &sma->sem_base[semnum];
827
828 switch (cmd) {
829 case GETVAL:
830 err = curr->semval;
831 goto out_unlock;
832 case GETPID:
833 err = curr->sempid;
834 goto out_unlock;
835 case GETNCNT:
836 err = count_semncnt(sma,semnum);
837 goto out_unlock;
838 case GETZCNT:
839 err = count_semzcnt(sma,semnum);
840 goto out_unlock;
841 case SETVAL:
842 {
843 int val = arg.val;
844 struct sem_undo *un;
845 err = -ERANGE;
846 if (val > SEMVMX || val < 0)
847 goto out_unlock;
848
849 for (un = sma->undo; un; un = un->id_next)
850 un->semadj[semnum] = 0;
851 curr->semval = val;
b488893a 852 curr->sempid = task_tgid_vnr(current);
1da177e4
LT
853 sma->sem_ctime = get_seconds();
854 /* maybe some queued-up processes were waiting for this */
855 update_queue(sma);
856 err = 0;
857 goto out_unlock;
858 }
859 }
860out_unlock:
861 sem_unlock(sma);
862out_free:
863 if(sem_io != fast_sem_io)
864 ipc_free(sem_io, sizeof(ushort)*nsems);
865 return err;
866}
867
868struct sem_setbuf {
869 uid_t uid;
870 gid_t gid;
871 mode_t mode;
872};
873
874static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
875{
876 switch(version) {
877 case IPC_64:
878 {
879 struct semid64_ds tbuf;
880
881 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
882 return -EFAULT;
883
884 out->uid = tbuf.sem_perm.uid;
885 out->gid = tbuf.sem_perm.gid;
886 out->mode = tbuf.sem_perm.mode;
887
888 return 0;
889 }
890 case IPC_OLD:
891 {
892 struct semid_ds tbuf_old;
893
894 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
895 return -EFAULT;
896
897 out->uid = tbuf_old.sem_perm.uid;
898 out->gid = tbuf_old.sem_perm.gid;
899 out->mode = tbuf_old.sem_perm.mode;
900
901 return 0;
902 }
903 default:
904 return -EINVAL;
905 }
906}
907
e3893534
KK
908static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
909 int cmd, int version, union semun arg)
1da177e4
LT
910{
911 struct sem_array *sma;
912 int err;
8e1c091c 913 struct sem_setbuf uninitialized_var(setbuf);
1da177e4
LT
914 struct kern_ipc_perm *ipcp;
915
916 if(cmd == IPC_SET) {
917 if(copy_semid_from_user (&setbuf, arg.buf, version))
918 return -EFAULT;
1da177e4 919 }
3e148c79 920 sma = sem_lock_check_down(ns, semid);
023a5355
ND
921 if (IS_ERR(sma))
922 return PTR_ERR(sma);
1da177e4 923
1da177e4 924 ipcp = &sma->sem_perm;
073115d6
SG
925
926 err = audit_ipc_obj(ipcp);
927 if (err)
928 goto out_unlock;
929
ac03221a
LK
930 if (cmd == IPC_SET) {
931 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
932 if (err)
933 goto out_unlock;
934 }
1da177e4
LT
935 if (current->euid != ipcp->cuid &&
936 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
937 err=-EPERM;
938 goto out_unlock;
939 }
940
941 err = security_sem_semctl(sma, cmd);
942 if (err)
943 goto out_unlock;
944
945 switch(cmd){
946 case IPC_RMID:
7ca7e564 947 freeary(ns, sma);
1da177e4
LT
948 err = 0;
949 break;
950 case IPC_SET:
951 ipcp->uid = setbuf.uid;
952 ipcp->gid = setbuf.gid;
953 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
954 | (setbuf.mode & S_IRWXUGO);
955 sma->sem_ctime = get_seconds();
956 sem_unlock(sma);
957 err = 0;
958 break;
959 default:
960 sem_unlock(sma);
961 err = -EINVAL;
962 break;
963 }
964 return err;
965
966out_unlock:
967 sem_unlock(sma);
968 return err;
969}
970
971asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
972{
973 int err = -EINVAL;
974 int version;
e3893534 975 struct ipc_namespace *ns;
1da177e4
LT
976
977 if (semid < 0)
978 return -EINVAL;
979
980 version = ipc_parse_version(&cmd);
e3893534 981 ns = current->nsproxy->ipc_ns;
1da177e4
LT
982
983 switch(cmd) {
984 case IPC_INFO:
985 case SEM_INFO:
4b9fcb0e 986 case IPC_STAT:
1da177e4 987 case SEM_STAT:
4b9fcb0e 988 err = semctl_nolock(ns, semid, cmd, version, arg);
1da177e4
LT
989 return err;
990 case GETALL:
991 case GETVAL:
992 case GETPID:
993 case GETNCNT:
994 case GETZCNT:
1da177e4
LT
995 case SETVAL:
996 case SETALL:
e3893534 997 err = semctl_main(ns,semid,semnum,cmd,version,arg);
1da177e4
LT
998 return err;
999 case IPC_RMID:
1000 case IPC_SET:
3e148c79 1001 down_write(&sem_ids(ns).rw_mutex);
e3893534 1002 err = semctl_down(ns,semid,semnum,cmd,version,arg);
3e148c79 1003 up_write(&sem_ids(ns).rw_mutex);
1da177e4
LT
1004 return err;
1005 default:
1006 return -EINVAL;
1007 }
1008}
1009
1da177e4
LT
1010/* If the task doesn't already have a undo_list, then allocate one
1011 * here. We guarantee there is only one thread using this undo list,
1012 * and current is THE ONE
1013 *
1014 * If this allocation and assignment succeeds, but later
1015 * portions of this code fail, there is no need to free the sem_undo_list.
1016 * Just let it stay associated with the task, and it'll be freed later
1017 * at exit time.
1018 *
1019 * This can block, so callers must hold no locks.
1020 */
1021static inline int get_undo_list(struct sem_undo_list **undo_listp)
1022{
1023 struct sem_undo_list *undo_list;
1da177e4
LT
1024
1025 undo_list = current->sysvsem.undo_list;
1026 if (!undo_list) {
2453a306 1027 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1da177e4
LT
1028 if (undo_list == NULL)
1029 return -ENOMEM;
00a5dfdb 1030 spin_lock_init(&undo_list->lock);
1da177e4
LT
1031 atomic_set(&undo_list->refcnt, 1);
1032 current->sysvsem.undo_list = undo_list;
1033 }
1034 *undo_listp = undo_list;
1035 return 0;
1036}
1037
1038static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1039{
1040 struct sem_undo **last, *un;
1041
1042 last = &ulp->proc_list;
1043 un = *last;
1044 while(un != NULL) {
1045 if(un->semid==semid)
1046 break;
1047 if(un->semid==-1) {
1048 *last=un->proc_next;
1049 kfree(un);
1050 } else {
1051 last=&un->proc_next;
1052 }
1053 un=*last;
1054 }
1055 return un;
1056}
1057
e3893534 1058static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1da177e4
LT
1059{
1060 struct sem_array *sma;
1061 struct sem_undo_list *ulp;
1062 struct sem_undo *un, *new;
1063 int nsems;
1064 int error;
1065
1066 error = get_undo_list(&ulp);
1067 if (error)
1068 return ERR_PTR(error);
1069
c530c6ac 1070 spin_lock(&ulp->lock);
1da177e4 1071 un = lookup_undo(ulp, semid);
c530c6ac 1072 spin_unlock(&ulp->lock);
1da177e4
LT
1073 if (likely(un!=NULL))
1074 goto out;
1075
1076 /* no undo structure around - allocate one. */
023a5355
ND
1077 sma = sem_lock_check(ns, semid);
1078 if (IS_ERR(sma))
1079 return ERR_PTR(PTR_ERR(sma));
1080
1da177e4
LT
1081 nsems = sma->sem_nsems;
1082 ipc_rcu_getref(sma);
1083 sem_unlock(sma);
1084
4668edc3 1085 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1da177e4
LT
1086 if (!new) {
1087 ipc_lock_by_ptr(&sma->sem_perm);
1088 ipc_rcu_putref(sma);
1089 sem_unlock(sma);
1090 return ERR_PTR(-ENOMEM);
1091 }
1da177e4
LT
1092 new->semadj = (short *) &new[1];
1093 new->semid = semid;
1094
c530c6ac 1095 spin_lock(&ulp->lock);
1da177e4
LT
1096 un = lookup_undo(ulp, semid);
1097 if (un) {
c530c6ac 1098 spin_unlock(&ulp->lock);
1da177e4
LT
1099 kfree(new);
1100 ipc_lock_by_ptr(&sma->sem_perm);
1101 ipc_rcu_putref(sma);
1102 sem_unlock(sma);
1103 goto out;
1104 }
1105 ipc_lock_by_ptr(&sma->sem_perm);
1106 ipc_rcu_putref(sma);
1107 if (sma->sem_perm.deleted) {
1108 sem_unlock(sma);
c530c6ac 1109 spin_unlock(&ulp->lock);
1da177e4
LT
1110 kfree(new);
1111 un = ERR_PTR(-EIDRM);
1112 goto out;
1113 }
1114 new->proc_next = ulp->proc_list;
1115 ulp->proc_list = new;
1116 new->id_next = sma->undo;
1117 sma->undo = new;
1118 sem_unlock(sma);
1119 un = new;
c530c6ac 1120 spin_unlock(&ulp->lock);
1da177e4
LT
1121out:
1122 return un;
1123}
1124
1125asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1126 unsigned nsops, const struct timespec __user *timeout)
1127{
1128 int error = -EINVAL;
1129 struct sem_array *sma;
1130 struct sembuf fast_sops[SEMOPM_FAST];
1131 struct sembuf* sops = fast_sops, *sop;
1132 struct sem_undo *un;
b78755ab 1133 int undos = 0, alter = 0, max;
1da177e4
LT
1134 struct sem_queue queue;
1135 unsigned long jiffies_left = 0;
e3893534
KK
1136 struct ipc_namespace *ns;
1137
1138 ns = current->nsproxy->ipc_ns;
1da177e4
LT
1139
1140 if (nsops < 1 || semid < 0)
1141 return -EINVAL;
e3893534 1142 if (nsops > ns->sc_semopm)
1da177e4
LT
1143 return -E2BIG;
1144 if(nsops > SEMOPM_FAST) {
1145 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1146 if(sops==NULL)
1147 return -ENOMEM;
1148 }
1149 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1150 error=-EFAULT;
1151 goto out_free;
1152 }
1153 if (timeout) {
1154 struct timespec _timeout;
1155 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1156 error = -EFAULT;
1157 goto out_free;
1158 }
1159 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1160 _timeout.tv_nsec >= 1000000000L) {
1161 error = -EINVAL;
1162 goto out_free;
1163 }
1164 jiffies_left = timespec_to_jiffies(&_timeout);
1165 }
1166 max = 0;
1167 for (sop = sops; sop < sops + nsops; sop++) {
1168 if (sop->sem_num >= max)
1169 max = sop->sem_num;
1170 if (sop->sem_flg & SEM_UNDO)
b78755ab
MS
1171 undos = 1;
1172 if (sop->sem_op != 0)
1da177e4
LT
1173 alter = 1;
1174 }
1da177e4
LT
1175
1176retry_undos:
1177 if (undos) {
e3893534 1178 un = find_undo(ns, semid);
1da177e4
LT
1179 if (IS_ERR(un)) {
1180 error = PTR_ERR(un);
1181 goto out_free;
1182 }
1183 } else
1184 un = NULL;
1185
023a5355
ND
1186 sma = sem_lock_check(ns, semid);
1187 if (IS_ERR(sma)) {
1188 error = PTR_ERR(sma);
1da177e4 1189 goto out_free;
023a5355
ND
1190 }
1191
1da177e4 1192 /*
023a5355 1193 * semid identifiers are not unique - find_undo may have
1da177e4
LT
1194 * allocated an undo structure, it was invalidated by an RMID
1195 * and now a new array with received the same id. Check and retry.
1196 */
1197 if (un && un->semid == -1) {
1198 sem_unlock(sma);
1199 goto retry_undos;
1200 }
1201 error = -EFBIG;
1202 if (max >= sma->sem_nsems)
1203 goto out_unlock_free;
1204
1205 error = -EACCES;
1206 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1207 goto out_unlock_free;
1208
1209 error = security_sem_semop(sma, sops, nsops, alter);
1210 if (error)
1211 goto out_unlock_free;
1212
b488893a 1213 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
1da177e4
LT
1214 if (error <= 0) {
1215 if (alter && error == 0)
1216 update_queue (sma);
1217 goto out_unlock_free;
1218 }
1219
1220 /* We need to sleep on this operation, so we put the current
1221 * task into the pending queue and go to sleep.
1222 */
1223
1224 queue.sma = sma;
1225 queue.sops = sops;
1226 queue.nsops = nsops;
1227 queue.undo = un;
b488893a 1228 queue.pid = task_tgid_vnr(current);
1da177e4
LT
1229 queue.id = semid;
1230 queue.alter = alter;
1231 if (alter)
1232 append_to_queue(sma ,&queue);
1233 else
1234 prepend_to_queue(sma ,&queue);
1235
1236 queue.status = -EINTR;
1237 queue.sleeper = current;
1238 current->state = TASK_INTERRUPTIBLE;
1239 sem_unlock(sma);
1240
1241 if (timeout)
1242 jiffies_left = schedule_timeout(jiffies_left);
1243 else
1244 schedule();
1245
1246 error = queue.status;
1247 while(unlikely(error == IN_WAKEUP)) {
1248 cpu_relax();
1249 error = queue.status;
1250 }
1251
1252 if (error != -EINTR) {
1253 /* fast path: update_queue already obtained all requested
1254 * resources */
1255 goto out_free;
1256 }
1257
e3893534 1258 sma = sem_lock(ns, semid);
023a5355 1259 if (IS_ERR(sma)) {
27315c96 1260 BUG_ON(queue.prev != NULL);
1da177e4
LT
1261 error = -EIDRM;
1262 goto out_free;
1263 }
1264
1265 /*
1266 * If queue.status != -EINTR we are woken up by another process
1267 */
1268 error = queue.status;
1269 if (error != -EINTR) {
1270 goto out_unlock_free;
1271 }
1272
1273 /*
1274 * If an interrupt occurred we have to clean up the queue
1275 */
1276 if (timeout && jiffies_left == 0)
1277 error = -EAGAIN;
1278 remove_from_queue(sma,&queue);
1279 goto out_unlock_free;
1280
1281out_unlock_free:
1282 sem_unlock(sma);
1283out_free:
1284 if(sops != fast_sops)
1285 kfree(sops);
1286 return error;
1287}
1288
1289asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1290{
1291 return sys_semtimedop(semid, tsops, nsops, NULL);
1292}
1293
1294/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1295 * parent and child tasks.
1da177e4
LT
1296 */
1297
1298int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1299{
1300 struct sem_undo_list *undo_list;
1301 int error;
1302
1303 if (clone_flags & CLONE_SYSVSEM) {
1304 error = get_undo_list(&undo_list);
1305 if (error)
1306 return error;
1da177e4
LT
1307 atomic_inc(&undo_list->refcnt);
1308 tsk->sysvsem.undo_list = undo_list;
1309 } else
1310 tsk->sysvsem.undo_list = NULL;
1311
1312 return 0;
1313}
1314
1315/*
1316 * add semadj values to semaphores, free undo structures.
1317 * undo structures are not freed when semaphore arrays are destroyed
1318 * so some of them may be out of date.
1319 * IMPLEMENTATION NOTE: There is some confusion over whether the
1320 * set of adjustments that needs to be done should be done in an atomic
1321 * manner or not. That is, if we are attempting to decrement the semval
1322 * should we queue up and wait until we can do so legally?
1323 * The original implementation attempted to do this (queue and wait).
1324 * The current implementation does not do so. The POSIX standard
1325 * and SVID should be consulted to determine what behavior is mandated.
1326 */
1327void exit_sem(struct task_struct *tsk)
1328{
1329 struct sem_undo_list *undo_list;
1330 struct sem_undo *u, **up;
e3893534 1331 struct ipc_namespace *ns;
1da177e4
LT
1332
1333 undo_list = tsk->sysvsem.undo_list;
1334 if (!undo_list)
1335 return;
1336
1337 if (!atomic_dec_and_test(&undo_list->refcnt))
1338 return;
1339
e3893534 1340 ns = tsk->nsproxy->ipc_ns;
1da177e4
LT
1341 /* There's no need to hold the semundo list lock, as current
1342 * is the last task exiting for this undo list.
1343 */
1344 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1345 struct sem_array *sma;
1346 int nsems, i;
1347 struct sem_undo *un, **unp;
1348 int semid;
1349
1350 semid = u->semid;
1351
1352 if(semid == -1)
1353 continue;
e3893534 1354 sma = sem_lock(ns, semid);
023a5355 1355 if (IS_ERR(sma))
1da177e4
LT
1356 continue;
1357
1358 if (u->semid == -1)
1359 goto next_entry;
1360
1b531f21 1361 BUG_ON(sem_checkid(sma, u->semid));
1da177e4
LT
1362
1363 /* remove u from the sma->undo list */
1364 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1365 if (u == un)
1366 goto found;
1367 }
1368 printk ("exit_sem undo list error id=%d\n", u->semid);
1369 goto next_entry;
1370found:
1371 *unp = un->id_next;
1372 /* perform adjustments registered in u */
1373 nsems = sma->sem_nsems;
1374 for (i = 0; i < nsems; i++) {
5f921ae9 1375 struct sem * semaphore = &sma->sem_base[i];
1da177e4 1376 if (u->semadj[i]) {
5f921ae9 1377 semaphore->semval += u->semadj[i];
1da177e4
LT
1378 /*
1379 * Range checks of the new semaphore value,
1380 * not defined by sus:
1381 * - Some unices ignore the undo entirely
1382 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1383 * - some cap the value (e.g. FreeBSD caps
1384 * at 0, but doesn't enforce SEMVMX)
1385 *
1386 * Linux caps the semaphore value, both at 0
1387 * and at SEMVMX.
1388 *
1389 * Manfred <manfred@colorfullife.com>
1390 */
5f921ae9
IM
1391 if (semaphore->semval < 0)
1392 semaphore->semval = 0;
1393 if (semaphore->semval > SEMVMX)
1394 semaphore->semval = SEMVMX;
b488893a 1395 semaphore->sempid = task_tgid_vnr(current);
1da177e4
LT
1396 }
1397 }
1398 sma->sem_otime = get_seconds();
1399 /* maybe some queued-up processes were waiting for this */
1400 update_queue(sma);
1401next_entry:
1402 sem_unlock(sma);
1403 }
1404 kfree(undo_list);
1405}
1406
1407#ifdef CONFIG_PROC_FS
19b4946c 1408static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1da177e4 1409{
19b4946c
MW
1410 struct sem_array *sma = it;
1411
1412 return seq_printf(s,
1413 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1414 sma->sem_perm.key,
7ca7e564 1415 sma->sem_perm.id,
19b4946c
MW
1416 sma->sem_perm.mode,
1417 sma->sem_nsems,
1418 sma->sem_perm.uid,
1419 sma->sem_perm.gid,
1420 sma->sem_perm.cuid,
1421 sma->sem_perm.cgid,
1422 sma->sem_otime,
1423 sma->sem_ctime);
1da177e4
LT
1424}
1425#endif