sys.c 49 KB

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