sys.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kexec.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/capability.h>
  20. #include <linux/device.h>
  21. #include <linux/key.h>
  22. #include <linux/times.h>
  23. #include <linux/posix-timers.h>
  24. #include <linux/security.h>
  25. #include <linux/dcookies.h>
  26. #include <linux/suspend.h>
  27. #include <linux/tty.h>
  28. #include <linux/signal.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/compat.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/kprobes.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/io.h>
  36. #include <asm/unistd.h>
  37. #ifndef SET_UNALIGN_CTL
  38. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  39. #endif
  40. #ifndef GET_UNALIGN_CTL
  41. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  42. #endif
  43. #ifndef SET_FPEMU_CTL
  44. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  45. #endif
  46. #ifndef GET_FPEMU_CTL
  47. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  48. #endif
  49. #ifndef SET_FPEXC_CTL
  50. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  51. #endif
  52. #ifndef GET_FPEXC_CTL
  53. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  54. #endif
  55. #ifndef GET_ENDIAN
  56. # define GET_ENDIAN(a,b) (-EINVAL)
  57. #endif
  58. #ifndef SET_ENDIAN
  59. # define SET_ENDIAN(a,b) (-EINVAL)
  60. #endif
  61. /*
  62. * this is where the system-wide overflow UID and GID are defined, for
  63. * architectures that now have 32-bit UID/GID but didn't in the past
  64. */
  65. int overflowuid = DEFAULT_OVERFLOWUID;
  66. int overflowgid = DEFAULT_OVERFLOWGID;
  67. #ifdef CONFIG_UID16
  68. EXPORT_SYMBOL(overflowuid);
  69. EXPORT_SYMBOL(overflowgid);
  70. #endif
  71. /*
  72. * the same as above, but for filesystems which can only store a 16-bit
  73. * UID and GID. as such, this is needed on all architectures
  74. */
  75. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  76. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  77. EXPORT_SYMBOL(fs_overflowuid);
  78. EXPORT_SYMBOL(fs_overflowgid);
  79. /*
  80. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  81. */
  82. int C_A_D = 1;
  83. int cad_pid = 1;
  84. /*
  85. * Notifier list for kernel code which wants to be called
  86. * at shutdown. This is used to stop any idling DMA operations
  87. * and the like.
  88. */
  89. static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  90. /*
  91. * Notifier chain core routines. The exported routines below
  92. * are layered on top of these, with appropriate locking added.
  93. */
  94. static int notifier_chain_register(struct notifier_block **nl,
  95. struct notifier_block *n)
  96. {
  97. while ((*nl) != NULL) {
  98. if (n->priority > (*nl)->priority)
  99. break;
  100. nl = &((*nl)->next);
  101. }
  102. n->next = *nl;
  103. rcu_assign_pointer(*nl, n);
  104. return 0;
  105. }
  106. static int notifier_chain_unregister(struct notifier_block **nl,
  107. struct notifier_block *n)
  108. {
  109. while ((*nl) != NULL) {
  110. if ((*nl) == n) {
  111. rcu_assign_pointer(*nl, n->next);
  112. return 0;
  113. }
  114. nl = &((*nl)->next);
  115. }
  116. return -ENOENT;
  117. }
  118. static int __kprobes notifier_call_chain(struct notifier_block **nl,
  119. unsigned long val, void *v)
  120. {
  121. int ret = NOTIFY_DONE;
  122. struct notifier_block *nb, *next_nb;
  123. nb = rcu_dereference(*nl);
  124. while (nb) {
  125. next_nb = rcu_dereference(nb->next);
  126. ret = nb->notifier_call(nb, val, v);
  127. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  128. break;
  129. nb = next_nb;
  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. static 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. static 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. void kernel_shutdown_prepare(enum system_states state)
  565. {
  566. blocking_notifier_call_chain(&reboot_notifier_list,
  567. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  568. system_state = state;
  569. device_shutdown();
  570. }
  571. /**
  572. * kernel_halt - halt the system
  573. *
  574. * Shutdown everything and perform a clean system halt.
  575. */
  576. void kernel_halt(void)
  577. {
  578. kernel_shutdown_prepare(SYSTEM_HALT);
  579. printk(KERN_EMERG "System halted.\n");
  580. machine_halt();
  581. }
  582. EXPORT_SYMBOL_GPL(kernel_halt);
  583. /**
  584. * kernel_power_off - power_off the system
  585. *
  586. * Shutdown everything and perform a clean system power_off.
  587. */
  588. void kernel_power_off(void)
  589. {
  590. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  591. printk(KERN_EMERG "Power down.\n");
  592. machine_power_off();
  593. }
  594. EXPORT_SYMBOL_GPL(kernel_power_off);
  595. /*
  596. * Reboot system call: for obvious reasons only root may call it,
  597. * and even root needs to set up some magic numbers in the registers
  598. * so that some mistake won't make this reboot the whole machine.
  599. * You can also set the meaning of the ctrl-alt-del-key here.
  600. *
  601. * reboot doesn't sync: do that yourself before calling this.
  602. */
  603. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  604. {
  605. char buffer[256];
  606. /* We only trust the superuser with rebooting the system. */
  607. if (!capable(CAP_SYS_BOOT))
  608. return -EPERM;
  609. /* For safety, we require "magic" arguments. */
  610. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  611. (magic2 != LINUX_REBOOT_MAGIC2 &&
  612. magic2 != LINUX_REBOOT_MAGIC2A &&
  613. magic2 != LINUX_REBOOT_MAGIC2B &&
  614. magic2 != LINUX_REBOOT_MAGIC2C))
  615. return -EINVAL;
  616. /* Instead of trying to make the power_off code look like
  617. * halt when pm_power_off is not set do it the easy way.
  618. */
  619. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  620. cmd = LINUX_REBOOT_CMD_HALT;
  621. lock_kernel();
  622. switch (cmd) {
  623. case LINUX_REBOOT_CMD_RESTART:
  624. kernel_restart(NULL);
  625. break;
  626. case LINUX_REBOOT_CMD_CAD_ON:
  627. C_A_D = 1;
  628. break;
  629. case LINUX_REBOOT_CMD_CAD_OFF:
  630. C_A_D = 0;
  631. break;
  632. case LINUX_REBOOT_CMD_HALT:
  633. kernel_halt();
  634. unlock_kernel();
  635. do_exit(0);
  636. break;
  637. case LINUX_REBOOT_CMD_POWER_OFF:
  638. kernel_power_off();
  639. unlock_kernel();
  640. do_exit(0);
  641. break;
  642. case LINUX_REBOOT_CMD_RESTART2:
  643. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  644. unlock_kernel();
  645. return -EFAULT;
  646. }
  647. buffer[sizeof(buffer) - 1] = '\0';
  648. kernel_restart(buffer);
  649. break;
  650. case LINUX_REBOOT_CMD_KEXEC:
  651. kernel_kexec();
  652. unlock_kernel();
  653. return -EINVAL;
  654. #ifdef CONFIG_SOFTWARE_SUSPEND
  655. case LINUX_REBOOT_CMD_SW_SUSPEND:
  656. {
  657. int ret = software_suspend();
  658. unlock_kernel();
  659. return ret;
  660. }
  661. #endif
  662. default:
  663. unlock_kernel();
  664. return -EINVAL;
  665. }
  666. unlock_kernel();
  667. return 0;
  668. }
  669. static void deferred_cad(void *dummy)
  670. {
  671. kernel_restart(NULL);
  672. }
  673. /*
  674. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  675. * As it's called within an interrupt, it may NOT sync: the only choice
  676. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  677. */
  678. void ctrl_alt_del(void)
  679. {
  680. static DECLARE_WORK(cad_work, deferred_cad, NULL);
  681. if (C_A_D)
  682. schedule_work(&cad_work);
  683. else
  684. kill_proc(cad_pid, SIGINT, 1);
  685. }
  686. /*
  687. * Unprivileged users may change the real gid to the effective gid
  688. * or vice versa. (BSD-style)
  689. *
  690. * If you set the real gid at all, or set the effective gid to a value not
  691. * equal to the real gid, then the saved gid is set to the new effective gid.
  692. *
  693. * This makes it possible for a setgid program to completely drop its
  694. * privileges, which is often a useful assertion to make when you are doing
  695. * a security audit over a program.
  696. *
  697. * The general idea is that a program which uses just setregid() will be
  698. * 100% compatible with BSD. A program which uses just setgid() will be
  699. * 100% compatible with POSIX with saved IDs.
  700. *
  701. * SMP: There are not races, the GIDs are checked only by filesystem
  702. * operations (as far as semantic preservation is concerned).
  703. */
  704. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  705. {
  706. int old_rgid = current->gid;
  707. int old_egid = current->egid;
  708. int new_rgid = old_rgid;
  709. int new_egid = old_egid;
  710. int retval;
  711. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  712. if (retval)
  713. return retval;
  714. if (rgid != (gid_t) -1) {
  715. if ((old_rgid == rgid) ||
  716. (current->egid==rgid) ||
  717. capable(CAP_SETGID))
  718. new_rgid = rgid;
  719. else
  720. return -EPERM;
  721. }
  722. if (egid != (gid_t) -1) {
  723. if ((old_rgid == egid) ||
  724. (current->egid == egid) ||
  725. (current->sgid == egid) ||
  726. capable(CAP_SETGID))
  727. new_egid = egid;
  728. else {
  729. return -EPERM;
  730. }
  731. }
  732. if (new_egid != old_egid)
  733. {
  734. current->mm->dumpable = suid_dumpable;
  735. smp_wmb();
  736. }
  737. if (rgid != (gid_t) -1 ||
  738. (egid != (gid_t) -1 && egid != old_rgid))
  739. current->sgid = new_egid;
  740. current->fsgid = new_egid;
  741. current->egid = new_egid;
  742. current->gid = new_rgid;
  743. key_fsgid_changed(current);
  744. proc_id_connector(current, PROC_EVENT_GID);
  745. return 0;
  746. }
  747. /*
  748. * setgid() is implemented like SysV w/ SAVED_IDS
  749. *
  750. * SMP: Same implicit races as above.
  751. */
  752. asmlinkage long sys_setgid(gid_t gid)
  753. {
  754. int old_egid = current->egid;
  755. int retval;
  756. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  757. if (retval)
  758. return retval;
  759. if (capable(CAP_SETGID))
  760. {
  761. if(old_egid != gid)
  762. {
  763. current->mm->dumpable = suid_dumpable;
  764. smp_wmb();
  765. }
  766. current->gid = current->egid = current->sgid = current->fsgid = gid;
  767. }
  768. else if ((gid == current->gid) || (gid == current->sgid))
  769. {
  770. if(old_egid != gid)
  771. {
  772. current->mm->dumpable = suid_dumpable;
  773. smp_wmb();
  774. }
  775. current->egid = current->fsgid = gid;
  776. }
  777. else
  778. return -EPERM;
  779. key_fsgid_changed(current);
  780. proc_id_connector(current, PROC_EVENT_GID);
  781. return 0;
  782. }
  783. static int set_user(uid_t new_ruid, int dumpclear)
  784. {
  785. struct user_struct *new_user;
  786. new_user = alloc_uid(new_ruid);
  787. if (!new_user)
  788. return -EAGAIN;
  789. if (atomic_read(&new_user->processes) >=
  790. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  791. new_user != &root_user) {
  792. free_uid(new_user);
  793. return -EAGAIN;
  794. }
  795. switch_uid(new_user);
  796. if(dumpclear)
  797. {
  798. current->mm->dumpable = suid_dumpable;
  799. smp_wmb();
  800. }
  801. current->uid = new_ruid;
  802. return 0;
  803. }
  804. /*
  805. * Unprivileged users may change the real uid to the effective uid
  806. * or vice versa. (BSD-style)
  807. *
  808. * If you set the real uid at all, or set the effective uid to a value not
  809. * equal to the real uid, then the saved uid is set to the new effective uid.
  810. *
  811. * This makes it possible for a setuid program to completely drop its
  812. * privileges, which is often a useful assertion to make when you are doing
  813. * a security audit over a program.
  814. *
  815. * The general idea is that a program which uses just setreuid() will be
  816. * 100% compatible with BSD. A program which uses just setuid() will be
  817. * 100% compatible with POSIX with saved IDs.
  818. */
  819. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  820. {
  821. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  822. int retval;
  823. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  824. if (retval)
  825. return retval;
  826. new_ruid = old_ruid = current->uid;
  827. new_euid = old_euid = current->euid;
  828. old_suid = current->suid;
  829. if (ruid != (uid_t) -1) {
  830. new_ruid = ruid;
  831. if ((old_ruid != ruid) &&
  832. (current->euid != ruid) &&
  833. !capable(CAP_SETUID))
  834. return -EPERM;
  835. }
  836. if (euid != (uid_t) -1) {
  837. new_euid = euid;
  838. if ((old_ruid != euid) &&
  839. (current->euid != euid) &&
  840. (current->suid != euid) &&
  841. !capable(CAP_SETUID))
  842. return -EPERM;
  843. }
  844. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  845. return -EAGAIN;
  846. if (new_euid != old_euid)
  847. {
  848. current->mm->dumpable = suid_dumpable;
  849. smp_wmb();
  850. }
  851. current->fsuid = current->euid = new_euid;
  852. if (ruid != (uid_t) -1 ||
  853. (euid != (uid_t) -1 && euid != old_ruid))
  854. current->suid = current->euid;
  855. current->fsuid = current->euid;
  856. key_fsuid_changed(current);
  857. proc_id_connector(current, PROC_EVENT_UID);
  858. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  859. }
  860. /*
  861. * setuid() is implemented like SysV with SAVED_IDS
  862. *
  863. * Note that SAVED_ID's is deficient in that a setuid root program
  864. * like sendmail, for example, cannot set its uid to be a normal
  865. * user and then switch back, because if you're root, setuid() sets
  866. * the saved uid too. If you don't like this, blame the bright people
  867. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  868. * will allow a root program to temporarily drop privileges and be able to
  869. * regain them by swapping the real and effective uid.
  870. */
  871. asmlinkage long sys_setuid(uid_t uid)
  872. {
  873. int old_euid = current->euid;
  874. int old_ruid, old_suid, new_ruid, new_suid;
  875. int retval;
  876. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  877. if (retval)
  878. return retval;
  879. old_ruid = new_ruid = current->uid;
  880. old_suid = current->suid;
  881. new_suid = old_suid;
  882. if (capable(CAP_SETUID)) {
  883. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  884. return -EAGAIN;
  885. new_suid = uid;
  886. } else if ((uid != current->uid) && (uid != new_suid))
  887. return -EPERM;
  888. if (old_euid != uid)
  889. {
  890. current->mm->dumpable = suid_dumpable;
  891. smp_wmb();
  892. }
  893. current->fsuid = current->euid = uid;
  894. current->suid = new_suid;
  895. key_fsuid_changed(current);
  896. proc_id_connector(current, PROC_EVENT_UID);
  897. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  898. }
  899. /*
  900. * This function implements a generic ability to update ruid, euid,
  901. * and suid. This allows you to implement the 4.4 compatible seteuid().
  902. */
  903. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  904. {
  905. int old_ruid = current->uid;
  906. int old_euid = current->euid;
  907. int old_suid = current->suid;
  908. int retval;
  909. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  910. if (retval)
  911. return retval;
  912. if (!capable(CAP_SETUID)) {
  913. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  914. (ruid != current->euid) && (ruid != current->suid))
  915. return -EPERM;
  916. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  917. (euid != current->euid) && (euid != current->suid))
  918. return -EPERM;
  919. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  920. (suid != current->euid) && (suid != current->suid))
  921. return -EPERM;
  922. }
  923. if (ruid != (uid_t) -1) {
  924. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  925. return -EAGAIN;
  926. }
  927. if (euid != (uid_t) -1) {
  928. if (euid != current->euid)
  929. {
  930. current->mm->dumpable = suid_dumpable;
  931. smp_wmb();
  932. }
  933. current->euid = euid;
  934. }
  935. current->fsuid = current->euid;
  936. if (suid != (uid_t) -1)
  937. current->suid = suid;
  938. key_fsuid_changed(current);
  939. proc_id_connector(current, PROC_EVENT_UID);
  940. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  941. }
  942. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  943. {
  944. int retval;
  945. if (!(retval = put_user(current->uid, ruid)) &&
  946. !(retval = put_user(current->euid, euid)))
  947. retval = put_user(current->suid, suid);
  948. return retval;
  949. }
  950. /*
  951. * Same as above, but for rgid, egid, sgid.
  952. */
  953. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  954. {
  955. int retval;
  956. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  957. if (retval)
  958. return retval;
  959. if (!capable(CAP_SETGID)) {
  960. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  961. (rgid != current->egid) && (rgid != current->sgid))
  962. return -EPERM;
  963. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  964. (egid != current->egid) && (egid != current->sgid))
  965. return -EPERM;
  966. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  967. (sgid != current->egid) && (sgid != current->sgid))
  968. return -EPERM;
  969. }
  970. if (egid != (gid_t) -1) {
  971. if (egid != current->egid)
  972. {
  973. current->mm->dumpable = suid_dumpable;
  974. smp_wmb();
  975. }
  976. current->egid = egid;
  977. }
  978. current->fsgid = current->egid;
  979. if (rgid != (gid_t) -1)
  980. current->gid = rgid;
  981. if (sgid != (gid_t) -1)
  982. current->sgid = sgid;
  983. key_fsgid_changed(current);
  984. proc_id_connector(current, PROC_EVENT_GID);
  985. return 0;
  986. }
  987. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  988. {
  989. int retval;
  990. if (!(retval = put_user(current->gid, rgid)) &&
  991. !(retval = put_user(current->egid, egid)))
  992. retval = put_user(current->sgid, sgid);
  993. return retval;
  994. }
  995. /*
  996. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  997. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  998. * whatever uid it wants to). It normally shadows "euid", except when
  999. * explicitly set by setfsuid() or for access..
  1000. */
  1001. asmlinkage long sys_setfsuid(uid_t uid)
  1002. {
  1003. int old_fsuid;
  1004. old_fsuid = current->fsuid;
  1005. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  1006. return old_fsuid;
  1007. if (uid == current->uid || uid == current->euid ||
  1008. uid == current->suid || uid == current->fsuid ||
  1009. capable(CAP_SETUID))
  1010. {
  1011. if (uid != old_fsuid)
  1012. {
  1013. current->mm->dumpable = suid_dumpable;
  1014. smp_wmb();
  1015. }
  1016. current->fsuid = uid;
  1017. }
  1018. key_fsuid_changed(current);
  1019. proc_id_connector(current, PROC_EVENT_UID);
  1020. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1021. return old_fsuid;
  1022. }
  1023. /*
  1024. * Samma på svenska..
  1025. */
  1026. asmlinkage long sys_setfsgid(gid_t gid)
  1027. {
  1028. int old_fsgid;
  1029. old_fsgid = current->fsgid;
  1030. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1031. return old_fsgid;
  1032. if (gid == current->gid || gid == current->egid ||
  1033. gid == current->sgid || gid == current->fsgid ||
  1034. capable(CAP_SETGID))
  1035. {
  1036. if (gid != old_fsgid)
  1037. {
  1038. current->mm->dumpable = suid_dumpable;
  1039. smp_wmb();
  1040. }
  1041. current->fsgid = gid;
  1042. key_fsgid_changed(current);
  1043. proc_id_connector(current, PROC_EVENT_GID);
  1044. }
  1045. return old_fsgid;
  1046. }
  1047. asmlinkage long sys_times(struct tms __user * tbuf)
  1048. {
  1049. /*
  1050. * In the SMP world we might just be unlucky and have one of
  1051. * the times increment as we use it. Since the value is an
  1052. * atomically safe type this is just fine. Conceptually its
  1053. * as if the syscall took an instant longer to occur.
  1054. */
  1055. if (tbuf) {
  1056. struct tms tmp;
  1057. struct task_struct *tsk = current;
  1058. struct task_struct *t;
  1059. cputime_t utime, stime, cutime, cstime;
  1060. spin_lock_irq(&tsk->sighand->siglock);
  1061. utime = tsk->signal->utime;
  1062. stime = tsk->signal->stime;
  1063. t = tsk;
  1064. do {
  1065. utime = cputime_add(utime, t->utime);
  1066. stime = cputime_add(stime, t->stime);
  1067. t = next_thread(t);
  1068. } while (t != tsk);
  1069. cutime = tsk->signal->cutime;
  1070. cstime = tsk->signal->cstime;
  1071. spin_unlock_irq(&tsk->sighand->siglock);
  1072. tmp.tms_utime = cputime_to_clock_t(utime);
  1073. tmp.tms_stime = cputime_to_clock_t(stime);
  1074. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1075. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1076. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1077. return -EFAULT;
  1078. }
  1079. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1080. }
  1081. /*
  1082. * This needs some heavy checking ...
  1083. * I just haven't the stomach for it. I also don't fully
  1084. * understand sessions/pgrp etc. Let somebody who does explain it.
  1085. *
  1086. * OK, I think I have the protection semantics right.... this is really
  1087. * only important on a multi-user system anyway, to make sure one user
  1088. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1089. *
  1090. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1091. * LBT 04.03.94
  1092. */
  1093. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1094. {
  1095. struct task_struct *p;
  1096. struct task_struct *group_leader = current->group_leader;
  1097. int err = -EINVAL;
  1098. if (!pid)
  1099. pid = group_leader->pid;
  1100. if (!pgid)
  1101. pgid = pid;
  1102. if (pgid < 0)
  1103. return -EINVAL;
  1104. /* From this point forward we keep holding onto the tasklist lock
  1105. * so that our parent does not change from under us. -DaveM
  1106. */
  1107. write_lock_irq(&tasklist_lock);
  1108. err = -ESRCH;
  1109. p = find_task_by_pid(pid);
  1110. if (!p)
  1111. goto out;
  1112. err = -EINVAL;
  1113. if (!thread_group_leader(p))
  1114. goto out;
  1115. if (p->real_parent == group_leader) {
  1116. err = -EPERM;
  1117. if (p->signal->session != group_leader->signal->session)
  1118. goto out;
  1119. err = -EACCES;
  1120. if (p->did_exec)
  1121. goto out;
  1122. } else {
  1123. err = -ESRCH;
  1124. if (p != group_leader)
  1125. goto out;
  1126. }
  1127. err = -EPERM;
  1128. if (p->signal->leader)
  1129. goto out;
  1130. if (pgid != pid) {
  1131. struct task_struct *p;
  1132. do_each_task_pid(pgid, PIDTYPE_PGID, p) {
  1133. if (p->signal->session == group_leader->signal->session)
  1134. goto ok_pgid;
  1135. } while_each_task_pid(pgid, PIDTYPE_PGID, p);
  1136. goto out;
  1137. }
  1138. ok_pgid:
  1139. err = security_task_setpgid(p, pgid);
  1140. if (err)
  1141. goto out;
  1142. if (process_group(p) != pgid) {
  1143. detach_pid(p, PIDTYPE_PGID);
  1144. p->signal->pgrp = pgid;
  1145. attach_pid(p, PIDTYPE_PGID, pgid);
  1146. }
  1147. err = 0;
  1148. out:
  1149. /* All paths lead to here, thus we are safe. -DaveM */
  1150. write_unlock_irq(&tasklist_lock);
  1151. return err;
  1152. }
  1153. asmlinkage long sys_getpgid(pid_t pid)
  1154. {
  1155. if (!pid) {
  1156. return process_group(current);
  1157. } else {
  1158. int retval;
  1159. struct task_struct *p;
  1160. read_lock(&tasklist_lock);
  1161. p = find_task_by_pid(pid);
  1162. retval = -ESRCH;
  1163. if (p) {
  1164. retval = security_task_getpgid(p);
  1165. if (!retval)
  1166. retval = process_group(p);
  1167. }
  1168. read_unlock(&tasklist_lock);
  1169. return retval;
  1170. }
  1171. }
  1172. #ifdef __ARCH_WANT_SYS_GETPGRP
  1173. asmlinkage long sys_getpgrp(void)
  1174. {
  1175. /* SMP - assuming writes are word atomic this is fine */
  1176. return process_group(current);
  1177. }
  1178. #endif
  1179. asmlinkage long sys_getsid(pid_t pid)
  1180. {
  1181. if (!pid) {
  1182. return current->signal->session;
  1183. } else {
  1184. int retval;
  1185. struct task_struct *p;
  1186. read_lock(&tasklist_lock);
  1187. p = find_task_by_pid(pid);
  1188. retval = -ESRCH;
  1189. if(p) {
  1190. retval = security_task_getsid(p);
  1191. if (!retval)
  1192. retval = p->signal->session;
  1193. }
  1194. read_unlock(&tasklist_lock);
  1195. return retval;
  1196. }
  1197. }
  1198. asmlinkage long sys_setsid(void)
  1199. {
  1200. struct task_struct *group_leader = current->group_leader;
  1201. pid_t session;
  1202. int err = -EPERM;
  1203. mutex_lock(&tty_mutex);
  1204. write_lock_irq(&tasklist_lock);
  1205. /* Fail if I am already a session leader */
  1206. if (group_leader->signal->leader)
  1207. goto out;
  1208. session = group_leader->pid;
  1209. /* Fail if a process group id already exists that equals the
  1210. * proposed session id.
  1211. *
  1212. * Don't check if session id == 1 because kernel threads use this
  1213. * session id and so the check will always fail and make it so
  1214. * init cannot successfully call setsid.
  1215. */
  1216. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1217. goto out;
  1218. group_leader->signal->leader = 1;
  1219. __set_special_pids(session, session);
  1220. group_leader->signal->tty = NULL;
  1221. group_leader->signal->tty_old_pgrp = 0;
  1222. err = process_group(group_leader);
  1223. out:
  1224. write_unlock_irq(&tasklist_lock);
  1225. mutex_unlock(&tty_mutex);
  1226. return err;
  1227. }
  1228. /*
  1229. * Supplementary group IDs
  1230. */
  1231. /* init to 2 - one for init_task, one to ensure it is never freed */
  1232. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1233. struct group_info *groups_alloc(int gidsetsize)
  1234. {
  1235. struct group_info *group_info;
  1236. int nblocks;
  1237. int i;
  1238. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1239. /* Make sure we always allocate at least one indirect block pointer */
  1240. nblocks = nblocks ? : 1;
  1241. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1242. if (!group_info)
  1243. return NULL;
  1244. group_info->ngroups = gidsetsize;
  1245. group_info->nblocks = nblocks;
  1246. atomic_set(&group_info->usage, 1);
  1247. if (gidsetsize <= NGROUPS_SMALL) {
  1248. group_info->blocks[0] = group_info->small_block;
  1249. } else {
  1250. for (i = 0; i < nblocks; i++) {
  1251. gid_t *b;
  1252. b = (void *)__get_free_page(GFP_USER);
  1253. if (!b)
  1254. goto out_undo_partial_alloc;
  1255. group_info->blocks[i] = b;
  1256. }
  1257. }
  1258. return group_info;
  1259. out_undo_partial_alloc:
  1260. while (--i >= 0) {
  1261. free_page((unsigned long)group_info->blocks[i]);
  1262. }
  1263. kfree(group_info);
  1264. return NULL;
  1265. }
  1266. EXPORT_SYMBOL(groups_alloc);
  1267. void groups_free(struct group_info *group_info)
  1268. {
  1269. if (group_info->blocks[0] != group_info->small_block) {
  1270. int i;
  1271. for (i = 0; i < group_info->nblocks; i++)
  1272. free_page((unsigned long)group_info->blocks[i]);
  1273. }
  1274. kfree(group_info);
  1275. }
  1276. EXPORT_SYMBOL(groups_free);
  1277. /* export the group_info to a user-space array */
  1278. static int groups_to_user(gid_t __user *grouplist,
  1279. struct group_info *group_info)
  1280. {
  1281. int i;
  1282. int count = group_info->ngroups;
  1283. for (i = 0; i < group_info->nblocks; i++) {
  1284. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1285. int off = i * NGROUPS_PER_BLOCK;
  1286. int len = cp_count * sizeof(*grouplist);
  1287. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1288. return -EFAULT;
  1289. count -= cp_count;
  1290. }
  1291. return 0;
  1292. }
  1293. /* fill a group_info from a user-space array - it must be allocated already */
  1294. static int groups_from_user(struct group_info *group_info,
  1295. gid_t __user *grouplist)
  1296. {
  1297. int i;
  1298. int count = group_info->ngroups;
  1299. for (i = 0; i < group_info->nblocks; i++) {
  1300. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1301. int off = i * NGROUPS_PER_BLOCK;
  1302. int len = cp_count * sizeof(*grouplist);
  1303. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1304. return -EFAULT;
  1305. count -= cp_count;
  1306. }
  1307. return 0;
  1308. }
  1309. /* a simple Shell sort */
  1310. static void groups_sort(struct group_info *group_info)
  1311. {
  1312. int base, max, stride;
  1313. int gidsetsize = group_info->ngroups;
  1314. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1315. ; /* nothing */
  1316. stride /= 3;
  1317. while (stride) {
  1318. max = gidsetsize - stride;
  1319. for (base = 0; base < max; base++) {
  1320. int left = base;
  1321. int right = left + stride;
  1322. gid_t tmp = GROUP_AT(group_info, right);
  1323. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1324. GROUP_AT(group_info, right) =
  1325. GROUP_AT(group_info, left);
  1326. right = left;
  1327. left -= stride;
  1328. }
  1329. GROUP_AT(group_info, right) = tmp;
  1330. }
  1331. stride /= 3;
  1332. }
  1333. }
  1334. /* a simple bsearch */
  1335. int groups_search(struct group_info *group_info, gid_t grp)
  1336. {
  1337. unsigned int left, right;
  1338. if (!group_info)
  1339. return 0;
  1340. left = 0;
  1341. right = group_info->ngroups;
  1342. while (left < right) {
  1343. unsigned int mid = (left+right)/2;
  1344. int cmp = grp - GROUP_AT(group_info, mid);
  1345. if (cmp > 0)
  1346. left = mid + 1;
  1347. else if (cmp < 0)
  1348. right = mid;
  1349. else
  1350. return 1;
  1351. }
  1352. return 0;
  1353. }
  1354. /* validate and set current->group_info */
  1355. int set_current_groups(struct group_info *group_info)
  1356. {
  1357. int retval;
  1358. struct group_info *old_info;
  1359. retval = security_task_setgroups(group_info);
  1360. if (retval)
  1361. return retval;
  1362. groups_sort(group_info);
  1363. get_group_info(group_info);
  1364. task_lock(current);
  1365. old_info = current->group_info;
  1366. current->group_info = group_info;
  1367. task_unlock(current);
  1368. put_group_info(old_info);
  1369. return 0;
  1370. }
  1371. EXPORT_SYMBOL(set_current_groups);
  1372. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1373. {
  1374. int i = 0;
  1375. /*
  1376. * SMP: Nobody else can change our grouplist. Thus we are
  1377. * safe.
  1378. */
  1379. if (gidsetsize < 0)
  1380. return -EINVAL;
  1381. /* no need to grab task_lock here; it cannot change */
  1382. i = current->group_info->ngroups;
  1383. if (gidsetsize) {
  1384. if (i > gidsetsize) {
  1385. i = -EINVAL;
  1386. goto out;
  1387. }
  1388. if (groups_to_user(grouplist, current->group_info)) {
  1389. i = -EFAULT;
  1390. goto out;
  1391. }
  1392. }
  1393. out:
  1394. return i;
  1395. }
  1396. /*
  1397. * SMP: Our groups are copy-on-write. We can set them safely
  1398. * without another task interfering.
  1399. */
  1400. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1401. {
  1402. struct group_info *group_info;
  1403. int retval;
  1404. if (!capable(CAP_SETGID))
  1405. return -EPERM;
  1406. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1407. return -EINVAL;
  1408. group_info = groups_alloc(gidsetsize);
  1409. if (!group_info)
  1410. return -ENOMEM;
  1411. retval = groups_from_user(group_info, grouplist);
  1412. if (retval) {
  1413. put_group_info(group_info);
  1414. return retval;
  1415. }
  1416. retval = set_current_groups(group_info);
  1417. put_group_info(group_info);
  1418. return retval;
  1419. }
  1420. /*
  1421. * Check whether we're fsgid/egid or in the supplemental group..
  1422. */
  1423. int in_group_p(gid_t grp)
  1424. {
  1425. int retval = 1;
  1426. if (grp != current->fsgid) {
  1427. retval = groups_search(current->group_info, grp);
  1428. }
  1429. return retval;
  1430. }
  1431. EXPORT_SYMBOL(in_group_p);
  1432. int in_egroup_p(gid_t grp)
  1433. {
  1434. int retval = 1;
  1435. if (grp != current->egid) {
  1436. retval = groups_search(current->group_info, grp);
  1437. }
  1438. return retval;
  1439. }
  1440. EXPORT_SYMBOL(in_egroup_p);
  1441. DECLARE_RWSEM(uts_sem);
  1442. EXPORT_SYMBOL(uts_sem);
  1443. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1444. {
  1445. int errno = 0;
  1446. down_read(&uts_sem);
  1447. if (copy_to_user(name,&system_utsname,sizeof *name))
  1448. errno = -EFAULT;
  1449. up_read(&uts_sem);
  1450. return errno;
  1451. }
  1452. asmlinkage long sys_sethostname(char __user *name, int len)
  1453. {
  1454. int errno;
  1455. char tmp[__NEW_UTS_LEN];
  1456. if (!capable(CAP_SYS_ADMIN))
  1457. return -EPERM;
  1458. if (len < 0 || len > __NEW_UTS_LEN)
  1459. return -EINVAL;
  1460. down_write(&uts_sem);
  1461. errno = -EFAULT;
  1462. if (!copy_from_user(tmp, name, len)) {
  1463. memcpy(system_utsname.nodename, tmp, len);
  1464. system_utsname.nodename[len] = 0;
  1465. errno = 0;
  1466. }
  1467. up_write(&uts_sem);
  1468. return errno;
  1469. }
  1470. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1471. asmlinkage long sys_gethostname(char __user *name, int len)
  1472. {
  1473. int i, errno;
  1474. if (len < 0)
  1475. return -EINVAL;
  1476. down_read(&uts_sem);
  1477. i = 1 + strlen(system_utsname.nodename);
  1478. if (i > len)
  1479. i = len;
  1480. errno = 0;
  1481. if (copy_to_user(name, system_utsname.nodename, i))
  1482. errno = -EFAULT;
  1483. up_read(&uts_sem);
  1484. return errno;
  1485. }
  1486. #endif
  1487. /*
  1488. * Only setdomainname; getdomainname can be implemented by calling
  1489. * uname()
  1490. */
  1491. asmlinkage long sys_setdomainname(char __user *name, int len)
  1492. {
  1493. int errno;
  1494. char tmp[__NEW_UTS_LEN];
  1495. if (!capable(CAP_SYS_ADMIN))
  1496. return -EPERM;
  1497. if (len < 0 || len > __NEW_UTS_LEN)
  1498. return -EINVAL;
  1499. down_write(&uts_sem);
  1500. errno = -EFAULT;
  1501. if (!copy_from_user(tmp, name, len)) {
  1502. memcpy(system_utsname.domainname, tmp, len);
  1503. system_utsname.domainname[len] = 0;
  1504. errno = 0;
  1505. }
  1506. up_write(&uts_sem);
  1507. return errno;
  1508. }
  1509. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1510. {
  1511. if (resource >= RLIM_NLIMITS)
  1512. return -EINVAL;
  1513. else {
  1514. struct rlimit value;
  1515. task_lock(current->group_leader);
  1516. value = current->signal->rlim[resource];
  1517. task_unlock(current->group_leader);
  1518. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1519. }
  1520. }
  1521. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1522. /*
  1523. * Back compatibility for getrlimit. Needed for some apps.
  1524. */
  1525. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1526. {
  1527. struct rlimit x;
  1528. if (resource >= RLIM_NLIMITS)
  1529. return -EINVAL;
  1530. task_lock(current->group_leader);
  1531. x = current->signal->rlim[resource];
  1532. task_unlock(current->group_leader);
  1533. if(x.rlim_cur > 0x7FFFFFFF)
  1534. x.rlim_cur = 0x7FFFFFFF;
  1535. if(x.rlim_max > 0x7FFFFFFF)
  1536. x.rlim_max = 0x7FFFFFFF;
  1537. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1538. }
  1539. #endif
  1540. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1541. {
  1542. struct rlimit new_rlim, *old_rlim;
  1543. unsigned long it_prof_secs;
  1544. int retval;
  1545. if (resource >= RLIM_NLIMITS)
  1546. return -EINVAL;
  1547. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1548. return -EFAULT;
  1549. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1550. return -EINVAL;
  1551. old_rlim = current->signal->rlim + resource;
  1552. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1553. !capable(CAP_SYS_RESOURCE))
  1554. return -EPERM;
  1555. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1556. return -EPERM;
  1557. retval = security_task_setrlimit(resource, &new_rlim);
  1558. if (retval)
  1559. return retval;
  1560. task_lock(current->group_leader);
  1561. *old_rlim = new_rlim;
  1562. task_unlock(current->group_leader);
  1563. if (resource != RLIMIT_CPU)
  1564. goto out;
  1565. /*
  1566. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1567. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1568. * very long-standing error, and fixing it now risks breakage of
  1569. * applications, so we live with it
  1570. */
  1571. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1572. goto out;
  1573. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1574. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1575. unsigned long rlim_cur = new_rlim.rlim_cur;
  1576. cputime_t cputime;
  1577. if (rlim_cur == 0) {
  1578. /*
  1579. * The caller is asking for an immediate RLIMIT_CPU
  1580. * expiry. But we use the zero value to mean "it was
  1581. * never set". So let's cheat and make it one second
  1582. * instead
  1583. */
  1584. rlim_cur = 1;
  1585. }
  1586. cputime = secs_to_cputime(rlim_cur);
  1587. read_lock(&tasklist_lock);
  1588. spin_lock_irq(&current->sighand->siglock);
  1589. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1590. spin_unlock_irq(&current->sighand->siglock);
  1591. read_unlock(&tasklist_lock);
  1592. }
  1593. out:
  1594. return 0;
  1595. }
  1596. /*
  1597. * It would make sense to put struct rusage in the task_struct,
  1598. * except that would make the task_struct be *really big*. After
  1599. * task_struct gets moved into malloc'ed memory, it would
  1600. * make sense to do this. It will make moving the rest of the information
  1601. * a lot simpler! (Which we're not doing right now because we're not
  1602. * measuring them yet).
  1603. *
  1604. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1605. * races with threads incrementing their own counters. But since word
  1606. * reads are atomic, we either get new values or old values and we don't
  1607. * care which for the sums. We always take the siglock to protect reading
  1608. * the c* fields from p->signal from races with exit.c updating those
  1609. * fields when reaping, so a sample either gets all the additions of a
  1610. * given child after it's reaped, or none so this sample is before reaping.
  1611. *
  1612. * Locking:
  1613. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1614. * for the cases current multithreaded, non-current single threaded
  1615. * non-current multithreaded. Thread traversal is now safe with
  1616. * the siglock held.
  1617. * Strictly speaking, we donot need to take the siglock if we are current and
  1618. * single threaded, as no one else can take our signal_struct away, no one
  1619. * else can reap the children to update signal->c* counters, and no one else
  1620. * can race with the signal-> fields. If we do not take any lock, the
  1621. * signal-> fields could be read out of order while another thread was just
  1622. * exiting. So we should place a read memory barrier when we avoid the lock.
  1623. * On the writer side, write memory barrier is implied in __exit_signal
  1624. * as __exit_signal releases the siglock spinlock after updating the signal->
  1625. * fields. But we don't do this yet to keep things simple.
  1626. *
  1627. */
  1628. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1629. {
  1630. struct task_struct *t;
  1631. unsigned long flags;
  1632. cputime_t utime, stime;
  1633. memset((char *) r, 0, sizeof *r);
  1634. utime = stime = cputime_zero;
  1635. rcu_read_lock();
  1636. if (!lock_task_sighand(p, &flags)) {
  1637. rcu_read_unlock();
  1638. return;
  1639. }
  1640. switch (who) {
  1641. case RUSAGE_BOTH:
  1642. case RUSAGE_CHILDREN:
  1643. utime = p->signal->cutime;
  1644. stime = p->signal->cstime;
  1645. r->ru_nvcsw = p->signal->cnvcsw;
  1646. r->ru_nivcsw = p->signal->cnivcsw;
  1647. r->ru_minflt = p->signal->cmin_flt;
  1648. r->ru_majflt = p->signal->cmaj_flt;
  1649. if (who == RUSAGE_CHILDREN)
  1650. break;
  1651. case RUSAGE_SELF:
  1652. utime = cputime_add(utime, p->signal->utime);
  1653. stime = cputime_add(stime, p->signal->stime);
  1654. r->ru_nvcsw += p->signal->nvcsw;
  1655. r->ru_nivcsw += p->signal->nivcsw;
  1656. r->ru_minflt += p->signal->min_flt;
  1657. r->ru_majflt += p->signal->maj_flt;
  1658. t = p;
  1659. do {
  1660. utime = cputime_add(utime, t->utime);
  1661. stime = cputime_add(stime, t->stime);
  1662. r->ru_nvcsw += t->nvcsw;
  1663. r->ru_nivcsw += t->nivcsw;
  1664. r->ru_minflt += t->min_flt;
  1665. r->ru_majflt += t->maj_flt;
  1666. t = next_thread(t);
  1667. } while (t != p);
  1668. break;
  1669. default:
  1670. BUG();
  1671. }
  1672. unlock_task_sighand(p, &flags);
  1673. rcu_read_unlock();
  1674. cputime_to_timeval(utime, &r->ru_utime);
  1675. cputime_to_timeval(stime, &r->ru_stime);
  1676. }
  1677. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1678. {
  1679. struct rusage r;
  1680. k_getrusage(p, who, &r);
  1681. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1682. }
  1683. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1684. {
  1685. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1686. return -EINVAL;
  1687. return getrusage(current, who, ru);
  1688. }
  1689. asmlinkage long sys_umask(int mask)
  1690. {
  1691. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1692. return mask;
  1693. }
  1694. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1695. unsigned long arg4, unsigned long arg5)
  1696. {
  1697. long error;
  1698. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1699. if (error)
  1700. return error;
  1701. switch (option) {
  1702. case PR_SET_PDEATHSIG:
  1703. if (!valid_signal(arg2)) {
  1704. error = -EINVAL;
  1705. break;
  1706. }
  1707. current->pdeath_signal = arg2;
  1708. break;
  1709. case PR_GET_PDEATHSIG:
  1710. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1711. break;
  1712. case PR_GET_DUMPABLE:
  1713. error = current->mm->dumpable;
  1714. break;
  1715. case PR_SET_DUMPABLE:
  1716. if (arg2 < 0 || arg2 > 1) {
  1717. error = -EINVAL;
  1718. break;
  1719. }
  1720. current->mm->dumpable = arg2;
  1721. break;
  1722. case PR_SET_UNALIGN:
  1723. error = SET_UNALIGN_CTL(current, arg2);
  1724. break;
  1725. case PR_GET_UNALIGN:
  1726. error = GET_UNALIGN_CTL(current, arg2);
  1727. break;
  1728. case PR_SET_FPEMU:
  1729. error = SET_FPEMU_CTL(current, arg2);
  1730. break;
  1731. case PR_GET_FPEMU:
  1732. error = GET_FPEMU_CTL(current, arg2);
  1733. break;
  1734. case PR_SET_FPEXC:
  1735. error = SET_FPEXC_CTL(current, arg2);
  1736. break;
  1737. case PR_GET_FPEXC:
  1738. error = GET_FPEXC_CTL(current, arg2);
  1739. break;
  1740. case PR_GET_TIMING:
  1741. error = PR_TIMING_STATISTICAL;
  1742. break;
  1743. case PR_SET_TIMING:
  1744. if (arg2 == PR_TIMING_STATISTICAL)
  1745. error = 0;
  1746. else
  1747. error = -EINVAL;
  1748. break;
  1749. case PR_GET_KEEPCAPS:
  1750. if (current->keep_capabilities)
  1751. error = 1;
  1752. break;
  1753. case PR_SET_KEEPCAPS:
  1754. if (arg2 != 0 && arg2 != 1) {
  1755. error = -EINVAL;
  1756. break;
  1757. }
  1758. current->keep_capabilities = arg2;
  1759. break;
  1760. case PR_SET_NAME: {
  1761. struct task_struct *me = current;
  1762. unsigned char ncomm[sizeof(me->comm)];
  1763. ncomm[sizeof(me->comm)-1] = 0;
  1764. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1765. sizeof(me->comm)-1) < 0)
  1766. return -EFAULT;
  1767. set_task_comm(me, ncomm);
  1768. return 0;
  1769. }
  1770. case PR_GET_NAME: {
  1771. struct task_struct *me = current;
  1772. unsigned char tcomm[sizeof(me->comm)];
  1773. get_task_comm(tcomm, me);
  1774. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1775. return -EFAULT;
  1776. return 0;
  1777. }
  1778. case PR_GET_ENDIAN:
  1779. error = GET_ENDIAN(current, arg2);
  1780. break;
  1781. case PR_SET_ENDIAN:
  1782. error = SET_ENDIAN(current, arg2);
  1783. break;
  1784. default:
  1785. error = -EINVAL;
  1786. break;
  1787. }
  1788. return error;
  1789. }
  1790. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1791. struct getcpu_cache __user *cache)
  1792. {
  1793. int err = 0;
  1794. int cpu = raw_smp_processor_id();
  1795. if (cpup)
  1796. err |= put_user(cpu, cpup);
  1797. if (nodep)
  1798. err |= put_user(cpu_to_node(cpu), nodep);
  1799. if (cache) {
  1800. /*
  1801. * The cache is not needed for this implementation,
  1802. * but make sure user programs pass something
  1803. * valid. vsyscall implementations can instead make
  1804. * good use of the cache. Only use t0 and t1 because
  1805. * these are available in both 32bit and 64bit ABI (no
  1806. * need for a compat_getcpu). 32bit has enough
  1807. * padding
  1808. */
  1809. unsigned long t0, t1;
  1810. get_user(t0, &cache->t0);
  1811. get_user(t1, &cache->t1);
  1812. t0++;
  1813. t1++;
  1814. put_user(t0, &cache->t0);
  1815. put_user(t1, &cache->t1);
  1816. }
  1817. return err ? -EFAULT : 0;
  1818. }