sysctl.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  1. /*
  2. * sysctl.c: General linux system control interface
  3. *
  4. * Begun 24 March 1995, Stephen Tweedie
  5. * Added /proc support, Dec 1995
  6. * Added bdflush entry and intvec min/max checking, 2/23/96, Tom Dyas.
  7. * Added hooks for /proc/sys/net (minor, minor patch), 96/4/1, Mike Shaver.
  8. * Added kernel/java-{interpreter,appletviewer}, 96/5/10, Mike Shaver.
  9. * Dynamic registration fixes, Stephen Tweedie.
  10. * Added kswapd-interval, ctrl-alt-del, printk stuff, 1/8/97, Chris Horn.
  11. * Made sysctl support optional via CONFIG_SYSCTL, 1/10/97, Chris
  12. * Horn.
  13. * Added proc_doulongvec_ms_jiffies_minmax, 09/08/99, Carlos H. Bauer.
  14. * Added proc_doulongvec_minmax, 09/08/99, Carlos H. Bauer.
  15. * Changed linked lists to use list.h instead of lists.h, 02/24/00, Bill
  16. * Wendling.
  17. * The list_for_each() macro wasn't appropriate for the sysctl loop.
  18. * Removed it and replaced it with older style, 03/23/00, Bill Wendling
  19. */
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/swap.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/capability.h>
  27. #include <linux/ctype.h>
  28. #include <linux/utsname.h>
  29. #include <linux/capability.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/init.h>
  32. #include <linux/kernel.h>
  33. #include <linux/kobject.h>
  34. #include <linux/net.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/highuid.h>
  37. #include <linux/writeback.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/security.h>
  40. #include <linux/initrd.h>
  41. #include <linux/times.h>
  42. #include <linux/limits.h>
  43. #include <linux/dcache.h>
  44. #include <linux/syscalls.h>
  45. #include <linux/nfs_fs.h>
  46. #include <linux/acpi.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/processor.h>
  49. extern int proc_nr_files(ctl_table *table, int write, struct file *filp,
  50. void __user *buffer, size_t *lenp, loff_t *ppos);
  51. #ifdef CONFIG_X86
  52. #include <asm/nmi.h>
  53. #include <asm/stacktrace.h>
  54. #endif
  55. #if defined(CONFIG_SYSCTL)
  56. /* External variables not in a header file. */
  57. extern int C_A_D;
  58. extern int sysctl_overcommit_memory;
  59. extern int sysctl_overcommit_ratio;
  60. extern int sysctl_panic_on_oom;
  61. extern int max_threads;
  62. extern int core_uses_pid;
  63. extern int suid_dumpable;
  64. extern char core_pattern[];
  65. extern int pid_max;
  66. extern int min_free_kbytes;
  67. extern int printk_ratelimit_jiffies;
  68. extern int printk_ratelimit_burst;
  69. extern int pid_max_min, pid_max_max;
  70. extern int sysctl_drop_caches;
  71. extern int percpu_pagelist_fraction;
  72. extern int compat_log;
  73. /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
  74. static int maxolduid = 65535;
  75. static int minolduid;
  76. static int min_percpu_pagelist_fract = 8;
  77. static int ngroups_max = NGROUPS_MAX;
  78. #ifdef CONFIG_KMOD
  79. extern char modprobe_path[];
  80. #endif
  81. #ifdef CONFIG_CHR_DEV_SG
  82. extern int sg_big_buff;
  83. #endif
  84. #ifdef __sparc__
  85. extern char reboot_command [];
  86. extern int stop_a_enabled;
  87. extern int scons_pwroff;
  88. #endif
  89. #ifdef __hppa__
  90. extern int pwrsw_enabled;
  91. extern int unaligned_enabled;
  92. #endif
  93. #ifdef CONFIG_S390
  94. #ifdef CONFIG_MATHEMU
  95. extern int sysctl_ieee_emulation_warnings;
  96. #endif
  97. extern int sysctl_userprocess_debug;
  98. extern int spin_retry;
  99. #endif
  100. extern int sysctl_hz_timer;
  101. #ifdef CONFIG_BSD_PROCESS_ACCT
  102. extern int acct_parm[];
  103. #endif
  104. #ifdef CONFIG_IA64
  105. extern int no_unaligned_warning;
  106. #endif
  107. #ifdef CONFIG_RT_MUTEXES
  108. extern int max_lock_depth;
  109. #endif
  110. #ifdef CONFIG_SYSCTL_SYSCALL
  111. static int parse_table(int __user *, int, void __user *, size_t __user *,
  112. void __user *, size_t, ctl_table *);
  113. #endif
  114. #ifdef CONFIG_PROC_SYSCTL
  115. static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
  116. void __user *buffer, size_t *lenp, loff_t *ppos);
  117. static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
  118. void __user *buffer, size_t *lenp, loff_t *ppos);
  119. #endif
  120. static ctl_table root_table[];
  121. static struct ctl_table_header root_table_header =
  122. { root_table, LIST_HEAD_INIT(root_table_header.ctl_entry) };
  123. static ctl_table kern_table[];
  124. static ctl_table vm_table[];
  125. static ctl_table fs_table[];
  126. static ctl_table debug_table[];
  127. static ctl_table dev_table[];
  128. extern ctl_table random_table[];
  129. #ifdef CONFIG_UNIX98_PTYS
  130. extern ctl_table pty_table[];
  131. #endif
  132. #ifdef CONFIG_INOTIFY_USER
  133. extern ctl_table inotify_table[];
  134. #endif
  135. #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
  136. int sysctl_legacy_va_layout;
  137. #endif
  138. /* /proc declarations: */
  139. #ifdef CONFIG_PROC_SYSCTL
  140. static ssize_t proc_readsys(struct file *, char __user *, size_t, loff_t *);
  141. static ssize_t proc_writesys(struct file *, const char __user *, size_t, loff_t *);
  142. static int proc_opensys(struct inode *, struct file *);
  143. const struct file_operations proc_sys_file_operations = {
  144. .open = proc_opensys,
  145. .read = proc_readsys,
  146. .write = proc_writesys,
  147. };
  148. extern struct proc_dir_entry *proc_sys_root;
  149. static void register_proc_table(ctl_table *, struct proc_dir_entry *, void *);
  150. static void unregister_proc_table(ctl_table *, struct proc_dir_entry *);
  151. #endif
  152. /* The default sysctl tables: */
  153. static ctl_table root_table[] = {
  154. {
  155. .ctl_name = CTL_KERN,
  156. .procname = "kernel",
  157. .mode = 0555,
  158. .child = kern_table,
  159. },
  160. {
  161. .ctl_name = CTL_VM,
  162. .procname = "vm",
  163. .mode = 0555,
  164. .child = vm_table,
  165. },
  166. #ifdef CONFIG_NET
  167. {
  168. .ctl_name = CTL_NET,
  169. .procname = "net",
  170. .mode = 0555,
  171. .child = net_table,
  172. },
  173. #endif
  174. {
  175. .ctl_name = CTL_FS,
  176. .procname = "fs",
  177. .mode = 0555,
  178. .child = fs_table,
  179. },
  180. {
  181. .ctl_name = CTL_DEBUG,
  182. .procname = "debug",
  183. .mode = 0555,
  184. .child = debug_table,
  185. },
  186. {
  187. .ctl_name = CTL_DEV,
  188. .procname = "dev",
  189. .mode = 0555,
  190. .child = dev_table,
  191. },
  192. { .ctl_name = 0 }
  193. };
  194. static ctl_table kern_table[] = {
  195. {
  196. .ctl_name = KERN_PANIC,
  197. .procname = "panic",
  198. .data = &panic_timeout,
  199. .maxlen = sizeof(int),
  200. .mode = 0644,
  201. .proc_handler = &proc_dointvec,
  202. },
  203. {
  204. .ctl_name = KERN_CORE_USES_PID,
  205. .procname = "core_uses_pid",
  206. .data = &core_uses_pid,
  207. .maxlen = sizeof(int),
  208. .mode = 0644,
  209. .proc_handler = &proc_dointvec,
  210. },
  211. {
  212. .ctl_name = KERN_CORE_PATTERN,
  213. .procname = "core_pattern",
  214. .data = core_pattern,
  215. .maxlen = 128,
  216. .mode = 0644,
  217. .proc_handler = &proc_dostring,
  218. .strategy = &sysctl_string,
  219. },
  220. #ifdef CONFIG_PROC_SYSCTL
  221. {
  222. .ctl_name = KERN_TAINTED,
  223. .procname = "tainted",
  224. .data = &tainted,
  225. .maxlen = sizeof(int),
  226. .mode = 0644,
  227. .proc_handler = &proc_dointvec_taint,
  228. },
  229. #endif
  230. {
  231. .ctl_name = KERN_CAP_BSET,
  232. .procname = "cap-bound",
  233. .data = &cap_bset,
  234. .maxlen = sizeof(kernel_cap_t),
  235. .mode = 0600,
  236. .proc_handler = &proc_dointvec_bset,
  237. },
  238. #ifdef CONFIG_BLK_DEV_INITRD
  239. {
  240. .ctl_name = KERN_REALROOTDEV,
  241. .procname = "real-root-dev",
  242. .data = &real_root_dev,
  243. .maxlen = sizeof(int),
  244. .mode = 0644,
  245. .proc_handler = &proc_dointvec,
  246. },
  247. #endif
  248. #ifdef __sparc__
  249. {
  250. .ctl_name = KERN_SPARC_REBOOT,
  251. .procname = "reboot-cmd",
  252. .data = reboot_command,
  253. .maxlen = 256,
  254. .mode = 0644,
  255. .proc_handler = &proc_dostring,
  256. .strategy = &sysctl_string,
  257. },
  258. {
  259. .ctl_name = KERN_SPARC_STOP_A,
  260. .procname = "stop-a",
  261. .data = &stop_a_enabled,
  262. .maxlen = sizeof (int),
  263. .mode = 0644,
  264. .proc_handler = &proc_dointvec,
  265. },
  266. {
  267. .ctl_name = KERN_SPARC_SCONS_PWROFF,
  268. .procname = "scons-poweroff",
  269. .data = &scons_pwroff,
  270. .maxlen = sizeof (int),
  271. .mode = 0644,
  272. .proc_handler = &proc_dointvec,
  273. },
  274. #endif
  275. #ifdef __hppa__
  276. {
  277. .ctl_name = KERN_HPPA_PWRSW,
  278. .procname = "soft-power",
  279. .data = &pwrsw_enabled,
  280. .maxlen = sizeof (int),
  281. .mode = 0644,
  282. .proc_handler = &proc_dointvec,
  283. },
  284. {
  285. .ctl_name = KERN_HPPA_UNALIGNED,
  286. .procname = "unaligned-trap",
  287. .data = &unaligned_enabled,
  288. .maxlen = sizeof (int),
  289. .mode = 0644,
  290. .proc_handler = &proc_dointvec,
  291. },
  292. #endif
  293. {
  294. .ctl_name = KERN_CTLALTDEL,
  295. .procname = "ctrl-alt-del",
  296. .data = &C_A_D,
  297. .maxlen = sizeof(int),
  298. .mode = 0644,
  299. .proc_handler = &proc_dointvec,
  300. },
  301. {
  302. .ctl_name = KERN_PRINTK,
  303. .procname = "printk",
  304. .data = &console_loglevel,
  305. .maxlen = 4*sizeof(int),
  306. .mode = 0644,
  307. .proc_handler = &proc_dointvec,
  308. },
  309. #ifdef CONFIG_KMOD
  310. {
  311. .ctl_name = KERN_MODPROBE,
  312. .procname = "modprobe",
  313. .data = &modprobe_path,
  314. .maxlen = KMOD_PATH_LEN,
  315. .mode = 0644,
  316. .proc_handler = &proc_dostring,
  317. .strategy = &sysctl_string,
  318. },
  319. #endif
  320. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  321. {
  322. .ctl_name = KERN_HOTPLUG,
  323. .procname = "hotplug",
  324. .data = &uevent_helper,
  325. .maxlen = UEVENT_HELPER_PATH_LEN,
  326. .mode = 0644,
  327. .proc_handler = &proc_dostring,
  328. .strategy = &sysctl_string,
  329. },
  330. #endif
  331. #ifdef CONFIG_CHR_DEV_SG
  332. {
  333. .ctl_name = KERN_SG_BIG_BUFF,
  334. .procname = "sg-big-buff",
  335. .data = &sg_big_buff,
  336. .maxlen = sizeof (int),
  337. .mode = 0444,
  338. .proc_handler = &proc_dointvec,
  339. },
  340. #endif
  341. #ifdef CONFIG_BSD_PROCESS_ACCT
  342. {
  343. .ctl_name = KERN_ACCT,
  344. .procname = "acct",
  345. .data = &acct_parm,
  346. .maxlen = 3*sizeof(int),
  347. .mode = 0644,
  348. .proc_handler = &proc_dointvec,
  349. },
  350. #endif
  351. #ifdef CONFIG_MAGIC_SYSRQ
  352. {
  353. .ctl_name = KERN_SYSRQ,
  354. .procname = "sysrq",
  355. .data = &__sysrq_enabled,
  356. .maxlen = sizeof (int),
  357. .mode = 0644,
  358. .proc_handler = &proc_dointvec,
  359. },
  360. #endif
  361. #ifdef CONFIG_PROC_SYSCTL
  362. {
  363. .ctl_name = KERN_CADPID,
  364. .procname = "cad_pid",
  365. .data = NULL,
  366. .maxlen = sizeof (int),
  367. .mode = 0600,
  368. .proc_handler = &proc_do_cad_pid,
  369. },
  370. #endif
  371. {
  372. .ctl_name = KERN_MAX_THREADS,
  373. .procname = "threads-max",
  374. .data = &max_threads,
  375. .maxlen = sizeof(int),
  376. .mode = 0644,
  377. .proc_handler = &proc_dointvec,
  378. },
  379. {
  380. .ctl_name = KERN_RANDOM,
  381. .procname = "random",
  382. .mode = 0555,
  383. .child = random_table,
  384. },
  385. #ifdef CONFIG_UNIX98_PTYS
  386. {
  387. .ctl_name = KERN_PTY,
  388. .procname = "pty",
  389. .mode = 0555,
  390. .child = pty_table,
  391. },
  392. #endif
  393. {
  394. .ctl_name = KERN_OVERFLOWUID,
  395. .procname = "overflowuid",
  396. .data = &overflowuid,
  397. .maxlen = sizeof(int),
  398. .mode = 0644,
  399. .proc_handler = &proc_dointvec_minmax,
  400. .strategy = &sysctl_intvec,
  401. .extra1 = &minolduid,
  402. .extra2 = &maxolduid,
  403. },
  404. {
  405. .ctl_name = KERN_OVERFLOWGID,
  406. .procname = "overflowgid",
  407. .data = &overflowgid,
  408. .maxlen = sizeof(int),
  409. .mode = 0644,
  410. .proc_handler = &proc_dointvec_minmax,
  411. .strategy = &sysctl_intvec,
  412. .extra1 = &minolduid,
  413. .extra2 = &maxolduid,
  414. },
  415. #ifdef CONFIG_S390
  416. #ifdef CONFIG_MATHEMU
  417. {
  418. .ctl_name = KERN_IEEE_EMULATION_WARNINGS,
  419. .procname = "ieee_emulation_warnings",
  420. .data = &sysctl_ieee_emulation_warnings,
  421. .maxlen = sizeof(int),
  422. .mode = 0644,
  423. .proc_handler = &proc_dointvec,
  424. },
  425. #endif
  426. #ifdef CONFIG_NO_IDLE_HZ
  427. {
  428. .ctl_name = KERN_HZ_TIMER,
  429. .procname = "hz_timer",
  430. .data = &sysctl_hz_timer,
  431. .maxlen = sizeof(int),
  432. .mode = 0644,
  433. .proc_handler = &proc_dointvec,
  434. },
  435. #endif
  436. {
  437. .ctl_name = KERN_S390_USER_DEBUG_LOGGING,
  438. .procname = "userprocess_debug",
  439. .data = &sysctl_userprocess_debug,
  440. .maxlen = sizeof(int),
  441. .mode = 0644,
  442. .proc_handler = &proc_dointvec,
  443. },
  444. #endif
  445. {
  446. .ctl_name = KERN_PIDMAX,
  447. .procname = "pid_max",
  448. .data = &pid_max,
  449. .maxlen = sizeof (int),
  450. .mode = 0644,
  451. .proc_handler = &proc_dointvec_minmax,
  452. .strategy = sysctl_intvec,
  453. .extra1 = &pid_max_min,
  454. .extra2 = &pid_max_max,
  455. },
  456. {
  457. .ctl_name = KERN_PANIC_ON_OOPS,
  458. .procname = "panic_on_oops",
  459. .data = &panic_on_oops,
  460. .maxlen = sizeof(int),
  461. .mode = 0644,
  462. .proc_handler = &proc_dointvec,
  463. },
  464. {
  465. .ctl_name = KERN_PRINTK_RATELIMIT,
  466. .procname = "printk_ratelimit",
  467. .data = &printk_ratelimit_jiffies,
  468. .maxlen = sizeof(int),
  469. .mode = 0644,
  470. .proc_handler = &proc_dointvec_jiffies,
  471. .strategy = &sysctl_jiffies,
  472. },
  473. {
  474. .ctl_name = KERN_PRINTK_RATELIMIT_BURST,
  475. .procname = "printk_ratelimit_burst",
  476. .data = &printk_ratelimit_burst,
  477. .maxlen = sizeof(int),
  478. .mode = 0644,
  479. .proc_handler = &proc_dointvec,
  480. },
  481. {
  482. .ctl_name = KERN_NGROUPS_MAX,
  483. .procname = "ngroups_max",
  484. .data = &ngroups_max,
  485. .maxlen = sizeof (int),
  486. .mode = 0444,
  487. .proc_handler = &proc_dointvec,
  488. },
  489. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
  490. {
  491. .ctl_name = KERN_UNKNOWN_NMI_PANIC,
  492. .procname = "unknown_nmi_panic",
  493. .data = &unknown_nmi_panic,
  494. .maxlen = sizeof (int),
  495. .mode = 0644,
  496. .proc_handler = &proc_dointvec,
  497. },
  498. {
  499. .ctl_name = KERN_NMI_WATCHDOG,
  500. .procname = "nmi_watchdog",
  501. .data = &nmi_watchdog_enabled,
  502. .maxlen = sizeof (int),
  503. .mode = 0644,
  504. .proc_handler = &proc_nmi_enabled,
  505. },
  506. #endif
  507. #if defined(CONFIG_X86)
  508. {
  509. .ctl_name = KERN_PANIC_ON_NMI,
  510. .procname = "panic_on_unrecovered_nmi",
  511. .data = &panic_on_unrecovered_nmi,
  512. .maxlen = sizeof(int),
  513. .mode = 0644,
  514. .proc_handler = &proc_dointvec,
  515. },
  516. {
  517. .ctl_name = KERN_BOOTLOADER_TYPE,
  518. .procname = "bootloader_type",
  519. .data = &bootloader_type,
  520. .maxlen = sizeof (int),
  521. .mode = 0444,
  522. .proc_handler = &proc_dointvec,
  523. },
  524. {
  525. .ctl_name = CTL_UNNUMBERED,
  526. .procname = "kstack_depth_to_print",
  527. .data = &kstack_depth_to_print,
  528. .maxlen = sizeof(int),
  529. .mode = 0644,
  530. .proc_handler = &proc_dointvec,
  531. },
  532. #endif
  533. #if defined(CONFIG_MMU)
  534. {
  535. .ctl_name = KERN_RANDOMIZE,
  536. .procname = "randomize_va_space",
  537. .data = &randomize_va_space,
  538. .maxlen = sizeof(int),
  539. .mode = 0644,
  540. .proc_handler = &proc_dointvec,
  541. },
  542. #endif
  543. #if defined(CONFIG_S390) && defined(CONFIG_SMP)
  544. {
  545. .ctl_name = KERN_SPIN_RETRY,
  546. .procname = "spin_retry",
  547. .data = &spin_retry,
  548. .maxlen = sizeof (int),
  549. .mode = 0644,
  550. .proc_handler = &proc_dointvec,
  551. },
  552. #endif
  553. #ifdef CONFIG_ACPI_SLEEP
  554. {
  555. .ctl_name = KERN_ACPI_VIDEO_FLAGS,
  556. .procname = "acpi_video_flags",
  557. .data = &acpi_video_flags,
  558. .maxlen = sizeof (unsigned long),
  559. .mode = 0644,
  560. .proc_handler = &proc_doulongvec_minmax,
  561. },
  562. #endif
  563. #ifdef CONFIG_IA64
  564. {
  565. .ctl_name = KERN_IA64_UNALIGNED,
  566. .procname = "ignore-unaligned-usertrap",
  567. .data = &no_unaligned_warning,
  568. .maxlen = sizeof (int),
  569. .mode = 0644,
  570. .proc_handler = &proc_dointvec,
  571. },
  572. #endif
  573. #ifdef CONFIG_COMPAT
  574. {
  575. .ctl_name = KERN_COMPAT_LOG,
  576. .procname = "compat-log",
  577. .data = &compat_log,
  578. .maxlen = sizeof (int),
  579. .mode = 0644,
  580. .proc_handler = &proc_dointvec,
  581. },
  582. #endif
  583. #ifdef CONFIG_RT_MUTEXES
  584. {
  585. .ctl_name = KERN_MAX_LOCK_DEPTH,
  586. .procname = "max_lock_depth",
  587. .data = &max_lock_depth,
  588. .maxlen = sizeof(int),
  589. .mode = 0644,
  590. .proc_handler = &proc_dointvec,
  591. },
  592. #endif
  593. { .ctl_name = 0 }
  594. };
  595. /* Constants for minimum and maximum testing in vm_table.
  596. We use these as one-element integer vectors. */
  597. static int zero;
  598. static int one_hundred = 100;
  599. static ctl_table vm_table[] = {
  600. {
  601. .ctl_name = VM_OVERCOMMIT_MEMORY,
  602. .procname = "overcommit_memory",
  603. .data = &sysctl_overcommit_memory,
  604. .maxlen = sizeof(sysctl_overcommit_memory),
  605. .mode = 0644,
  606. .proc_handler = &proc_dointvec,
  607. },
  608. {
  609. .ctl_name = VM_PANIC_ON_OOM,
  610. .procname = "panic_on_oom",
  611. .data = &sysctl_panic_on_oom,
  612. .maxlen = sizeof(sysctl_panic_on_oom),
  613. .mode = 0644,
  614. .proc_handler = &proc_dointvec,
  615. },
  616. {
  617. .ctl_name = VM_OVERCOMMIT_RATIO,
  618. .procname = "overcommit_ratio",
  619. .data = &sysctl_overcommit_ratio,
  620. .maxlen = sizeof(sysctl_overcommit_ratio),
  621. .mode = 0644,
  622. .proc_handler = &proc_dointvec,
  623. },
  624. {
  625. .ctl_name = VM_PAGE_CLUSTER,
  626. .procname = "page-cluster",
  627. .data = &page_cluster,
  628. .maxlen = sizeof(int),
  629. .mode = 0644,
  630. .proc_handler = &proc_dointvec,
  631. },
  632. {
  633. .ctl_name = VM_DIRTY_BACKGROUND,
  634. .procname = "dirty_background_ratio",
  635. .data = &dirty_background_ratio,
  636. .maxlen = sizeof(dirty_background_ratio),
  637. .mode = 0644,
  638. .proc_handler = &proc_dointvec_minmax,
  639. .strategy = &sysctl_intvec,
  640. .extra1 = &zero,
  641. .extra2 = &one_hundred,
  642. },
  643. {
  644. .ctl_name = VM_DIRTY_RATIO,
  645. .procname = "dirty_ratio",
  646. .data = &vm_dirty_ratio,
  647. .maxlen = sizeof(vm_dirty_ratio),
  648. .mode = 0644,
  649. .proc_handler = &proc_dointvec_minmax,
  650. .strategy = &sysctl_intvec,
  651. .extra1 = &zero,
  652. .extra2 = &one_hundred,
  653. },
  654. {
  655. .ctl_name = VM_DIRTY_WB_CS,
  656. .procname = "dirty_writeback_centisecs",
  657. .data = &dirty_writeback_interval,
  658. .maxlen = sizeof(dirty_writeback_interval),
  659. .mode = 0644,
  660. .proc_handler = &dirty_writeback_centisecs_handler,
  661. },
  662. {
  663. .ctl_name = VM_DIRTY_EXPIRE_CS,
  664. .procname = "dirty_expire_centisecs",
  665. .data = &dirty_expire_interval,
  666. .maxlen = sizeof(dirty_expire_interval),
  667. .mode = 0644,
  668. .proc_handler = &proc_dointvec_userhz_jiffies,
  669. },
  670. {
  671. .ctl_name = VM_NR_PDFLUSH_THREADS,
  672. .procname = "nr_pdflush_threads",
  673. .data = &nr_pdflush_threads,
  674. .maxlen = sizeof nr_pdflush_threads,
  675. .mode = 0444 /* read-only*/,
  676. .proc_handler = &proc_dointvec,
  677. },
  678. {
  679. .ctl_name = VM_SWAPPINESS,
  680. .procname = "swappiness",
  681. .data = &vm_swappiness,
  682. .maxlen = sizeof(vm_swappiness),
  683. .mode = 0644,
  684. .proc_handler = &proc_dointvec_minmax,
  685. .strategy = &sysctl_intvec,
  686. .extra1 = &zero,
  687. .extra2 = &one_hundred,
  688. },
  689. #ifdef CONFIG_HUGETLB_PAGE
  690. {
  691. .ctl_name = VM_HUGETLB_PAGES,
  692. .procname = "nr_hugepages",
  693. .data = &max_huge_pages,
  694. .maxlen = sizeof(unsigned long),
  695. .mode = 0644,
  696. .proc_handler = &hugetlb_sysctl_handler,
  697. .extra1 = (void *)&hugetlb_zero,
  698. .extra2 = (void *)&hugetlb_infinity,
  699. },
  700. {
  701. .ctl_name = VM_HUGETLB_GROUP,
  702. .procname = "hugetlb_shm_group",
  703. .data = &sysctl_hugetlb_shm_group,
  704. .maxlen = sizeof(gid_t),
  705. .mode = 0644,
  706. .proc_handler = &proc_dointvec,
  707. },
  708. #endif
  709. {
  710. .ctl_name = VM_LOWMEM_RESERVE_RATIO,
  711. .procname = "lowmem_reserve_ratio",
  712. .data = &sysctl_lowmem_reserve_ratio,
  713. .maxlen = sizeof(sysctl_lowmem_reserve_ratio),
  714. .mode = 0644,
  715. .proc_handler = &lowmem_reserve_ratio_sysctl_handler,
  716. .strategy = &sysctl_intvec,
  717. },
  718. {
  719. .ctl_name = VM_DROP_PAGECACHE,
  720. .procname = "drop_caches",
  721. .data = &sysctl_drop_caches,
  722. .maxlen = sizeof(int),
  723. .mode = 0644,
  724. .proc_handler = drop_caches_sysctl_handler,
  725. .strategy = &sysctl_intvec,
  726. },
  727. {
  728. .ctl_name = VM_MIN_FREE_KBYTES,
  729. .procname = "min_free_kbytes",
  730. .data = &min_free_kbytes,
  731. .maxlen = sizeof(min_free_kbytes),
  732. .mode = 0644,
  733. .proc_handler = &min_free_kbytes_sysctl_handler,
  734. .strategy = &sysctl_intvec,
  735. .extra1 = &zero,
  736. },
  737. {
  738. .ctl_name = VM_PERCPU_PAGELIST_FRACTION,
  739. .procname = "percpu_pagelist_fraction",
  740. .data = &percpu_pagelist_fraction,
  741. .maxlen = sizeof(percpu_pagelist_fraction),
  742. .mode = 0644,
  743. .proc_handler = &percpu_pagelist_fraction_sysctl_handler,
  744. .strategy = &sysctl_intvec,
  745. .extra1 = &min_percpu_pagelist_fract,
  746. },
  747. #ifdef CONFIG_MMU
  748. {
  749. .ctl_name = VM_MAX_MAP_COUNT,
  750. .procname = "max_map_count",
  751. .data = &sysctl_max_map_count,
  752. .maxlen = sizeof(sysctl_max_map_count),
  753. .mode = 0644,
  754. .proc_handler = &proc_dointvec
  755. },
  756. #endif
  757. {
  758. .ctl_name = VM_LAPTOP_MODE,
  759. .procname = "laptop_mode",
  760. .data = &laptop_mode,
  761. .maxlen = sizeof(laptop_mode),
  762. .mode = 0644,
  763. .proc_handler = &proc_dointvec_jiffies,
  764. .strategy = &sysctl_jiffies,
  765. },
  766. {
  767. .ctl_name = VM_BLOCK_DUMP,
  768. .procname = "block_dump",
  769. .data = &block_dump,
  770. .maxlen = sizeof(block_dump),
  771. .mode = 0644,
  772. .proc_handler = &proc_dointvec,
  773. .strategy = &sysctl_intvec,
  774. .extra1 = &zero,
  775. },
  776. {
  777. .ctl_name = VM_VFS_CACHE_PRESSURE,
  778. .procname = "vfs_cache_pressure",
  779. .data = &sysctl_vfs_cache_pressure,
  780. .maxlen = sizeof(sysctl_vfs_cache_pressure),
  781. .mode = 0644,
  782. .proc_handler = &proc_dointvec,
  783. .strategy = &sysctl_intvec,
  784. .extra1 = &zero,
  785. },
  786. #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
  787. {
  788. .ctl_name = VM_LEGACY_VA_LAYOUT,
  789. .procname = "legacy_va_layout",
  790. .data = &sysctl_legacy_va_layout,
  791. .maxlen = sizeof(sysctl_legacy_va_layout),
  792. .mode = 0644,
  793. .proc_handler = &proc_dointvec,
  794. .strategy = &sysctl_intvec,
  795. .extra1 = &zero,
  796. },
  797. #endif
  798. #ifdef CONFIG_NUMA
  799. {
  800. .ctl_name = VM_ZONE_RECLAIM_MODE,
  801. .procname = "zone_reclaim_mode",
  802. .data = &zone_reclaim_mode,
  803. .maxlen = sizeof(zone_reclaim_mode),
  804. .mode = 0644,
  805. .proc_handler = &proc_dointvec,
  806. .strategy = &sysctl_intvec,
  807. .extra1 = &zero,
  808. },
  809. {
  810. .ctl_name = VM_MIN_UNMAPPED,
  811. .procname = "min_unmapped_ratio",
  812. .data = &sysctl_min_unmapped_ratio,
  813. .maxlen = sizeof(sysctl_min_unmapped_ratio),
  814. .mode = 0644,
  815. .proc_handler = &sysctl_min_unmapped_ratio_sysctl_handler,
  816. .strategy = &sysctl_intvec,
  817. .extra1 = &zero,
  818. .extra2 = &one_hundred,
  819. },
  820. {
  821. .ctl_name = VM_MIN_SLAB,
  822. .procname = "min_slab_ratio",
  823. .data = &sysctl_min_slab_ratio,
  824. .maxlen = sizeof(sysctl_min_slab_ratio),
  825. .mode = 0644,
  826. .proc_handler = &sysctl_min_slab_ratio_sysctl_handler,
  827. .strategy = &sysctl_intvec,
  828. .extra1 = &zero,
  829. .extra2 = &one_hundred,
  830. },
  831. #endif
  832. #ifdef CONFIG_X86_32
  833. {
  834. .ctl_name = VM_VDSO_ENABLED,
  835. .procname = "vdso_enabled",
  836. .data = &vdso_enabled,
  837. .maxlen = sizeof(vdso_enabled),
  838. .mode = 0644,
  839. .proc_handler = &proc_dointvec,
  840. .strategy = &sysctl_intvec,
  841. .extra1 = &zero,
  842. },
  843. #endif
  844. { .ctl_name = 0 }
  845. };
  846. #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
  847. static ctl_table binfmt_misc_table[] = {
  848. { .ctl_name = 0 }
  849. };
  850. #endif
  851. static ctl_table fs_table[] = {
  852. {
  853. .ctl_name = FS_NRINODE,
  854. .procname = "inode-nr",
  855. .data = &inodes_stat,
  856. .maxlen = 2*sizeof(int),
  857. .mode = 0444,
  858. .proc_handler = &proc_dointvec,
  859. },
  860. {
  861. .ctl_name = FS_STATINODE,
  862. .procname = "inode-state",
  863. .data = &inodes_stat,
  864. .maxlen = 7*sizeof(int),
  865. .mode = 0444,
  866. .proc_handler = &proc_dointvec,
  867. },
  868. {
  869. .ctl_name = FS_NRFILE,
  870. .procname = "file-nr",
  871. .data = &files_stat,
  872. .maxlen = 3*sizeof(int),
  873. .mode = 0444,
  874. .proc_handler = &proc_nr_files,
  875. },
  876. {
  877. .ctl_name = FS_MAXFILE,
  878. .procname = "file-max",
  879. .data = &files_stat.max_files,
  880. .maxlen = sizeof(int),
  881. .mode = 0644,
  882. .proc_handler = &proc_dointvec,
  883. },
  884. {
  885. .ctl_name = FS_DENTRY,
  886. .procname = "dentry-state",
  887. .data = &dentry_stat,
  888. .maxlen = 6*sizeof(int),
  889. .mode = 0444,
  890. .proc_handler = &proc_dointvec,
  891. },
  892. {
  893. .ctl_name = FS_OVERFLOWUID,
  894. .procname = "overflowuid",
  895. .data = &fs_overflowuid,
  896. .maxlen = sizeof(int),
  897. .mode = 0644,
  898. .proc_handler = &proc_dointvec_minmax,
  899. .strategy = &sysctl_intvec,
  900. .extra1 = &minolduid,
  901. .extra2 = &maxolduid,
  902. },
  903. {
  904. .ctl_name = FS_OVERFLOWGID,
  905. .procname = "overflowgid",
  906. .data = &fs_overflowgid,
  907. .maxlen = sizeof(int),
  908. .mode = 0644,
  909. .proc_handler = &proc_dointvec_minmax,
  910. .strategy = &sysctl_intvec,
  911. .extra1 = &minolduid,
  912. .extra2 = &maxolduid,
  913. },
  914. {
  915. .ctl_name = FS_LEASES,
  916. .procname = "leases-enable",
  917. .data = &leases_enable,
  918. .maxlen = sizeof(int),
  919. .mode = 0644,
  920. .proc_handler = &proc_dointvec,
  921. },
  922. #ifdef CONFIG_DNOTIFY
  923. {
  924. .ctl_name = FS_DIR_NOTIFY,
  925. .procname = "dir-notify-enable",
  926. .data = &dir_notify_enable,
  927. .maxlen = sizeof(int),
  928. .mode = 0644,
  929. .proc_handler = &proc_dointvec,
  930. },
  931. #endif
  932. #ifdef CONFIG_MMU
  933. {
  934. .ctl_name = FS_LEASE_TIME,
  935. .procname = "lease-break-time",
  936. .data = &lease_break_time,
  937. .maxlen = sizeof(int),
  938. .mode = 0644,
  939. .proc_handler = &proc_dointvec,
  940. },
  941. {
  942. .ctl_name = FS_AIO_NR,
  943. .procname = "aio-nr",
  944. .data = &aio_nr,
  945. .maxlen = sizeof(aio_nr),
  946. .mode = 0444,
  947. .proc_handler = &proc_doulongvec_minmax,
  948. },
  949. {
  950. .ctl_name = FS_AIO_MAX_NR,
  951. .procname = "aio-max-nr",
  952. .data = &aio_max_nr,
  953. .maxlen = sizeof(aio_max_nr),
  954. .mode = 0644,
  955. .proc_handler = &proc_doulongvec_minmax,
  956. },
  957. #ifdef CONFIG_INOTIFY_USER
  958. {
  959. .ctl_name = FS_INOTIFY,
  960. .procname = "inotify",
  961. .mode = 0555,
  962. .child = inotify_table,
  963. },
  964. #endif
  965. #endif
  966. {
  967. .ctl_name = KERN_SETUID_DUMPABLE,
  968. .procname = "suid_dumpable",
  969. .data = &suid_dumpable,
  970. .maxlen = sizeof(int),
  971. .mode = 0644,
  972. .proc_handler = &proc_dointvec,
  973. },
  974. #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
  975. {
  976. .ctl_name = CTL_UNNUMBERED,
  977. .procname = "binfmt_misc",
  978. .mode = 0555,
  979. .child = binfmt_misc_table,
  980. },
  981. #endif
  982. { .ctl_name = 0 }
  983. };
  984. static ctl_table debug_table[] = {
  985. { .ctl_name = 0 }
  986. };
  987. static ctl_table dev_table[] = {
  988. { .ctl_name = 0 }
  989. };
  990. static DEFINE_SPINLOCK(sysctl_lock);
  991. /* called under sysctl_lock */
  992. static int use_table(struct ctl_table_header *p)
  993. {
  994. if (unlikely(p->unregistering))
  995. return 0;
  996. p->used++;
  997. return 1;
  998. }
  999. /* called under sysctl_lock */
  1000. static void unuse_table(struct ctl_table_header *p)
  1001. {
  1002. if (!--p->used)
  1003. if (unlikely(p->unregistering))
  1004. complete(p->unregistering);
  1005. }
  1006. /* called under sysctl_lock, will reacquire if has to wait */
  1007. static void start_unregistering(struct ctl_table_header *p)
  1008. {
  1009. /*
  1010. * if p->used is 0, nobody will ever touch that entry again;
  1011. * we'll eliminate all paths to it before dropping sysctl_lock
  1012. */
  1013. if (unlikely(p->used)) {
  1014. struct completion wait;
  1015. init_completion(&wait);
  1016. p->unregistering = &wait;
  1017. spin_unlock(&sysctl_lock);
  1018. wait_for_completion(&wait);
  1019. spin_lock(&sysctl_lock);
  1020. }
  1021. /*
  1022. * do not remove from the list until nobody holds it; walking the
  1023. * list in do_sysctl() relies on that.
  1024. */
  1025. list_del_init(&p->ctl_entry);
  1026. }
  1027. void __init sysctl_init(void)
  1028. {
  1029. #ifdef CONFIG_PROC_SYSCTL
  1030. register_proc_table(root_table, proc_sys_root, &root_table_header);
  1031. #endif
  1032. }
  1033. #ifdef CONFIG_SYSCTL_SYSCALL
  1034. int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
  1035. void __user *newval, size_t newlen)
  1036. {
  1037. struct list_head *tmp;
  1038. int error = -ENOTDIR;
  1039. if (nlen <= 0 || nlen >= CTL_MAXNAME)
  1040. return -ENOTDIR;
  1041. if (oldval) {
  1042. int old_len;
  1043. if (!oldlenp || get_user(old_len, oldlenp))
  1044. return -EFAULT;
  1045. }
  1046. spin_lock(&sysctl_lock);
  1047. tmp = &root_table_header.ctl_entry;
  1048. do {
  1049. struct ctl_table_header *head =
  1050. list_entry(tmp, struct ctl_table_header, ctl_entry);
  1051. if (!use_table(head))
  1052. continue;
  1053. spin_unlock(&sysctl_lock);
  1054. error = parse_table(name, nlen, oldval, oldlenp,
  1055. newval, newlen, head->ctl_table);
  1056. spin_lock(&sysctl_lock);
  1057. unuse_table(head);
  1058. if (error != -ENOTDIR)
  1059. break;
  1060. } while ((tmp = tmp->next) != &root_table_header.ctl_entry);
  1061. spin_unlock(&sysctl_lock);
  1062. return error;
  1063. }
  1064. asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
  1065. {
  1066. struct __sysctl_args tmp;
  1067. int error;
  1068. if (copy_from_user(&tmp, args, sizeof(tmp)))
  1069. return -EFAULT;
  1070. lock_kernel();
  1071. error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
  1072. tmp.newval, tmp.newlen);
  1073. unlock_kernel();
  1074. return error;
  1075. }
  1076. #endif /* CONFIG_SYSCTL_SYSCALL */
  1077. /*
  1078. * ctl_perm does NOT grant the superuser all rights automatically, because
  1079. * some sysctl variables are readonly even to root.
  1080. */
  1081. static int test_perm(int mode, int op)
  1082. {
  1083. if (!current->euid)
  1084. mode >>= 6;
  1085. else if (in_egroup_p(0))
  1086. mode >>= 3;
  1087. if ((mode & op & 0007) == op)
  1088. return 0;
  1089. return -EACCES;
  1090. }
  1091. static inline int ctl_perm(ctl_table *table, int op)
  1092. {
  1093. int error;
  1094. error = security_sysctl(table, op);
  1095. if (error)
  1096. return error;
  1097. return test_perm(table->mode, op);
  1098. }
  1099. #ifdef CONFIG_SYSCTL_SYSCALL
  1100. static int parse_table(int __user *name, int nlen,
  1101. void __user *oldval, size_t __user *oldlenp,
  1102. void __user *newval, size_t newlen,
  1103. ctl_table *table)
  1104. {
  1105. int n;
  1106. repeat:
  1107. if (!nlen)
  1108. return -ENOTDIR;
  1109. if (get_user(n, name))
  1110. return -EFAULT;
  1111. for ( ; table->ctl_name || table->procname; table++) {
  1112. if (!table->ctl_name)
  1113. continue;
  1114. if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
  1115. int error;
  1116. if (table->child) {
  1117. if (ctl_perm(table, 001))
  1118. return -EPERM;
  1119. if (table->strategy) {
  1120. error = table->strategy(
  1121. table, name, nlen,
  1122. oldval, oldlenp,
  1123. newval, newlen);
  1124. if (error)
  1125. return error;
  1126. }
  1127. name++;
  1128. nlen--;
  1129. table = table->child;
  1130. goto repeat;
  1131. }
  1132. error = do_sysctl_strategy(table, name, nlen,
  1133. oldval, oldlenp,
  1134. newval, newlen);
  1135. return error;
  1136. }
  1137. }
  1138. return -ENOTDIR;
  1139. }
  1140. /* Perform the actual read/write of a sysctl table entry. */
  1141. int do_sysctl_strategy (ctl_table *table,
  1142. int __user *name, int nlen,
  1143. void __user *oldval, size_t __user *oldlenp,
  1144. void __user *newval, size_t newlen)
  1145. {
  1146. int op = 0, rc;
  1147. size_t len;
  1148. if (oldval)
  1149. op |= 004;
  1150. if (newval)
  1151. op |= 002;
  1152. if (ctl_perm(table, op))
  1153. return -EPERM;
  1154. if (table->strategy) {
  1155. rc = table->strategy(table, name, nlen, oldval, oldlenp,
  1156. newval, newlen);
  1157. if (rc < 0)
  1158. return rc;
  1159. if (rc > 0)
  1160. return 0;
  1161. }
  1162. /* If there is no strategy routine, or if the strategy returns
  1163. * zero, proceed with automatic r/w */
  1164. if (table->data && table->maxlen) {
  1165. if (oldval && oldlenp) {
  1166. if (get_user(len, oldlenp))
  1167. return -EFAULT;
  1168. if (len) {
  1169. if (len > table->maxlen)
  1170. len = table->maxlen;
  1171. if(copy_to_user(oldval, table->data, len))
  1172. return -EFAULT;
  1173. if(put_user(len, oldlenp))
  1174. return -EFAULT;
  1175. }
  1176. }
  1177. if (newval && newlen) {
  1178. len = newlen;
  1179. if (len > table->maxlen)
  1180. len = table->maxlen;
  1181. if(copy_from_user(table->data, newval, len))
  1182. return -EFAULT;
  1183. }
  1184. }
  1185. return 0;
  1186. }
  1187. #endif /* CONFIG_SYSCTL_SYSCALL */
  1188. /**
  1189. * register_sysctl_table - register a sysctl hierarchy
  1190. * @table: the top-level table structure
  1191. * @insert_at_head: whether the entry should be inserted in front or at the end
  1192. *
  1193. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1194. * array. An entry with a ctl_name of 0 terminates the table.
  1195. *
  1196. * The members of the &ctl_table structure are used as follows:
  1197. *
  1198. * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
  1199. * must be unique within that level of sysctl
  1200. *
  1201. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  1202. * enter a sysctl file
  1203. *
  1204. * data - a pointer to data for use by proc_handler
  1205. *
  1206. * maxlen - the maximum size in bytes of the data
  1207. *
  1208. * mode - the file permissions for the /proc/sys file, and for sysctl(2)
  1209. *
  1210. * child - a pointer to the child sysctl table if this entry is a directory, or
  1211. * %NULL.
  1212. *
  1213. * proc_handler - the text handler routine (described below)
  1214. *
  1215. * strategy - the strategy routine (described below)
  1216. *
  1217. * de - for internal use by the sysctl routines
  1218. *
  1219. * extra1, extra2 - extra pointers usable by the proc handler routines
  1220. *
  1221. * Leaf nodes in the sysctl tree will be represented by a single file
  1222. * under /proc; non-leaf nodes will be represented by directories.
  1223. *
  1224. * sysctl(2) can automatically manage read and write requests through
  1225. * the sysctl table. The data and maxlen fields of the ctl_table
  1226. * struct enable minimal validation of the values being written to be
  1227. * performed, and the mode field allows minimal authentication.
  1228. *
  1229. * More sophisticated management can be enabled by the provision of a
  1230. * strategy routine with the table entry. This will be called before
  1231. * any automatic read or write of the data is performed.
  1232. *
  1233. * The strategy routine may return
  1234. *
  1235. * < 0 - Error occurred (error is passed to user process)
  1236. *
  1237. * 0 - OK - proceed with automatic read or write.
  1238. *
  1239. * > 0 - OK - read or write has been done by the strategy routine, so
  1240. * return immediately.
  1241. *
  1242. * There must be a proc_handler routine for any terminal nodes
  1243. * mirrored under /proc/sys (non-terminals are handled by a built-in
  1244. * directory handler). Several default handlers are available to
  1245. * cover common cases -
  1246. *
  1247. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  1248. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  1249. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  1250. *
  1251. * It is the handler's job to read the input buffer from user memory
  1252. * and process it. The handler should return 0 on success.
  1253. *
  1254. * This routine returns %NULL on a failure to register, and a pointer
  1255. * to the table header on success.
  1256. */
  1257. struct ctl_table_header *register_sysctl_table(ctl_table * table,
  1258. int insert_at_head)
  1259. {
  1260. struct ctl_table_header *tmp;
  1261. tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
  1262. if (!tmp)
  1263. return NULL;
  1264. tmp->ctl_table = table;
  1265. INIT_LIST_HEAD(&tmp->ctl_entry);
  1266. tmp->used = 0;
  1267. tmp->unregistering = NULL;
  1268. spin_lock(&sysctl_lock);
  1269. if (insert_at_head)
  1270. list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
  1271. else
  1272. list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
  1273. spin_unlock(&sysctl_lock);
  1274. #ifdef CONFIG_PROC_SYSCTL
  1275. register_proc_table(table, proc_sys_root, tmp);
  1276. #endif
  1277. return tmp;
  1278. }
  1279. /**
  1280. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1281. * @header: the header returned from register_sysctl_table
  1282. *
  1283. * Unregisters the sysctl table and all children. proc entries may not
  1284. * actually be removed until they are no longer used by anyone.
  1285. */
  1286. void unregister_sysctl_table(struct ctl_table_header * header)
  1287. {
  1288. might_sleep();
  1289. spin_lock(&sysctl_lock);
  1290. start_unregistering(header);
  1291. #ifdef CONFIG_PROC_SYSCTL
  1292. unregister_proc_table(header->ctl_table, proc_sys_root);
  1293. #endif
  1294. spin_unlock(&sysctl_lock);
  1295. kfree(header);
  1296. }
  1297. #else /* !CONFIG_SYSCTL */
  1298. struct ctl_table_header * register_sysctl_table(ctl_table * table,
  1299. int insert_at_head)
  1300. {
  1301. return NULL;
  1302. }
  1303. void unregister_sysctl_table(struct ctl_table_header * table)
  1304. {
  1305. }
  1306. #endif /* CONFIG_SYSCTL */
  1307. /*
  1308. * /proc/sys support
  1309. */
  1310. #ifdef CONFIG_PROC_SYSCTL
  1311. /* Scan the sysctl entries in table and add them all into /proc */
  1312. static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
  1313. {
  1314. struct proc_dir_entry *de;
  1315. int len;
  1316. mode_t mode;
  1317. for (; table->ctl_name || table->procname; table++) {
  1318. /* Can't do anything without a proc name. */
  1319. if (!table->procname)
  1320. continue;
  1321. /* Maybe we can't do anything with it... */
  1322. if (!table->proc_handler && !table->child) {
  1323. printk(KERN_WARNING "SYSCTL: Can't register %s\n",
  1324. table->procname);
  1325. continue;
  1326. }
  1327. len = strlen(table->procname);
  1328. mode = table->mode;
  1329. de = NULL;
  1330. if (table->proc_handler)
  1331. mode |= S_IFREG;
  1332. else {
  1333. mode |= S_IFDIR;
  1334. for (de = root->subdir; de; de = de->next) {
  1335. if (proc_match(len, table->procname, de))
  1336. break;
  1337. }
  1338. /* If the subdir exists already, de is non-NULL */
  1339. }
  1340. if (!de) {
  1341. de = create_proc_entry(table->procname, mode, root);
  1342. if (!de)
  1343. continue;
  1344. de->set = set;
  1345. de->data = (void *) table;
  1346. if (table->proc_handler)
  1347. de->proc_fops = &proc_sys_file_operations;
  1348. }
  1349. table->de = de;
  1350. if (de->mode & S_IFDIR)
  1351. register_proc_table(table->child, de, set);
  1352. }
  1353. }
  1354. /*
  1355. * Unregister a /proc sysctl table and any subdirectories.
  1356. */
  1357. static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
  1358. {
  1359. struct proc_dir_entry *de;
  1360. for (; table->ctl_name || table->procname; table++) {
  1361. if (!(de = table->de))
  1362. continue;
  1363. if (de->mode & S_IFDIR) {
  1364. if (!table->child) {
  1365. printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
  1366. continue;
  1367. }
  1368. unregister_proc_table(table->child, de);
  1369. /* Don't unregister directories which still have entries.. */
  1370. if (de->subdir)
  1371. continue;
  1372. }
  1373. /*
  1374. * In any case, mark the entry as goner; we'll keep it
  1375. * around if it's busy, but we'll know to do nothing with
  1376. * its fields. We are under sysctl_lock here.
  1377. */
  1378. de->data = NULL;
  1379. /* Don't unregister proc entries that are still being used.. */
  1380. if (atomic_read(&de->count))
  1381. continue;
  1382. table->de = NULL;
  1383. remove_proc_entry(table->procname, root);
  1384. }
  1385. }
  1386. static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
  1387. size_t count, loff_t *ppos)
  1388. {
  1389. int op;
  1390. struct proc_dir_entry *de = PDE(file->f_path.dentry->d_inode);
  1391. struct ctl_table *table;
  1392. size_t res;
  1393. ssize_t error = -ENOTDIR;
  1394. spin_lock(&sysctl_lock);
  1395. if (de && de->data && use_table(de->set)) {
  1396. /*
  1397. * at that point we know that sysctl was not unregistered
  1398. * and won't be until we finish
  1399. */
  1400. spin_unlock(&sysctl_lock);
  1401. table = (struct ctl_table *) de->data;
  1402. if (!table || !table->proc_handler)
  1403. goto out;
  1404. error = -EPERM;
  1405. op = (write ? 002 : 004);
  1406. if (ctl_perm(table, op))
  1407. goto out;
  1408. /* careful: calling conventions are nasty here */
  1409. res = count;
  1410. error = (*table->proc_handler)(table, write, file,
  1411. buf, &res, ppos);
  1412. if (!error)
  1413. error = res;
  1414. out:
  1415. spin_lock(&sysctl_lock);
  1416. unuse_table(de->set);
  1417. }
  1418. spin_unlock(&sysctl_lock);
  1419. return error;
  1420. }
  1421. static int proc_opensys(struct inode *inode, struct file *file)
  1422. {
  1423. if (file->f_mode & FMODE_WRITE) {
  1424. /*
  1425. * sysctl entries that are not writable,
  1426. * are _NOT_ writable, capabilities or not.
  1427. */
  1428. if (!(inode->i_mode & S_IWUSR))
  1429. return -EPERM;
  1430. }
  1431. return 0;
  1432. }
  1433. static ssize_t proc_readsys(struct file * file, char __user * buf,
  1434. size_t count, loff_t *ppos)
  1435. {
  1436. return do_rw_proc(0, file, buf, count, ppos);
  1437. }
  1438. static ssize_t proc_writesys(struct file * file, const char __user * buf,
  1439. size_t count, loff_t *ppos)
  1440. {
  1441. return do_rw_proc(1, file, (char __user *) buf, count, ppos);
  1442. }
  1443. static int _proc_do_string(void* data, int maxlen, int write,
  1444. struct file *filp, void __user *buffer,
  1445. size_t *lenp, loff_t *ppos)
  1446. {
  1447. size_t len;
  1448. char __user *p;
  1449. char c;
  1450. if (!data || !maxlen || !*lenp) {
  1451. *lenp = 0;
  1452. return 0;
  1453. }
  1454. if (write) {
  1455. len = 0;
  1456. p = buffer;
  1457. while (len < *lenp) {
  1458. if (get_user(c, p++))
  1459. return -EFAULT;
  1460. if (c == 0 || c == '\n')
  1461. break;
  1462. len++;
  1463. }
  1464. if (len >= maxlen)
  1465. len = maxlen-1;
  1466. if(copy_from_user(data, buffer, len))
  1467. return -EFAULT;
  1468. ((char *) data)[len] = 0;
  1469. *ppos += *lenp;
  1470. } else {
  1471. len = strlen(data);
  1472. if (len > maxlen)
  1473. len = maxlen;
  1474. if (*ppos > len) {
  1475. *lenp = 0;
  1476. return 0;
  1477. }
  1478. data += *ppos;
  1479. len -= *ppos;
  1480. if (len > *lenp)
  1481. len = *lenp;
  1482. if (len)
  1483. if(copy_to_user(buffer, data, len))
  1484. return -EFAULT;
  1485. if (len < *lenp) {
  1486. if(put_user('\n', ((char __user *) buffer) + len))
  1487. return -EFAULT;
  1488. len++;
  1489. }
  1490. *lenp = len;
  1491. *ppos += len;
  1492. }
  1493. return 0;
  1494. }
  1495. /**
  1496. * proc_dostring - read a string sysctl
  1497. * @table: the sysctl table
  1498. * @write: %TRUE if this is a write to the sysctl file
  1499. * @filp: the file structure
  1500. * @buffer: the user buffer
  1501. * @lenp: the size of the user buffer
  1502. * @ppos: file position
  1503. *
  1504. * Reads/writes a string from/to the user buffer. If the kernel
  1505. * buffer provided is not large enough to hold the string, the
  1506. * string is truncated. The copied string is %NULL-terminated.
  1507. * If the string is being read by the user process, it is copied
  1508. * and a newline '\n' is added. It is truncated if the buffer is
  1509. * not large enough.
  1510. *
  1511. * Returns 0 on success.
  1512. */
  1513. int proc_dostring(ctl_table *table, int write, struct file *filp,
  1514. void __user *buffer, size_t *lenp, loff_t *ppos)
  1515. {
  1516. return _proc_do_string(table->data, table->maxlen, write, filp,
  1517. buffer, lenp, ppos);
  1518. }
  1519. static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
  1520. int *valp,
  1521. int write, void *data)
  1522. {
  1523. if (write) {
  1524. *valp = *negp ? -*lvalp : *lvalp;
  1525. } else {
  1526. int val = *valp;
  1527. if (val < 0) {
  1528. *negp = -1;
  1529. *lvalp = (unsigned long)-val;
  1530. } else {
  1531. *negp = 0;
  1532. *lvalp = (unsigned long)val;
  1533. }
  1534. }
  1535. return 0;
  1536. }
  1537. static int __do_proc_dointvec(void *tbl_data, ctl_table *table,
  1538. int write, struct file *filp, void __user *buffer,
  1539. size_t *lenp, loff_t *ppos,
  1540. int (*conv)(int *negp, unsigned long *lvalp, int *valp,
  1541. int write, void *data),
  1542. void *data)
  1543. {
  1544. #define TMPBUFLEN 21
  1545. int *i, vleft, first=1, neg, val;
  1546. unsigned long lval;
  1547. size_t left, len;
  1548. char buf[TMPBUFLEN], *p;
  1549. char __user *s = buffer;
  1550. if (!tbl_data || !table->maxlen || !*lenp ||
  1551. (*ppos && !write)) {
  1552. *lenp = 0;
  1553. return 0;
  1554. }
  1555. i = (int *) tbl_data;
  1556. vleft = table->maxlen / sizeof(*i);
  1557. left = *lenp;
  1558. if (!conv)
  1559. conv = do_proc_dointvec_conv;
  1560. for (; left && vleft--; i++, first=0) {
  1561. if (write) {
  1562. while (left) {
  1563. char c;
  1564. if (get_user(c, s))
  1565. return -EFAULT;
  1566. if (!isspace(c))
  1567. break;
  1568. left--;
  1569. s++;
  1570. }
  1571. if (!left)
  1572. break;
  1573. neg = 0;
  1574. len = left;
  1575. if (len > sizeof(buf) - 1)
  1576. len = sizeof(buf) - 1;
  1577. if (copy_from_user(buf, s, len))
  1578. return -EFAULT;
  1579. buf[len] = 0;
  1580. p = buf;
  1581. if (*p == '-' && left > 1) {
  1582. neg = 1;
  1583. p++;
  1584. }
  1585. if (*p < '0' || *p > '9')
  1586. break;
  1587. lval = simple_strtoul(p, &p, 0);
  1588. len = p-buf;
  1589. if ((len < left) && *p && !isspace(*p))
  1590. break;
  1591. if (neg)
  1592. val = -val;
  1593. s += len;
  1594. left -= len;
  1595. if (conv(&neg, &lval, i, 1, data))
  1596. break;
  1597. } else {
  1598. p = buf;
  1599. if (!first)
  1600. *p++ = '\t';
  1601. if (conv(&neg, &lval, i, 0, data))
  1602. break;
  1603. sprintf(p, "%s%lu", neg ? "-" : "", lval);
  1604. len = strlen(buf);
  1605. if (len > left)
  1606. len = left;
  1607. if(copy_to_user(s, buf, len))
  1608. return -EFAULT;
  1609. left -= len;
  1610. s += len;
  1611. }
  1612. }
  1613. if (!write && !first && left) {
  1614. if(put_user('\n', s))
  1615. return -EFAULT;
  1616. left--, s++;
  1617. }
  1618. if (write) {
  1619. while (left) {
  1620. char c;
  1621. if (get_user(c, s++))
  1622. return -EFAULT;
  1623. if (!isspace(c))
  1624. break;
  1625. left--;
  1626. }
  1627. }
  1628. if (write && first)
  1629. return -EINVAL;
  1630. *lenp -= left;
  1631. *ppos += *lenp;
  1632. return 0;
  1633. #undef TMPBUFLEN
  1634. }
  1635. static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
  1636. void __user *buffer, size_t *lenp, loff_t *ppos,
  1637. int (*conv)(int *negp, unsigned long *lvalp, int *valp,
  1638. int write, void *data),
  1639. void *data)
  1640. {
  1641. return __do_proc_dointvec(table->data, table, write, filp,
  1642. buffer, lenp, ppos, conv, data);
  1643. }
  1644. /**
  1645. * proc_dointvec - read a vector of integers
  1646. * @table: the sysctl table
  1647. * @write: %TRUE if this is a write to the sysctl file
  1648. * @filp: the file structure
  1649. * @buffer: the user buffer
  1650. * @lenp: the size of the user buffer
  1651. * @ppos: file position
  1652. *
  1653. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  1654. * values from/to the user buffer, treated as an ASCII string.
  1655. *
  1656. * Returns 0 on success.
  1657. */
  1658. int proc_dointvec(ctl_table *table, int write, struct file *filp,
  1659. void __user *buffer, size_t *lenp, loff_t *ppos)
  1660. {
  1661. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1662. NULL,NULL);
  1663. }
  1664. #define OP_SET 0
  1665. #define OP_AND 1
  1666. #define OP_OR 2
  1667. static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
  1668. int *valp,
  1669. int write, void *data)
  1670. {
  1671. int op = *(int *)data;
  1672. if (write) {
  1673. int val = *negp ? -*lvalp : *lvalp;
  1674. switch(op) {
  1675. case OP_SET: *valp = val; break;
  1676. case OP_AND: *valp &= val; break;
  1677. case OP_OR: *valp |= val; break;
  1678. }
  1679. } else {
  1680. int val = *valp;
  1681. if (val < 0) {
  1682. *negp = -1;
  1683. *lvalp = (unsigned long)-val;
  1684. } else {
  1685. *negp = 0;
  1686. *lvalp = (unsigned long)val;
  1687. }
  1688. }
  1689. return 0;
  1690. }
  1691. /*
  1692. * init may raise the set.
  1693. */
  1694. int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
  1695. void __user *buffer, size_t *lenp, loff_t *ppos)
  1696. {
  1697. int op;
  1698. if (write && !capable(CAP_SYS_MODULE)) {
  1699. return -EPERM;
  1700. }
  1701. op = is_init(current) ? OP_SET : OP_AND;
  1702. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1703. do_proc_dointvec_bset_conv,&op);
  1704. }
  1705. /*
  1706. * Taint values can only be increased
  1707. */
  1708. static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
  1709. void __user *buffer, size_t *lenp, loff_t *ppos)
  1710. {
  1711. int op;
  1712. if (!capable(CAP_SYS_ADMIN))
  1713. return -EPERM;
  1714. op = OP_OR;
  1715. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1716. do_proc_dointvec_bset_conv,&op);
  1717. }
  1718. struct do_proc_dointvec_minmax_conv_param {
  1719. int *min;
  1720. int *max;
  1721. };
  1722. static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp,
  1723. int *valp,
  1724. int write, void *data)
  1725. {
  1726. struct do_proc_dointvec_minmax_conv_param *param = data;
  1727. if (write) {
  1728. int val = *negp ? -*lvalp : *lvalp;
  1729. if ((param->min && *param->min > val) ||
  1730. (param->max && *param->max < val))
  1731. return -EINVAL;
  1732. *valp = val;
  1733. } else {
  1734. int val = *valp;
  1735. if (val < 0) {
  1736. *negp = -1;
  1737. *lvalp = (unsigned long)-val;
  1738. } else {
  1739. *negp = 0;
  1740. *lvalp = (unsigned long)val;
  1741. }
  1742. }
  1743. return 0;
  1744. }
  1745. /**
  1746. * proc_dointvec_minmax - read a vector of integers with min/max values
  1747. * @table: the sysctl table
  1748. * @write: %TRUE if this is a write to the sysctl file
  1749. * @filp: the file structure
  1750. * @buffer: the user buffer
  1751. * @lenp: the size of the user buffer
  1752. * @ppos: file position
  1753. *
  1754. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  1755. * values from/to the user buffer, treated as an ASCII string.
  1756. *
  1757. * This routine will ensure the values are within the range specified by
  1758. * table->extra1 (min) and table->extra2 (max).
  1759. *
  1760. * Returns 0 on success.
  1761. */
  1762. int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
  1763. void __user *buffer, size_t *lenp, loff_t *ppos)
  1764. {
  1765. struct do_proc_dointvec_minmax_conv_param param = {
  1766. .min = (int *) table->extra1,
  1767. .max = (int *) table->extra2,
  1768. };
  1769. return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
  1770. do_proc_dointvec_minmax_conv, &param);
  1771. }
  1772. static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write,
  1773. struct file *filp,
  1774. void __user *buffer,
  1775. size_t *lenp, loff_t *ppos,
  1776. unsigned long convmul,
  1777. unsigned long convdiv)
  1778. {
  1779. #define TMPBUFLEN 21
  1780. unsigned long *i, *min, *max, val;
  1781. int vleft, first=1, neg;
  1782. size_t len, left;
  1783. char buf[TMPBUFLEN], *p;
  1784. char __user *s = buffer;
  1785. if (!data || !table->maxlen || !*lenp ||
  1786. (*ppos && !write)) {
  1787. *lenp = 0;
  1788. return 0;
  1789. }
  1790. i = (unsigned long *) data;
  1791. min = (unsigned long *) table->extra1;
  1792. max = (unsigned long *) table->extra2;
  1793. vleft = table->maxlen / sizeof(unsigned long);
  1794. left = *lenp;
  1795. for (; left && vleft--; i++, min++, max++, first=0) {
  1796. if (write) {
  1797. while (left) {
  1798. char c;
  1799. if (get_user(c, s))
  1800. return -EFAULT;
  1801. if (!isspace(c))
  1802. break;
  1803. left--;
  1804. s++;
  1805. }
  1806. if (!left)
  1807. break;
  1808. neg = 0;
  1809. len = left;
  1810. if (len > TMPBUFLEN-1)
  1811. len = TMPBUFLEN-1;
  1812. if (copy_from_user(buf, s, len))
  1813. return -EFAULT;
  1814. buf[len] = 0;
  1815. p = buf;
  1816. if (*p == '-' && left > 1) {
  1817. neg = 1;
  1818. p++;
  1819. }
  1820. if (*p < '0' || *p > '9')
  1821. break;
  1822. val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
  1823. len = p-buf;
  1824. if ((len < left) && *p && !isspace(*p))
  1825. break;
  1826. if (neg)
  1827. val = -val;
  1828. s += len;
  1829. left -= len;
  1830. if(neg)
  1831. continue;
  1832. if ((min && val < *min) || (max && val > *max))
  1833. continue;
  1834. *i = val;
  1835. } else {
  1836. p = buf;
  1837. if (!first)
  1838. *p++ = '\t';
  1839. sprintf(p, "%lu", convdiv * (*i) / convmul);
  1840. len = strlen(buf);
  1841. if (len > left)
  1842. len = left;
  1843. if(copy_to_user(s, buf, len))
  1844. return -EFAULT;
  1845. left -= len;
  1846. s += len;
  1847. }
  1848. }
  1849. if (!write && !first && left) {
  1850. if(put_user('\n', s))
  1851. return -EFAULT;
  1852. left--, s++;
  1853. }
  1854. if (write) {
  1855. while (left) {
  1856. char c;
  1857. if (get_user(c, s++))
  1858. return -EFAULT;
  1859. if (!isspace(c))
  1860. break;
  1861. left--;
  1862. }
  1863. }
  1864. if (write && first)
  1865. return -EINVAL;
  1866. *lenp -= left;
  1867. *ppos += *lenp;
  1868. return 0;
  1869. #undef TMPBUFLEN
  1870. }
  1871. static int do_proc_doulongvec_minmax(ctl_table *table, int write,
  1872. struct file *filp,
  1873. void __user *buffer,
  1874. size_t *lenp, loff_t *ppos,
  1875. unsigned long convmul,
  1876. unsigned long convdiv)
  1877. {
  1878. return __do_proc_doulongvec_minmax(table->data, table, write,
  1879. filp, buffer, lenp, ppos, convmul, convdiv);
  1880. }
  1881. /**
  1882. * proc_doulongvec_minmax - read a vector of long integers with min/max values
  1883. * @table: the sysctl table
  1884. * @write: %TRUE if this is a write to the sysctl file
  1885. * @filp: the file structure
  1886. * @buffer: the user buffer
  1887. * @lenp: the size of the user buffer
  1888. * @ppos: file position
  1889. *
  1890. * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
  1891. * values from/to the user buffer, treated as an ASCII string.
  1892. *
  1893. * This routine will ensure the values are within the range specified by
  1894. * table->extra1 (min) and table->extra2 (max).
  1895. *
  1896. * Returns 0 on success.
  1897. */
  1898. int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
  1899. void __user *buffer, size_t *lenp, loff_t *ppos)
  1900. {
  1901. return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
  1902. }
  1903. /**
  1904. * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
  1905. * @table: the sysctl table
  1906. * @write: %TRUE if this is a write to the sysctl file
  1907. * @filp: the file structure
  1908. * @buffer: the user buffer
  1909. * @lenp: the size of the user buffer
  1910. * @ppos: file position
  1911. *
  1912. * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
  1913. * values from/to the user buffer, treated as an ASCII string. The values
  1914. * are treated as milliseconds, and converted to jiffies when they are stored.
  1915. *
  1916. * This routine will ensure the values are within the range specified by
  1917. * table->extra1 (min) and table->extra2 (max).
  1918. *
  1919. * Returns 0 on success.
  1920. */
  1921. int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
  1922. struct file *filp,
  1923. void __user *buffer,
  1924. size_t *lenp, loff_t *ppos)
  1925. {
  1926. return do_proc_doulongvec_minmax(table, write, filp, buffer,
  1927. lenp, ppos, HZ, 1000l);
  1928. }
  1929. static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
  1930. int *valp,
  1931. int write, void *data)
  1932. {
  1933. if (write) {
  1934. if (*lvalp > LONG_MAX / HZ)
  1935. return 1;
  1936. *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
  1937. } else {
  1938. int val = *valp;
  1939. unsigned long lval;
  1940. if (val < 0) {
  1941. *negp = -1;
  1942. lval = (unsigned long)-val;
  1943. } else {
  1944. *negp = 0;
  1945. lval = (unsigned long)val;
  1946. }
  1947. *lvalp = lval / HZ;
  1948. }
  1949. return 0;
  1950. }
  1951. static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
  1952. int *valp,
  1953. int write, void *data)
  1954. {
  1955. if (write) {
  1956. if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
  1957. return 1;
  1958. *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
  1959. } else {
  1960. int val = *valp;
  1961. unsigned long lval;
  1962. if (val < 0) {
  1963. *negp = -1;
  1964. lval = (unsigned long)-val;
  1965. } else {
  1966. *negp = 0;
  1967. lval = (unsigned long)val;
  1968. }
  1969. *lvalp = jiffies_to_clock_t(lval);
  1970. }
  1971. return 0;
  1972. }
  1973. static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
  1974. int *valp,
  1975. int write, void *data)
  1976. {
  1977. if (write) {
  1978. *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
  1979. } else {
  1980. int val = *valp;
  1981. unsigned long lval;
  1982. if (val < 0) {
  1983. *negp = -1;
  1984. lval = (unsigned long)-val;
  1985. } else {
  1986. *negp = 0;
  1987. lval = (unsigned long)val;
  1988. }
  1989. *lvalp = jiffies_to_msecs(lval);
  1990. }
  1991. return 0;
  1992. }
  1993. /**
  1994. * proc_dointvec_jiffies - read a vector of integers as seconds
  1995. * @table: the sysctl table
  1996. * @write: %TRUE if this is a write to the sysctl file
  1997. * @filp: the file structure
  1998. * @buffer: the user buffer
  1999. * @lenp: the size of the user buffer
  2000. * @ppos: file position
  2001. *
  2002. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2003. * values from/to the user buffer, treated as an ASCII string.
  2004. * The values read are assumed to be in seconds, and are converted into
  2005. * jiffies.
  2006. *
  2007. * Returns 0 on success.
  2008. */
  2009. int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
  2010. void __user *buffer, size_t *lenp, loff_t *ppos)
  2011. {
  2012. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  2013. do_proc_dointvec_jiffies_conv,NULL);
  2014. }
  2015. /**
  2016. * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
  2017. * @table: the sysctl table
  2018. * @write: %TRUE if this is a write to the sysctl file
  2019. * @filp: the file structure
  2020. * @buffer: the user buffer
  2021. * @lenp: the size of the user buffer
  2022. * @ppos: pointer to the file position
  2023. *
  2024. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2025. * values from/to the user buffer, treated as an ASCII string.
  2026. * The values read are assumed to be in 1/USER_HZ seconds, and
  2027. * are converted into jiffies.
  2028. *
  2029. * Returns 0 on success.
  2030. */
  2031. int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
  2032. void __user *buffer, size_t *lenp, loff_t *ppos)
  2033. {
  2034. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  2035. do_proc_dointvec_userhz_jiffies_conv,NULL);
  2036. }
  2037. /**
  2038. * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
  2039. * @table: the sysctl table
  2040. * @write: %TRUE if this is a write to the sysctl file
  2041. * @filp: the file structure
  2042. * @buffer: the user buffer
  2043. * @lenp: the size of the user buffer
  2044. * @ppos: file position
  2045. * @ppos: the current position in the file
  2046. *
  2047. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2048. * values from/to the user buffer, treated as an ASCII string.
  2049. * The values read are assumed to be in 1/1000 seconds, and
  2050. * are converted into jiffies.
  2051. *
  2052. * Returns 0 on success.
  2053. */
  2054. int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
  2055. void __user *buffer, size_t *lenp, loff_t *ppos)
  2056. {
  2057. return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
  2058. do_proc_dointvec_ms_jiffies_conv, NULL);
  2059. }
  2060. static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
  2061. void __user *buffer, size_t *lenp, loff_t *ppos)
  2062. {
  2063. struct pid *new_pid;
  2064. pid_t tmp;
  2065. int r;
  2066. tmp = pid_nr(cad_pid);
  2067. r = __do_proc_dointvec(&tmp, table, write, filp, buffer,
  2068. lenp, ppos, NULL, NULL);
  2069. if (r || !write)
  2070. return r;
  2071. new_pid = find_get_pid(tmp);
  2072. if (!new_pid)
  2073. return -ESRCH;
  2074. put_pid(xchg(&cad_pid, new_pid));
  2075. return 0;
  2076. }
  2077. #else /* CONFIG_PROC_FS */
  2078. int proc_dostring(ctl_table *table, int write, struct file *filp,
  2079. void __user *buffer, size_t *lenp, loff_t *ppos)
  2080. {
  2081. return -ENOSYS;
  2082. }
  2083. int proc_dointvec(ctl_table *table, int write, struct file *filp,
  2084. void __user *buffer, size_t *lenp, loff_t *ppos)
  2085. {
  2086. return -ENOSYS;
  2087. }
  2088. int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
  2089. void __user *buffer, size_t *lenp, loff_t *ppos)
  2090. {
  2091. return -ENOSYS;
  2092. }
  2093. int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
  2094. void __user *buffer, size_t *lenp, loff_t *ppos)
  2095. {
  2096. return -ENOSYS;
  2097. }
  2098. int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
  2099. void __user *buffer, size_t *lenp, loff_t *ppos)
  2100. {
  2101. return -ENOSYS;
  2102. }
  2103. int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
  2104. void __user *buffer, size_t *lenp, loff_t *ppos)
  2105. {
  2106. return -ENOSYS;
  2107. }
  2108. int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
  2109. void __user *buffer, size_t *lenp, loff_t *ppos)
  2110. {
  2111. return -ENOSYS;
  2112. }
  2113. int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
  2114. void __user *buffer, size_t *lenp, loff_t *ppos)
  2115. {
  2116. return -ENOSYS;
  2117. }
  2118. int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
  2119. struct file *filp,
  2120. void __user *buffer,
  2121. size_t *lenp, loff_t *ppos)
  2122. {
  2123. return -ENOSYS;
  2124. }
  2125. #endif /* CONFIG_PROC_FS */
  2126. #ifdef CONFIG_SYSCTL_SYSCALL
  2127. /*
  2128. * General sysctl support routines
  2129. */
  2130. /* The generic string strategy routine: */
  2131. int sysctl_string(ctl_table *table, int __user *name, int nlen,
  2132. void __user *oldval, size_t __user *oldlenp,
  2133. void __user *newval, size_t newlen)
  2134. {
  2135. if (!table->data || !table->maxlen)
  2136. return -ENOTDIR;
  2137. if (oldval && oldlenp) {
  2138. size_t bufsize;
  2139. if (get_user(bufsize, oldlenp))
  2140. return -EFAULT;
  2141. if (bufsize) {
  2142. size_t len = strlen(table->data), copied;
  2143. /* This shouldn't trigger for a well-formed sysctl */
  2144. if (len > table->maxlen)
  2145. len = table->maxlen;
  2146. /* Copy up to a max of bufsize-1 bytes of the string */
  2147. copied = (len >= bufsize) ? bufsize - 1 : len;
  2148. if (copy_to_user(oldval, table->data, copied) ||
  2149. put_user(0, (char __user *)(oldval + copied)))
  2150. return -EFAULT;
  2151. if (put_user(len, oldlenp))
  2152. return -EFAULT;
  2153. }
  2154. }
  2155. if (newval && newlen) {
  2156. size_t len = newlen;
  2157. if (len > table->maxlen)
  2158. len = table->maxlen;
  2159. if(copy_from_user(table->data, newval, len))
  2160. return -EFAULT;
  2161. if (len == table->maxlen)
  2162. len--;
  2163. ((char *) table->data)[len] = 0;
  2164. }
  2165. return 1;
  2166. }
  2167. /*
  2168. * This function makes sure that all of the integers in the vector
  2169. * are between the minimum and maximum values given in the arrays
  2170. * table->extra1 and table->extra2, respectively.
  2171. */
  2172. int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
  2173. void __user *oldval, size_t __user *oldlenp,
  2174. void __user *newval, size_t newlen)
  2175. {
  2176. if (newval && newlen) {
  2177. int __user *vec = (int __user *) newval;
  2178. int *min = (int *) table->extra1;
  2179. int *max = (int *) table->extra2;
  2180. size_t length;
  2181. int i;
  2182. if (newlen % sizeof(int) != 0)
  2183. return -EINVAL;
  2184. if (!table->extra1 && !table->extra2)
  2185. return 0;
  2186. if (newlen > table->maxlen)
  2187. newlen = table->maxlen;
  2188. length = newlen / sizeof(int);
  2189. for (i = 0; i < length; i++) {
  2190. int value;
  2191. if (get_user(value, vec + i))
  2192. return -EFAULT;
  2193. if (min && value < min[i])
  2194. return -EINVAL;
  2195. if (max && value > max[i])
  2196. return -EINVAL;
  2197. }
  2198. }
  2199. return 0;
  2200. }
  2201. /* Strategy function to convert jiffies to seconds */
  2202. int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
  2203. void __user *oldval, size_t __user *oldlenp,
  2204. void __user *newval, size_t newlen)
  2205. {
  2206. if (oldval && oldlenp) {
  2207. size_t olen;
  2208. if (get_user(olen, oldlenp))
  2209. return -EFAULT;
  2210. if (olen) {
  2211. int val;
  2212. if (olen < sizeof(int))
  2213. return -EINVAL;
  2214. val = *(int *)(table->data) / HZ;
  2215. if (put_user(val, (int __user *)oldval))
  2216. return -EFAULT;
  2217. if (put_user(sizeof(int), oldlenp))
  2218. return -EFAULT;
  2219. }
  2220. }
  2221. if (newval && newlen) {
  2222. int new;
  2223. if (newlen != sizeof(int))
  2224. return -EINVAL;
  2225. if (get_user(new, (int __user *)newval))
  2226. return -EFAULT;
  2227. *(int *)(table->data) = new*HZ;
  2228. }
  2229. return 1;
  2230. }
  2231. /* Strategy function to convert jiffies to seconds */
  2232. int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
  2233. void __user *oldval, size_t __user *oldlenp,
  2234. void __user *newval, size_t newlen)
  2235. {
  2236. if (oldval && oldlenp) {
  2237. size_t olen;
  2238. if (get_user(olen, oldlenp))
  2239. return -EFAULT;
  2240. if (olen) {
  2241. int val;
  2242. if (olen < sizeof(int))
  2243. return -EINVAL;
  2244. val = jiffies_to_msecs(*(int *)(table->data));
  2245. if (put_user(val, (int __user *)oldval))
  2246. return -EFAULT;
  2247. if (put_user(sizeof(int), oldlenp))
  2248. return -EFAULT;
  2249. }
  2250. }
  2251. if (newval && newlen) {
  2252. int new;
  2253. if (newlen != sizeof(int))
  2254. return -EINVAL;
  2255. if (get_user(new, (int __user *)newval))
  2256. return -EFAULT;
  2257. *(int *)(table->data) = msecs_to_jiffies(new);
  2258. }
  2259. return 1;
  2260. }
  2261. #else /* CONFIG_SYSCTL_SYSCALL */
  2262. asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
  2263. {
  2264. static int msg_count;
  2265. struct __sysctl_args tmp;
  2266. int name[CTL_MAXNAME];
  2267. int i;
  2268. /* Read in the sysctl name for better debug message logging */
  2269. if (copy_from_user(&tmp, args, sizeof(tmp)))
  2270. return -EFAULT;
  2271. if (tmp.nlen <= 0 || tmp.nlen >= CTL_MAXNAME)
  2272. return -ENOTDIR;
  2273. for (i = 0; i < tmp.nlen; i++)
  2274. if (get_user(name[i], tmp.name + i))
  2275. return -EFAULT;
  2276. /* Ignore accesses to kernel.version */
  2277. if ((tmp.nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION))
  2278. goto out;
  2279. if (msg_count < 5) {
  2280. msg_count++;
  2281. printk(KERN_INFO
  2282. "warning: process `%s' used the removed sysctl "
  2283. "system call with ", current->comm);
  2284. for (i = 0; i < tmp.nlen; i++)
  2285. printk("%d.", name[i]);
  2286. printk("\n");
  2287. }
  2288. out:
  2289. return -ENOSYS;
  2290. }
  2291. int sysctl_string(ctl_table *table, int __user *name, int nlen,
  2292. void __user *oldval, size_t __user *oldlenp,
  2293. void __user *newval, size_t newlen)
  2294. {
  2295. return -ENOSYS;
  2296. }
  2297. int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
  2298. void __user *oldval, size_t __user *oldlenp,
  2299. void __user *newval, size_t newlen)
  2300. {
  2301. return -ENOSYS;
  2302. }
  2303. int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
  2304. void __user *oldval, size_t __user *oldlenp,
  2305. void __user *newval, size_t newlen)
  2306. {
  2307. return -ENOSYS;
  2308. }
  2309. int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
  2310. void __user *oldval, size_t __user *oldlenp,
  2311. void __user *newval, size_t newlen)
  2312. {
  2313. return -ENOSYS;
  2314. }
  2315. #endif /* CONFIG_SYSCTL_SYSCALL */
  2316. /*
  2317. * No sense putting this after each symbol definition, twice,
  2318. * exception granted :-)
  2319. */
  2320. EXPORT_SYMBOL(proc_dointvec);
  2321. EXPORT_SYMBOL(proc_dointvec_jiffies);
  2322. EXPORT_SYMBOL(proc_dointvec_minmax);
  2323. EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
  2324. EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
  2325. EXPORT_SYMBOL(proc_dostring);
  2326. EXPORT_SYMBOL(proc_doulongvec_minmax);
  2327. EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
  2328. EXPORT_SYMBOL(register_sysctl_table);
  2329. EXPORT_SYMBOL(sysctl_intvec);
  2330. EXPORT_SYMBOL(sysctl_jiffies);
  2331. EXPORT_SYMBOL(sysctl_ms_jiffies);
  2332. EXPORT_SYMBOL(sysctl_string);
  2333. EXPORT_SYMBOL(unregister_sysctl_table);