sysctl.c 55 KB

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