sem.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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/smp_lock.h>
  78. #include <linux/security.h>
  79. #include <linux/syscalls.h>
  80. #include <linux/audit.h>
  81. #include <linux/capability.h>
  82. #include <linux/seq_file.h>
  83. #include <linux/mutex.h>
  84. #include <linux/nsproxy.h>
  85. #include <asm/uaccess.h>
  86. #include "util.h"
  87. #define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
  88. #define sem_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
  89. #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
  90. #define sem_rmid(ns, id) ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
  91. #define sem_checkid(ns, sma, semid) \
  92. ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
  93. #define sem_buildid(ns, id, seq) \
  94. ipc_buildid(&sem_ids(ns), id, seq)
  95. static struct ipc_ids init_sem_ids;
  96. static int newary(struct ipc_namespace *, key_t, int, int);
  97. static void freeary(struct ipc_namespace *ns, struct sem_array *sma, int id);
  98. #ifdef CONFIG_PROC_FS
  99. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  100. #endif
  101. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  102. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  103. /*
  104. * linked list protection:
  105. * sem_undo.id_next,
  106. * sem_array.sem_pending{,last},
  107. * sem_array.sem_undo: sem_lock() for read/write
  108. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  109. *
  110. */
  111. #define sc_semmsl sem_ctls[0]
  112. #define sc_semmns sem_ctls[1]
  113. #define sc_semopm sem_ctls[2]
  114. #define sc_semmni sem_ctls[3]
  115. static void __ipc_init __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
  116. {
  117. ns->ids[IPC_SEM_IDS] = ids;
  118. ns->sc_semmsl = SEMMSL;
  119. ns->sc_semmns = SEMMNS;
  120. ns->sc_semopm = SEMOPM;
  121. ns->sc_semmni = SEMMNI;
  122. ns->used_sems = 0;
  123. ipc_init_ids(ids, ns->sc_semmni);
  124. }
  125. #ifdef CONFIG_IPC_NS
  126. int sem_init_ns(struct ipc_namespace *ns)
  127. {
  128. struct ipc_ids *ids;
  129. ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
  130. if (ids == NULL)
  131. return -ENOMEM;
  132. __sem_init_ns(ns, ids);
  133. return 0;
  134. }
  135. void sem_exit_ns(struct ipc_namespace *ns)
  136. {
  137. int i;
  138. struct sem_array *sma;
  139. mutex_lock(&sem_ids(ns).mutex);
  140. for (i = 0; i <= sem_ids(ns).max_id; i++) {
  141. sma = sem_lock(ns, i);
  142. if (sma == NULL)
  143. continue;
  144. freeary(ns, sma, i);
  145. }
  146. mutex_unlock(&sem_ids(ns).mutex);
  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 = (struct sem_undo *) kmalloc(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. memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
  955. new->semadj = (short *) &new[1];
  956. new->semid = semid;
  957. lock_semundo();
  958. un = lookup_undo(ulp, semid);
  959. if (un) {
  960. unlock_semundo();
  961. kfree(new);
  962. ipc_lock_by_ptr(&sma->sem_perm);
  963. ipc_rcu_putref(sma);
  964. sem_unlock(sma);
  965. goto out;
  966. }
  967. ipc_lock_by_ptr(&sma->sem_perm);
  968. ipc_rcu_putref(sma);
  969. if (sma->sem_perm.deleted) {
  970. sem_unlock(sma);
  971. unlock_semundo();
  972. kfree(new);
  973. un = ERR_PTR(-EIDRM);
  974. goto out;
  975. }
  976. new->proc_next = ulp->proc_list;
  977. ulp->proc_list = new;
  978. new->id_next = sma->undo;
  979. sma->undo = new;
  980. sem_unlock(sma);
  981. un = new;
  982. unlock_semundo();
  983. out:
  984. return un;
  985. }
  986. asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
  987. unsigned nsops, const struct timespec __user *timeout)
  988. {
  989. int error = -EINVAL;
  990. struct sem_array *sma;
  991. struct sembuf fast_sops[SEMOPM_FAST];
  992. struct sembuf* sops = fast_sops, *sop;
  993. struct sem_undo *un;
  994. int undos = 0, alter = 0, max;
  995. struct sem_queue queue;
  996. unsigned long jiffies_left = 0;
  997. struct ipc_namespace *ns;
  998. ns = current->nsproxy->ipc_ns;
  999. if (nsops < 1 || semid < 0)
  1000. return -EINVAL;
  1001. if (nsops > ns->sc_semopm)
  1002. return -E2BIG;
  1003. if(nsops > SEMOPM_FAST) {
  1004. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  1005. if(sops==NULL)
  1006. return -ENOMEM;
  1007. }
  1008. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  1009. error=-EFAULT;
  1010. goto out_free;
  1011. }
  1012. if (timeout) {
  1013. struct timespec _timeout;
  1014. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1015. error = -EFAULT;
  1016. goto out_free;
  1017. }
  1018. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1019. _timeout.tv_nsec >= 1000000000L) {
  1020. error = -EINVAL;
  1021. goto out_free;
  1022. }
  1023. jiffies_left = timespec_to_jiffies(&_timeout);
  1024. }
  1025. max = 0;
  1026. for (sop = sops; sop < sops + nsops; sop++) {
  1027. if (sop->sem_num >= max)
  1028. max = sop->sem_num;
  1029. if (sop->sem_flg & SEM_UNDO)
  1030. undos = 1;
  1031. if (sop->sem_op != 0)
  1032. alter = 1;
  1033. }
  1034. retry_undos:
  1035. if (undos) {
  1036. un = find_undo(ns, semid);
  1037. if (IS_ERR(un)) {
  1038. error = PTR_ERR(un);
  1039. goto out_free;
  1040. }
  1041. } else
  1042. un = NULL;
  1043. sma = sem_lock(ns, semid);
  1044. error=-EINVAL;
  1045. if(sma==NULL)
  1046. goto out_free;
  1047. error = -EIDRM;
  1048. if (sem_checkid(ns,sma,semid))
  1049. goto out_unlock_free;
  1050. /*
  1051. * semid identifies are not unique - find_undo may have
  1052. * allocated an undo structure, it was invalidated by an RMID
  1053. * and now a new array with received the same id. Check and retry.
  1054. */
  1055. if (un && un->semid == -1) {
  1056. sem_unlock(sma);
  1057. goto retry_undos;
  1058. }
  1059. error = -EFBIG;
  1060. if (max >= sma->sem_nsems)
  1061. goto out_unlock_free;
  1062. error = -EACCES;
  1063. if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1064. goto out_unlock_free;
  1065. error = security_sem_semop(sma, sops, nsops, alter);
  1066. if (error)
  1067. goto out_unlock_free;
  1068. error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
  1069. if (error <= 0) {
  1070. if (alter && error == 0)
  1071. update_queue (sma);
  1072. goto out_unlock_free;
  1073. }
  1074. /* We need to sleep on this operation, so we put the current
  1075. * task into the pending queue and go to sleep.
  1076. */
  1077. queue.sma = sma;
  1078. queue.sops = sops;
  1079. queue.nsops = nsops;
  1080. queue.undo = un;
  1081. queue.pid = current->tgid;
  1082. queue.id = semid;
  1083. queue.alter = alter;
  1084. if (alter)
  1085. append_to_queue(sma ,&queue);
  1086. else
  1087. prepend_to_queue(sma ,&queue);
  1088. queue.status = -EINTR;
  1089. queue.sleeper = current;
  1090. current->state = TASK_INTERRUPTIBLE;
  1091. sem_unlock(sma);
  1092. if (timeout)
  1093. jiffies_left = schedule_timeout(jiffies_left);
  1094. else
  1095. schedule();
  1096. error = queue.status;
  1097. while(unlikely(error == IN_WAKEUP)) {
  1098. cpu_relax();
  1099. error = queue.status;
  1100. }
  1101. if (error != -EINTR) {
  1102. /* fast path: update_queue already obtained all requested
  1103. * resources */
  1104. goto out_free;
  1105. }
  1106. sma = sem_lock(ns, semid);
  1107. if(sma==NULL) {
  1108. BUG_ON(queue.prev != NULL);
  1109. error = -EIDRM;
  1110. goto out_free;
  1111. }
  1112. /*
  1113. * If queue.status != -EINTR we are woken up by another process
  1114. */
  1115. error = queue.status;
  1116. if (error != -EINTR) {
  1117. goto out_unlock_free;
  1118. }
  1119. /*
  1120. * If an interrupt occurred we have to clean up the queue
  1121. */
  1122. if (timeout && jiffies_left == 0)
  1123. error = -EAGAIN;
  1124. remove_from_queue(sma,&queue);
  1125. goto out_unlock_free;
  1126. out_unlock_free:
  1127. sem_unlock(sma);
  1128. out_free:
  1129. if(sops != fast_sops)
  1130. kfree(sops);
  1131. return error;
  1132. }
  1133. asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
  1134. {
  1135. return sys_semtimedop(semid, tsops, nsops, NULL);
  1136. }
  1137. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1138. * parent and child tasks.
  1139. *
  1140. * See the notes above unlock_semundo() regarding the spin_lock_init()
  1141. * in this code. Initialize the undo_list->lock here instead of get_undo_list()
  1142. * because of the reasoning in the comment above unlock_semundo.
  1143. */
  1144. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1145. {
  1146. struct sem_undo_list *undo_list;
  1147. int error;
  1148. if (clone_flags & CLONE_SYSVSEM) {
  1149. error = get_undo_list(&undo_list);
  1150. if (error)
  1151. return error;
  1152. atomic_inc(&undo_list->refcnt);
  1153. tsk->sysvsem.undo_list = undo_list;
  1154. } else
  1155. tsk->sysvsem.undo_list = NULL;
  1156. return 0;
  1157. }
  1158. /*
  1159. * add semadj values to semaphores, free undo structures.
  1160. * undo structures are not freed when semaphore arrays are destroyed
  1161. * so some of them may be out of date.
  1162. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1163. * set of adjustments that needs to be done should be done in an atomic
  1164. * manner or not. That is, if we are attempting to decrement the semval
  1165. * should we queue up and wait until we can do so legally?
  1166. * The original implementation attempted to do this (queue and wait).
  1167. * The current implementation does not do so. The POSIX standard
  1168. * and SVID should be consulted to determine what behavior is mandated.
  1169. */
  1170. void exit_sem(struct task_struct *tsk)
  1171. {
  1172. struct sem_undo_list *undo_list;
  1173. struct sem_undo *u, **up;
  1174. struct ipc_namespace *ns;
  1175. undo_list = tsk->sysvsem.undo_list;
  1176. if (!undo_list)
  1177. return;
  1178. if (!atomic_dec_and_test(&undo_list->refcnt))
  1179. return;
  1180. ns = tsk->nsproxy->ipc_ns;
  1181. /* There's no need to hold the semundo list lock, as current
  1182. * is the last task exiting for this undo list.
  1183. */
  1184. for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
  1185. struct sem_array *sma;
  1186. int nsems, i;
  1187. struct sem_undo *un, **unp;
  1188. int semid;
  1189. semid = u->semid;
  1190. if(semid == -1)
  1191. continue;
  1192. sma = sem_lock(ns, semid);
  1193. if (sma == NULL)
  1194. continue;
  1195. if (u->semid == -1)
  1196. goto next_entry;
  1197. BUG_ON(sem_checkid(ns,sma,u->semid));
  1198. /* remove u from the sma->undo list */
  1199. for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
  1200. if (u == un)
  1201. goto found;
  1202. }
  1203. printk ("exit_sem undo list error id=%d\n", u->semid);
  1204. goto next_entry;
  1205. found:
  1206. *unp = un->id_next;
  1207. /* perform adjustments registered in u */
  1208. nsems = sma->sem_nsems;
  1209. for (i = 0; i < nsems; i++) {
  1210. struct sem * semaphore = &sma->sem_base[i];
  1211. if (u->semadj[i]) {
  1212. semaphore->semval += u->semadj[i];
  1213. /*
  1214. * Range checks of the new semaphore value,
  1215. * not defined by sus:
  1216. * - Some unices ignore the undo entirely
  1217. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1218. * - some cap the value (e.g. FreeBSD caps
  1219. * at 0, but doesn't enforce SEMVMX)
  1220. *
  1221. * Linux caps the semaphore value, both at 0
  1222. * and at SEMVMX.
  1223. *
  1224. * Manfred <manfred@colorfullife.com>
  1225. */
  1226. if (semaphore->semval < 0)
  1227. semaphore->semval = 0;
  1228. if (semaphore->semval > SEMVMX)
  1229. semaphore->semval = SEMVMX;
  1230. semaphore->sempid = current->tgid;
  1231. }
  1232. }
  1233. sma->sem_otime = get_seconds();
  1234. /* maybe some queued-up processes were waiting for this */
  1235. update_queue(sma);
  1236. next_entry:
  1237. sem_unlock(sma);
  1238. }
  1239. kfree(undo_list);
  1240. }
  1241. #ifdef CONFIG_PROC_FS
  1242. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1243. {
  1244. struct sem_array *sma = it;
  1245. return seq_printf(s,
  1246. "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
  1247. sma->sem_perm.key,
  1248. sma->sem_id,
  1249. sma->sem_perm.mode,
  1250. sma->sem_nsems,
  1251. sma->sem_perm.uid,
  1252. sma->sem_perm.gid,
  1253. sma->sem_perm.cuid,
  1254. sma->sem_perm.cgid,
  1255. sma->sem_otime,
  1256. sma->sem_ctime);
  1257. }
  1258. #endif