sysctl.c 58 KB

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