sysctl.c 55 KB

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