sys.c 49 KB

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