sem.c 35 KB

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