sys.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/config.h>
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/utsname.h>
  10. #include <linux/mman.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/notifier.h>
  13. #include <linux/reboot.h>
  14. #include <linux/prctl.h>
  15. #include <linux/init.h>
  16. #include <linux/highuid.h>
  17. #include <linux/fs.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kexec.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/capability.h>
  22. #include <linux/device.h>
  23. #include <linux/key.h>
  24. #include <linux/times.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/security.h>
  27. #include <linux/dcookies.h>
  28. #include <linux/suspend.h>
  29. #include <linux/tty.h>
  30. #include <linux/signal.h>
  31. #include <linux/cn_proc.h>
  32. #include <linux/compat.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/kprobes.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <asm/unistd.h>
  38. #ifndef SET_UNALIGN_CTL
  39. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  40. #endif
  41. #ifndef GET_UNALIGN_CTL
  42. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  43. #endif
  44. #ifndef SET_FPEMU_CTL
  45. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  46. #endif
  47. #ifndef GET_FPEMU_CTL
  48. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  49. #endif
  50. #ifndef SET_FPEXC_CTL
  51. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  52. #endif
  53. #ifndef GET_FPEXC_CTL
  54. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  55. #endif
  56. #ifndef GET_ENDIAN
  57. # define GET_ENDIAN(a,b) (-EINVAL)
  58. #endif
  59. #ifndef SET_ENDIAN
  60. # define SET_ENDIAN(a,b) (-EINVAL)
  61. #endif
  62. /*
  63. * this is where the system-wide overflow UID and GID are defined, for
  64. * architectures that now have 32-bit UID/GID but didn't in the past
  65. */
  66. int overflowuid = DEFAULT_OVERFLOWUID;
  67. int overflowgid = DEFAULT_OVERFLOWGID;
  68. #ifdef CONFIG_UID16
  69. EXPORT_SYMBOL(overflowuid);
  70. EXPORT_SYMBOL(overflowgid);
  71. #endif
  72. /*
  73. * the same as above, but for filesystems which can only store a 16-bit
  74. * UID and GID. as such, this is needed on all architectures
  75. */
  76. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  77. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  78. EXPORT_SYMBOL(fs_overflowuid);
  79. EXPORT_SYMBOL(fs_overflowgid);
  80. /*
  81. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  82. */
  83. int C_A_D = 1;
  84. int cad_pid = 1;
  85. /*
  86. * Notifier list for kernel code which wants to be called
  87. * at shutdown. This is used to stop any idling DMA operations
  88. * and the like.
  89. */
  90. static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  91. /*
  92. * Notifier chain core routines. The exported routines below
  93. * are layered on top of these, with appropriate locking added.
  94. */
  95. static int notifier_chain_register(struct notifier_block **nl,
  96. struct notifier_block *n)
  97. {
  98. while ((*nl) != NULL) {
  99. if (n->priority > (*nl)->priority)
  100. break;
  101. nl = &((*nl)->next);
  102. }
  103. n->next = *nl;
  104. rcu_assign_pointer(*nl, n);
  105. return 0;
  106. }
  107. static int notifier_chain_unregister(struct notifier_block **nl,
  108. struct notifier_block *n)
  109. {
  110. while ((*nl) != NULL) {
  111. if ((*nl) == n) {
  112. rcu_assign_pointer(*nl, n->next);
  113. return 0;
  114. }
  115. nl = &((*nl)->next);
  116. }
  117. return -ENOENT;
  118. }
  119. static int __kprobes notifier_call_chain(struct notifier_block **nl,
  120. unsigned long val, void *v)
  121. {
  122. int ret = NOTIFY_DONE;
  123. struct notifier_block *nb;
  124. nb = rcu_dereference(*nl);
  125. while (nb) {
  126. ret = nb->notifier_call(nb, val, v);
  127. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  128. break;
  129. nb = rcu_dereference(nb->next);
  130. }
  131. return ret;
  132. }
  133. /*
  134. * Atomic notifier chain routines. Registration and unregistration
  135. * use a mutex, and call_chain is synchronized by RCU (no locks).
  136. */
  137. /**
  138. * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
  139. * @nh: Pointer to head of the atomic notifier chain
  140. * @n: New entry in notifier chain
  141. *
  142. * Adds a notifier to an atomic notifier chain.
  143. *
  144. * Currently always returns zero.
  145. */
  146. int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  147. struct notifier_block *n)
  148. {
  149. unsigned long flags;
  150. int ret;
  151. spin_lock_irqsave(&nh->lock, flags);
  152. ret = notifier_chain_register(&nh->head, n);
  153. spin_unlock_irqrestore(&nh->lock, flags);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
  157. /**
  158. * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
  159. * @nh: Pointer to head of the atomic notifier chain
  160. * @n: Entry to remove from notifier chain
  161. *
  162. * Removes a notifier from an atomic notifier chain.
  163. *
  164. * Returns zero on success or %-ENOENT on failure.
  165. */
  166. int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
  167. struct notifier_block *n)
  168. {
  169. unsigned long flags;
  170. int ret;
  171. spin_lock_irqsave(&nh->lock, flags);
  172. ret = notifier_chain_unregister(&nh->head, n);
  173. spin_unlock_irqrestore(&nh->lock, flags);
  174. synchronize_rcu();
  175. return ret;
  176. }
  177. EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
  178. /**
  179. * atomic_notifier_call_chain - Call functions in an atomic notifier chain
  180. * @nh: Pointer to head of the atomic notifier chain
  181. * @val: Value passed unmodified to notifier function
  182. * @v: Pointer passed unmodified to notifier function
  183. *
  184. * Calls each function in a notifier chain in turn. The functions
  185. * run in an atomic context, so they must not block.
  186. * This routine uses RCU to synchronize with changes to the chain.
  187. *
  188. * If the return value of the notifier can be and'ed
  189. * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
  190. * will return immediately, with the return value of
  191. * the notifier function which halted execution.
  192. * Otherwise the return value is the return value
  193. * of the last notifier function called.
  194. */
  195. int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  196. unsigned long val, void *v)
  197. {
  198. int ret;
  199. rcu_read_lock();
  200. ret = notifier_call_chain(&nh->head, val, v);
  201. rcu_read_unlock();
  202. return ret;
  203. }
  204. EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
  205. /*
  206. * Blocking notifier chain routines. All access to the chain is
  207. * synchronized by an rwsem.
  208. */
  209. /**
  210. * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
  211. * @nh: Pointer to head of the blocking notifier chain
  212. * @n: New entry in notifier chain
  213. *
  214. * Adds a notifier to a blocking notifier chain.
  215. * Must be called in process context.
  216. *
  217. * Currently always returns zero.
  218. */
  219. int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
  220. struct notifier_block *n)
  221. {
  222. int ret;
  223. /*
  224. * This code gets used during boot-up, when task switching is
  225. * not yet working and interrupts must remain disabled. At
  226. * such times we must not call down_write().
  227. */
  228. if (unlikely(system_state == SYSTEM_BOOTING))
  229. return notifier_chain_register(&nh->head, n);
  230. down_write(&nh->rwsem);
  231. ret = notifier_chain_register(&nh->head, n);
  232. up_write(&nh->rwsem);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
  236. /**
  237. * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
  238. * @nh: Pointer to head of the blocking notifier chain
  239. * @n: Entry to remove from notifier chain
  240. *
  241. * Removes a notifier from a blocking notifier chain.
  242. * Must be called from process context.
  243. *
  244. * Returns zero on success or %-ENOENT on failure.
  245. */
  246. int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
  247. struct notifier_block *n)
  248. {
  249. int ret;
  250. /*
  251. * This code gets used during boot-up, when task switching is
  252. * not yet working and interrupts must remain disabled. At
  253. * such times we must not call down_write().
  254. */
  255. if (unlikely(system_state == SYSTEM_BOOTING))
  256. return notifier_chain_unregister(&nh->head, n);
  257. down_write(&nh->rwsem);
  258. ret = notifier_chain_unregister(&nh->head, n);
  259. up_write(&nh->rwsem);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
  263. /**
  264. * blocking_notifier_call_chain - Call functions in a blocking notifier chain
  265. * @nh: Pointer to head of the blocking notifier chain
  266. * @val: Value passed unmodified to notifier function
  267. * @v: Pointer passed unmodified to notifier function
  268. *
  269. * Calls each function in a notifier chain in turn. The functions
  270. * run in a process context, so they are allowed to block.
  271. *
  272. * If the return value of the notifier can be and'ed
  273. * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
  274. * will return immediately, with the return value of
  275. * the notifier function which halted execution.
  276. * Otherwise the return value is the return value
  277. * of the last notifier function called.
  278. */
  279. int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  280. unsigned long val, void *v)
  281. {
  282. int ret;
  283. down_read(&nh->rwsem);
  284. ret = notifier_call_chain(&nh->head, val, v);
  285. up_read(&nh->rwsem);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
  289. /*
  290. * Raw notifier chain routines. There is no protection;
  291. * the caller must provide it. Use at your own risk!
  292. */
  293. /**
  294. * raw_notifier_chain_register - Add notifier to a raw notifier chain
  295. * @nh: Pointer to head of the raw notifier chain
  296. * @n: New entry in notifier chain
  297. *
  298. * Adds a notifier to a raw notifier chain.
  299. * All locking must be provided by the caller.
  300. *
  301. * Currently always returns zero.
  302. */
  303. int raw_notifier_chain_register(struct raw_notifier_head *nh,
  304. struct notifier_block *n)
  305. {
  306. return notifier_chain_register(&nh->head, n);
  307. }
  308. EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
  309. /**
  310. * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
  311. * @nh: Pointer to head of the raw notifier chain
  312. * @n: Entry to remove from notifier chain
  313. *
  314. * Removes a notifier from a raw notifier chain.
  315. * All locking must be provided by the caller.
  316. *
  317. * Returns zero on success or %-ENOENT on failure.
  318. */
  319. int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
  320. struct notifier_block *n)
  321. {
  322. return notifier_chain_unregister(&nh->head, n);
  323. }
  324. EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
  325. /**
  326. * raw_notifier_call_chain - Call functions in a raw notifier chain
  327. * @nh: Pointer to head of the raw notifier chain
  328. * @val: Value passed unmodified to notifier function
  329. * @v: Pointer passed unmodified to notifier function
  330. *
  331. * Calls each function in a notifier chain in turn. The functions
  332. * run in an undefined context.
  333. * All locking must be provided by the caller.
  334. *
  335. * If the return value of the notifier can be and'ed
  336. * with %NOTIFY_STOP_MASK then raw_notifier_call_chain
  337. * will return immediately, with the return value of
  338. * the notifier function which halted execution.
  339. * Otherwise the return value is the return value
  340. * of the last notifier function called.
  341. */
  342. int raw_notifier_call_chain(struct raw_notifier_head *nh,
  343. unsigned long val, void *v)
  344. {
  345. return notifier_call_chain(&nh->head, val, v);
  346. }
  347. EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
  348. /**
  349. * register_reboot_notifier - Register function to be called at reboot time
  350. * @nb: Info about notifier function to be called
  351. *
  352. * Registers a function with the list of functions
  353. * to be called at reboot time.
  354. *
  355. * Currently always returns zero, as blocking_notifier_chain_register
  356. * always returns zero.
  357. */
  358. int register_reboot_notifier(struct notifier_block * nb)
  359. {
  360. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  361. }
  362. EXPORT_SYMBOL(register_reboot_notifier);
  363. /**
  364. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  365. * @nb: Hook to be unregistered
  366. *
  367. * Unregisters a previously registered reboot
  368. * notifier function.
  369. *
  370. * Returns zero on success, or %-ENOENT on failure.
  371. */
  372. int unregister_reboot_notifier(struct notifier_block * nb)
  373. {
  374. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  375. }
  376. EXPORT_SYMBOL(unregister_reboot_notifier);
  377. static int set_one_prio(struct task_struct *p, int niceval, int error)
  378. {
  379. int no_nice;
  380. if (p->uid != current->euid &&
  381. p->euid != current->euid && !capable(CAP_SYS_NICE)) {
  382. error = -EPERM;
  383. goto out;
  384. }
  385. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  386. error = -EACCES;
  387. goto out;
  388. }
  389. no_nice = security_task_setnice(p, niceval);
  390. if (no_nice) {
  391. error = no_nice;
  392. goto out;
  393. }
  394. if (error == -ESRCH)
  395. error = 0;
  396. set_user_nice(p, niceval);
  397. out:
  398. return error;
  399. }
  400. asmlinkage long sys_setpriority(int which, int who, int niceval)
  401. {
  402. struct task_struct *g, *p;
  403. struct user_struct *user;
  404. int error = -EINVAL;
  405. if (which > 2 || which < 0)
  406. goto out;
  407. /* normalize: avoid signed division (rounding problems) */
  408. error = -ESRCH;
  409. if (niceval < -20)
  410. niceval = -20;
  411. if (niceval > 19)
  412. niceval = 19;
  413. read_lock(&tasklist_lock);
  414. switch (which) {
  415. case PRIO_PROCESS:
  416. if (!who)
  417. who = current->pid;
  418. p = find_task_by_pid(who);
  419. if (p)
  420. error = set_one_prio(p, niceval, error);
  421. break;
  422. case PRIO_PGRP:
  423. if (!who)
  424. who = process_group(current);
  425. do_each_task_pid(who, PIDTYPE_PGID, p) {
  426. error = set_one_prio(p, niceval, error);
  427. } while_each_task_pid(who, PIDTYPE_PGID, p);
  428. break;
  429. case PRIO_USER:
  430. user = current->user;
  431. if (!who)
  432. who = current->uid;
  433. else
  434. if ((who != current->uid) && !(user = find_user(who)))
  435. goto out_unlock; /* No processes for this user */
  436. do_each_thread(g, p)
  437. if (p->uid == who)
  438. error = set_one_prio(p, niceval, error);
  439. while_each_thread(g, p);
  440. if (who != current->uid)
  441. free_uid(user); /* For find_user() */
  442. break;
  443. }
  444. out_unlock:
  445. read_unlock(&tasklist_lock);
  446. out:
  447. return error;
  448. }
  449. /*
  450. * Ugh. To avoid negative return values, "getpriority()" will
  451. * not return the normal nice-value, but a negated value that
  452. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  453. * to stay compatible.
  454. */
  455. asmlinkage long sys_getpriority(int which, int who)
  456. {
  457. struct task_struct *g, *p;
  458. struct user_struct *user;
  459. long niceval, retval = -ESRCH;
  460. if (which > 2 || which < 0)
  461. return -EINVAL;
  462. read_lock(&tasklist_lock);
  463. switch (which) {
  464. case PRIO_PROCESS:
  465. if (!who)
  466. who = current->pid;
  467. p = find_task_by_pid(who);
  468. if (p) {
  469. niceval = 20 - task_nice(p);
  470. if (niceval > retval)
  471. retval = niceval;
  472. }
  473. break;
  474. case PRIO_PGRP:
  475. if (!who)
  476. who = process_group(current);
  477. do_each_task_pid(who, PIDTYPE_PGID, p) {
  478. niceval = 20 - task_nice(p);
  479. if (niceval > retval)
  480. retval = niceval;
  481. } while_each_task_pid(who, PIDTYPE_PGID, p);
  482. break;
  483. case PRIO_USER:
  484. user = current->user;
  485. if (!who)
  486. who = current->uid;
  487. else
  488. if ((who != current->uid) && !(user = find_user(who)))
  489. goto out_unlock; /* No processes for this user */
  490. do_each_thread(g, p)
  491. if (p->uid == who) {
  492. niceval = 20 - task_nice(p);
  493. if (niceval > retval)
  494. retval = niceval;
  495. }
  496. while_each_thread(g, p);
  497. if (who != current->uid)
  498. free_uid(user); /* for find_user() */
  499. break;
  500. }
  501. out_unlock:
  502. read_unlock(&tasklist_lock);
  503. return retval;
  504. }
  505. /**
  506. * emergency_restart - reboot the system
  507. *
  508. * Without shutting down any hardware or taking any locks
  509. * reboot the system. This is called when we know we are in
  510. * trouble so this is our best effort to reboot. This is
  511. * safe to call in interrupt context.
  512. */
  513. void emergency_restart(void)
  514. {
  515. machine_emergency_restart();
  516. }
  517. EXPORT_SYMBOL_GPL(emergency_restart);
  518. void kernel_restart_prepare(char *cmd)
  519. {
  520. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  521. system_state = SYSTEM_RESTART;
  522. device_shutdown();
  523. }
  524. /**
  525. * kernel_restart - reboot the system
  526. * @cmd: pointer to buffer containing command to execute for restart
  527. * or %NULL
  528. *
  529. * Shutdown everything and perform a clean reboot.
  530. * This is not safe to call in interrupt context.
  531. */
  532. void kernel_restart(char *cmd)
  533. {
  534. kernel_restart_prepare(cmd);
  535. if (!cmd) {
  536. printk(KERN_EMERG "Restarting system.\n");
  537. } else {
  538. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  539. }
  540. printk(".\n");
  541. machine_restart(cmd);
  542. }
  543. EXPORT_SYMBOL_GPL(kernel_restart);
  544. /**
  545. * kernel_kexec - reboot the system
  546. *
  547. * Move into place and start executing a preloaded standalone
  548. * executable. If nothing was preloaded return an error.
  549. */
  550. void kernel_kexec(void)
  551. {
  552. #ifdef CONFIG_KEXEC
  553. struct kimage *image;
  554. image = xchg(&kexec_image, NULL);
  555. if (!image) {
  556. return;
  557. }
  558. kernel_restart_prepare(NULL);
  559. printk(KERN_EMERG "Starting new kernel\n");
  560. machine_shutdown();
  561. machine_kexec(image);
  562. #endif
  563. }
  564. EXPORT_SYMBOL_GPL(kernel_kexec);
  565. void kernel_shutdown_prepare(enum system_states state)
  566. {
  567. blocking_notifier_call_chain(&reboot_notifier_list,
  568. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  569. system_state = state;
  570. device_shutdown();
  571. }
  572. /**
  573. * kernel_halt - halt the system
  574. *
  575. * Shutdown everything and perform a clean system halt.
  576. */
  577. void kernel_halt(void)
  578. {
  579. kernel_shutdown_prepare(SYSTEM_HALT);
  580. printk(KERN_EMERG "System halted.\n");
  581. machine_halt();
  582. }
  583. EXPORT_SYMBOL_GPL(kernel_halt);
  584. /**
  585. * kernel_power_off - power_off the system
  586. *
  587. * Shutdown everything and perform a clean system power_off.
  588. */
  589. void kernel_power_off(void)
  590. {
  591. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  592. printk(KERN_EMERG "Power down.\n");
  593. machine_power_off();
  594. }
  595. EXPORT_SYMBOL_GPL(kernel_power_off);
  596. /*
  597. * Reboot system call: for obvious reasons only root may call it,
  598. * and even root needs to set up some magic numbers in the registers
  599. * so that some mistake won't make this reboot the whole machine.
  600. * You can also set the meaning of the ctrl-alt-del-key here.
  601. *
  602. * reboot doesn't sync: do that yourself before calling this.
  603. */
  604. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  605. {
  606. char buffer[256];
  607. /* We only trust the superuser with rebooting the system. */
  608. if (!capable(CAP_SYS_BOOT))
  609. return -EPERM;
  610. /* For safety, we require "magic" arguments. */
  611. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  612. (magic2 != LINUX_REBOOT_MAGIC2 &&
  613. magic2 != LINUX_REBOOT_MAGIC2A &&
  614. magic2 != LINUX_REBOOT_MAGIC2B &&
  615. magic2 != LINUX_REBOOT_MAGIC2C))
  616. return -EINVAL;
  617. /* Instead of trying to make the power_off code look like
  618. * halt when pm_power_off is not set do it the easy way.
  619. */
  620. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  621. cmd = LINUX_REBOOT_CMD_HALT;
  622. lock_kernel();
  623. switch (cmd) {
  624. case LINUX_REBOOT_CMD_RESTART:
  625. kernel_restart(NULL);
  626. break;
  627. case LINUX_REBOOT_CMD_CAD_ON:
  628. C_A_D = 1;
  629. break;
  630. case LINUX_REBOOT_CMD_CAD_OFF:
  631. C_A_D = 0;
  632. break;
  633. case LINUX_REBOOT_CMD_HALT:
  634. kernel_halt();
  635. unlock_kernel();
  636. do_exit(0);
  637. break;
  638. case LINUX_REBOOT_CMD_POWER_OFF:
  639. kernel_power_off();
  640. unlock_kernel();
  641. do_exit(0);
  642. break;
  643. case LINUX_REBOOT_CMD_RESTART2:
  644. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  645. unlock_kernel();
  646. return -EFAULT;
  647. }
  648. buffer[sizeof(buffer) - 1] = '\0';
  649. kernel_restart(buffer);
  650. break;
  651. case LINUX_REBOOT_CMD_KEXEC:
  652. kernel_kexec();
  653. unlock_kernel();
  654. return -EINVAL;
  655. #ifdef CONFIG_SOFTWARE_SUSPEND
  656. case LINUX_REBOOT_CMD_SW_SUSPEND:
  657. {
  658. int ret = software_suspend();
  659. unlock_kernel();
  660. return ret;
  661. }
  662. #endif
  663. default:
  664. unlock_kernel();
  665. return -EINVAL;
  666. }
  667. unlock_kernel();
  668. return 0;
  669. }
  670. static void deferred_cad(void *dummy)
  671. {
  672. kernel_restart(NULL);
  673. }
  674. /*
  675. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  676. * As it's called within an interrupt, it may NOT sync: the only choice
  677. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  678. */
  679. void ctrl_alt_del(void)
  680. {
  681. static DECLARE_WORK(cad_work, deferred_cad, NULL);
  682. if (C_A_D)
  683. schedule_work(&cad_work);
  684. else
  685. kill_proc(cad_pid, SIGINT, 1);
  686. }
  687. /*
  688. * Unprivileged users may change the real gid to the effective gid
  689. * or vice versa. (BSD-style)
  690. *
  691. * If you set the real gid at all, or set the effective gid to a value not
  692. * equal to the real gid, then the saved gid is set to the new effective gid.
  693. *
  694. * This makes it possible for a setgid program to completely drop its
  695. * privileges, which is often a useful assertion to make when you are doing
  696. * a security audit over a program.
  697. *
  698. * The general idea is that a program which uses just setregid() will be
  699. * 100% compatible with BSD. A program which uses just setgid() will be
  700. * 100% compatible with POSIX with saved IDs.
  701. *
  702. * SMP: There are not races, the GIDs are checked only by filesystem
  703. * operations (as far as semantic preservation is concerned).
  704. */
  705. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  706. {
  707. int old_rgid = current->gid;
  708. int old_egid = current->egid;
  709. int new_rgid = old_rgid;
  710. int new_egid = old_egid;
  711. int retval;
  712. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  713. if (retval)
  714. return retval;
  715. if (rgid != (gid_t) -1) {
  716. if ((old_rgid == rgid) ||
  717. (current->egid==rgid) ||
  718. capable(CAP_SETGID))
  719. new_rgid = rgid;
  720. else
  721. return -EPERM;
  722. }
  723. if (egid != (gid_t) -1) {
  724. if ((old_rgid == egid) ||
  725. (current->egid == egid) ||
  726. (current->sgid == egid) ||
  727. capable(CAP_SETGID))
  728. new_egid = egid;
  729. else {
  730. return -EPERM;
  731. }
  732. }
  733. if (new_egid != old_egid)
  734. {
  735. current->mm->dumpable = suid_dumpable;
  736. smp_wmb();
  737. }
  738. if (rgid != (gid_t) -1 ||
  739. (egid != (gid_t) -1 && egid != old_rgid))
  740. current->sgid = new_egid;
  741. current->fsgid = new_egid;
  742. current->egid = new_egid;
  743. current->gid = new_rgid;
  744. key_fsgid_changed(current);
  745. proc_id_connector(current, PROC_EVENT_GID);
  746. return 0;
  747. }
  748. /*
  749. * setgid() is implemented like SysV w/ SAVED_IDS
  750. *
  751. * SMP: Same implicit races as above.
  752. */
  753. asmlinkage long sys_setgid(gid_t gid)
  754. {
  755. int old_egid = current->egid;
  756. int retval;
  757. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  758. if (retval)
  759. return retval;
  760. if (capable(CAP_SETGID))
  761. {
  762. if(old_egid != gid)
  763. {
  764. current->mm->dumpable = suid_dumpable;
  765. smp_wmb();
  766. }
  767. current->gid = current->egid = current->sgid = current->fsgid = gid;
  768. }
  769. else if ((gid == current->gid) || (gid == current->sgid))
  770. {
  771. if(old_egid != gid)
  772. {
  773. current->mm->dumpable = suid_dumpable;
  774. smp_wmb();
  775. }
  776. current->egid = current->fsgid = gid;
  777. }
  778. else
  779. return -EPERM;
  780. key_fsgid_changed(current);
  781. proc_id_connector(current, PROC_EVENT_GID);
  782. return 0;
  783. }
  784. static int set_user(uid_t new_ruid, int dumpclear)
  785. {
  786. struct user_struct *new_user;
  787. new_user = alloc_uid(new_ruid);
  788. if (!new_user)
  789. return -EAGAIN;
  790. if (atomic_read(&new_user->processes) >=
  791. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  792. new_user != &root_user) {
  793. free_uid(new_user);
  794. return -EAGAIN;
  795. }
  796. switch_uid(new_user);
  797. if(dumpclear)
  798. {
  799. current->mm->dumpable = suid_dumpable;
  800. smp_wmb();
  801. }
  802. current->uid = new_ruid;
  803. return 0;
  804. }
  805. /*
  806. * Unprivileged users may change the real uid to the effective uid
  807. * or vice versa. (BSD-style)
  808. *
  809. * If you set the real uid at all, or set the effective uid to a value not
  810. * equal to the real uid, then the saved uid is set to the new effective uid.
  811. *
  812. * This makes it possible for a setuid program to completely drop its
  813. * privileges, which is often a useful assertion to make when you are doing
  814. * a security audit over a program.
  815. *
  816. * The general idea is that a program which uses just setreuid() will be
  817. * 100% compatible with BSD. A program which uses just setuid() will be
  818. * 100% compatible with POSIX with saved IDs.
  819. */
  820. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  821. {
  822. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  823. int retval;
  824. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  825. if (retval)
  826. return retval;
  827. new_ruid = old_ruid = current->uid;
  828. new_euid = old_euid = current->euid;
  829. old_suid = current->suid;
  830. if (ruid != (uid_t) -1) {
  831. new_ruid = ruid;
  832. if ((old_ruid != ruid) &&
  833. (current->euid != ruid) &&
  834. !capable(CAP_SETUID))
  835. return -EPERM;
  836. }
  837. if (euid != (uid_t) -1) {
  838. new_euid = euid;
  839. if ((old_ruid != euid) &&
  840. (current->euid != euid) &&
  841. (current->suid != euid) &&
  842. !capable(CAP_SETUID))
  843. return -EPERM;
  844. }
  845. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  846. return -EAGAIN;
  847. if (new_euid != old_euid)
  848. {
  849. current->mm->dumpable = suid_dumpable;
  850. smp_wmb();
  851. }
  852. current->fsuid = current->euid = new_euid;
  853. if (ruid != (uid_t) -1 ||
  854. (euid != (uid_t) -1 && euid != old_ruid))
  855. current->suid = current->euid;
  856. current->fsuid = current->euid;
  857. key_fsuid_changed(current);
  858. proc_id_connector(current, PROC_EVENT_UID);
  859. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  860. }
  861. /*
  862. * setuid() is implemented like SysV with SAVED_IDS
  863. *
  864. * Note that SAVED_ID's is deficient in that a setuid root program
  865. * like sendmail, for example, cannot set its uid to be a normal
  866. * user and then switch back, because if you're root, setuid() sets
  867. * the saved uid too. If you don't like this, blame the bright people
  868. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  869. * will allow a root program to temporarily drop privileges and be able to
  870. * regain them by swapping the real and effective uid.
  871. */
  872. asmlinkage long sys_setuid(uid_t uid)
  873. {
  874. int old_euid = current->euid;
  875. int old_ruid, old_suid, new_ruid, new_suid;
  876. int retval;
  877. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  878. if (retval)
  879. return retval;
  880. old_ruid = new_ruid = current->uid;
  881. old_suid = current->suid;
  882. new_suid = old_suid;
  883. if (capable(CAP_SETUID)) {
  884. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  885. return -EAGAIN;
  886. new_suid = uid;
  887. } else if ((uid != current->uid) && (uid != new_suid))
  888. return -EPERM;
  889. if (old_euid != uid)
  890. {
  891. current->mm->dumpable = suid_dumpable;
  892. smp_wmb();
  893. }
  894. current->fsuid = current->euid = uid;
  895. current->suid = new_suid;
  896. key_fsuid_changed(current);
  897. proc_id_connector(current, PROC_EVENT_UID);
  898. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  899. }
  900. /*
  901. * This function implements a generic ability to update ruid, euid,
  902. * and suid. This allows you to implement the 4.4 compatible seteuid().
  903. */
  904. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  905. {
  906. int old_ruid = current->uid;
  907. int old_euid = current->euid;
  908. int old_suid = current->suid;
  909. int retval;
  910. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  911. if (retval)
  912. return retval;
  913. if (!capable(CAP_SETUID)) {
  914. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  915. (ruid != current->euid) && (ruid != current->suid))
  916. return -EPERM;
  917. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  918. (euid != current->euid) && (euid != current->suid))
  919. return -EPERM;
  920. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  921. (suid != current->euid) && (suid != current->suid))
  922. return -EPERM;
  923. }
  924. if (ruid != (uid_t) -1) {
  925. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  926. return -EAGAIN;
  927. }
  928. if (euid != (uid_t) -1) {
  929. if (euid != current->euid)
  930. {
  931. current->mm->dumpable = suid_dumpable;
  932. smp_wmb();
  933. }
  934. current->euid = euid;
  935. }
  936. current->fsuid = current->euid;
  937. if (suid != (uid_t) -1)
  938. current->suid = suid;
  939. key_fsuid_changed(current);
  940. proc_id_connector(current, PROC_EVENT_UID);
  941. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  942. }
  943. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  944. {
  945. int retval;
  946. if (!(retval = put_user(current->uid, ruid)) &&
  947. !(retval = put_user(current->euid, euid)))
  948. retval = put_user(current->suid, suid);
  949. return retval;
  950. }
  951. /*
  952. * Same as above, but for rgid, egid, sgid.
  953. */
  954. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  955. {
  956. int retval;
  957. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  958. if (retval)
  959. return retval;
  960. if (!capable(CAP_SETGID)) {
  961. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  962. (rgid != current->egid) && (rgid != current->sgid))
  963. return -EPERM;
  964. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  965. (egid != current->egid) && (egid != current->sgid))
  966. return -EPERM;
  967. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  968. (sgid != current->egid) && (sgid != current->sgid))
  969. return -EPERM;
  970. }
  971. if (egid != (gid_t) -1) {
  972. if (egid != current->egid)
  973. {
  974. current->mm->dumpable = suid_dumpable;
  975. smp_wmb();
  976. }
  977. current->egid = egid;
  978. }
  979. current->fsgid = current->egid;
  980. if (rgid != (gid_t) -1)
  981. current->gid = rgid;
  982. if (sgid != (gid_t) -1)
  983. current->sgid = sgid;
  984. key_fsgid_changed(current);
  985. proc_id_connector(current, PROC_EVENT_GID);
  986. return 0;
  987. }
  988. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  989. {
  990. int retval;
  991. if (!(retval = put_user(current->gid, rgid)) &&
  992. !(retval = put_user(current->egid, egid)))
  993. retval = put_user(current->sgid, sgid);
  994. return retval;
  995. }
  996. /*
  997. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  998. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  999. * whatever uid it wants to). It normally shadows "euid", except when
  1000. * explicitly set by setfsuid() or for access..
  1001. */
  1002. asmlinkage long sys_setfsuid(uid_t uid)
  1003. {
  1004. int old_fsuid;
  1005. old_fsuid = current->fsuid;
  1006. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  1007. return old_fsuid;
  1008. if (uid == current->uid || uid == current->euid ||
  1009. uid == current->suid || uid == current->fsuid ||
  1010. capable(CAP_SETUID))
  1011. {
  1012. if (uid != old_fsuid)
  1013. {
  1014. current->mm->dumpable = suid_dumpable;
  1015. smp_wmb();
  1016. }
  1017. current->fsuid = uid;
  1018. }
  1019. key_fsuid_changed(current);
  1020. proc_id_connector(current, PROC_EVENT_UID);
  1021. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1022. return old_fsuid;
  1023. }
  1024. /*
  1025. * Samma på svenska..
  1026. */
  1027. asmlinkage long sys_setfsgid(gid_t gid)
  1028. {
  1029. int old_fsgid;
  1030. old_fsgid = current->fsgid;
  1031. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1032. return old_fsgid;
  1033. if (gid == current->gid || gid == current->egid ||
  1034. gid == current->sgid || gid == current->fsgid ||
  1035. capable(CAP_SETGID))
  1036. {
  1037. if (gid != old_fsgid)
  1038. {
  1039. current->mm->dumpable = suid_dumpable;
  1040. smp_wmb();
  1041. }
  1042. current->fsgid = gid;
  1043. key_fsgid_changed(current);
  1044. proc_id_connector(current, PROC_EVENT_GID);
  1045. }
  1046. return old_fsgid;
  1047. }
  1048. asmlinkage long sys_times(struct tms __user * tbuf)
  1049. {
  1050. /*
  1051. * In the SMP world we might just be unlucky and have one of
  1052. * the times increment as we use it. Since the value is an
  1053. * atomically safe type this is just fine. Conceptually its
  1054. * as if the syscall took an instant longer to occur.
  1055. */
  1056. if (tbuf) {
  1057. struct tms tmp;
  1058. struct task_struct *tsk = current;
  1059. struct task_struct *t;
  1060. cputime_t utime, stime, cutime, cstime;
  1061. spin_lock_irq(&tsk->sighand->siglock);
  1062. utime = tsk->signal->utime;
  1063. stime = tsk->signal->stime;
  1064. t = tsk;
  1065. do {
  1066. utime = cputime_add(utime, t->utime);
  1067. stime = cputime_add(stime, t->stime);
  1068. t = next_thread(t);
  1069. } while (t != tsk);
  1070. cutime = tsk->signal->cutime;
  1071. cstime = tsk->signal->cstime;
  1072. spin_unlock_irq(&tsk->sighand->siglock);
  1073. tmp.tms_utime = cputime_to_clock_t(utime);
  1074. tmp.tms_stime = cputime_to_clock_t(stime);
  1075. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1076. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1077. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1078. return -EFAULT;
  1079. }
  1080. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1081. }
  1082. /*
  1083. * This needs some heavy checking ...
  1084. * I just haven't the stomach for it. I also don't fully
  1085. * understand sessions/pgrp etc. Let somebody who does explain it.
  1086. *
  1087. * OK, I think I have the protection semantics right.... this is really
  1088. * only important on a multi-user system anyway, to make sure one user
  1089. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1090. *
  1091. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1092. * LBT 04.03.94
  1093. */
  1094. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1095. {
  1096. struct task_struct *p;
  1097. struct task_struct *group_leader = current->group_leader;
  1098. int err = -EINVAL;
  1099. if (!pid)
  1100. pid = group_leader->pid;
  1101. if (!pgid)
  1102. pgid = pid;
  1103. if (pgid < 0)
  1104. return -EINVAL;
  1105. /* From this point forward we keep holding onto the tasklist lock
  1106. * so that our parent does not change from under us. -DaveM
  1107. */
  1108. write_lock_irq(&tasklist_lock);
  1109. err = -ESRCH;
  1110. p = find_task_by_pid(pid);
  1111. if (!p)
  1112. goto out;
  1113. err = -EINVAL;
  1114. if (!thread_group_leader(p))
  1115. goto out;
  1116. if (p->real_parent == group_leader) {
  1117. err = -EPERM;
  1118. if (p->signal->session != group_leader->signal->session)
  1119. goto out;
  1120. err = -EACCES;
  1121. if (p->did_exec)
  1122. goto out;
  1123. } else {
  1124. err = -ESRCH;
  1125. if (p != group_leader)
  1126. goto out;
  1127. }
  1128. err = -EPERM;
  1129. if (p->signal->leader)
  1130. goto out;
  1131. if (pgid != pid) {
  1132. struct task_struct *p;
  1133. do_each_task_pid(pgid, PIDTYPE_PGID, p) {
  1134. if (p->signal->session == group_leader->signal->session)
  1135. goto ok_pgid;
  1136. } while_each_task_pid(pgid, PIDTYPE_PGID, p);
  1137. goto out;
  1138. }
  1139. ok_pgid:
  1140. err = security_task_setpgid(p, pgid);
  1141. if (err)
  1142. goto out;
  1143. if (process_group(p) != pgid) {
  1144. detach_pid(p, PIDTYPE_PGID);
  1145. p->signal->pgrp = pgid;
  1146. attach_pid(p, PIDTYPE_PGID, pgid);
  1147. }
  1148. err = 0;
  1149. out:
  1150. /* All paths lead to here, thus we are safe. -DaveM */
  1151. write_unlock_irq(&tasklist_lock);
  1152. return err;
  1153. }
  1154. asmlinkage long sys_getpgid(pid_t pid)
  1155. {
  1156. if (!pid) {
  1157. return process_group(current);
  1158. } else {
  1159. int retval;
  1160. struct task_struct *p;
  1161. read_lock(&tasklist_lock);
  1162. p = find_task_by_pid(pid);
  1163. retval = -ESRCH;
  1164. if (p) {
  1165. retval = security_task_getpgid(p);
  1166. if (!retval)
  1167. retval = process_group(p);
  1168. }
  1169. read_unlock(&tasklist_lock);
  1170. return retval;
  1171. }
  1172. }
  1173. #ifdef __ARCH_WANT_SYS_GETPGRP
  1174. asmlinkage long sys_getpgrp(void)
  1175. {
  1176. /* SMP - assuming writes are word atomic this is fine */
  1177. return process_group(current);
  1178. }
  1179. #endif
  1180. asmlinkage long sys_getsid(pid_t pid)
  1181. {
  1182. if (!pid) {
  1183. return current->signal->session;
  1184. } else {
  1185. int retval;
  1186. struct task_struct *p;
  1187. read_lock(&tasklist_lock);
  1188. p = find_task_by_pid(pid);
  1189. retval = -ESRCH;
  1190. if(p) {
  1191. retval = security_task_getsid(p);
  1192. if (!retval)
  1193. retval = p->signal->session;
  1194. }
  1195. read_unlock(&tasklist_lock);
  1196. return retval;
  1197. }
  1198. }
  1199. asmlinkage long sys_setsid(void)
  1200. {
  1201. struct task_struct *group_leader = current->group_leader;
  1202. pid_t session;
  1203. int err = -EPERM;
  1204. mutex_lock(&tty_mutex);
  1205. write_lock_irq(&tasklist_lock);
  1206. /* Fail if I am already a session leader */
  1207. if (group_leader->signal->leader)
  1208. goto out;
  1209. session = group_leader->pid;
  1210. /* Fail if a process group id already exists that equals the
  1211. * proposed session id.
  1212. *
  1213. * Don't check if session id == 1 because kernel threads use this
  1214. * session id and so the check will always fail and make it so
  1215. * init cannot successfully call setsid.
  1216. */
  1217. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1218. goto out;
  1219. group_leader->signal->leader = 1;
  1220. __set_special_pids(session, session);
  1221. group_leader->signal->tty = NULL;
  1222. group_leader->signal->tty_old_pgrp = 0;
  1223. err = process_group(group_leader);
  1224. out:
  1225. write_unlock_irq(&tasklist_lock);
  1226. mutex_unlock(&tty_mutex);
  1227. return err;
  1228. }
  1229. /*
  1230. * Supplementary group IDs
  1231. */
  1232. /* init to 2 - one for init_task, one to ensure it is never freed */
  1233. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1234. struct group_info *groups_alloc(int gidsetsize)
  1235. {
  1236. struct group_info *group_info;
  1237. int nblocks;
  1238. int i;
  1239. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1240. /* Make sure we always allocate at least one indirect block pointer */
  1241. nblocks = nblocks ? : 1;
  1242. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1243. if (!group_info)
  1244. return NULL;
  1245. group_info->ngroups = gidsetsize;
  1246. group_info->nblocks = nblocks;
  1247. atomic_set(&group_info->usage, 1);
  1248. if (gidsetsize <= NGROUPS_SMALL) {
  1249. group_info->blocks[0] = group_info->small_block;
  1250. } else {
  1251. for (i = 0; i < nblocks; i++) {
  1252. gid_t *b;
  1253. b = (void *)__get_free_page(GFP_USER);
  1254. if (!b)
  1255. goto out_undo_partial_alloc;
  1256. group_info->blocks[i] = b;
  1257. }
  1258. }
  1259. return group_info;
  1260. out_undo_partial_alloc:
  1261. while (--i >= 0) {
  1262. free_page((unsigned long)group_info->blocks[i]);
  1263. }
  1264. kfree(group_info);
  1265. return NULL;
  1266. }
  1267. EXPORT_SYMBOL(groups_alloc);
  1268. void groups_free(struct group_info *group_info)
  1269. {
  1270. if (group_info->blocks[0] != group_info->small_block) {
  1271. int i;
  1272. for (i = 0; i < group_info->nblocks; i++)
  1273. free_page((unsigned long)group_info->blocks[i]);
  1274. }
  1275. kfree(group_info);
  1276. }
  1277. EXPORT_SYMBOL(groups_free);
  1278. /* export the group_info to a user-space array */
  1279. static int groups_to_user(gid_t __user *grouplist,
  1280. struct group_info *group_info)
  1281. {
  1282. int i;
  1283. int count = group_info->ngroups;
  1284. for (i = 0; i < group_info->nblocks; i++) {
  1285. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1286. int off = i * NGROUPS_PER_BLOCK;
  1287. int len = cp_count * sizeof(*grouplist);
  1288. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1289. return -EFAULT;
  1290. count -= cp_count;
  1291. }
  1292. return 0;
  1293. }
  1294. /* fill a group_info from a user-space array - it must be allocated already */
  1295. static int groups_from_user(struct group_info *group_info,
  1296. gid_t __user *grouplist)
  1297. {
  1298. int i;
  1299. int count = group_info->ngroups;
  1300. for (i = 0; i < group_info->nblocks; i++) {
  1301. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1302. int off = i * NGROUPS_PER_BLOCK;
  1303. int len = cp_count * sizeof(*grouplist);
  1304. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1305. return -EFAULT;
  1306. count -= cp_count;
  1307. }
  1308. return 0;
  1309. }
  1310. /* a simple Shell sort */
  1311. static void groups_sort(struct group_info *group_info)
  1312. {
  1313. int base, max, stride;
  1314. int gidsetsize = group_info->ngroups;
  1315. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1316. ; /* nothing */
  1317. stride /= 3;
  1318. while (stride) {
  1319. max = gidsetsize - stride;
  1320. for (base = 0; base < max; base++) {
  1321. int left = base;
  1322. int right = left + stride;
  1323. gid_t tmp = GROUP_AT(group_info, right);
  1324. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1325. GROUP_AT(group_info, right) =
  1326. GROUP_AT(group_info, left);
  1327. right = left;
  1328. left -= stride;
  1329. }
  1330. GROUP_AT(group_info, right) = tmp;
  1331. }
  1332. stride /= 3;
  1333. }
  1334. }
  1335. /* a simple bsearch */
  1336. int groups_search(struct group_info *group_info, gid_t grp)
  1337. {
  1338. unsigned int left, right;
  1339. if (!group_info)
  1340. return 0;
  1341. left = 0;
  1342. right = group_info->ngroups;
  1343. while (left < right) {
  1344. unsigned int mid = (left+right)/2;
  1345. int cmp = grp - GROUP_AT(group_info, mid);
  1346. if (cmp > 0)
  1347. left = mid + 1;
  1348. else if (cmp < 0)
  1349. right = mid;
  1350. else
  1351. return 1;
  1352. }
  1353. return 0;
  1354. }
  1355. /* validate and set current->group_info */
  1356. int set_current_groups(struct group_info *group_info)
  1357. {
  1358. int retval;
  1359. struct group_info *old_info;
  1360. retval = security_task_setgroups(group_info);
  1361. if (retval)
  1362. return retval;
  1363. groups_sort(group_info);
  1364. get_group_info(group_info);
  1365. task_lock(current);
  1366. old_info = current->group_info;
  1367. current->group_info = group_info;
  1368. task_unlock(current);
  1369. put_group_info(old_info);
  1370. return 0;
  1371. }
  1372. EXPORT_SYMBOL(set_current_groups);
  1373. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1374. {
  1375. int i = 0;
  1376. /*
  1377. * SMP: Nobody else can change our grouplist. Thus we are
  1378. * safe.
  1379. */
  1380. if (gidsetsize < 0)
  1381. return -EINVAL;
  1382. /* no need to grab task_lock here; it cannot change */
  1383. i = current->group_info->ngroups;
  1384. if (gidsetsize) {
  1385. if (i > gidsetsize) {
  1386. i = -EINVAL;
  1387. goto out;
  1388. }
  1389. if (groups_to_user(grouplist, current->group_info)) {
  1390. i = -EFAULT;
  1391. goto out;
  1392. }
  1393. }
  1394. out:
  1395. return i;
  1396. }
  1397. /*
  1398. * SMP: Our groups are copy-on-write. We can set them safely
  1399. * without another task interfering.
  1400. */
  1401. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1402. {
  1403. struct group_info *group_info;
  1404. int retval;
  1405. if (!capable(CAP_SETGID))
  1406. return -EPERM;
  1407. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1408. return -EINVAL;
  1409. group_info = groups_alloc(gidsetsize);
  1410. if (!group_info)
  1411. return -ENOMEM;
  1412. retval = groups_from_user(group_info, grouplist);
  1413. if (retval) {
  1414. put_group_info(group_info);
  1415. return retval;
  1416. }
  1417. retval = set_current_groups(group_info);
  1418. put_group_info(group_info);
  1419. return retval;
  1420. }
  1421. /*
  1422. * Check whether we're fsgid/egid or in the supplemental group..
  1423. */
  1424. int in_group_p(gid_t grp)
  1425. {
  1426. int retval = 1;
  1427. if (grp != current->fsgid) {
  1428. retval = groups_search(current->group_info, grp);
  1429. }
  1430. return retval;
  1431. }
  1432. EXPORT_SYMBOL(in_group_p);
  1433. int in_egroup_p(gid_t grp)
  1434. {
  1435. int retval = 1;
  1436. if (grp != current->egid) {
  1437. retval = groups_search(current->group_info, grp);
  1438. }
  1439. return retval;
  1440. }
  1441. EXPORT_SYMBOL(in_egroup_p);
  1442. DECLARE_RWSEM(uts_sem);
  1443. EXPORT_SYMBOL(uts_sem);
  1444. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1445. {
  1446. int errno = 0;
  1447. down_read(&uts_sem);
  1448. if (copy_to_user(name,&system_utsname,sizeof *name))
  1449. errno = -EFAULT;
  1450. up_read(&uts_sem);
  1451. return errno;
  1452. }
  1453. asmlinkage long sys_sethostname(char __user *name, int len)
  1454. {
  1455. int errno;
  1456. char tmp[__NEW_UTS_LEN];
  1457. if (!capable(CAP_SYS_ADMIN))
  1458. return -EPERM;
  1459. if (len < 0 || len > __NEW_UTS_LEN)
  1460. return -EINVAL;
  1461. down_write(&uts_sem);
  1462. errno = -EFAULT;
  1463. if (!copy_from_user(tmp, name, len)) {
  1464. memcpy(system_utsname.nodename, tmp, len);
  1465. system_utsname.nodename[len] = 0;
  1466. errno = 0;
  1467. }
  1468. up_write(&uts_sem);
  1469. return errno;
  1470. }
  1471. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1472. asmlinkage long sys_gethostname(char __user *name, int len)
  1473. {
  1474. int i, errno;
  1475. if (len < 0)
  1476. return -EINVAL;
  1477. down_read(&uts_sem);
  1478. i = 1 + strlen(system_utsname.nodename);
  1479. if (i > len)
  1480. i = len;
  1481. errno = 0;
  1482. if (copy_to_user(name, system_utsname.nodename, i))
  1483. errno = -EFAULT;
  1484. up_read(&uts_sem);
  1485. return errno;
  1486. }
  1487. #endif
  1488. /*
  1489. * Only setdomainname; getdomainname can be implemented by calling
  1490. * uname()
  1491. */
  1492. asmlinkage long sys_setdomainname(char __user *name, int len)
  1493. {
  1494. int errno;
  1495. char tmp[__NEW_UTS_LEN];
  1496. if (!capable(CAP_SYS_ADMIN))
  1497. return -EPERM;
  1498. if (len < 0 || len > __NEW_UTS_LEN)
  1499. return -EINVAL;
  1500. down_write(&uts_sem);
  1501. errno = -EFAULT;
  1502. if (!copy_from_user(tmp, name, len)) {
  1503. memcpy(system_utsname.domainname, tmp, len);
  1504. system_utsname.domainname[len] = 0;
  1505. errno = 0;
  1506. }
  1507. up_write(&uts_sem);
  1508. return errno;
  1509. }
  1510. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1511. {
  1512. if (resource >= RLIM_NLIMITS)
  1513. return -EINVAL;
  1514. else {
  1515. struct rlimit value;
  1516. task_lock(current->group_leader);
  1517. value = current->signal->rlim[resource];
  1518. task_unlock(current->group_leader);
  1519. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1520. }
  1521. }
  1522. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1523. /*
  1524. * Back compatibility for getrlimit. Needed for some apps.
  1525. */
  1526. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1527. {
  1528. struct rlimit x;
  1529. if (resource >= RLIM_NLIMITS)
  1530. return -EINVAL;
  1531. task_lock(current->group_leader);
  1532. x = current->signal->rlim[resource];
  1533. task_unlock(current->group_leader);
  1534. if(x.rlim_cur > 0x7FFFFFFF)
  1535. x.rlim_cur = 0x7FFFFFFF;
  1536. if(x.rlim_max > 0x7FFFFFFF)
  1537. x.rlim_max = 0x7FFFFFFF;
  1538. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1539. }
  1540. #endif
  1541. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1542. {
  1543. struct rlimit new_rlim, *old_rlim;
  1544. unsigned long it_prof_secs;
  1545. int retval;
  1546. if (resource >= RLIM_NLIMITS)
  1547. return -EINVAL;
  1548. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1549. return -EFAULT;
  1550. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1551. return -EINVAL;
  1552. old_rlim = current->signal->rlim + resource;
  1553. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1554. !capable(CAP_SYS_RESOURCE))
  1555. return -EPERM;
  1556. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1557. return -EPERM;
  1558. retval = security_task_setrlimit(resource, &new_rlim);
  1559. if (retval)
  1560. return retval;
  1561. task_lock(current->group_leader);
  1562. *old_rlim = new_rlim;
  1563. task_unlock(current->group_leader);
  1564. if (resource != RLIMIT_CPU)
  1565. goto out;
  1566. /*
  1567. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1568. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1569. * very long-standing error, and fixing it now risks breakage of
  1570. * applications, so we live with it
  1571. */
  1572. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1573. goto out;
  1574. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1575. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1576. unsigned long rlim_cur = new_rlim.rlim_cur;
  1577. cputime_t cputime;
  1578. if (rlim_cur == 0) {
  1579. /*
  1580. * The caller is asking for an immediate RLIMIT_CPU
  1581. * expiry. But we use the zero value to mean "it was
  1582. * never set". So let's cheat and make it one second
  1583. * instead
  1584. */
  1585. rlim_cur = 1;
  1586. }
  1587. cputime = secs_to_cputime(rlim_cur);
  1588. read_lock(&tasklist_lock);
  1589. spin_lock_irq(&current->sighand->siglock);
  1590. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1591. spin_unlock_irq(&current->sighand->siglock);
  1592. read_unlock(&tasklist_lock);
  1593. }
  1594. out:
  1595. return 0;
  1596. }
  1597. /*
  1598. * It would make sense to put struct rusage in the task_struct,
  1599. * except that would make the task_struct be *really big*. After
  1600. * task_struct gets moved into malloc'ed memory, it would
  1601. * make sense to do this. It will make moving the rest of the information
  1602. * a lot simpler! (Which we're not doing right now because we're not
  1603. * measuring them yet).
  1604. *
  1605. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1606. * races with threads incrementing their own counters. But since word
  1607. * reads are atomic, we either get new values or old values and we don't
  1608. * care which for the sums. We always take the siglock to protect reading
  1609. * the c* fields from p->signal from races with exit.c updating those
  1610. * fields when reaping, so a sample either gets all the additions of a
  1611. * given child after it's reaped, or none so this sample is before reaping.
  1612. *
  1613. * Locking:
  1614. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1615. * for the cases current multithreaded, non-current single threaded
  1616. * non-current multithreaded. Thread traversal is now safe with
  1617. * the siglock held.
  1618. * Strictly speaking, we donot need to take the siglock if we are current and
  1619. * single threaded, as no one else can take our signal_struct away, no one
  1620. * else can reap the children to update signal->c* counters, and no one else
  1621. * can race with the signal-> fields. If we do not take any lock, the
  1622. * signal-> fields could be read out of order while another thread was just
  1623. * exiting. So we should place a read memory barrier when we avoid the lock.
  1624. * On the writer side, write memory barrier is implied in __exit_signal
  1625. * as __exit_signal releases the siglock spinlock after updating the signal->
  1626. * fields. But we don't do this yet to keep things simple.
  1627. *
  1628. */
  1629. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1630. {
  1631. struct task_struct *t;
  1632. unsigned long flags;
  1633. cputime_t utime, stime;
  1634. memset((char *) r, 0, sizeof *r);
  1635. utime = stime = cputime_zero;
  1636. rcu_read_lock();
  1637. if (!lock_task_sighand(p, &flags)) {
  1638. rcu_read_unlock();
  1639. return;
  1640. }
  1641. switch (who) {
  1642. case RUSAGE_BOTH:
  1643. case RUSAGE_CHILDREN:
  1644. utime = p->signal->cutime;
  1645. stime = p->signal->cstime;
  1646. r->ru_nvcsw = p->signal->cnvcsw;
  1647. r->ru_nivcsw = p->signal->cnivcsw;
  1648. r->ru_minflt = p->signal->cmin_flt;
  1649. r->ru_majflt = p->signal->cmaj_flt;
  1650. if (who == RUSAGE_CHILDREN)
  1651. break;
  1652. case RUSAGE_SELF:
  1653. utime = cputime_add(utime, p->signal->utime);
  1654. stime = cputime_add(stime, p->signal->stime);
  1655. r->ru_nvcsw += p->signal->nvcsw;
  1656. r->ru_nivcsw += p->signal->nivcsw;
  1657. r->ru_minflt += p->signal->min_flt;
  1658. r->ru_majflt += p->signal->maj_flt;
  1659. t = p;
  1660. do {
  1661. utime = cputime_add(utime, t->utime);
  1662. stime = cputime_add(stime, t->stime);
  1663. r->ru_nvcsw += t->nvcsw;
  1664. r->ru_nivcsw += t->nivcsw;
  1665. r->ru_minflt += t->min_flt;
  1666. r->ru_majflt += t->maj_flt;
  1667. t = next_thread(t);
  1668. } while (t != p);
  1669. break;
  1670. default:
  1671. BUG();
  1672. }
  1673. unlock_task_sighand(p, &flags);
  1674. rcu_read_unlock();
  1675. cputime_to_timeval(utime, &r->ru_utime);
  1676. cputime_to_timeval(stime, &r->ru_stime);
  1677. }
  1678. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1679. {
  1680. struct rusage r;
  1681. k_getrusage(p, who, &r);
  1682. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1683. }
  1684. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1685. {
  1686. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1687. return -EINVAL;
  1688. return getrusage(current, who, ru);
  1689. }
  1690. asmlinkage long sys_umask(int mask)
  1691. {
  1692. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1693. return mask;
  1694. }
  1695. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1696. unsigned long arg4, unsigned long arg5)
  1697. {
  1698. long error;
  1699. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1700. if (error)
  1701. return error;
  1702. switch (option) {
  1703. case PR_SET_PDEATHSIG:
  1704. if (!valid_signal(arg2)) {
  1705. error = -EINVAL;
  1706. break;
  1707. }
  1708. current->pdeath_signal = arg2;
  1709. break;
  1710. case PR_GET_PDEATHSIG:
  1711. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1712. break;
  1713. case PR_GET_DUMPABLE:
  1714. error = current->mm->dumpable;
  1715. break;
  1716. case PR_SET_DUMPABLE:
  1717. if (arg2 < 0 || arg2 > 2) {
  1718. error = -EINVAL;
  1719. break;
  1720. }
  1721. current->mm->dumpable = arg2;
  1722. break;
  1723. case PR_SET_UNALIGN:
  1724. error = SET_UNALIGN_CTL(current, arg2);
  1725. break;
  1726. case PR_GET_UNALIGN:
  1727. error = GET_UNALIGN_CTL(current, arg2);
  1728. break;
  1729. case PR_SET_FPEMU:
  1730. error = SET_FPEMU_CTL(current, arg2);
  1731. break;
  1732. case PR_GET_FPEMU:
  1733. error = GET_FPEMU_CTL(current, arg2);
  1734. break;
  1735. case PR_SET_FPEXC:
  1736. error = SET_FPEXC_CTL(current, arg2);
  1737. break;
  1738. case PR_GET_FPEXC:
  1739. error = GET_FPEXC_CTL(current, arg2);
  1740. break;
  1741. case PR_GET_TIMING:
  1742. error = PR_TIMING_STATISTICAL;
  1743. break;
  1744. case PR_SET_TIMING:
  1745. if (arg2 == PR_TIMING_STATISTICAL)
  1746. error = 0;
  1747. else
  1748. error = -EINVAL;
  1749. break;
  1750. case PR_GET_KEEPCAPS:
  1751. if (current->keep_capabilities)
  1752. error = 1;
  1753. break;
  1754. case PR_SET_KEEPCAPS:
  1755. if (arg2 != 0 && arg2 != 1) {
  1756. error = -EINVAL;
  1757. break;
  1758. }
  1759. current->keep_capabilities = arg2;
  1760. break;
  1761. case PR_SET_NAME: {
  1762. struct task_struct *me = current;
  1763. unsigned char ncomm[sizeof(me->comm)];
  1764. ncomm[sizeof(me->comm)-1] = 0;
  1765. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1766. sizeof(me->comm)-1) < 0)
  1767. return -EFAULT;
  1768. set_task_comm(me, ncomm);
  1769. return 0;
  1770. }
  1771. case PR_GET_NAME: {
  1772. struct task_struct *me = current;
  1773. unsigned char tcomm[sizeof(me->comm)];
  1774. get_task_comm(tcomm, me);
  1775. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1776. return -EFAULT;
  1777. return 0;
  1778. }
  1779. case PR_GET_ENDIAN:
  1780. error = GET_ENDIAN(current, arg2);
  1781. break;
  1782. case PR_SET_ENDIAN:
  1783. error = SET_ENDIAN(current, arg2);
  1784. break;
  1785. default:
  1786. error = -EINVAL;
  1787. break;
  1788. }
  1789. return error;
  1790. }