Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 |
| 3 | * |
| 4 | * Author: Eric Biederman <ebiederm@xmision.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2 of the |
| 9 | * License. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/uts.h> |
| 14 | #include <linux/utsname.h> |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 15 | #include <linux/sysctl.h> |
| 16 | |
| 17 | static void *get_uts(ctl_table *table, int write) |
| 18 | { |
| 19 | char *which = table->data; |
Pavel Emelyanov | 32df81c | 2007-11-28 16:21:38 -0800 | [diff] [blame] | 20 | struct uts_namespace *uts_ns; |
| 21 | |
| 22 | uts_ns = current->nsproxy->uts_ns; |
| 23 | which = (which - (char *)&init_uts_ns) + (char *)uts_ns; |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 24 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 25 | if (!write) |
| 26 | down_read(&uts_sem); |
| 27 | else |
| 28 | down_write(&uts_sem); |
| 29 | return which; |
| 30 | } |
| 31 | |
| 32 | static void put_uts(ctl_table *table, int write, void *which) |
| 33 | { |
| 34 | if (!write) |
| 35 | up_read(&uts_sem); |
| 36 | else |
| 37 | up_write(&uts_sem); |
| 38 | } |
| 39 | |
| 40 | #ifdef CONFIG_PROC_FS |
| 41 | /* |
| 42 | * Special case of dostring for the UTS structure. This has locks |
| 43 | * to observe. Should this be in kernel/sys.c ???? |
| 44 | */ |
| 45 | static int proc_do_uts_string(ctl_table *table, int write, struct file *filp, |
| 46 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 47 | { |
| 48 | struct ctl_table uts_table; |
| 49 | int r; |
| 50 | memcpy(&uts_table, table, sizeof(uts_table)); |
| 51 | uts_table.data = get_uts(table, write); |
| 52 | r = proc_dostring(&uts_table,write,filp,buffer,lenp, ppos); |
| 53 | put_uts(table, write, uts_table.data); |
| 54 | return r; |
| 55 | } |
| 56 | #else |
| 57 | #define proc_do_uts_string NULL |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | #ifdef CONFIG_SYSCTL_SYSCALL |
| 62 | /* The generic string strategy routine: */ |
| 63 | static int sysctl_uts_string(ctl_table *table, int __user *name, int nlen, |
| 64 | void __user *oldval, size_t __user *oldlenp, |
| 65 | void __user *newval, size_t newlen) |
| 66 | { |
| 67 | struct ctl_table uts_table; |
| 68 | int r, write; |
| 69 | write = newval && newlen; |
| 70 | memcpy(&uts_table, table, sizeof(uts_table)); |
| 71 | uts_table.data = get_uts(table, write); |
| 72 | r = sysctl_string(&uts_table, name, nlen, |
| 73 | oldval, oldlenp, newval, newlen); |
| 74 | put_uts(table, write, uts_table.data); |
| 75 | return r; |
| 76 | } |
| 77 | #else |
| 78 | #define sysctl_uts_string NULL |
| 79 | #endif |
| 80 | |
| 81 | static struct ctl_table uts_kern_table[] = { |
| 82 | { |
| 83 | .ctl_name = KERN_OSTYPE, |
| 84 | .procname = "ostype", |
| 85 | .data = init_uts_ns.name.sysname, |
| 86 | .maxlen = sizeof(init_uts_ns.name.sysname), |
| 87 | .mode = 0444, |
| 88 | .proc_handler = proc_do_uts_string, |
| 89 | .strategy = sysctl_uts_string, |
| 90 | }, |
| 91 | { |
| 92 | .ctl_name = KERN_OSRELEASE, |
| 93 | .procname = "osrelease", |
| 94 | .data = init_uts_ns.name.release, |
| 95 | .maxlen = sizeof(init_uts_ns.name.release), |
| 96 | .mode = 0444, |
| 97 | .proc_handler = proc_do_uts_string, |
| 98 | .strategy = sysctl_uts_string, |
| 99 | }, |
| 100 | { |
| 101 | .ctl_name = KERN_VERSION, |
| 102 | .procname = "version", |
| 103 | .data = init_uts_ns.name.version, |
| 104 | .maxlen = sizeof(init_uts_ns.name.version), |
| 105 | .mode = 0444, |
| 106 | .proc_handler = proc_do_uts_string, |
| 107 | .strategy = sysctl_uts_string, |
| 108 | }, |
| 109 | { |
| 110 | .ctl_name = KERN_NODENAME, |
| 111 | .procname = "hostname", |
| 112 | .data = init_uts_ns.name.nodename, |
| 113 | .maxlen = sizeof(init_uts_ns.name.nodename), |
| 114 | .mode = 0644, |
| 115 | .proc_handler = proc_do_uts_string, |
| 116 | .strategy = sysctl_uts_string, |
| 117 | }, |
| 118 | { |
| 119 | .ctl_name = KERN_DOMAINNAME, |
| 120 | .procname = "domainname", |
| 121 | .data = init_uts_ns.name.domainname, |
| 122 | .maxlen = sizeof(init_uts_ns.name.domainname), |
| 123 | .mode = 0644, |
| 124 | .proc_handler = proc_do_uts_string, |
| 125 | .strategy = sysctl_uts_string, |
| 126 | }, |
| 127 | {} |
| 128 | }; |
| 129 | |
| 130 | static struct ctl_table uts_root_table[] = { |
| 131 | { |
| 132 | .ctl_name = CTL_KERN, |
| 133 | .procname = "kernel", |
| 134 | .mode = 0555, |
| 135 | .child = uts_kern_table, |
| 136 | }, |
| 137 | {} |
| 138 | }; |
| 139 | |
| 140 | static int __init utsname_sysctl_init(void) |
| 141 | { |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 142 | register_sysctl_table(uts_root_table); |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | __initcall(utsname_sysctl_init); |