sysctl.c 59 KB

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