sys.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/export.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/reboot.h>
  11. #include <linux/prctl.h>
  12. #include <linux/highuid.h>
  13. #include <linux/fs.h>
  14. #include <linux/kmod.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/resource.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kexec.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/capability.h>
  21. #include <linux/device.h>
  22. #include <linux/key.h>
  23. #include <linux/times.h>
  24. #include <linux/posix-timers.h>
  25. #include <linux/security.h>
  26. #include <linux/dcookies.h>
  27. #include <linux/suspend.h>
  28. #include <linux/tty.h>
  29. #include <linux/signal.h>
  30. #include <linux/cn_proc.h>
  31. #include <linux/getcpu.h>
  32. #include <linux/task_io_accounting_ops.h>
  33. #include <linux/seccomp.h>
  34. #include <linux/cpu.h>
  35. #include <linux/personality.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/fs_struct.h>
  38. #include <linux/gfp.h>
  39. #include <linux/syscore_ops.h>
  40. #include <linux/version.h>
  41. #include <linux/ctype.h>
  42. #include <linux/compat.h>
  43. #include <linux/syscalls.h>
  44. #include <linux/kprobes.h>
  45. #include <linux/user_namespace.h>
  46. #include <linux/kmsg_dump.h>
  47. /* Move somewhere else to avoid recompiling? */
  48. #include <generated/utsrelease.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. #include <asm/unistd.h>
  52. #ifndef SET_UNALIGN_CTL
  53. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  54. #endif
  55. #ifndef GET_UNALIGN_CTL
  56. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  57. #endif
  58. #ifndef SET_FPEMU_CTL
  59. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  60. #endif
  61. #ifndef GET_FPEMU_CTL
  62. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  63. #endif
  64. #ifndef SET_FPEXC_CTL
  65. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  66. #endif
  67. #ifndef GET_FPEXC_CTL
  68. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  69. #endif
  70. #ifndef GET_ENDIAN
  71. # define GET_ENDIAN(a,b) (-EINVAL)
  72. #endif
  73. #ifndef SET_ENDIAN
  74. # define SET_ENDIAN(a,b) (-EINVAL)
  75. #endif
  76. #ifndef GET_TSC_CTL
  77. # define GET_TSC_CTL(a) (-EINVAL)
  78. #endif
  79. #ifndef SET_TSC_CTL
  80. # define SET_TSC_CTL(a) (-EINVAL)
  81. #endif
  82. /*
  83. * this is where the system-wide overflow UID and GID are defined, for
  84. * architectures that now have 32-bit UID/GID but didn't in the past
  85. */
  86. int overflowuid = DEFAULT_OVERFLOWUID;
  87. int overflowgid = DEFAULT_OVERFLOWGID;
  88. #ifdef CONFIG_UID16
  89. EXPORT_SYMBOL(overflowuid);
  90. EXPORT_SYMBOL(overflowgid);
  91. #endif
  92. /*
  93. * the same as above, but for filesystems which can only store a 16-bit
  94. * UID and GID. as such, this is needed on all architectures
  95. */
  96. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  97. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  98. EXPORT_SYMBOL(fs_overflowuid);
  99. EXPORT_SYMBOL(fs_overflowgid);
  100. /*
  101. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  102. */
  103. int C_A_D = 1;
  104. struct pid *cad_pid;
  105. EXPORT_SYMBOL(cad_pid);
  106. /*
  107. * If set, this is used for preparing the system to power off.
  108. */
  109. void (*pm_power_off_prepare)(void);
  110. /*
  111. * Returns true if current's euid is same as p's uid or euid,
  112. * or has CAP_SYS_NICE to p's user_ns.
  113. *
  114. * Called with rcu_read_lock, creds are safe
  115. */
  116. static bool set_one_prio_perm(struct task_struct *p)
  117. {
  118. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  119. if (pcred->user->user_ns == cred->user->user_ns &&
  120. (pcred->uid == cred->euid ||
  121. pcred->euid == cred->euid))
  122. return true;
  123. if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE))
  124. return true;
  125. return false;
  126. }
  127. /*
  128. * set the priority of a task
  129. * - the caller must hold the RCU read lock
  130. */
  131. static int set_one_prio(struct task_struct *p, int niceval, int error)
  132. {
  133. int no_nice;
  134. if (!set_one_prio_perm(p)) {
  135. error = -EPERM;
  136. goto out;
  137. }
  138. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  139. error = -EACCES;
  140. goto out;
  141. }
  142. no_nice = security_task_setnice(p, niceval);
  143. if (no_nice) {
  144. error = no_nice;
  145. goto out;
  146. }
  147. if (error == -ESRCH)
  148. error = 0;
  149. set_user_nice(p, niceval);
  150. out:
  151. return error;
  152. }
  153. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  154. {
  155. struct task_struct *g, *p;
  156. struct user_struct *user;
  157. const struct cred *cred = current_cred();
  158. int error = -EINVAL;
  159. struct pid *pgrp;
  160. if (which > PRIO_USER || which < PRIO_PROCESS)
  161. goto out;
  162. /* normalize: avoid signed division (rounding problems) */
  163. error = -ESRCH;
  164. if (niceval < -20)
  165. niceval = -20;
  166. if (niceval > 19)
  167. niceval = 19;
  168. rcu_read_lock();
  169. read_lock(&tasklist_lock);
  170. switch (which) {
  171. case PRIO_PROCESS:
  172. if (who)
  173. p = find_task_by_vpid(who);
  174. else
  175. p = current;
  176. if (p)
  177. error = set_one_prio(p, niceval, error);
  178. break;
  179. case PRIO_PGRP:
  180. if (who)
  181. pgrp = find_vpid(who);
  182. else
  183. pgrp = task_pgrp(current);
  184. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  185. error = set_one_prio(p, niceval, error);
  186. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  187. break;
  188. case PRIO_USER:
  189. user = (struct user_struct *) cred->user;
  190. if (!who)
  191. who = cred->uid;
  192. else if ((who != cred->uid) &&
  193. !(user = find_user(who)))
  194. goto out_unlock; /* No processes for this user */
  195. do_each_thread(g, p) {
  196. if (__task_cred(p)->uid == who)
  197. error = set_one_prio(p, niceval, error);
  198. } while_each_thread(g, p);
  199. if (who != cred->uid)
  200. free_uid(user); /* For find_user() */
  201. break;
  202. }
  203. out_unlock:
  204. read_unlock(&tasklist_lock);
  205. rcu_read_unlock();
  206. out:
  207. return error;
  208. }
  209. /*
  210. * Ugh. To avoid negative return values, "getpriority()" will
  211. * not return the normal nice-value, but a negated value that
  212. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  213. * to stay compatible.
  214. */
  215. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  216. {
  217. struct task_struct *g, *p;
  218. struct user_struct *user;
  219. const struct cred *cred = current_cred();
  220. long niceval, retval = -ESRCH;
  221. struct pid *pgrp;
  222. if (which > PRIO_USER || which < PRIO_PROCESS)
  223. return -EINVAL;
  224. rcu_read_lock();
  225. read_lock(&tasklist_lock);
  226. switch (which) {
  227. case PRIO_PROCESS:
  228. if (who)
  229. p = find_task_by_vpid(who);
  230. else
  231. p = current;
  232. if (p) {
  233. niceval = 20 - task_nice(p);
  234. if (niceval > retval)
  235. retval = niceval;
  236. }
  237. break;
  238. case PRIO_PGRP:
  239. if (who)
  240. pgrp = find_vpid(who);
  241. else
  242. pgrp = task_pgrp(current);
  243. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  244. niceval = 20 - task_nice(p);
  245. if (niceval > retval)
  246. retval = niceval;
  247. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  248. break;
  249. case PRIO_USER:
  250. user = (struct user_struct *) cred->user;
  251. if (!who)
  252. who = cred->uid;
  253. else if ((who != cred->uid) &&
  254. !(user = find_user(who)))
  255. goto out_unlock; /* No processes for this user */
  256. do_each_thread(g, p) {
  257. if (__task_cred(p)->uid == who) {
  258. niceval = 20 - task_nice(p);
  259. if (niceval > retval)
  260. retval = niceval;
  261. }
  262. } while_each_thread(g, p);
  263. if (who != cred->uid)
  264. free_uid(user); /* for find_user() */
  265. break;
  266. }
  267. out_unlock:
  268. read_unlock(&tasklist_lock);
  269. rcu_read_unlock();
  270. return retval;
  271. }
  272. /**
  273. * emergency_restart - reboot the system
  274. *
  275. * Without shutting down any hardware or taking any locks
  276. * reboot the system. This is called when we know we are in
  277. * trouble so this is our best effort to reboot. This is
  278. * safe to call in interrupt context.
  279. */
  280. void emergency_restart(void)
  281. {
  282. kmsg_dump(KMSG_DUMP_EMERG);
  283. machine_emergency_restart();
  284. }
  285. EXPORT_SYMBOL_GPL(emergency_restart);
  286. void kernel_restart_prepare(char *cmd)
  287. {
  288. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  289. system_state = SYSTEM_RESTART;
  290. usermodehelper_disable();
  291. device_shutdown();
  292. syscore_shutdown();
  293. }
  294. /**
  295. * register_reboot_notifier - Register function to be called at reboot time
  296. * @nb: Info about notifier function to be called
  297. *
  298. * Registers a function with the list of functions
  299. * to be called at reboot time.
  300. *
  301. * Currently always returns zero, as blocking_notifier_chain_register()
  302. * always returns zero.
  303. */
  304. int register_reboot_notifier(struct notifier_block *nb)
  305. {
  306. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  307. }
  308. EXPORT_SYMBOL(register_reboot_notifier);
  309. /**
  310. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  311. * @nb: Hook to be unregistered
  312. *
  313. * Unregisters a previously registered reboot
  314. * notifier function.
  315. *
  316. * Returns zero on success, or %-ENOENT on failure.
  317. */
  318. int unregister_reboot_notifier(struct notifier_block *nb)
  319. {
  320. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  321. }
  322. EXPORT_SYMBOL(unregister_reboot_notifier);
  323. /**
  324. * kernel_restart - reboot the system
  325. * @cmd: pointer to buffer containing command to execute for restart
  326. * or %NULL
  327. *
  328. * Shutdown everything and perform a clean reboot.
  329. * This is not safe to call in interrupt context.
  330. */
  331. void kernel_restart(char *cmd)
  332. {
  333. kernel_restart_prepare(cmd);
  334. if (!cmd)
  335. printk(KERN_EMERG "Restarting system.\n");
  336. else
  337. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  338. kmsg_dump(KMSG_DUMP_RESTART);
  339. machine_restart(cmd);
  340. }
  341. EXPORT_SYMBOL_GPL(kernel_restart);
  342. static void kernel_shutdown_prepare(enum system_states state)
  343. {
  344. blocking_notifier_call_chain(&reboot_notifier_list,
  345. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  346. system_state = state;
  347. usermodehelper_disable();
  348. device_shutdown();
  349. }
  350. /**
  351. * kernel_halt - halt the system
  352. *
  353. * Shutdown everything and perform a clean system halt.
  354. */
  355. void kernel_halt(void)
  356. {
  357. kernel_shutdown_prepare(SYSTEM_HALT);
  358. syscore_shutdown();
  359. printk(KERN_EMERG "System halted.\n");
  360. kmsg_dump(KMSG_DUMP_HALT);
  361. machine_halt();
  362. }
  363. EXPORT_SYMBOL_GPL(kernel_halt);
  364. /**
  365. * kernel_power_off - power_off the system
  366. *
  367. * Shutdown everything and perform a clean system power_off.
  368. */
  369. void kernel_power_off(void)
  370. {
  371. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  372. if (pm_power_off_prepare)
  373. pm_power_off_prepare();
  374. disable_nonboot_cpus();
  375. syscore_shutdown();
  376. printk(KERN_EMERG "Power down.\n");
  377. kmsg_dump(KMSG_DUMP_POWEROFF);
  378. machine_power_off();
  379. }
  380. EXPORT_SYMBOL_GPL(kernel_power_off);
  381. static DEFINE_MUTEX(reboot_mutex);
  382. /*
  383. * Reboot system call: for obvious reasons only root may call it,
  384. * and even root needs to set up some magic numbers in the registers
  385. * so that some mistake won't make this reboot the whole machine.
  386. * You can also set the meaning of the ctrl-alt-del-key here.
  387. *
  388. * reboot doesn't sync: do that yourself before calling this.
  389. */
  390. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  391. void __user *, arg)
  392. {
  393. char buffer[256];
  394. int ret = 0;
  395. /* We only trust the superuser with rebooting the system. */
  396. if (!capable(CAP_SYS_BOOT))
  397. return -EPERM;
  398. /* For safety, we require "magic" arguments. */
  399. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  400. (magic2 != LINUX_REBOOT_MAGIC2 &&
  401. magic2 != LINUX_REBOOT_MAGIC2A &&
  402. magic2 != LINUX_REBOOT_MAGIC2B &&
  403. magic2 != LINUX_REBOOT_MAGIC2C))
  404. return -EINVAL;
  405. /* Instead of trying to make the power_off code look like
  406. * halt when pm_power_off is not set do it the easy way.
  407. */
  408. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  409. cmd = LINUX_REBOOT_CMD_HALT;
  410. mutex_lock(&reboot_mutex);
  411. switch (cmd) {
  412. case LINUX_REBOOT_CMD_RESTART:
  413. kernel_restart(NULL);
  414. break;
  415. case LINUX_REBOOT_CMD_CAD_ON:
  416. C_A_D = 1;
  417. break;
  418. case LINUX_REBOOT_CMD_CAD_OFF:
  419. C_A_D = 0;
  420. break;
  421. case LINUX_REBOOT_CMD_HALT:
  422. kernel_halt();
  423. do_exit(0);
  424. panic("cannot halt");
  425. case LINUX_REBOOT_CMD_POWER_OFF:
  426. kernel_power_off();
  427. do_exit(0);
  428. break;
  429. case LINUX_REBOOT_CMD_RESTART2:
  430. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  431. ret = -EFAULT;
  432. break;
  433. }
  434. buffer[sizeof(buffer) - 1] = '\0';
  435. kernel_restart(buffer);
  436. break;
  437. #ifdef CONFIG_KEXEC
  438. case LINUX_REBOOT_CMD_KEXEC:
  439. ret = kernel_kexec();
  440. break;
  441. #endif
  442. #ifdef CONFIG_HIBERNATION
  443. case LINUX_REBOOT_CMD_SW_SUSPEND:
  444. ret = hibernate();
  445. break;
  446. #endif
  447. default:
  448. ret = -EINVAL;
  449. break;
  450. }
  451. mutex_unlock(&reboot_mutex);
  452. return ret;
  453. }
  454. static void deferred_cad(struct work_struct *dummy)
  455. {
  456. kernel_restart(NULL);
  457. }
  458. /*
  459. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  460. * As it's called within an interrupt, it may NOT sync: the only choice
  461. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  462. */
  463. void ctrl_alt_del(void)
  464. {
  465. static DECLARE_WORK(cad_work, deferred_cad);
  466. if (C_A_D)
  467. schedule_work(&cad_work);
  468. else
  469. kill_cad_pid(SIGINT, 1);
  470. }
  471. /*
  472. * Unprivileged users may change the real gid to the effective gid
  473. * or vice versa. (BSD-style)
  474. *
  475. * If you set the real gid at all, or set the effective gid to a value not
  476. * equal to the real gid, then the saved gid is set to the new effective gid.
  477. *
  478. * This makes it possible for a setgid program to completely drop its
  479. * privileges, which is often a useful assertion to make when you are doing
  480. * a security audit over a program.
  481. *
  482. * The general idea is that a program which uses just setregid() will be
  483. * 100% compatible with BSD. A program which uses just setgid() will be
  484. * 100% compatible with POSIX with saved IDs.
  485. *
  486. * SMP: There are not races, the GIDs are checked only by filesystem
  487. * operations (as far as semantic preservation is concerned).
  488. */
  489. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  490. {
  491. const struct cred *old;
  492. struct cred *new;
  493. int retval;
  494. new = prepare_creds();
  495. if (!new)
  496. return -ENOMEM;
  497. old = current_cred();
  498. retval = -EPERM;
  499. if (rgid != (gid_t) -1) {
  500. if (old->gid == rgid ||
  501. old->egid == rgid ||
  502. nsown_capable(CAP_SETGID))
  503. new->gid = rgid;
  504. else
  505. goto error;
  506. }
  507. if (egid != (gid_t) -1) {
  508. if (old->gid == egid ||
  509. old->egid == egid ||
  510. old->sgid == egid ||
  511. nsown_capable(CAP_SETGID))
  512. new->egid = egid;
  513. else
  514. goto error;
  515. }
  516. if (rgid != (gid_t) -1 ||
  517. (egid != (gid_t) -1 && egid != old->gid))
  518. new->sgid = new->egid;
  519. new->fsgid = new->egid;
  520. return commit_creds(new);
  521. error:
  522. abort_creds(new);
  523. return retval;
  524. }
  525. /*
  526. * setgid() is implemented like SysV w/ SAVED_IDS
  527. *
  528. * SMP: Same implicit races as above.
  529. */
  530. SYSCALL_DEFINE1(setgid, gid_t, gid)
  531. {
  532. const struct cred *old;
  533. struct cred *new;
  534. int retval;
  535. new = prepare_creds();
  536. if (!new)
  537. return -ENOMEM;
  538. old = current_cred();
  539. retval = -EPERM;
  540. if (nsown_capable(CAP_SETGID))
  541. new->gid = new->egid = new->sgid = new->fsgid = gid;
  542. else if (gid == old->gid || gid == old->sgid)
  543. new->egid = new->fsgid = gid;
  544. else
  545. goto error;
  546. return commit_creds(new);
  547. error:
  548. abort_creds(new);
  549. return retval;
  550. }
  551. /*
  552. * change the user struct in a credentials set to match the new UID
  553. */
  554. static int set_user(struct cred *new)
  555. {
  556. struct user_struct *new_user;
  557. new_user = alloc_uid(current_user_ns(), new->uid);
  558. if (!new_user)
  559. return -EAGAIN;
  560. /*
  561. * We don't fail in case of NPROC limit excess here because too many
  562. * poorly written programs don't check set*uid() return code, assuming
  563. * it never fails if called by root. We may still enforce NPROC limit
  564. * for programs doing set*uid()+execve() by harmlessly deferring the
  565. * failure to the execve() stage.
  566. */
  567. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  568. new_user != INIT_USER)
  569. current->flags |= PF_NPROC_EXCEEDED;
  570. else
  571. current->flags &= ~PF_NPROC_EXCEEDED;
  572. free_uid(new->user);
  573. new->user = new_user;
  574. return 0;
  575. }
  576. /*
  577. * Unprivileged users may change the real uid to the effective uid
  578. * or vice versa. (BSD-style)
  579. *
  580. * If you set the real uid at all, or set the effective uid to a value not
  581. * equal to the real uid, then the saved uid is set to the new effective uid.
  582. *
  583. * This makes it possible for a setuid program to completely drop its
  584. * privileges, which is often a useful assertion to make when you are doing
  585. * a security audit over a program.
  586. *
  587. * The general idea is that a program which uses just setreuid() will be
  588. * 100% compatible with BSD. A program which uses just setuid() will be
  589. * 100% compatible with POSIX with saved IDs.
  590. */
  591. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  592. {
  593. const struct cred *old;
  594. struct cred *new;
  595. int retval;
  596. new = prepare_creds();
  597. if (!new)
  598. return -ENOMEM;
  599. old = current_cred();
  600. retval = -EPERM;
  601. if (ruid != (uid_t) -1) {
  602. new->uid = ruid;
  603. if (old->uid != ruid &&
  604. old->euid != ruid &&
  605. !nsown_capable(CAP_SETUID))
  606. goto error;
  607. }
  608. if (euid != (uid_t) -1) {
  609. new->euid = euid;
  610. if (old->uid != euid &&
  611. old->euid != euid &&
  612. old->suid != euid &&
  613. !nsown_capable(CAP_SETUID))
  614. goto error;
  615. }
  616. if (new->uid != old->uid) {
  617. retval = set_user(new);
  618. if (retval < 0)
  619. goto error;
  620. }
  621. if (ruid != (uid_t) -1 ||
  622. (euid != (uid_t) -1 && euid != old->uid))
  623. new->suid = new->euid;
  624. new->fsuid = new->euid;
  625. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  626. if (retval < 0)
  627. goto error;
  628. return commit_creds(new);
  629. error:
  630. abort_creds(new);
  631. return retval;
  632. }
  633. /*
  634. * setuid() is implemented like SysV with SAVED_IDS
  635. *
  636. * Note that SAVED_ID's is deficient in that a setuid root program
  637. * like sendmail, for example, cannot set its uid to be a normal
  638. * user and then switch back, because if you're root, setuid() sets
  639. * the saved uid too. If you don't like this, blame the bright people
  640. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  641. * will allow a root program to temporarily drop privileges and be able to
  642. * regain them by swapping the real and effective uid.
  643. */
  644. SYSCALL_DEFINE1(setuid, uid_t, uid)
  645. {
  646. const struct cred *old;
  647. struct cred *new;
  648. int retval;
  649. new = prepare_creds();
  650. if (!new)
  651. return -ENOMEM;
  652. old = current_cred();
  653. retval = -EPERM;
  654. if (nsown_capable(CAP_SETUID)) {
  655. new->suid = new->uid = uid;
  656. if (uid != old->uid) {
  657. retval = set_user(new);
  658. if (retval < 0)
  659. goto error;
  660. }
  661. } else if (uid != old->uid && uid != new->suid) {
  662. goto error;
  663. }
  664. new->fsuid = new->euid = uid;
  665. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  666. if (retval < 0)
  667. goto error;
  668. return commit_creds(new);
  669. error:
  670. abort_creds(new);
  671. return retval;
  672. }
  673. /*
  674. * This function implements a generic ability to update ruid, euid,
  675. * and suid. This allows you to implement the 4.4 compatible seteuid().
  676. */
  677. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  678. {
  679. const struct cred *old;
  680. struct cred *new;
  681. int retval;
  682. new = prepare_creds();
  683. if (!new)
  684. return -ENOMEM;
  685. old = current_cred();
  686. retval = -EPERM;
  687. if (!nsown_capable(CAP_SETUID)) {
  688. if (ruid != (uid_t) -1 && ruid != old->uid &&
  689. ruid != old->euid && ruid != old->suid)
  690. goto error;
  691. if (euid != (uid_t) -1 && euid != old->uid &&
  692. euid != old->euid && euid != old->suid)
  693. goto error;
  694. if (suid != (uid_t) -1 && suid != old->uid &&
  695. suid != old->euid && suid != old->suid)
  696. goto error;
  697. }
  698. if (ruid != (uid_t) -1) {
  699. new->uid = ruid;
  700. if (ruid != old->uid) {
  701. retval = set_user(new);
  702. if (retval < 0)
  703. goto error;
  704. }
  705. }
  706. if (euid != (uid_t) -1)
  707. new->euid = euid;
  708. if (suid != (uid_t) -1)
  709. new->suid = suid;
  710. new->fsuid = new->euid;
  711. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  712. if (retval < 0)
  713. goto error;
  714. return commit_creds(new);
  715. error:
  716. abort_creds(new);
  717. return retval;
  718. }
  719. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  720. {
  721. const struct cred *cred = current_cred();
  722. int retval;
  723. if (!(retval = put_user(cred->uid, ruid)) &&
  724. !(retval = put_user(cred->euid, euid)))
  725. retval = put_user(cred->suid, suid);
  726. return retval;
  727. }
  728. /*
  729. * Same as above, but for rgid, egid, sgid.
  730. */
  731. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  732. {
  733. const struct cred *old;
  734. struct cred *new;
  735. int retval;
  736. new = prepare_creds();
  737. if (!new)
  738. return -ENOMEM;
  739. old = current_cred();
  740. retval = -EPERM;
  741. if (!nsown_capable(CAP_SETGID)) {
  742. if (rgid != (gid_t) -1 && rgid != old->gid &&
  743. rgid != old->egid && rgid != old->sgid)
  744. goto error;
  745. if (egid != (gid_t) -1 && egid != old->gid &&
  746. egid != old->egid && egid != old->sgid)
  747. goto error;
  748. if (sgid != (gid_t) -1 && sgid != old->gid &&
  749. sgid != old->egid && sgid != old->sgid)
  750. goto error;
  751. }
  752. if (rgid != (gid_t) -1)
  753. new->gid = rgid;
  754. if (egid != (gid_t) -1)
  755. new->egid = egid;
  756. if (sgid != (gid_t) -1)
  757. new->sgid = sgid;
  758. new->fsgid = new->egid;
  759. return commit_creds(new);
  760. error:
  761. abort_creds(new);
  762. return retval;
  763. }
  764. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  765. {
  766. const struct cred *cred = current_cred();
  767. int retval;
  768. if (!(retval = put_user(cred->gid, rgid)) &&
  769. !(retval = put_user(cred->egid, egid)))
  770. retval = put_user(cred->sgid, sgid);
  771. return retval;
  772. }
  773. /*
  774. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  775. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  776. * whatever uid it wants to). It normally shadows "euid", except when
  777. * explicitly set by setfsuid() or for access..
  778. */
  779. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  780. {
  781. const struct cred *old;
  782. struct cred *new;
  783. uid_t old_fsuid;
  784. new = prepare_creds();
  785. if (!new)
  786. return current_fsuid();
  787. old = current_cred();
  788. old_fsuid = old->fsuid;
  789. if (uid == old->uid || uid == old->euid ||
  790. uid == old->suid || uid == old->fsuid ||
  791. nsown_capable(CAP_SETUID)) {
  792. if (uid != old_fsuid) {
  793. new->fsuid = uid;
  794. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  795. goto change_okay;
  796. }
  797. }
  798. abort_creds(new);
  799. return old_fsuid;
  800. change_okay:
  801. commit_creds(new);
  802. return old_fsuid;
  803. }
  804. /*
  805. * Samma på svenska..
  806. */
  807. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  808. {
  809. const struct cred *old;
  810. struct cred *new;
  811. gid_t old_fsgid;
  812. new = prepare_creds();
  813. if (!new)
  814. return current_fsgid();
  815. old = current_cred();
  816. old_fsgid = old->fsgid;
  817. if (gid == old->gid || gid == old->egid ||
  818. gid == old->sgid || gid == old->fsgid ||
  819. nsown_capable(CAP_SETGID)) {
  820. if (gid != old_fsgid) {
  821. new->fsgid = gid;
  822. goto change_okay;
  823. }
  824. }
  825. abort_creds(new);
  826. return old_fsgid;
  827. change_okay:
  828. commit_creds(new);
  829. return old_fsgid;
  830. }
  831. void do_sys_times(struct tms *tms)
  832. {
  833. cputime_t tgutime, tgstime, cutime, cstime;
  834. spin_lock_irq(&current->sighand->siglock);
  835. thread_group_times(current, &tgutime, &tgstime);
  836. cutime = current->signal->cutime;
  837. cstime = current->signal->cstime;
  838. spin_unlock_irq(&current->sighand->siglock);
  839. tms->tms_utime = cputime_to_clock_t(tgutime);
  840. tms->tms_stime = cputime_to_clock_t(tgstime);
  841. tms->tms_cutime = cputime_to_clock_t(cutime);
  842. tms->tms_cstime = cputime_to_clock_t(cstime);
  843. }
  844. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  845. {
  846. if (tbuf) {
  847. struct tms tmp;
  848. do_sys_times(&tmp);
  849. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  850. return -EFAULT;
  851. }
  852. force_successful_syscall_return();
  853. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  854. }
  855. /*
  856. * This needs some heavy checking ...
  857. * I just haven't the stomach for it. I also don't fully
  858. * understand sessions/pgrp etc. Let somebody who does explain it.
  859. *
  860. * OK, I think I have the protection semantics right.... this is really
  861. * only important on a multi-user system anyway, to make sure one user
  862. * can't send a signal to a process owned by another. -TYT, 12/12/91
  863. *
  864. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  865. * LBT 04.03.94
  866. */
  867. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  868. {
  869. struct task_struct *p;
  870. struct task_struct *group_leader = current->group_leader;
  871. struct pid *pgrp;
  872. int err;
  873. if (!pid)
  874. pid = task_pid_vnr(group_leader);
  875. if (!pgid)
  876. pgid = pid;
  877. if (pgid < 0)
  878. return -EINVAL;
  879. rcu_read_lock();
  880. /* From this point forward we keep holding onto the tasklist lock
  881. * so that our parent does not change from under us. -DaveM
  882. */
  883. write_lock_irq(&tasklist_lock);
  884. err = -ESRCH;
  885. p = find_task_by_vpid(pid);
  886. if (!p)
  887. goto out;
  888. err = -EINVAL;
  889. if (!thread_group_leader(p))
  890. goto out;
  891. if (same_thread_group(p->real_parent, group_leader)) {
  892. err = -EPERM;
  893. if (task_session(p) != task_session(group_leader))
  894. goto out;
  895. err = -EACCES;
  896. if (p->did_exec)
  897. goto out;
  898. } else {
  899. err = -ESRCH;
  900. if (p != group_leader)
  901. goto out;
  902. }
  903. err = -EPERM;
  904. if (p->signal->leader)
  905. goto out;
  906. pgrp = task_pid(p);
  907. if (pgid != pid) {
  908. struct task_struct *g;
  909. pgrp = find_vpid(pgid);
  910. g = pid_task(pgrp, PIDTYPE_PGID);
  911. if (!g || task_session(g) != task_session(group_leader))
  912. goto out;
  913. }
  914. err = security_task_setpgid(p, pgid);
  915. if (err)
  916. goto out;
  917. if (task_pgrp(p) != pgrp)
  918. change_pid(p, PIDTYPE_PGID, pgrp);
  919. err = 0;
  920. out:
  921. /* All paths lead to here, thus we are safe. -DaveM */
  922. write_unlock_irq(&tasklist_lock);
  923. rcu_read_unlock();
  924. return err;
  925. }
  926. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  927. {
  928. struct task_struct *p;
  929. struct pid *grp;
  930. int retval;
  931. rcu_read_lock();
  932. if (!pid)
  933. grp = task_pgrp(current);
  934. else {
  935. retval = -ESRCH;
  936. p = find_task_by_vpid(pid);
  937. if (!p)
  938. goto out;
  939. grp = task_pgrp(p);
  940. if (!grp)
  941. goto out;
  942. retval = security_task_getpgid(p);
  943. if (retval)
  944. goto out;
  945. }
  946. retval = pid_vnr(grp);
  947. out:
  948. rcu_read_unlock();
  949. return retval;
  950. }
  951. #ifdef __ARCH_WANT_SYS_GETPGRP
  952. SYSCALL_DEFINE0(getpgrp)
  953. {
  954. return sys_getpgid(0);
  955. }
  956. #endif
  957. SYSCALL_DEFINE1(getsid, pid_t, pid)
  958. {
  959. struct task_struct *p;
  960. struct pid *sid;
  961. int retval;
  962. rcu_read_lock();
  963. if (!pid)
  964. sid = task_session(current);
  965. else {
  966. retval = -ESRCH;
  967. p = find_task_by_vpid(pid);
  968. if (!p)
  969. goto out;
  970. sid = task_session(p);
  971. if (!sid)
  972. goto out;
  973. retval = security_task_getsid(p);
  974. if (retval)
  975. goto out;
  976. }
  977. retval = pid_vnr(sid);
  978. out:
  979. rcu_read_unlock();
  980. return retval;
  981. }
  982. SYSCALL_DEFINE0(setsid)
  983. {
  984. struct task_struct *group_leader = current->group_leader;
  985. struct pid *sid = task_pid(group_leader);
  986. pid_t session = pid_vnr(sid);
  987. int err = -EPERM;
  988. write_lock_irq(&tasklist_lock);
  989. /* Fail if I am already a session leader */
  990. if (group_leader->signal->leader)
  991. goto out;
  992. /* Fail if a process group id already exists that equals the
  993. * proposed session id.
  994. */
  995. if (pid_task(sid, PIDTYPE_PGID))
  996. goto out;
  997. group_leader->signal->leader = 1;
  998. __set_special_pids(sid);
  999. proc_clear_tty(group_leader);
  1000. err = session;
  1001. out:
  1002. write_unlock_irq(&tasklist_lock);
  1003. if (err > 0) {
  1004. proc_sid_connector(group_leader);
  1005. sched_autogroup_create_attach(group_leader);
  1006. }
  1007. return err;
  1008. }
  1009. DECLARE_RWSEM(uts_sem);
  1010. #ifdef COMPAT_UTS_MACHINE
  1011. #define override_architecture(name) \
  1012. (personality(current->personality) == PER_LINUX32 && \
  1013. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  1014. sizeof(COMPAT_UTS_MACHINE)))
  1015. #else
  1016. #define override_architecture(name) 0
  1017. #endif
  1018. /*
  1019. * Work around broken programs that cannot handle "Linux 3.0".
  1020. * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
  1021. */
  1022. static int override_release(char __user *release, int len)
  1023. {
  1024. int ret = 0;
  1025. char buf[65];
  1026. if (current->personality & UNAME26) {
  1027. char *rest = UTS_RELEASE;
  1028. int ndots = 0;
  1029. unsigned v;
  1030. while (*rest) {
  1031. if (*rest == '.' && ++ndots >= 3)
  1032. break;
  1033. if (!isdigit(*rest) && *rest != '.')
  1034. break;
  1035. rest++;
  1036. }
  1037. v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
  1038. snprintf(buf, len, "2.6.%u%s", v, rest);
  1039. ret = copy_to_user(release, buf, len);
  1040. }
  1041. return ret;
  1042. }
  1043. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  1044. {
  1045. int errno = 0;
  1046. down_read(&uts_sem);
  1047. if (copy_to_user(name, utsname(), sizeof *name))
  1048. errno = -EFAULT;
  1049. up_read(&uts_sem);
  1050. if (!errno && override_release(name->release, sizeof(name->release)))
  1051. errno = -EFAULT;
  1052. if (!errno && override_architecture(name))
  1053. errno = -EFAULT;
  1054. return errno;
  1055. }
  1056. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  1057. /*
  1058. * Old cruft
  1059. */
  1060. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  1061. {
  1062. int error = 0;
  1063. if (!name)
  1064. return -EFAULT;
  1065. down_read(&uts_sem);
  1066. if (copy_to_user(name, utsname(), sizeof(*name)))
  1067. error = -EFAULT;
  1068. up_read(&uts_sem);
  1069. if (!error && override_release(name->release, sizeof(name->release)))
  1070. error = -EFAULT;
  1071. if (!error && override_architecture(name))
  1072. error = -EFAULT;
  1073. return error;
  1074. }
  1075. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1076. {
  1077. int error;
  1078. if (!name)
  1079. return -EFAULT;
  1080. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1081. return -EFAULT;
  1082. down_read(&uts_sem);
  1083. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1084. __OLD_UTS_LEN);
  1085. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1086. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1087. __OLD_UTS_LEN);
  1088. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1089. error |= __copy_to_user(&name->release, &utsname()->release,
  1090. __OLD_UTS_LEN);
  1091. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1092. error |= __copy_to_user(&name->version, &utsname()->version,
  1093. __OLD_UTS_LEN);
  1094. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1095. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1096. __OLD_UTS_LEN);
  1097. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1098. up_read(&uts_sem);
  1099. if (!error && override_architecture(name))
  1100. error = -EFAULT;
  1101. if (!error && override_release(name->release, sizeof(name->release)))
  1102. error = -EFAULT;
  1103. return error ? -EFAULT : 0;
  1104. }
  1105. #endif
  1106. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1107. {
  1108. int errno;
  1109. char tmp[__NEW_UTS_LEN];
  1110. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1111. return -EPERM;
  1112. if (len < 0 || len > __NEW_UTS_LEN)
  1113. return -EINVAL;
  1114. down_write(&uts_sem);
  1115. errno = -EFAULT;
  1116. if (!copy_from_user(tmp, name, len)) {
  1117. struct new_utsname *u = utsname();
  1118. memcpy(u->nodename, tmp, len);
  1119. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1120. errno = 0;
  1121. }
  1122. uts_proc_notify(UTS_PROC_HOSTNAME);
  1123. up_write(&uts_sem);
  1124. return errno;
  1125. }
  1126. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1127. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1128. {
  1129. int i, errno;
  1130. struct new_utsname *u;
  1131. if (len < 0)
  1132. return -EINVAL;
  1133. down_read(&uts_sem);
  1134. u = utsname();
  1135. i = 1 + strlen(u->nodename);
  1136. if (i > len)
  1137. i = len;
  1138. errno = 0;
  1139. if (copy_to_user(name, u->nodename, i))
  1140. errno = -EFAULT;
  1141. up_read(&uts_sem);
  1142. return errno;
  1143. }
  1144. #endif
  1145. /*
  1146. * Only setdomainname; getdomainname can be implemented by calling
  1147. * uname()
  1148. */
  1149. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1150. {
  1151. int errno;
  1152. char tmp[__NEW_UTS_LEN];
  1153. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1154. return -EPERM;
  1155. if (len < 0 || len > __NEW_UTS_LEN)
  1156. return -EINVAL;
  1157. down_write(&uts_sem);
  1158. errno = -EFAULT;
  1159. if (!copy_from_user(tmp, name, len)) {
  1160. struct new_utsname *u = utsname();
  1161. memcpy(u->domainname, tmp, len);
  1162. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1163. errno = 0;
  1164. }
  1165. uts_proc_notify(UTS_PROC_DOMAINNAME);
  1166. up_write(&uts_sem);
  1167. return errno;
  1168. }
  1169. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1170. {
  1171. struct rlimit value;
  1172. int ret;
  1173. ret = do_prlimit(current, resource, NULL, &value);
  1174. if (!ret)
  1175. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1176. return ret;
  1177. }
  1178. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1179. /*
  1180. * Back compatibility for getrlimit. Needed for some apps.
  1181. */
  1182. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1183. struct rlimit __user *, rlim)
  1184. {
  1185. struct rlimit x;
  1186. if (resource >= RLIM_NLIMITS)
  1187. return -EINVAL;
  1188. task_lock(current->group_leader);
  1189. x = current->signal->rlim[resource];
  1190. task_unlock(current->group_leader);
  1191. if (x.rlim_cur > 0x7FFFFFFF)
  1192. x.rlim_cur = 0x7FFFFFFF;
  1193. if (x.rlim_max > 0x7FFFFFFF)
  1194. x.rlim_max = 0x7FFFFFFF;
  1195. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1196. }
  1197. #endif
  1198. static inline bool rlim64_is_infinity(__u64 rlim64)
  1199. {
  1200. #if BITS_PER_LONG < 64
  1201. return rlim64 >= ULONG_MAX;
  1202. #else
  1203. return rlim64 == RLIM64_INFINITY;
  1204. #endif
  1205. }
  1206. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1207. {
  1208. if (rlim->rlim_cur == RLIM_INFINITY)
  1209. rlim64->rlim_cur = RLIM64_INFINITY;
  1210. else
  1211. rlim64->rlim_cur = rlim->rlim_cur;
  1212. if (rlim->rlim_max == RLIM_INFINITY)
  1213. rlim64->rlim_max = RLIM64_INFINITY;
  1214. else
  1215. rlim64->rlim_max = rlim->rlim_max;
  1216. }
  1217. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1218. {
  1219. if (rlim64_is_infinity(rlim64->rlim_cur))
  1220. rlim->rlim_cur = RLIM_INFINITY;
  1221. else
  1222. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1223. if (rlim64_is_infinity(rlim64->rlim_max))
  1224. rlim->rlim_max = RLIM_INFINITY;
  1225. else
  1226. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1227. }
  1228. /* make sure you are allowed to change @tsk limits before calling this */
  1229. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1230. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1231. {
  1232. struct rlimit *rlim;
  1233. int retval = 0;
  1234. if (resource >= RLIM_NLIMITS)
  1235. return -EINVAL;
  1236. if (new_rlim) {
  1237. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1238. return -EINVAL;
  1239. if (resource == RLIMIT_NOFILE &&
  1240. new_rlim->rlim_max > sysctl_nr_open)
  1241. return -EPERM;
  1242. }
  1243. /* protect tsk->signal and tsk->sighand from disappearing */
  1244. read_lock(&tasklist_lock);
  1245. if (!tsk->sighand) {
  1246. retval = -ESRCH;
  1247. goto out;
  1248. }
  1249. rlim = tsk->signal->rlim + resource;
  1250. task_lock(tsk->group_leader);
  1251. if (new_rlim) {
  1252. /* Keep the capable check against init_user_ns until
  1253. cgroups can contain all limits */
  1254. if (new_rlim->rlim_max > rlim->rlim_max &&
  1255. !capable(CAP_SYS_RESOURCE))
  1256. retval = -EPERM;
  1257. if (!retval)
  1258. retval = security_task_setrlimit(tsk->group_leader,
  1259. resource, new_rlim);
  1260. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1261. /*
  1262. * The caller is asking for an immediate RLIMIT_CPU
  1263. * expiry. But we use the zero value to mean "it was
  1264. * never set". So let's cheat and make it one second
  1265. * instead
  1266. */
  1267. new_rlim->rlim_cur = 1;
  1268. }
  1269. }
  1270. if (!retval) {
  1271. if (old_rlim)
  1272. *old_rlim = *rlim;
  1273. if (new_rlim)
  1274. *rlim = *new_rlim;
  1275. }
  1276. task_unlock(tsk->group_leader);
  1277. /*
  1278. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1279. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1280. * very long-standing error, and fixing it now risks breakage of
  1281. * applications, so we live with it
  1282. */
  1283. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1284. new_rlim->rlim_cur != RLIM_INFINITY)
  1285. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1286. out:
  1287. read_unlock(&tasklist_lock);
  1288. return retval;
  1289. }
  1290. /* rcu lock must be held */
  1291. static int check_prlimit_permission(struct task_struct *task)
  1292. {
  1293. const struct cred *cred = current_cred(), *tcred;
  1294. if (current == task)
  1295. return 0;
  1296. tcred = __task_cred(task);
  1297. if (cred->user->user_ns == tcred->user->user_ns &&
  1298. (cred->uid == tcred->euid &&
  1299. cred->uid == tcred->suid &&
  1300. cred->uid == tcred->uid &&
  1301. cred->gid == tcred->egid &&
  1302. cred->gid == tcred->sgid &&
  1303. cred->gid == tcred->gid))
  1304. return 0;
  1305. if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE))
  1306. return 0;
  1307. return -EPERM;
  1308. }
  1309. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1310. const struct rlimit64 __user *, new_rlim,
  1311. struct rlimit64 __user *, old_rlim)
  1312. {
  1313. struct rlimit64 old64, new64;
  1314. struct rlimit old, new;
  1315. struct task_struct *tsk;
  1316. int ret;
  1317. if (new_rlim) {
  1318. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1319. return -EFAULT;
  1320. rlim64_to_rlim(&new64, &new);
  1321. }
  1322. rcu_read_lock();
  1323. tsk = pid ? find_task_by_vpid(pid) : current;
  1324. if (!tsk) {
  1325. rcu_read_unlock();
  1326. return -ESRCH;
  1327. }
  1328. ret = check_prlimit_permission(tsk);
  1329. if (ret) {
  1330. rcu_read_unlock();
  1331. return ret;
  1332. }
  1333. get_task_struct(tsk);
  1334. rcu_read_unlock();
  1335. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1336. old_rlim ? &old : NULL);
  1337. if (!ret && old_rlim) {
  1338. rlim_to_rlim64(&old, &old64);
  1339. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1340. ret = -EFAULT;
  1341. }
  1342. put_task_struct(tsk);
  1343. return ret;
  1344. }
  1345. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1346. {
  1347. struct rlimit new_rlim;
  1348. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1349. return -EFAULT;
  1350. return do_prlimit(current, resource, &new_rlim, NULL);
  1351. }
  1352. /*
  1353. * It would make sense to put struct rusage in the task_struct,
  1354. * except that would make the task_struct be *really big*. After
  1355. * task_struct gets moved into malloc'ed memory, it would
  1356. * make sense to do this. It will make moving the rest of the information
  1357. * a lot simpler! (Which we're not doing right now because we're not
  1358. * measuring them yet).
  1359. *
  1360. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1361. * races with threads incrementing their own counters. But since word
  1362. * reads are atomic, we either get new values or old values and we don't
  1363. * care which for the sums. We always take the siglock to protect reading
  1364. * the c* fields from p->signal from races with exit.c updating those
  1365. * fields when reaping, so a sample either gets all the additions of a
  1366. * given child after it's reaped, or none so this sample is before reaping.
  1367. *
  1368. * Locking:
  1369. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1370. * for the cases current multithreaded, non-current single threaded
  1371. * non-current multithreaded. Thread traversal is now safe with
  1372. * the siglock held.
  1373. * Strictly speaking, we donot need to take the siglock if we are current and
  1374. * single threaded, as no one else can take our signal_struct away, no one
  1375. * else can reap the children to update signal->c* counters, and no one else
  1376. * can race with the signal-> fields. If we do not take any lock, the
  1377. * signal-> fields could be read out of order while another thread was just
  1378. * exiting. So we should place a read memory barrier when we avoid the lock.
  1379. * On the writer side, write memory barrier is implied in __exit_signal
  1380. * as __exit_signal releases the siglock spinlock after updating the signal->
  1381. * fields. But we don't do this yet to keep things simple.
  1382. *
  1383. */
  1384. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1385. {
  1386. r->ru_nvcsw += t->nvcsw;
  1387. r->ru_nivcsw += t->nivcsw;
  1388. r->ru_minflt += t->min_flt;
  1389. r->ru_majflt += t->maj_flt;
  1390. r->ru_inblock += task_io_get_inblock(t);
  1391. r->ru_oublock += task_io_get_oublock(t);
  1392. }
  1393. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1394. {
  1395. struct task_struct *t;
  1396. unsigned long flags;
  1397. cputime_t tgutime, tgstime, utime, stime;
  1398. unsigned long maxrss = 0;
  1399. memset((char *) r, 0, sizeof *r);
  1400. utime = stime = 0;
  1401. if (who == RUSAGE_THREAD) {
  1402. task_times(current, &utime, &stime);
  1403. accumulate_thread_rusage(p, r);
  1404. maxrss = p->signal->maxrss;
  1405. goto out;
  1406. }
  1407. if (!lock_task_sighand(p, &flags))
  1408. return;
  1409. switch (who) {
  1410. case RUSAGE_BOTH:
  1411. case RUSAGE_CHILDREN:
  1412. utime = p->signal->cutime;
  1413. stime = p->signal->cstime;
  1414. r->ru_nvcsw = p->signal->cnvcsw;
  1415. r->ru_nivcsw = p->signal->cnivcsw;
  1416. r->ru_minflt = p->signal->cmin_flt;
  1417. r->ru_majflt = p->signal->cmaj_flt;
  1418. r->ru_inblock = p->signal->cinblock;
  1419. r->ru_oublock = p->signal->coublock;
  1420. maxrss = p->signal->cmaxrss;
  1421. if (who == RUSAGE_CHILDREN)
  1422. break;
  1423. case RUSAGE_SELF:
  1424. thread_group_times(p, &tgutime, &tgstime);
  1425. utime += tgutime;
  1426. stime += tgstime;
  1427. r->ru_nvcsw += p->signal->nvcsw;
  1428. r->ru_nivcsw += p->signal->nivcsw;
  1429. r->ru_minflt += p->signal->min_flt;
  1430. r->ru_majflt += p->signal->maj_flt;
  1431. r->ru_inblock += p->signal->inblock;
  1432. r->ru_oublock += p->signal->oublock;
  1433. if (maxrss < p->signal->maxrss)
  1434. maxrss = p->signal->maxrss;
  1435. t = p;
  1436. do {
  1437. accumulate_thread_rusage(t, r);
  1438. t = next_thread(t);
  1439. } while (t != p);
  1440. break;
  1441. default:
  1442. BUG();
  1443. }
  1444. unlock_task_sighand(p, &flags);
  1445. out:
  1446. cputime_to_timeval(utime, &r->ru_utime);
  1447. cputime_to_timeval(stime, &r->ru_stime);
  1448. if (who != RUSAGE_CHILDREN) {
  1449. struct mm_struct *mm = get_task_mm(p);
  1450. if (mm) {
  1451. setmax_mm_hiwater_rss(&maxrss, mm);
  1452. mmput(mm);
  1453. }
  1454. }
  1455. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1456. }
  1457. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1458. {
  1459. struct rusage r;
  1460. k_getrusage(p, who, &r);
  1461. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1462. }
  1463. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1464. {
  1465. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1466. who != RUSAGE_THREAD)
  1467. return -EINVAL;
  1468. return getrusage(current, who, ru);
  1469. }
  1470. SYSCALL_DEFINE1(umask, int, mask)
  1471. {
  1472. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1473. return mask;
  1474. }
  1475. #ifdef CONFIG_CHECKPOINT_RESTORE
  1476. static int prctl_set_mm(int opt, unsigned long addr,
  1477. unsigned long arg4, unsigned long arg5)
  1478. {
  1479. unsigned long rlim = rlimit(RLIMIT_DATA);
  1480. unsigned long vm_req_flags;
  1481. unsigned long vm_bad_flags;
  1482. struct vm_area_struct *vma;
  1483. int error = 0;
  1484. struct mm_struct *mm = current->mm;
  1485. if (arg4 | arg5)
  1486. return -EINVAL;
  1487. if (!capable(CAP_SYS_ADMIN))
  1488. return -EPERM;
  1489. if (addr >= TASK_SIZE)
  1490. return -EINVAL;
  1491. down_read(&mm->mmap_sem);
  1492. vma = find_vma(mm, addr);
  1493. if (opt != PR_SET_MM_START_BRK && opt != PR_SET_MM_BRK) {
  1494. /* It must be existing VMA */
  1495. if (!vma || vma->vm_start > addr)
  1496. goto out;
  1497. }
  1498. error = -EINVAL;
  1499. switch (opt) {
  1500. case PR_SET_MM_START_CODE:
  1501. case PR_SET_MM_END_CODE:
  1502. vm_req_flags = VM_READ | VM_EXEC;
  1503. vm_bad_flags = VM_WRITE | VM_MAYSHARE;
  1504. if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
  1505. (vma->vm_flags & vm_bad_flags))
  1506. goto out;
  1507. if (opt == PR_SET_MM_START_CODE)
  1508. mm->start_code = addr;
  1509. else
  1510. mm->end_code = addr;
  1511. break;
  1512. case PR_SET_MM_START_DATA:
  1513. case PR_SET_MM_END_DATA:
  1514. vm_req_flags = VM_READ | VM_WRITE;
  1515. vm_bad_flags = VM_EXEC | VM_MAYSHARE;
  1516. if ((vma->vm_flags & vm_req_flags) != vm_req_flags ||
  1517. (vma->vm_flags & vm_bad_flags))
  1518. goto out;
  1519. if (opt == PR_SET_MM_START_DATA)
  1520. mm->start_data = addr;
  1521. else
  1522. mm->end_data = addr;
  1523. break;
  1524. case PR_SET_MM_START_STACK:
  1525. #ifdef CONFIG_STACK_GROWSUP
  1526. vm_req_flags = VM_READ | VM_WRITE | VM_GROWSUP;
  1527. #else
  1528. vm_req_flags = VM_READ | VM_WRITE | VM_GROWSDOWN;
  1529. #endif
  1530. if ((vma->vm_flags & vm_req_flags) != vm_req_flags)
  1531. goto out;
  1532. mm->start_stack = addr;
  1533. break;
  1534. case PR_SET_MM_START_BRK:
  1535. if (addr <= mm->end_data)
  1536. goto out;
  1537. if (rlim < RLIM_INFINITY &&
  1538. (mm->brk - addr) +
  1539. (mm->end_data - mm->start_data) > rlim)
  1540. goto out;
  1541. mm->start_brk = addr;
  1542. break;
  1543. case PR_SET_MM_BRK:
  1544. if (addr <= mm->end_data)
  1545. goto out;
  1546. if (rlim < RLIM_INFINITY &&
  1547. (addr - mm->start_brk) +
  1548. (mm->end_data - mm->start_data) > rlim)
  1549. goto out;
  1550. mm->brk = addr;
  1551. break;
  1552. default:
  1553. error = -EINVAL;
  1554. goto out;
  1555. }
  1556. error = 0;
  1557. out:
  1558. up_read(&mm->mmap_sem);
  1559. return error;
  1560. }
  1561. #else /* CONFIG_CHECKPOINT_RESTORE */
  1562. static int prctl_set_mm(int opt, unsigned long addr,
  1563. unsigned long arg4, unsigned long arg5)
  1564. {
  1565. return -EINVAL;
  1566. }
  1567. #endif
  1568. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1569. unsigned long, arg4, unsigned long, arg5)
  1570. {
  1571. struct task_struct *me = current;
  1572. unsigned char comm[sizeof(me->comm)];
  1573. long error;
  1574. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1575. if (error != -ENOSYS)
  1576. return error;
  1577. error = 0;
  1578. switch (option) {
  1579. case PR_SET_PDEATHSIG:
  1580. if (!valid_signal(arg2)) {
  1581. error = -EINVAL;
  1582. break;
  1583. }
  1584. me->pdeath_signal = arg2;
  1585. error = 0;
  1586. break;
  1587. case PR_GET_PDEATHSIG:
  1588. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1589. break;
  1590. case PR_GET_DUMPABLE:
  1591. error = get_dumpable(me->mm);
  1592. break;
  1593. case PR_SET_DUMPABLE:
  1594. if (arg2 < 0 || arg2 > 1) {
  1595. error = -EINVAL;
  1596. break;
  1597. }
  1598. set_dumpable(me->mm, arg2);
  1599. error = 0;
  1600. break;
  1601. case PR_SET_UNALIGN:
  1602. error = SET_UNALIGN_CTL(me, arg2);
  1603. break;
  1604. case PR_GET_UNALIGN:
  1605. error = GET_UNALIGN_CTL(me, arg2);
  1606. break;
  1607. case PR_SET_FPEMU:
  1608. error = SET_FPEMU_CTL(me, arg2);
  1609. break;
  1610. case PR_GET_FPEMU:
  1611. error = GET_FPEMU_CTL(me, arg2);
  1612. break;
  1613. case PR_SET_FPEXC:
  1614. error = SET_FPEXC_CTL(me, arg2);
  1615. break;
  1616. case PR_GET_FPEXC:
  1617. error = GET_FPEXC_CTL(me, arg2);
  1618. break;
  1619. case PR_GET_TIMING:
  1620. error = PR_TIMING_STATISTICAL;
  1621. break;
  1622. case PR_SET_TIMING:
  1623. if (arg2 != PR_TIMING_STATISTICAL)
  1624. error = -EINVAL;
  1625. else
  1626. error = 0;
  1627. break;
  1628. case PR_SET_NAME:
  1629. comm[sizeof(me->comm)-1] = 0;
  1630. if (strncpy_from_user(comm, (char __user *)arg2,
  1631. sizeof(me->comm) - 1) < 0)
  1632. return -EFAULT;
  1633. set_task_comm(me, comm);
  1634. proc_comm_connector(me);
  1635. return 0;
  1636. case PR_GET_NAME:
  1637. get_task_comm(comm, me);
  1638. if (copy_to_user((char __user *)arg2, comm,
  1639. sizeof(comm)))
  1640. return -EFAULT;
  1641. return 0;
  1642. case PR_GET_ENDIAN:
  1643. error = GET_ENDIAN(me, arg2);
  1644. break;
  1645. case PR_SET_ENDIAN:
  1646. error = SET_ENDIAN(me, arg2);
  1647. break;
  1648. case PR_GET_SECCOMP:
  1649. error = prctl_get_seccomp();
  1650. break;
  1651. case PR_SET_SECCOMP:
  1652. error = prctl_set_seccomp(arg2);
  1653. break;
  1654. case PR_GET_TSC:
  1655. error = GET_TSC_CTL(arg2);
  1656. break;
  1657. case PR_SET_TSC:
  1658. error = SET_TSC_CTL(arg2);
  1659. break;
  1660. case PR_TASK_PERF_EVENTS_DISABLE:
  1661. error = perf_event_task_disable();
  1662. break;
  1663. case PR_TASK_PERF_EVENTS_ENABLE:
  1664. error = perf_event_task_enable();
  1665. break;
  1666. case PR_GET_TIMERSLACK:
  1667. error = current->timer_slack_ns;
  1668. break;
  1669. case PR_SET_TIMERSLACK:
  1670. if (arg2 <= 0)
  1671. current->timer_slack_ns =
  1672. current->default_timer_slack_ns;
  1673. else
  1674. current->timer_slack_ns = arg2;
  1675. error = 0;
  1676. break;
  1677. case PR_MCE_KILL:
  1678. if (arg4 | arg5)
  1679. return -EINVAL;
  1680. switch (arg2) {
  1681. case PR_MCE_KILL_CLEAR:
  1682. if (arg3 != 0)
  1683. return -EINVAL;
  1684. current->flags &= ~PF_MCE_PROCESS;
  1685. break;
  1686. case PR_MCE_KILL_SET:
  1687. current->flags |= PF_MCE_PROCESS;
  1688. if (arg3 == PR_MCE_KILL_EARLY)
  1689. current->flags |= PF_MCE_EARLY;
  1690. else if (arg3 == PR_MCE_KILL_LATE)
  1691. current->flags &= ~PF_MCE_EARLY;
  1692. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1693. current->flags &=
  1694. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1695. else
  1696. return -EINVAL;
  1697. break;
  1698. default:
  1699. return -EINVAL;
  1700. }
  1701. error = 0;
  1702. break;
  1703. case PR_MCE_KILL_GET:
  1704. if (arg2 | arg3 | arg4 | arg5)
  1705. return -EINVAL;
  1706. if (current->flags & PF_MCE_PROCESS)
  1707. error = (current->flags & PF_MCE_EARLY) ?
  1708. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1709. else
  1710. error = PR_MCE_KILL_DEFAULT;
  1711. break;
  1712. case PR_SET_MM:
  1713. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  1714. break;
  1715. default:
  1716. error = -EINVAL;
  1717. break;
  1718. }
  1719. return error;
  1720. }
  1721. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1722. struct getcpu_cache __user *, unused)
  1723. {
  1724. int err = 0;
  1725. int cpu = raw_smp_processor_id();
  1726. if (cpup)
  1727. err |= put_user(cpu, cpup);
  1728. if (nodep)
  1729. err |= put_user(cpu_to_node(cpu), nodep);
  1730. return err ? -EFAULT : 0;
  1731. }
  1732. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1733. static void argv_cleanup(struct subprocess_info *info)
  1734. {
  1735. argv_free(info->argv);
  1736. }
  1737. /**
  1738. * orderly_poweroff - Trigger an orderly system poweroff
  1739. * @force: force poweroff if command execution fails
  1740. *
  1741. * This may be called from any context to trigger a system shutdown.
  1742. * If the orderly shutdown fails, it will force an immediate shutdown.
  1743. */
  1744. int orderly_poweroff(bool force)
  1745. {
  1746. int argc;
  1747. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1748. static char *envp[] = {
  1749. "HOME=/",
  1750. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1751. NULL
  1752. };
  1753. int ret = -ENOMEM;
  1754. struct subprocess_info *info;
  1755. if (argv == NULL) {
  1756. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1757. __func__, poweroff_cmd);
  1758. goto out;
  1759. }
  1760. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1761. if (info == NULL) {
  1762. argv_free(argv);
  1763. goto out;
  1764. }
  1765. call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
  1766. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1767. out:
  1768. if (ret && force) {
  1769. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1770. "forcing the issue\n");
  1771. /* I guess this should try to kick off some daemon to
  1772. sync and poweroff asap. Or not even bother syncing
  1773. if we're doing an emergency shutdown? */
  1774. emergency_sync();
  1775. kernel_power_off();
  1776. }
  1777. return ret;
  1778. }
  1779. EXPORT_SYMBOL_GPL(orderly_poweroff);