sys.c 53 KB

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