sysctl.c 59 KB

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