sys.c 53 KB

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