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