summaryrefslogtreecommitdiffstats
path: root/ipc/sem.c
blob: 655047cf2a13ddd2d6a4b1fa4411e9603b792a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 * linux/ipc/sem.c
 * Copyright (C) 1992 Krishna Balasubramanian
 * Copyright (C) 1995 Eric Schenk, Bruno Haible
 *
 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
 * This code underwent a massive rewrite in order to solve some problems
 * with the original code. In particular the original code failed to
 * wake up processes that were waiting for semval to go to 0 if the
 * value went to 0 and was then incremented rapidly enough. In solving
 * this problem I have also modified the implementation so that it
 * processes pending operations in a FIFO manner, thus give a guarantee
 * that processes waiting for a lock on the semaphore won't starve
 * unless another locking process fails to unlock.
 * In addition the following two changes in behavior have been introduced:
 * - The original implementation of semop returned the value
 *   last semaphore element examined on success. This does not
 *   match the manual page specifications, and effectively
 *   allows the user to read the semaphore even if they do not
 *   have read permissions. The implementation now returns 0
 *   on success as stated in the manual page.
 * - There is some confusion over whether the set of undo adjustments
 *   to be performed at exit should be done in an atomic manner.
 *   That is, if we are attempting to decrement the semval should we queue
 *   up and wait until we can do so legally?
 *   The original implementation attempted to do this.
 *   The current implementation does not do so. This is because I don't
 *   think it is the right thing (TM) to do, and because I couldn't
 *   see a clean way to get the old behavior with the new design.
 *   The POSIX standard and SVID should be consulted to determine
 *   what behavior is mandated.
 */

#include <linux/errno.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/sem.h>
#include <linux/ipc.h>
#include <linux/stat.h>
#include <linux/malloc.h>
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <linux/init.h>

#include <asm/uaccess.h>

extern int ipcperms (struct ipc_perm *ipcp, short semflg);
static int newary (key_t, int, int);
static int findkey (key_t key);
static void freeary (int id);

static struct semid_ds *semary[SEMMNI];
static int used_sems = 0, used_semids = 0;
static struct wait_queue *sem_lock = NULL;
static int max_semid = 0;

static unsigned short sem_seq = 0;

void __init sem_init (void)
{
	int i;

	sem_lock = NULL;
	used_sems = used_semids = max_semid = sem_seq = 0;
	for (i = 0; i < SEMMNI; i++)
		semary[i] = (struct semid_ds *) IPC_UNUSED;
	return;
}

static int findkey (key_t key)
{
	int id;
	struct semid_ds *sma;

	for (id = 0; id <= max_semid; id++) {
		while ((sma = semary[id]) == IPC_NOID)
			interruptible_sleep_on (&sem_lock);
		if (sma == IPC_UNUSED)
			continue;
		if (key == sma->sem_perm.key)
			return id;
	}
	return -1;
}

static int newary (key_t key, int nsems, int semflg)
{
	int id;
	struct semid_ds *sma;
	struct ipc_perm *ipcp;
	int size;

	if (!nsems)
		return -EINVAL;
	if (used_sems + nsems > SEMMNS)
		return -ENOSPC;
	for (id = 0; id < SEMMNI; id++)
		if (semary[id] == IPC_UNUSED) {
			semary[id] = (struct semid_ds *) IPC_NOID;
			goto found;
		}
	return -ENOSPC;
found:
	size = sizeof (*sma) + nsems * sizeof (struct sem);
	used_sems += nsems;
	sma = (struct semid_ds *) kmalloc (size, GFP_KERNEL);
	if (!sma) {
		semary[id] = (struct semid_ds *) IPC_UNUSED;
		used_sems -= nsems;
		wake_up (&sem_lock);
		return -ENOMEM;
	}
	memset (sma, 0, size);
	sma->sem_base = (struct sem *) &sma[1];
	ipcp = &sma->sem_perm;
	ipcp->mode = (semflg & S_IRWXUGO);
	ipcp->key = key;
	ipcp->cuid = ipcp->uid = current->euid;
	ipcp->gid = ipcp->cgid = current->egid;
	sma->sem_perm.seq = sem_seq;
	/* sma->sem_pending = NULL; */
	sma->sem_pending_last = &sma->sem_pending;
	/* sma->undo = NULL; */
	sma->sem_nsems = nsems;
	sma->sem_ctime = CURRENT_TIME;
	if (id > max_semid)
		max_semid = id;
	used_semids++;
	semary[id] = sma;
	wake_up (&sem_lock);
	return (unsigned int) sma->sem_perm.seq * SEMMNI + id;
}

