sem.c 35 KB

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