sysctl.c 54 KB

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