asmlinkage int sys_semget (key_t key, int nsems, int semflg)
{
	int id, err = -EINVAL;
	struct semid_ds *sma;

	lock_kernel();
	if (nsems < 0 || nsems > SEMMSL)
		goto out;
	if (key == IPC_PRIVATE) {
		err = newary(key, nsems, semflg);
	} else if ((id = findkey (key)) == -1) {  /* key not used */
		if (!(semflg & IPC_CREAT))
			err = -ENOENT;
		else
			err = newary(key, nsems, semflg);
	} else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
		err = -EEXIST;
	} else {
		sma = semary[id];
		if (nsems > sma->sem_nsems)
			err = -EINVAL;
		else if (ipcperms(&sma->sem_perm, semflg))
			err = -EACCES;
		else
			err = (int) sma->sem_perm.seq * SEMMNI + id;
	}
out:
	unlock_kernel();
	return err;
}

/* Manage the doubly linked list sma->sem_pending as a FIFO:
 * insert new queue elements at the tail sma->sem_pending_last.
 */
static inline void insert_into_queue (struct semid_ds * sma, struct sem_queue * q)
{
	*(q->prev = sma->sem_pending_last) = q;
	*(sma->sem_pending_last = &q->next) = NULL;
}
static inline void remove_from_queue (struct semid_ds * sma, struct sem_queue * q)
{
	*(q->prev) = q->next;
	if (q->next)
		q->next->prev = q->prev;
	else /* sma->sem_pending_last == &q->next */
		sma->sem_pending_last = q->prev;
	q->prev = NULL; /* mark as removed */
}

/* Determine whether a sequence of semaphore operations would succeed
 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
 */
static int try_semop (struct semid_ds * sma, struct sembuf * sops, int nsops)
{
	int result = 0;
	int i = 0;

	while (i < nsops) {
		struct sembuf * sop = &sops[i];
		struct sem * curr = &sma->sem_base[sop->sem_num];
		if (sop->sem_op + curr->semval > SEMVMX) {
			result = -ERANGE;
			break;
		}
		if (!sop->sem_op && curr->semval) {
			if (sop->sem_flg & IPC_NOWAIT)
				result = -EAGAIN;
			else
				result = 1;
			break;
		}
		i++;
		curr->semval += sop->sem_op;
		if (curr->semval < 0) {
			if (sop->sem_flg & IPC_NOWAIT)
				result = -EAGAIN;
			else
				result = 1;
			break;
		}
	}
	while (--i >= 0) {
		struct sembuf * sop = &sops[i];
		struct sem * curr = &sma->sem_base[sop->sem_num];
		curr->semval -= sop->sem_op;
	}
	return result;
}

/* Actually perform a sequence of semaphore operations. Atomically. */
/* This assumes that try_semop() already returned 0. */
static int do_semop (struct semid_ds * sma, struct sembuf * sops, int nsops,
		     struct sem_undo * un, int pid)
{
	int i;

	for (i = 0; i < nsops; i++) {
		struct sembuf * sop = &sops[i];
		struct sem * curr = &sma->sem_base[sop->sem_num];
		if (sop->sem_op + curr->semval > SEMVMX) {
			printk("do_semop: race\n");
			break;
		}
		if (!sop->sem_op) {
			if (curr->semval) {
				printk("do_semop: race\n");
				break;
			}
		} else {
			curr->semval += sop->sem_op;
			if (curr->semval < 0) {
				printk("do_semop: race\n");
				break;
			}
			if (sop->sem_flg & SEM_UNDO)
				un->semadj[sop->sem_num] -= sop->sem_op;
		}
		curr->sempid = pid;
	}
	sma->sem_otime = CURRENT_TIME;

