sem.c 34 KB

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