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