	/* Previous implementation returned the last semaphore's semval.
	 * This is wrong because we may not have checked read permission,
	 * only write permission.
	 */
	return 0;
}

/* Go through the pending queue for the indicated semaphore
 * looking for tasks that can be completed. Keep cycling through
 * the queue until a pass is made in which no process is woken up.
 */
static void update_queue (struct semid_ds * sma)
{
	int wokeup, error;
	struct sem_queue * q;

	do {
		wokeup = 0;
		for (q = sma->sem_pending; q; q = q->next) {
			error = try_semop(sma, q->sops, q->nsops);
			/* Does q->sleeper still need to sleep? */
			if (error > 0)
				continue;
			/* Perform the operations the sleeper was waiting for */
			if (!error)
				error = do_semop(sma, q->sops, q->nsops, q->undo, q->pid);
			q->status = error;
			/* Remove it from the queue */
			remove_from_queue(sma,q);
			/* Wake it up */
			wake_up_interruptible(&q->sleeper); /* doesn't sleep! */
			wokeup++;
		}
	} while (wokeup);
}

/* The following counts are associated to each semaphore:
 *   semncnt        number of tasks waiting on semval being nonzero
 *   semzcnt        number of tasks waiting on semval being zero
 * This model assumes that a task waits on exactly one semaphore.
 * Since semaphore operations are to be performed atomically, tasks actually
 * wait on a whole sequence of semaphores simultaneously.
 * The counts we return here are a rough approximation, but still
 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
 */
static int count_semncnt (struct semid_ds * sma, ushort semnum)
{
	int semncnt;
	struct sem_queue * q;

	semncnt = 0;
	for (q = sma->sem_pending; q; q = q->next) {
		struct sembuf * sops = q->sops;
		int nsops = q->nsops;
		int i;
		for (i = 0; i < nsops; i++)
			if (sops[i].sem_num == semnum
			    && (sops[i].sem_op < 0)
			    && !(sops[i].sem_flg & IPC_NOWAIT))
				semncnt++;
	}
	return semncnt;
}
static int count_semzcnt (struct semid_ds * sma, ushort semnum)
{
	int semzcnt;
	struct sem_queue * q;

	semzcnt = 0;
	for (q = sma->sem_pending; q; q = q->next) {
		struct sembuf * sops = q->sops;
		int nsops = q->nsops;
		int i;
		for (i = 0; i < nsops; i++)
			if (sops[i].sem_num == semnum
			    && (sops[i].sem_op == 0)
			    && !(sops[i].sem_flg & IPC_NOWAIT))
				semzcnt++;
	}
	return semzcnt;
}

/* Free a semaphore set. */
static void freeary (int id)
{
	struct semid_ds *sma = semary[id];
	struct sem_undo *un;
	struct sem_queue *q;

	/* Invalidate this semaphore set */
	sma->sem_perm.seq++;
	sem_seq = (sem_seq+1) % ((unsigned)(1<<31)/SEMMNI); /* increment, but avoid overflow */
	used_sems -= sma->sem_nsems;
	if (id == max_semid)
		while (max_semid && (semary[--max_semid] == IPC_UNUSED));
	semary[id] = (struct semid_ds *) IPC_UNUSED;
	used_semids--;

	/* Invalidate the existing undo structures for this semaphore set.
	 * (They will be freed without any further action in sem_exit().)
	 */
	for (un = sma->undo; un; un = un->id_next)
		un->semid = -1;

	/* Wake up all pending processes and let them fail with EIDRM. */
	for (q = sma->sem_pending; q; q = q->next) {
		q->status = -EIDRM;
		q->prev = NULL;
		wake_up_interruptible(&q->sleeper); /* doesn't sleep! */
	}

	kfree(sma);
}

