sem.c 33 KB

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