sem.c 34 KB

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