sem.c 33 KB

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