sysctl.c 58 KB

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