sysctl.c 58 KB

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