sem.c 34 KB

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