sysctl.c 58 KB

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