sem.c 33 KB

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