sysctl.c 58 KB

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