sysctl.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  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 sysctl_head_finish(struct ctl_table_header *head)
  1028. {
  1029. if (!head)
  1030. return;
  1031. spin_lock(&sysctl_lock);
  1032. unuse_table(head);
  1033. spin_unlock(&sysctl_lock);
  1034. }
  1035. struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
  1036. {
  1037. struct ctl_table_header *head;
  1038. struct list_head *tmp;
  1039. spin_lock(&sysctl_lock);
  1040. if (prev) {
  1041. tmp = &prev->ctl_entry;
  1042. unuse_table(prev);
  1043. goto next;
  1044. }
  1045. tmp = &root_table_header.ctl_entry;
  1046. for (;;) {
  1047. head = list_entry(tmp, struct ctl_table_header, ctl_entry);
  1048. if (!use_table(head))
  1049. goto next;
  1050. spin_unlock(&sysctl_lock);
  1051. return head;
  1052. next:
  1053. tmp = tmp->next;
  1054. if (tmp == &root_table_header.ctl_entry)
  1055. break;
  1056. }
  1057. spin_unlock(&sysctl_lock);
  1058. return NULL;
  1059. }
  1060. void __init sysctl_init(void)
  1061. {
  1062. #ifdef CONFIG_PROC_SYSCTL
  1063. register_proc_table(root_table, proc_sys_root, &root_table_header);
  1064. #endif
  1065. }
  1066. #ifdef CONFIG_SYSCTL_SYSCALL
  1067. int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *oldlenp,
  1068. void __user *newval, size_t newlen)
  1069. {
  1070. struct ctl_table_header *head;
  1071. int error = -ENOTDIR;
  1072. if (nlen <= 0 || nlen >= CTL_MAXNAME)
  1073. return -ENOTDIR;
  1074. if (oldval) {
  1075. int old_len;
  1076. if (!oldlenp || get_user(old_len, oldlenp))
  1077. return -EFAULT;
  1078. }
  1079. for (head = sysctl_head_next(NULL); head;
  1080. head = sysctl_head_next(head)) {
  1081. error = parse_table(name, nlen, oldval, oldlenp,
  1082. newval, newlen, head->ctl_table);
  1083. if (error != -ENOTDIR) {
  1084. sysctl_head_finish(head);
  1085. break;
  1086. }
  1087. }
  1088. return error;
  1089. }
  1090. asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
  1091. {
  1092. struct __sysctl_args tmp;
  1093. int error;
  1094. if (copy_from_user(&tmp, args, sizeof(tmp)))
  1095. return -EFAULT;
  1096. lock_kernel();
  1097. error = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, tmp.oldlenp,
  1098. tmp.newval, tmp.newlen);
  1099. unlock_kernel();
  1100. return error;
  1101. }
  1102. #endif /* CONFIG_SYSCTL_SYSCALL */
  1103. /*
  1104. * sysctl_perm does NOT grant the superuser all rights automatically, because
  1105. * some sysctl variables are readonly even to root.
  1106. */
  1107. static int test_perm(int mode, int op)
  1108. {
  1109. if (!current->euid)
  1110. mode >>= 6;
  1111. else if (in_egroup_p(0))
  1112. mode >>= 3;
  1113. if ((mode & op & 0007) == op)
  1114. return 0;
  1115. return -EACCES;
  1116. }
  1117. int sysctl_perm(ctl_table *table, int op)
  1118. {
  1119. int error;
  1120. error = security_sysctl(table, op);
  1121. if (error)
  1122. return error;
  1123. return test_perm(table->mode, op);
  1124. }
  1125. #ifdef CONFIG_SYSCTL_SYSCALL
  1126. static int parse_table(int __user *name, int nlen,
  1127. void __user *oldval, size_t __user *oldlenp,
  1128. void __user *newval, size_t newlen,
  1129. ctl_table *table)
  1130. {
  1131. int n;
  1132. repeat:
  1133. if (!nlen)
  1134. return -ENOTDIR;
  1135. if (get_user(n, name))
  1136. return -EFAULT;
  1137. for ( ; table->ctl_name || table->procname; table++) {
  1138. if (!table->ctl_name)
  1139. continue;
  1140. if (n == table->ctl_name) {
  1141. int error;
  1142. if (table->child) {
  1143. if (sysctl_perm(table, 001))
  1144. return -EPERM;
  1145. name++;
  1146. nlen--;
  1147. table = table->child;
  1148. goto repeat;
  1149. }
  1150. error = do_sysctl_strategy(table, name, nlen,
  1151. oldval, oldlenp,
  1152. newval, newlen);
  1153. return error;
  1154. }
  1155. }
  1156. return -ENOTDIR;
  1157. }
  1158. /* Perform the actual read/write of a sysctl table entry. */
  1159. int do_sysctl_strategy (ctl_table *table,
  1160. int __user *name, int nlen,
  1161. void __user *oldval, size_t __user *oldlenp,
  1162. void __user *newval, size_t newlen)
  1163. {
  1164. int op = 0, rc;
  1165. size_t len;
  1166. if (oldval)
  1167. op |= 004;
  1168. if (newval)
  1169. op |= 002;
  1170. if (sysctl_perm(table, op))
  1171. return -EPERM;
  1172. if (table->strategy) {
  1173. rc = table->strategy(table, name, nlen, oldval, oldlenp,
  1174. newval, newlen);
  1175. if (rc < 0)
  1176. return rc;
  1177. if (rc > 0)
  1178. return 0;
  1179. }
  1180. /* If there is no strategy routine, or if the strategy returns
  1181. * zero, proceed with automatic r/w */
  1182. if (table->data && table->maxlen) {
  1183. if (oldval && oldlenp) {
  1184. if (get_user(len, oldlenp))
  1185. return -EFAULT;
  1186. if (len) {
  1187. if (len > table->maxlen)
  1188. len = table->maxlen;
  1189. if(copy_to_user(oldval, table->data, len))
  1190. return -EFAULT;
  1191. if(put_user(len, oldlenp))
  1192. return -EFAULT;
  1193. }
  1194. }
  1195. if (newval && newlen) {
  1196. len = newlen;
  1197. if (len > table->maxlen)
  1198. len = table->maxlen;
  1199. if(copy_from_user(table->data, newval, len))
  1200. return -EFAULT;
  1201. }
  1202. }
  1203. return 0;
  1204. }
  1205. #endif /* CONFIG_SYSCTL_SYSCALL */
  1206. /**
  1207. * register_sysctl_table - register a sysctl hierarchy
  1208. * @table: the top-level table structure
  1209. *
  1210. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1211. * array. An entry with a ctl_name of 0 terminates the table.
  1212. *
  1213. * The members of the &ctl_table structure are used as follows:
  1214. *
  1215. * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
  1216. * must be unique within that level of sysctl
  1217. *
  1218. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  1219. * enter a sysctl file
  1220. *
  1221. * data - a pointer to data for use by proc_handler
  1222. *
  1223. * maxlen - the maximum size in bytes of the data
  1224. *
  1225. * mode - the file permissions for the /proc/sys file, and for sysctl(2)
  1226. *
  1227. * child - a pointer to the child sysctl table if this entry is a directory, or
  1228. * %NULL.
  1229. *
  1230. * proc_handler - the text handler routine (described below)
  1231. *
  1232. * strategy - the strategy routine (described below)
  1233. *
  1234. * de - for internal use by the sysctl routines
  1235. *
  1236. * extra1, extra2 - extra pointers usable by the proc handler routines
  1237. *
  1238. * Leaf nodes in the sysctl tree will be represented by a single file
  1239. * under /proc; non-leaf nodes will be represented by directories.
  1240. *
  1241. * sysctl(2) can automatically manage read and write requests through
  1242. * the sysctl table. The data and maxlen fields of the ctl_table
  1243. * struct enable minimal validation of the values being written to be
  1244. * performed, and the mode field allows minimal authentication.
  1245. *
  1246. * More sophisticated management can be enabled by the provision of a
  1247. * strategy routine with the table entry. This will be called before
  1248. * any automatic read or write of the data is performed.
  1249. *
  1250. * The strategy routine may return
  1251. *
  1252. * < 0 - Error occurred (error is passed to user process)
  1253. *
  1254. * 0 - OK - proceed with automatic read or write.
  1255. *
  1256. * > 0 - OK - read or write has been done by the strategy routine, so
  1257. * return immediately.
  1258. *
  1259. * There must be a proc_handler routine for any terminal nodes
  1260. * mirrored under /proc/sys (non-terminals are handled by a built-in
  1261. * directory handler). Several default handlers are available to
  1262. * cover common cases -
  1263. *
  1264. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  1265. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  1266. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  1267. *
  1268. * It is the handler's job to read the input buffer from user memory
  1269. * and process it. The handler should return 0 on success.
  1270. *
  1271. * This routine returns %NULL on a failure to register, and a pointer
  1272. * to the table header on success.
  1273. */
  1274. struct ctl_table_header *register_sysctl_table(ctl_table * table)
  1275. {
  1276. struct ctl_table_header *tmp;
  1277. tmp = kmalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
  1278. if (!tmp)
  1279. return NULL;
  1280. tmp->ctl_table = table;
  1281. INIT_LIST_HEAD(&tmp->ctl_entry);
  1282. tmp->used = 0;
  1283. tmp->unregistering = NULL;
  1284. spin_lock(&sysctl_lock);
  1285. list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
  1286. spin_unlock(&sysctl_lock);
  1287. #ifdef CONFIG_PROC_SYSCTL
  1288. register_proc_table(table, proc_sys_root, tmp);
  1289. #endif
  1290. return tmp;
  1291. }
  1292. /**
  1293. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1294. * @header: the header returned from register_sysctl_table
  1295. *
  1296. * Unregisters the sysctl table and all children. proc entries may not
  1297. * actually be removed until they are no longer used by anyone.
  1298. */
  1299. void unregister_sysctl_table(struct ctl_table_header * header)
  1300. {
  1301. might_sleep();
  1302. spin_lock(&sysctl_lock);
  1303. start_unregistering(header);
  1304. #ifdef CONFIG_PROC_SYSCTL
  1305. unregister_proc_table(header->ctl_table, proc_sys_root);
  1306. #endif
  1307. spin_unlock(&sysctl_lock);
  1308. kfree(header);
  1309. }
  1310. #else /* !CONFIG_SYSCTL */
  1311. struct ctl_table_header * register_sysctl_table(ctl_table * table,
  1312. int insert_at_head)
  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. /* Scan the sysctl entries in table and add them all into /proc */
  1325. static void register_proc_table(ctl_table * table, struct proc_dir_entry *root, void *set)
  1326. {
  1327. struct proc_dir_entry *de;
  1328. int len;
  1329. mode_t mode;
  1330. for (; table->ctl_name || table->procname; table++) {
  1331. /* Can't do anything without a proc name. */
  1332. if (!table->procname)
  1333. continue;
  1334. /* Maybe we can't do anything with it... */
  1335. if (!table->proc_handler && !table->child) {
  1336. printk(KERN_WARNING "SYSCTL: Can't register %s\n",
  1337. table->procname);
  1338. continue;
  1339. }
  1340. len = strlen(table->procname);
  1341. mode = table->mode;
  1342. de = NULL;
  1343. if (table->proc_handler)
  1344. mode |= S_IFREG;
  1345. else {
  1346. mode |= S_IFDIR;
  1347. for (de = root->subdir; de; de = de->next) {
  1348. if (proc_match(len, table->procname, de))
  1349. break;
  1350. }
  1351. /* If the subdir exists already, de is non-NULL */
  1352. }
  1353. if (!de) {
  1354. de = create_proc_entry(table->procname, mode, root);
  1355. if (!de)
  1356. continue;
  1357. de->set = set;
  1358. de->data = (void *) table;
  1359. if (table->proc_handler)
  1360. de->proc_fops = &proc_sys_file_operations;
  1361. }
  1362. table->de = de;
  1363. if (de->mode & S_IFDIR)
  1364. register_proc_table(table->child, de, set);
  1365. }
  1366. }
  1367. /*
  1368. * Unregister a /proc sysctl table and any subdirectories.
  1369. */
  1370. static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root)
  1371. {
  1372. struct proc_dir_entry *de;
  1373. for (; table->ctl_name || table->procname; table++) {
  1374. if (!(de = table->de))
  1375. continue;
  1376. if (de->mode & S_IFDIR) {
  1377. if (!table->child) {
  1378. printk (KERN_ALERT "Help - malformed sysctl tree on free\n");
  1379. continue;
  1380. }
  1381. unregister_proc_table(table->child, de);
  1382. /* Don't unregister directories which still have entries.. */
  1383. if (de->subdir)
  1384. continue;
  1385. }
  1386. /*
  1387. * In any case, mark the entry as goner; we'll keep it
  1388. * around if it's busy, but we'll know to do nothing with
  1389. * its fields. We are under sysctl_lock here.
  1390. */
  1391. de->data = NULL;
  1392. /* Don't unregister proc entries that are still being used.. */
  1393. if (atomic_read(&de->count))
  1394. continue;
  1395. table->de = NULL;
  1396. remove_proc_entry(table->procname, root);
  1397. }
  1398. }
  1399. static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
  1400. size_t count, loff_t *ppos)
  1401. {
  1402. int op;
  1403. struct proc_dir_entry *de = PDE(file->f_path.dentry->d_inode);
  1404. struct ctl_table *table;
  1405. size_t res;
  1406. ssize_t error = -ENOTDIR;
  1407. spin_lock(&sysctl_lock);
  1408. if (de && de->data && use_table(de->set)) {
  1409. /*
  1410. * at that point we know that sysctl was not unregistered
  1411. * and won't be until we finish
  1412. */
  1413. spin_unlock(&sysctl_lock);
  1414. table = (struct ctl_table *) de->data;
  1415. if (!table || !table->proc_handler)
  1416. goto out;
  1417. error = -EPERM;
  1418. op = (write ? 002 : 004);
  1419. if (sysctl_perm(table, op))
  1420. goto out;
  1421. /* careful: calling conventions are nasty here */
  1422. res = count;
  1423. error = (*table->proc_handler)(table, write, file,
  1424. buf, &res, ppos);
  1425. if (!error)
  1426. error = res;
  1427. out:
  1428. spin_lock(&sysctl_lock);
  1429. unuse_table(de->set);
  1430. }
  1431. spin_unlock(&sysctl_lock);
  1432. return error;
  1433. }
  1434. static int proc_opensys(struct inode *inode, struct file *file)
  1435. {
  1436. if (file->f_mode & FMODE_WRITE) {
  1437. /*
  1438. * sysctl entries that are not writable,
  1439. * are _NOT_ writable, capabilities or not.
  1440. */
  1441. if (!(inode->i_mode & S_IWUSR))
  1442. return -EPERM;
  1443. }
  1444. return 0;
  1445. }
  1446. static ssize_t proc_readsys(struct file * file, char __user * buf,
  1447. size_t count, loff_t *ppos)
  1448. {
  1449. return do_rw_proc(0, file, buf, count, ppos);
  1450. }
  1451. static ssize_t proc_writesys(struct file * file, const char __user * buf,
  1452. size_t count, loff_t *ppos)
  1453. {
  1454. return do_rw_proc(1, file, (char __user *) buf, count, ppos);
  1455. }
  1456. static int _proc_do_string(void* data, int maxlen, int write,
  1457. struct file *filp, void __user *buffer,
  1458. size_t *lenp, loff_t *ppos)
  1459. {
  1460. size_t len;
  1461. char __user *p;
  1462. char c;
  1463. if (!data || !maxlen || !*lenp) {
  1464. *lenp = 0;
  1465. return 0;
  1466. }
  1467. if (write) {
  1468. len = 0;
  1469. p = buffer;
  1470. while (len < *lenp) {
  1471. if (get_user(c, p++))
  1472. return -EFAULT;
  1473. if (c == 0 || c == '\n')
  1474. break;
  1475. len++;
  1476. }
  1477. if (len >= maxlen)
  1478. len = maxlen-1;
  1479. if(copy_from_user(data, buffer, len))
  1480. return -EFAULT;
  1481. ((char *) data)[len] = 0;
  1482. *ppos += *lenp;
  1483. } else {
  1484. len = strlen(data);
  1485. if (len > maxlen)
  1486. len = maxlen;
  1487. if (*ppos > len) {
  1488. *lenp = 0;
  1489. return 0;
  1490. }
  1491. data += *ppos;
  1492. len -= *ppos;
  1493. if (len > *lenp)
  1494. len = *lenp;
  1495. if (len)
  1496. if(copy_to_user(buffer, data, len))
  1497. return -EFAULT;
  1498. if (len < *lenp) {
  1499. if(put_user('\n', ((char __user *) buffer) + len))
  1500. return -EFAULT;
  1501. len++;
  1502. }
  1503. *lenp = len;
  1504. *ppos += len;
  1505. }
  1506. return 0;
  1507. }
  1508. /**
  1509. * proc_dostring - read a string sysctl
  1510. * @table: the sysctl table
  1511. * @write: %TRUE if this is a write to the sysctl file
  1512. * @filp: the file structure
  1513. * @buffer: the user buffer
  1514. * @lenp: the size of the user buffer
  1515. * @ppos: file position
  1516. *
  1517. * Reads/writes a string from/to the user buffer. If the kernel
  1518. * buffer provided is not large enough to hold the string, the
  1519. * string is truncated. The copied string is %NULL-terminated.
  1520. * If the string is being read by the user process, it is copied
  1521. * and a newline '\n' is added. It is truncated if the buffer is
  1522. * not large enough.
  1523. *
  1524. * Returns 0 on success.
  1525. */
  1526. int proc_dostring(ctl_table *table, int write, struct file *filp,
  1527. void __user *buffer, size_t *lenp, loff_t *ppos)
  1528. {
  1529. return _proc_do_string(table->data, table->maxlen, write, filp,
  1530. buffer, lenp, ppos);
  1531. }
  1532. static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp,
  1533. int *valp,
  1534. int write, void *data)
  1535. {
  1536. if (write) {
  1537. *valp = *negp ? -*lvalp : *lvalp;
  1538. } else {
  1539. int val = *valp;
  1540. if (val < 0) {
  1541. *negp = -1;
  1542. *lvalp = (unsigned long)-val;
  1543. } else {
  1544. *negp = 0;
  1545. *lvalp = (unsigned long)val;
  1546. }
  1547. }
  1548. return 0;
  1549. }
  1550. static int __do_proc_dointvec(void *tbl_data, ctl_table *table,
  1551. int write, struct file *filp, void __user *buffer,
  1552. size_t *lenp, loff_t *ppos,
  1553. int (*conv)(int *negp, unsigned long *lvalp, int *valp,
  1554. int write, void *data),
  1555. void *data)
  1556. {
  1557. #define TMPBUFLEN 21
  1558. int *i, vleft, first=1, neg, val;
  1559. unsigned long lval;
  1560. size_t left, len;
  1561. char buf[TMPBUFLEN], *p;
  1562. char __user *s = buffer;
  1563. if (!tbl_data || !table->maxlen || !*lenp ||
  1564. (*ppos && !write)) {
  1565. *lenp = 0;
  1566. return 0;
  1567. }
  1568. i = (int *) tbl_data;
  1569. vleft = table->maxlen / sizeof(*i);
  1570. left = *lenp;
  1571. if (!conv)
  1572. conv = do_proc_dointvec_conv;
  1573. for (; left && vleft--; i++, first=0) {
  1574. if (write) {
  1575. while (left) {
  1576. char c;
  1577. if (get_user(c, s))
  1578. return -EFAULT;
  1579. if (!isspace(c))
  1580. break;
  1581. left--;
  1582. s++;
  1583. }
  1584. if (!left)
  1585. break;
  1586. neg = 0;
  1587. len = left;
  1588. if (len > sizeof(buf) - 1)
  1589. len = sizeof(buf) - 1;
  1590. if (copy_from_user(buf, s, len))
  1591. return -EFAULT;
  1592. buf[len] = 0;
  1593. p = buf;
  1594. if (*p == '-' && left > 1) {
  1595. neg = 1;
  1596. p++;
  1597. }
  1598. if (*p < '0' || *p > '9')
  1599. break;
  1600. lval = simple_strtoul(p, &p, 0);
  1601. len = p-buf;
  1602. if ((len < left) && *p && !isspace(*p))
  1603. break;
  1604. if (neg)
  1605. val = -val;
  1606. s += len;
  1607. left -= len;
  1608. if (conv(&neg, &lval, i, 1, data))
  1609. break;
  1610. } else {
  1611. p = buf;
  1612. if (!first)
  1613. *p++ = '\t';
  1614. if (conv(&neg, &lval, i, 0, data))
  1615. break;
  1616. sprintf(p, "%s%lu", neg ? "-" : "", lval);
  1617. len = strlen(buf);
  1618. if (len > left)
  1619. len = left;
  1620. if(copy_to_user(s, buf, len))
  1621. return -EFAULT;
  1622. left -= len;
  1623. s += len;
  1624. }
  1625. }
  1626. if (!write && !first && left) {
  1627. if(put_user('\n', s))
  1628. return -EFAULT;
  1629. left--, s++;
  1630. }
  1631. if (write) {
  1632. while (left) {
  1633. char c;
  1634. if (get_user(c, s++))
  1635. return -EFAULT;
  1636. if (!isspace(c))
  1637. break;
  1638. left--;
  1639. }
  1640. }
  1641. if (write && first)
  1642. return -EINVAL;
  1643. *lenp -= left;
  1644. *ppos += *lenp;
  1645. return 0;
  1646. #undef TMPBUFLEN
  1647. }
  1648. static int do_proc_dointvec(ctl_table *table, int write, struct file *filp,
  1649. void __user *buffer, size_t *lenp, loff_t *ppos,
  1650. int (*conv)(int *negp, unsigned long *lvalp, int *valp,
  1651. int write, void *data),
  1652. void *data)
  1653. {
  1654. return __do_proc_dointvec(table->data, table, write, filp,
  1655. buffer, lenp, ppos, conv, data);
  1656. }
  1657. /**
  1658. * proc_dointvec - read a vector of integers
  1659. * @table: the sysctl table
  1660. * @write: %TRUE if this is a write to the sysctl file
  1661. * @filp: the file structure
  1662. * @buffer: the user buffer
  1663. * @lenp: the size of the user buffer
  1664. * @ppos: file position
  1665. *
  1666. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  1667. * values from/to the user buffer, treated as an ASCII string.
  1668. *
  1669. * Returns 0 on success.
  1670. */
  1671. int proc_dointvec(ctl_table *table, int write, struct file *filp,
  1672. void __user *buffer, size_t *lenp, loff_t *ppos)
  1673. {
  1674. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1675. NULL,NULL);
  1676. }
  1677. #define OP_SET 0
  1678. #define OP_AND 1
  1679. #define OP_OR 2
  1680. static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
  1681. int *valp,
  1682. int write, void *data)
  1683. {
  1684. int op = *(int *)data;
  1685. if (write) {
  1686. int val = *negp ? -*lvalp : *lvalp;
  1687. switch(op) {
  1688. case OP_SET: *valp = val; break;
  1689. case OP_AND: *valp &= val; break;
  1690. case OP_OR: *valp |= val; break;
  1691. }
  1692. } else {
  1693. int val = *valp;
  1694. if (val < 0) {
  1695. *negp = -1;
  1696. *lvalp = (unsigned long)-val;
  1697. } else {
  1698. *negp = 0;
  1699. *lvalp = (unsigned long)val;
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. /*
  1705. * init may raise the set.
  1706. */
  1707. int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
  1708. void __user *buffer, size_t *lenp, loff_t *ppos)
  1709. {
  1710. int op;
  1711. if (write && !capable(CAP_SYS_MODULE)) {
  1712. return -EPERM;
  1713. }
  1714. op = is_init(current) ? OP_SET : OP_AND;
  1715. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1716. do_proc_dointvec_bset_conv,&op);
  1717. }
  1718. /*
  1719. * Taint values can only be increased
  1720. */
  1721. static int proc_dointvec_taint(ctl_table *table, int write, struct file *filp,
  1722. void __user *buffer, size_t *lenp, loff_t *ppos)
  1723. {
  1724. int op;
  1725. if (!capable(CAP_SYS_ADMIN))
  1726. return -EPERM;
  1727. op = OP_OR;
  1728. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  1729. do_proc_dointvec_bset_conv,&op);
  1730. }
  1731. struct do_proc_dointvec_minmax_conv_param {
  1732. int *min;
  1733. int *max;
  1734. };
  1735. static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp,
  1736. int *valp,
  1737. int write, void *data)
  1738. {
  1739. struct do_proc_dointvec_minmax_conv_param *param = data;
  1740. if (write) {
  1741. int val = *negp ? -*lvalp : *lvalp;
  1742. if ((param->min && *param->min > val) ||
  1743. (param->max && *param->max < val))
  1744. return -EINVAL;
  1745. *valp = val;
  1746. } else {
  1747. int val = *valp;
  1748. if (val < 0) {
  1749. *negp = -1;
  1750. *lvalp = (unsigned long)-val;
  1751. } else {
  1752. *negp = 0;
  1753. *lvalp = (unsigned long)val;
  1754. }
  1755. }
  1756. return 0;
  1757. }
  1758. /**
  1759. * proc_dointvec_minmax - read a vector of integers with min/max values
  1760. * @table: the sysctl table
  1761. * @write: %TRUE if this is a write to the sysctl file
  1762. * @filp: the file structure
  1763. * @buffer: the user buffer
  1764. * @lenp: the size of the user buffer
  1765. * @ppos: file position
  1766. *
  1767. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  1768. * values from/to the user buffer, treated as an ASCII string.
  1769. *
  1770. * This routine will ensure the values are within the range specified by
  1771. * table->extra1 (min) and table->extra2 (max).
  1772. *
  1773. * Returns 0 on success.
  1774. */
  1775. int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
  1776. void __user *buffer, size_t *lenp, loff_t *ppos)
  1777. {
  1778. struct do_proc_dointvec_minmax_conv_param param = {
  1779. .min = (int *) table->extra1,
  1780. .max = (int *) table->extra2,
  1781. };
  1782. return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
  1783. do_proc_dointvec_minmax_conv, &param);
  1784. }
  1785. static int __do_proc_doulongvec_minmax(void *data, ctl_table *table, int write,
  1786. struct file *filp,
  1787. void __user *buffer,
  1788. size_t *lenp, loff_t *ppos,
  1789. unsigned long convmul,
  1790. unsigned long convdiv)
  1791. {
  1792. #define TMPBUFLEN 21
  1793. unsigned long *i, *min, *max, val;
  1794. int vleft, first=1, neg;
  1795. size_t len, left;
  1796. char buf[TMPBUFLEN], *p;
  1797. char __user *s = buffer;
  1798. if (!data || !table->maxlen || !*lenp ||
  1799. (*ppos && !write)) {
  1800. *lenp = 0;
  1801. return 0;
  1802. }
  1803. i = (unsigned long *) data;
  1804. min = (unsigned long *) table->extra1;
  1805. max = (unsigned long *) table->extra2;
  1806. vleft = table->maxlen / sizeof(unsigned long);
  1807. left = *lenp;
  1808. for (; left && vleft--; i++, min++, max++, first=0) {
  1809. if (write) {
  1810. while (left) {
  1811. char c;
  1812. if (get_user(c, s))
  1813. return -EFAULT;
  1814. if (!isspace(c))
  1815. break;
  1816. left--;
  1817. s++;
  1818. }
  1819. if (!left)
  1820. break;
  1821. neg = 0;
  1822. len = left;
  1823. if (len > TMPBUFLEN-1)
  1824. len = TMPBUFLEN-1;
  1825. if (copy_from_user(buf, s, len))
  1826. return -EFAULT;
  1827. buf[len] = 0;
  1828. p = buf;
  1829. if (*p == '-' && left > 1) {
  1830. neg = 1;
  1831. p++;
  1832. }
  1833. if (*p < '0' || *p > '9')
  1834. break;
  1835. val = simple_strtoul(p, &p, 0) * convmul / convdiv ;
  1836. len = p-buf;
  1837. if ((len < left) && *p && !isspace(*p))
  1838. break;
  1839. if (neg)
  1840. val = -val;
  1841. s += len;
  1842. left -= len;
  1843. if(neg)
  1844. continue;
  1845. if ((min && val < *min) || (max && val > *max))
  1846. continue;
  1847. *i = val;
  1848. } else {
  1849. p = buf;
  1850. if (!first)
  1851. *p++ = '\t';
  1852. sprintf(p, "%lu", convdiv * (*i) / convmul);
  1853. len = strlen(buf);
  1854. if (len > left)
  1855. len = left;
  1856. if(copy_to_user(s, buf, len))
  1857. return -EFAULT;
  1858. left -= len;
  1859. s += len;
  1860. }
  1861. }
  1862. if (!write && !first && left) {
  1863. if(put_user('\n', s))
  1864. return -EFAULT;
  1865. left--, s++;
  1866. }
  1867. if (write) {
  1868. while (left) {
  1869. char c;
  1870. if (get_user(c, s++))
  1871. return -EFAULT;
  1872. if (!isspace(c))
  1873. break;
  1874. left--;
  1875. }
  1876. }
  1877. if (write && first)
  1878. return -EINVAL;
  1879. *lenp -= left;
  1880. *ppos += *lenp;
  1881. return 0;
  1882. #undef TMPBUFLEN
  1883. }
  1884. static int do_proc_doulongvec_minmax(ctl_table *table, int write,
  1885. struct file *filp,
  1886. void __user *buffer,
  1887. size_t *lenp, loff_t *ppos,
  1888. unsigned long convmul,
  1889. unsigned long convdiv)
  1890. {
  1891. return __do_proc_doulongvec_minmax(table->data, table, write,
  1892. filp, buffer, lenp, ppos, convmul, convdiv);
  1893. }
  1894. /**
  1895. * proc_doulongvec_minmax - read a vector of long integers with min/max values
  1896. * @table: the sysctl table
  1897. * @write: %TRUE if this is a write to the sysctl file
  1898. * @filp: the file structure
  1899. * @buffer: the user buffer
  1900. * @lenp: the size of the user buffer
  1901. * @ppos: file position
  1902. *
  1903. * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
  1904. * values from/to the user buffer, treated as an ASCII string.
  1905. *
  1906. * This routine will ensure the values are within the range specified by
  1907. * table->extra1 (min) and table->extra2 (max).
  1908. *
  1909. * Returns 0 on success.
  1910. */
  1911. int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
  1912. void __user *buffer, size_t *lenp, loff_t *ppos)
  1913. {
  1914. return do_proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos, 1l, 1l);
  1915. }
  1916. /**
  1917. * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
  1918. * @table: the sysctl table
  1919. * @write: %TRUE if this is a write to the sysctl file
  1920. * @filp: the file structure
  1921. * @buffer: the user buffer
  1922. * @lenp: the size of the user buffer
  1923. * @ppos: file position
  1924. *
  1925. * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
  1926. * values from/to the user buffer, treated as an ASCII string. The values
  1927. * are treated as milliseconds, and converted to jiffies when they are stored.
  1928. *
  1929. * This routine will ensure the values are within the range specified by
  1930. * table->extra1 (min) and table->extra2 (max).
  1931. *
  1932. * Returns 0 on success.
  1933. */
  1934. int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
  1935. struct file *filp,
  1936. void __user *buffer,
  1937. size_t *lenp, loff_t *ppos)
  1938. {
  1939. return do_proc_doulongvec_minmax(table, write, filp, buffer,
  1940. lenp, ppos, HZ, 1000l);
  1941. }
  1942. static int do_proc_dointvec_jiffies_conv(int *negp, unsigned long *lvalp,
  1943. int *valp,
  1944. int write, void *data)
  1945. {
  1946. if (write) {
  1947. if (*lvalp > LONG_MAX / HZ)
  1948. return 1;
  1949. *valp = *negp ? -(*lvalp*HZ) : (*lvalp*HZ);
  1950. } else {
  1951. int val = *valp;
  1952. unsigned long lval;
  1953. if (val < 0) {
  1954. *negp = -1;
  1955. lval = (unsigned long)-val;
  1956. } else {
  1957. *negp = 0;
  1958. lval = (unsigned long)val;
  1959. }
  1960. *lvalp = lval / HZ;
  1961. }
  1962. return 0;
  1963. }
  1964. static int do_proc_dointvec_userhz_jiffies_conv(int *negp, unsigned long *lvalp,
  1965. int *valp,
  1966. int write, void *data)
  1967. {
  1968. if (write) {
  1969. if (USER_HZ < HZ && *lvalp > (LONG_MAX / HZ) * USER_HZ)
  1970. return 1;
  1971. *valp = clock_t_to_jiffies(*negp ? -*lvalp : *lvalp);
  1972. } else {
  1973. int val = *valp;
  1974. unsigned long lval;
  1975. if (val < 0) {
  1976. *negp = -1;
  1977. lval = (unsigned long)-val;
  1978. } else {
  1979. *negp = 0;
  1980. lval = (unsigned long)val;
  1981. }
  1982. *lvalp = jiffies_to_clock_t(lval);
  1983. }
  1984. return 0;
  1985. }
  1986. static int do_proc_dointvec_ms_jiffies_conv(int *negp, unsigned long *lvalp,
  1987. int *valp,
  1988. int write, void *data)
  1989. {
  1990. if (write) {
  1991. *valp = msecs_to_jiffies(*negp ? -*lvalp : *lvalp);
  1992. } else {
  1993. int val = *valp;
  1994. unsigned long lval;
  1995. if (val < 0) {
  1996. *negp = -1;
  1997. lval = (unsigned long)-val;
  1998. } else {
  1999. *negp = 0;
  2000. lval = (unsigned long)val;
  2001. }
  2002. *lvalp = jiffies_to_msecs(lval);
  2003. }
  2004. return 0;
  2005. }
  2006. /**
  2007. * proc_dointvec_jiffies - read a vector of integers as seconds
  2008. * @table: the sysctl table
  2009. * @write: %TRUE if this is a write to the sysctl file
  2010. * @filp: the file structure
  2011. * @buffer: the user buffer
  2012. * @lenp: the size of the user buffer
  2013. * @ppos: file position
  2014. *
  2015. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2016. * values from/to the user buffer, treated as an ASCII string.
  2017. * The values read are assumed to be in seconds, and are converted into
  2018. * jiffies.
  2019. *
  2020. * Returns 0 on success.
  2021. */
  2022. int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
  2023. void __user *buffer, size_t *lenp, loff_t *ppos)
  2024. {
  2025. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  2026. do_proc_dointvec_jiffies_conv,NULL);
  2027. }
  2028. /**
  2029. * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
  2030. * @table: the sysctl table
  2031. * @write: %TRUE if this is a write to the sysctl file
  2032. * @filp: the file structure
  2033. * @buffer: the user buffer
  2034. * @lenp: the size of the user buffer
  2035. * @ppos: pointer to the file position
  2036. *
  2037. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2038. * values from/to the user buffer, treated as an ASCII string.
  2039. * The values read are assumed to be in 1/USER_HZ seconds, and
  2040. * are converted into jiffies.
  2041. *
  2042. * Returns 0 on success.
  2043. */
  2044. int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
  2045. void __user *buffer, size_t *lenp, loff_t *ppos)
  2046. {
  2047. return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
  2048. do_proc_dointvec_userhz_jiffies_conv,NULL);
  2049. }
  2050. /**
  2051. * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
  2052. * @table: the sysctl table
  2053. * @write: %TRUE if this is a write to the sysctl file
  2054. * @filp: the file structure
  2055. * @buffer: the user buffer
  2056. * @lenp: the size of the user buffer
  2057. * @ppos: file position
  2058. * @ppos: the current position in the file
  2059. *
  2060. * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
  2061. * values from/to the user buffer, treated as an ASCII string.
  2062. * The values read are assumed to be in 1/1000 seconds, and
  2063. * are converted into jiffies.
  2064. *
  2065. * Returns 0 on success.
  2066. */
  2067. int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
  2068. void __user *buffer, size_t *lenp, loff_t *ppos)
  2069. {
  2070. return do_proc_dointvec(table, write, filp, buffer, lenp, ppos,
  2071. do_proc_dointvec_ms_jiffies_conv, NULL);
  2072. }
  2073. static int proc_do_cad_pid(ctl_table *table, int write, struct file *filp,
  2074. void __user *buffer, size_t *lenp, loff_t *ppos)
  2075. {
  2076. struct pid *new_pid;
  2077. pid_t tmp;
  2078. int r;
  2079. tmp = pid_nr(cad_pid);
  2080. r = __do_proc_dointvec(&tmp, table, write, filp, buffer,
  2081. lenp, ppos, NULL, NULL);
  2082. if (r || !write)
  2083. return r;
  2084. new_pid = find_get_pid(tmp);
  2085. if (!new_pid)
  2086. return -ESRCH;
  2087. put_pid(xchg(&cad_pid, new_pid));
  2088. return 0;
  2089. }
  2090. #else /* CONFIG_PROC_FS */
  2091. int proc_dostring(ctl_table *table, int write, struct file *filp,
  2092. void __user *buffer, size_t *lenp, loff_t *ppos)
  2093. {
  2094. return -ENOSYS;
  2095. }
  2096. int proc_dointvec(ctl_table *table, int write, struct file *filp,
  2097. void __user *buffer, size_t *lenp, loff_t *ppos)
  2098. {
  2099. return -ENOSYS;
  2100. }
  2101. int proc_dointvec_bset(ctl_table *table, int write, struct file *filp,
  2102. void __user *buffer, size_t *lenp, loff_t *ppos)
  2103. {
  2104. return -ENOSYS;
  2105. }
  2106. int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp,
  2107. void __user *buffer, size_t *lenp, loff_t *ppos)
  2108. {
  2109. return -ENOSYS;
  2110. }
  2111. int proc_dointvec_jiffies(ctl_table *table, int write, struct file *filp,
  2112. void __user *buffer, size_t *lenp, loff_t *ppos)
  2113. {
  2114. return -ENOSYS;
  2115. }
  2116. int proc_dointvec_userhz_jiffies(ctl_table *table, int write, struct file *filp,
  2117. void __user *buffer, size_t *lenp, loff_t *ppos)
  2118. {
  2119. return -ENOSYS;
  2120. }
  2121. int proc_dointvec_ms_jiffies(ctl_table *table, int write, struct file *filp,
  2122. void __user *buffer, size_t *lenp, loff_t *ppos)
  2123. {
  2124. return -ENOSYS;
  2125. }
  2126. int proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp,
  2127. void __user *buffer, size_t *lenp, loff_t *ppos)
  2128. {
  2129. return -ENOSYS;
  2130. }
  2131. int proc_doulongvec_ms_jiffies_minmax(ctl_table *table, int write,
  2132. struct file *filp,
  2133. void __user *buffer,
  2134. size_t *lenp, loff_t *ppos)
  2135. {
  2136. return -ENOSYS;
  2137. }
  2138. #endif /* CONFIG_PROC_FS */
  2139. #ifdef CONFIG_SYSCTL_SYSCALL
  2140. /*
  2141. * General sysctl support routines
  2142. */
  2143. /* The generic string strategy routine: */
  2144. int sysctl_string(ctl_table *table, int __user *name, int nlen,
  2145. void __user *oldval, size_t __user *oldlenp,
  2146. void __user *newval, size_t newlen)
  2147. {
  2148. if (!table->data || !table->maxlen)
  2149. return -ENOTDIR;
  2150. if (oldval && oldlenp) {
  2151. size_t bufsize;
  2152. if (get_user(bufsize, oldlenp))
  2153. return -EFAULT;
  2154. if (bufsize) {
  2155. size_t len = strlen(table->data), copied;
  2156. /* This shouldn't trigger for a well-formed sysctl */
  2157. if (len > table->maxlen)
  2158. len = table->maxlen;
  2159. /* Copy up to a max of bufsize-1 bytes of the string */
  2160. copied = (len >= bufsize) ? bufsize - 1 : len;
  2161. if (copy_to_user(oldval, table->data, copied) ||
  2162. put_user(0, (char __user *)(oldval + copied)))
  2163. return -EFAULT;
  2164. if (put_user(len, oldlenp))
  2165. return -EFAULT;
  2166. }
  2167. }
  2168. if (newval && newlen) {
  2169. size_t len = newlen;
  2170. if (len > table->maxlen)
  2171. len = table->maxlen;
  2172. if(copy_from_user(table->data, newval, len))
  2173. return -EFAULT;
  2174. if (len == table->maxlen)
  2175. len--;
  2176. ((char *) table->data)[len] = 0;
  2177. }
  2178. return 1;
  2179. }
  2180. /*
  2181. * This function makes sure that all of the integers in the vector
  2182. * are between the minimum and maximum values given in the arrays
  2183. * table->extra1 and table->extra2, respectively.
  2184. */
  2185. int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
  2186. void __user *oldval, size_t __user *oldlenp,
  2187. void __user *newval, size_t newlen)
  2188. {
  2189. if (newval && newlen) {
  2190. int __user *vec = (int __user *) newval;
  2191. int *min = (int *) table->extra1;
  2192. int *max = (int *) table->extra2;
  2193. size_t length;
  2194. int i;
  2195. if (newlen % sizeof(int) != 0)
  2196. return -EINVAL;
  2197. if (!table->extra1 && !table->extra2)
  2198. return 0;
  2199. if (newlen > table->maxlen)
  2200. newlen = table->maxlen;
  2201. length = newlen / sizeof(int);
  2202. for (i = 0; i < length; i++) {
  2203. int value;
  2204. if (get_user(value, vec + i))
  2205. return -EFAULT;
  2206. if (min && value < min[i])
  2207. return -EINVAL;
  2208. if (max && value > max[i])
  2209. return -EINVAL;
  2210. }
  2211. }
  2212. return 0;
  2213. }
  2214. /* Strategy function to convert jiffies to seconds */
  2215. int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
  2216. void __user *oldval, size_t __user *oldlenp,
  2217. void __user *newval, size_t newlen)
  2218. {
  2219. if (oldval && oldlenp) {
  2220. size_t olen;
  2221. if (get_user(olen, oldlenp))
  2222. return -EFAULT;
  2223. if (olen) {
  2224. int val;
  2225. if (olen < sizeof(int))
  2226. return -EINVAL;
  2227. val = *(int *)(table->data) / HZ;
  2228. if (put_user(val, (int __user *)oldval))
  2229. return -EFAULT;
  2230. if (put_user(sizeof(int), oldlenp))
  2231. return -EFAULT;
  2232. }
  2233. }
  2234. if (newval && newlen) {
  2235. int new;
  2236. if (newlen != sizeof(int))
  2237. return -EINVAL;
  2238. if (get_user(new, (int __user *)newval))
  2239. return -EFAULT;
  2240. *(int *)(table->data) = new*HZ;
  2241. }
  2242. return 1;
  2243. }
  2244. /* Strategy function to convert jiffies to seconds */
  2245. int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
  2246. void __user *oldval, size_t __user *oldlenp,
  2247. void __user *newval, size_t newlen)
  2248. {
  2249. if (oldval && oldlenp) {
  2250. size_t olen;
  2251. if (get_user(olen, oldlenp))
  2252. return -EFAULT;
  2253. if (olen) {
  2254. int val;
  2255. if (olen < sizeof(int))
  2256. return -EINVAL;
  2257. val = jiffies_to_msecs(*(int *)(table->data));
  2258. if (put_user(val, (int __user *)oldval))
  2259. return -EFAULT;
  2260. if (put_user(sizeof(int), oldlenp))
  2261. return -EFAULT;
  2262. }
  2263. }
  2264. if (newval && newlen) {
  2265. int new;
  2266. if (newlen != sizeof(int))
  2267. return -EINVAL;
  2268. if (get_user(new, (int __user *)newval))
  2269. return -EFAULT;
  2270. *(int *)(table->data) = msecs_to_jiffies(new);
  2271. }
  2272. return 1;
  2273. }
  2274. #else /* CONFIG_SYSCTL_SYSCALL */
  2275. asmlinkage long sys_sysctl(struct __sysctl_args __user *args)
  2276. {
  2277. static int msg_count;
  2278. struct __sysctl_args tmp;
  2279. int name[CTL_MAXNAME];
  2280. int i;
  2281. /* Read in the sysctl name for better debug message logging */
  2282. if (copy_from_user(&tmp, args, sizeof(tmp)))
  2283. return -EFAULT;
  2284. if (tmp.nlen <= 0 || tmp.nlen >= CTL_MAXNAME)
  2285. return -ENOTDIR;
  2286. for (i = 0; i < tmp.nlen; i++)
  2287. if (get_user(name[i], tmp.name + i))
  2288. return -EFAULT;
  2289. /* Ignore accesses to kernel.version */
  2290. if ((tmp.nlen == 2) && (name[0] == CTL_KERN) && (name[1] == KERN_VERSION))
  2291. goto out;
  2292. if (msg_count < 5) {
  2293. msg_count++;
  2294. printk(KERN_INFO
  2295. "warning: process `%s' used the removed sysctl "
  2296. "system call with ", current->comm);
  2297. for (i = 0; i < tmp.nlen; i++)
  2298. printk("%d.", name[i]);
  2299. printk("\n");
  2300. }
  2301. out:
  2302. return -ENOSYS;
  2303. }
  2304. int sysctl_string(ctl_table *table, int __user *name, int nlen,
  2305. void __user *oldval, size_t __user *oldlenp,
  2306. void __user *newval, size_t newlen)
  2307. {
  2308. return -ENOSYS;
  2309. }
  2310. int sysctl_intvec(ctl_table *table, int __user *name, int nlen,
  2311. void __user *oldval, size_t __user *oldlenp,
  2312. void __user *newval, size_t newlen)
  2313. {
  2314. return -ENOSYS;
  2315. }
  2316. int sysctl_jiffies(ctl_table *table, int __user *name, int nlen,
  2317. void __user *oldval, size_t __user *oldlenp,
  2318. void __user *newval, size_t newlen)
  2319. {
  2320. return -ENOSYS;
  2321. }
  2322. int sysctl_ms_jiffies(ctl_table *table, int __user *name, int nlen,
  2323. void __user *oldval, size_t __user *oldlenp,
  2324. void __user *newval, size_t newlen)
  2325. {
  2326. return -ENOSYS;
  2327. }
  2328. #endif /* CONFIG_SYSCTL_SYSCALL */
  2329. /*
  2330. * No sense putting this after each symbol definition, twice,
  2331. * exception granted :-)
  2332. */
  2333. EXPORT_SYMBOL(proc_dointvec);
  2334. EXPORT_SYMBOL(proc_dointvec_jiffies);
  2335. EXPORT_SYMBOL(proc_dointvec_minmax);
  2336. EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
  2337. EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
  2338. EXPORT_SYMBOL(proc_dostring);
  2339. EXPORT_SYMBOL(proc_doulongvec_minmax);
  2340. EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
  2341. EXPORT_SYMBOL(register_sysctl_table);
  2342. EXPORT_SYMBOL(sysctl_intvec);
  2343. EXPORT_SYMBOL(sysctl_jiffies);
  2344. EXPORT_SYMBOL(sysctl_ms_jiffies);
  2345. EXPORT_SYMBOL(sysctl_string);
  2346. EXPORT_SYMBOL(unregister_sysctl_table);