asmlinkage int sys_semctl (int semid, int semnum, int cmd, union semun arg)
{
	struct semid_ds *buf = NULL;
	struct semid_ds tbuf;
	int i, id, val = 0;
	struct semid_ds *sma;
	struct ipc_perm *ipcp;
	struct sem *curr = NULL;
	struct sem_undo *un;
	unsigned int nsems;
	ushort *array = NULL;
	ushort sem_io[SEMMSL];
	int err = -EINVAL;

	lock_kernel();
	if (semid < 0 || semnum < 0 || cmd < 0)
		goto out;

	switch (cmd) {
	case IPC_INFO:
	case SEM_INFO:
	{
		struct seminfo seminfo, *tmp = arg.__buf;
		seminfo.semmni = SEMMNI;
		seminfo.semmns = SEMMNS;
		seminfo.semmsl = SEMMSL;
		seminfo.semopm = SEMOPM;
		seminfo.semvmx = SEMVMX;
		seminfo.semmnu = SEMMNU;
		seminfo.semmap = SEMMAP;
		seminfo.semume = SEMUME;
		seminfo.semusz = SEMUSZ;
		seminfo.semaem = SEMAEM;
		if (cmd == SEM_INFO) {
			seminfo.semusz = used_semids;
			seminfo.semaem = used_sems;
		}
		err = -EFAULT;
		if (copy_to_user (tmp, &seminfo, sizeof(struct seminfo))) 
			goto out;
		err = max_semid;
		goto out;
	}

	case SEM_STAT:
		buf = arg.buf;
		err = -EINVAL;
		if (semid > max_semid)
			goto out;
		sma = semary[semid];
		if (sma == IPC_UNUSED || sma == IPC_NOID)
			goto out;
		err = -EACCES;
		if (ipcperms (&sma->sem_perm, S_IRUGO))
			goto out;
		id = (unsigned int) sma->sem_perm.seq * SEMMNI + semid;
		tbuf.sem_perm   = sma->sem_perm;
		tbuf.sem_otime  = sma->sem_otime;
		tbuf.sem_ctime  = sma->sem_ctime;
		tbuf.sem_nsems  = sma->sem_nsems;
		err = -EFAULT;
		if (copy_to_user (buf, &tbuf, sizeof(*buf)) == 0)
			err = id;
		goto out;
	}

	id = (unsigned int) semid % SEMMNI;
	sma = semary [id];
	err = -EINVAL;
	if (sma == IPC_UNUSED || sma == IPC_NOID)
		goto out;
	ipcp = &sma->sem_perm;
	nsems = sma->sem_nsems;
	err = -EIDRM;
	if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI)
		goto out;

	switch (cmd) {
	case GETVAL:
	case GETPID:
	case GETNCNT:
	case GETZCNT:
	case SETVAL:
		err = -EINVAL;
		if (semnum >= nsems)
			goto out;
		curr = &sma->sem_base[semnum];
		break;
	}

	switch (cmd) {
	case GETVAL:
	case GETPID:
	case GETNCNT:
	case GETZCNT:
	case GETALL:
		err = -EACCES;
		if (ipcperms (ipcp, S_IRUGO))
			goto out;
		switch (cmd) {
		case GETVAL : err = curr->semval; goto out;
		case GETPID : err = curr->sempid; goto out;
		case GETNCNT: err = count_semncnt(sma,semnum); goto out;
		case GETZCNT: err = count_semzcnt(sma,semnum); goto out;
		case GETALL:
			array = arg.array;
			break;
		}
		break;
	case SETVAL:
		val = arg.val;
		err = -ERANGE;
		if (val > SEMVMX || val < 0)
			goto out;
		break;
	case IPC_RMID:
		if (current->euid == ipcp->cuid || 
		    current->euid == ipcp->uid || capable(CAP_SYS_ADMIN)) {
			freeary (id);
			err = 0;
			goto out;
		}
		err = -EPERM;
		goto out;
	case SETALL: /* arg is a pointer to an array of ushort */
		array = arg.array;
		err = -EFAULT;
		if (copy_from_user (sem_io, array, nsems*sizeof(ushort)))
		       goto out;
		err = 0;
		for (i = 0; i < nsems; i++)
			if (sem_io[i] > SEMVMX) {
				err = -ERANGE;
				goto out;
			}
		break;
	case IPC_STAT:
		buf = arg.buf;
		break;
	case IPC_SET:
		buf = arg.buf;
		err = copy_from_user (&tbuf, buf, sizeof (*buf));
		if (err)
			err = -EFAULT;
		break;
	}

	err = -EIDRM;
	if (semary[id] == IPC_UNUSED || semary[id] == IPC_NOID)
		goto out;
	if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI)
		goto out;

	switch (cmd) {
	case GETALL:
		err = -EACCES;
		if (ipcperms (ipcp, S_IRUGO))
			goto out;
		for (i = 0; i < sma->sem_nsems; i++)
			sem_io[i] = sma->sem_base[i].semval;
		if (copy_to_user (array, sem_io, nsems*sizeof(ushort)))
			err = -EFAULT;
		break;
	case SETVAL:
		err = -EACCES;
		if (ipcperms (ipcp, S_IWUGO))
			goto out;
		for (un = sma->undo; un; un = un->id_next)
			un->semadj[semnum] = 0;
		curr->semval = val;
		sma->sem_ctime = CURRENT_TIME;
		/* maybe some queued-up processes were waiting for this */
		update_queue(sma);
		break;
	case IPC_SET:
		if (current->euid == ipcp->cuid || 
		    current->euid == ipcp->uid || capable(CAP_SYS_ADMIN)) {
			ipcp->uid = tbuf.sem_perm.uid;
			ipcp->gid = tbuf.sem_perm.gid;
			ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
				| (tbuf.sem_perm.mode & S_IRWXUGO);
			sma->sem_ctime = CURRENT_TIME;
			err = 0;
			goto out;
		}
		err = -EPERM;
		goto out;
	case IPC_STAT:
		err = -EACCES;
		if (ipcperms (ipcp, S_IRUGO))
			goto out;
		tbuf.sem_perm   = sma->sem_perm;
		tbuf.sem_otime  = sma->sem_otime;
		tbuf.sem_ctime  = sma->sem_ctime;
		tbuf.sem_nsems  = sma->sem_nsems;
		if (copy_to_user (buf, &tbuf, sizeof(*buf)))
			err = -EFAULT;
		break;
	case SETALL:
		err = -EACCES;
		if (ipcperms (ipcp, S_IWUGO))
			goto out;
		for (i = 0; i < nsems; i++)
			sma->sem_base[i].semval = sem_io[i];
		for (un = sma->undo; un; un = un->id_next)
			for (i = 0; i < nsems; i++)
				un->semadj[i] = 0;
		sma->sem_ctime = CURRENT_TIME;
		/* maybe some queued-up processes were waiting for this */
		update_queue(sma);
		break;
	default:
		err = -EINVAL;
		goto out;
	}
	err = 0;
