sysctl.c 57 KB

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