sem.c 33 KB

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