out:
	unlock_kernel();
	return err;
}

asmlinkage int sys_semop (int semid, struct sembuf *tsops, unsigned nsops)
{
	int i, id, size, error = -EINVAL;
	struct semid_ds *sma;
	struct sembuf sops[SEMOPM], *sop;
	struct sem_undo *un;
	int undos = 0, alter = 0;

	lock_kernel();
	if (nsops < 1 || semid < 0)
		goto out;
	error = -E2BIG;
	if (nsops > SEMOPM)
		goto out;
	error = -EFAULT;
	if (!tsops)
		goto out;
	if (copy_from_user (sops, tsops, nsops * sizeof(*tsops)))
		goto out;
	id = (unsigned int) semid % SEMMNI;
	error = -EINVAL;
	if ((sma = semary[id]) == IPC_UNUSED || sma == IPC_NOID)
		goto out;
	error = -EIDRM;
	if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI)
		goto out;
	for (i = 0; i < nsops; i++) {
		sop = &sops[i];
		error = -EFBIG;
		if (sop->sem_num >= sma->sem_nsems)
			goto out;
		if (sop->sem_flg & SEM_UNDO)
			undos++;
		if (sop->sem_op)
			alter++;
	}
	error = -EACCES;
	if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
		goto out;
	error = try_semop(sma, sops, nsops);
	if (error < 0)
		goto out;
	if (undos) {
		/* Make sure we have an undo structure
		 * for this process and this semaphore set.
		 */
		for (un = current->semundo; un; un = un->proc_next)
			if (un->semid == semid)
				break;
		if (!un) {
			size = sizeof(struct sem_undo) + sizeof(short)*sma->sem_nsems;
			un = (struct sem_undo *) kmalloc(size, GFP_ATOMIC);
			if (!un) {
				error = -ENOMEM;
				goto out;
			}
			memset(un, 0, size);
			un->semadj = (short *) &un[1];
			un->semid = semid;
			un->proc_next = current->semundo;
			current->semundo = un;
			un->id_next = sma->undo;
			sma->undo = un;
		}
	} else
		un = NULL;
	if (error == 0) {
		/* the operations go through immediately */
		error = do_semop(sma, sops, nsops, un, current->pid);
		/* maybe some queued-up processes were waiting for this */
		update_queue(sma);
		goto out;
	} else {
		/* We need to sleep on this operation, so we put the current
		 * task into the pending queue and go to sleep.
		 */
		struct sem_queue queue;

		queue.sma = sma;
		queue.sops = sops;
		queue.nsops = nsops;
		queue.undo = un;
		queue.pid = current->pid;
		queue.status = 0;
		insert_into_queue(sma,&queue);
		queue.sleeper = NULL;
		current->semsleeping = &queue;
		interruptible_sleep_on(&queue.sleeper);
		current->semsleeping = NULL;
		/* When we wake up, either the operation is finished,
		 * or some kind of error happened.
		 */
		if (!queue.prev) {
			/* operation is finished, update_queue() removed us */
			error = queue.status;
		} else {
			remove_from_queue(sma,&queue);
			error = -EINTR;
		}
	}
