sysctl.c 57 KB

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