sys.c 50 KB

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