sysctl.c 58 KB

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