sem.c 33 KB

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