sysctl.c 59 KB

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