sysctl.c 57 KB

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