out:
	unlock_kernel();
	return error;
}

/*
 * add semadj values to semaphores, free undo structures.
 * undo structures are not freed when semaphore arrays are destroyed
 * so some of them may be out of date.
 * IMPLEMENTATION NOTE: There is some confusion over whether the
 * set of adjustments that needs to be done should be done in an atomic
 * manner or not. That is, if we are attempting to decrement the semval
 * should we queue up and wait until we can do so legally?
 * The original implementation attempted to do this (queue and wait).
 * The current implementation does not do so. The POSIX standard
 * and SVID should be consulted to determine what behavior is mandated.
 */
void sem_exit (void)
{
	struct sem_queue *q;
	struct sem_undo *u, *un = NULL, **up, **unp;
	struct semid_ds *sma;
	int nsems, i;

	/* If the current process was sleeping for a semaphore,
	 * remove it from the queue.
	 */
	if ((q = current->semsleeping)) {
		if (q->prev)
			remove_from_queue(q->sma,q);
		current->semsleeping = NULL;
	}

	for (up = &current->semundo; (u = *up); *up = u->proc_next, kfree(u)) {
		if (u->semid == -1)
			continue;
		sma = semary[(unsigned int) u->semid % SEMMNI];
		if (sma == IPC_UNUSED || sma == IPC_NOID)
			continue;
		if (sma->sem_perm.seq != (unsigned int) u->semid / SEMMNI)
			continue;
		/* remove u from the sma->undo list */
		for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
			if (u == un)
				goto found;
		}
		printk ("sem_exit undo list error id=%d\n", u->semid);
		break;
found:
		*unp = un->id_next;
		/* perform adjustments registered in u */
		nsems = sma->sem_nsems;
		for (i = 0; i < nsems; i++) {
			struct sem * sem = &sma->sem_base[i];
			sem->semval += u->semadj[i];
			if (sem->semval < 0)
				sem->semval = 0; /* shouldn't happen */
			sem->sempid = current->pid;
		}
		sma->sem_otime = CURRENT_TIME;
		/* maybe some queued-up processes were waiting for this */
		update_queue(sma);
	}
	current->semundo = NULL;
}