sysctl.c 59 KB

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