Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * /proc/sys support |
| 3 | */ |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 4 | #include <linux/init.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 5 | #include <linux/sysctl.h> |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 6 | #include <linux/poll.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 7 | #include <linux/proc_fs.h> |
| 8 | #include <linux/security.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 11 | #include "internal.h" |
| 12 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 13 | static const struct dentry_operations proc_sys_dentry_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 14 | static const struct file_operations proc_sys_file_operations; |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 15 | static const struct inode_operations proc_sys_inode_operations; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 16 | static const struct file_operations proc_sys_dir_file_operations; |
| 17 | static const struct inode_operations proc_sys_dir_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 18 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 19 | void proc_sys_poll_notify(struct ctl_table_poll *poll) |
| 20 | { |
| 21 | if (!poll) |
| 22 | return; |
| 23 | |
| 24 | atomic_inc(&poll->event); |
| 25 | wake_up_interruptible(&poll->wait); |
| 26 | } |
| 27 | |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 28 | static struct ctl_table root_table[] = { |
| 29 | { |
| 30 | .procname = "", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 31 | .mode = S_IFDIR|S_IRUGO|S_IXUGO, |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 32 | }, |
| 33 | { } |
| 34 | }; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 35 | static struct ctl_table_root sysctl_table_root = { |
| 36 | .default_set.list = LIST_HEAD_INIT(sysctl_table_root.default_set.dir.header.ctl_entry), |
| 37 | .default_set.dir.header = { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 38 | {{.count = 1, |
| 39 | .nreg = 1, |
| 40 | .ctl_table = root_table, |
| 41 | .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}}, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 42 | .ctl_table_arg = root_table, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 43 | .root = &sysctl_table_root, |
| 44 | .set = &sysctl_table_root.default_set, |
| 45 | }, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 46 | }; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 47 | |
| 48 | static DEFINE_SPINLOCK(sysctl_lock); |
| 49 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 50 | static void drop_sysctl_table(struct ctl_table_header *header); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 51 | static int sysctl_follow_link(struct ctl_table_header **phead, |
| 52 | struct ctl_table **pentry, struct nsproxy *namespaces); |
| 53 | static int insert_links(struct ctl_table_header *head); |
| 54 | static void put_links(struct ctl_table_header *header); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 55 | |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame] | 56 | static void sysctl_print_dir(struct ctl_dir *dir) |
| 57 | { |
| 58 | if (dir->header.parent) |
| 59 | sysctl_print_dir(dir->header.parent); |
| 60 | printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname); |
| 61 | } |
| 62 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 63 | static int namecmp(const char *name1, int len1, const char *name2, int len2) |
| 64 | { |
| 65 | int minlen; |
| 66 | int cmp; |
| 67 | |
| 68 | minlen = len1; |
| 69 | if (minlen > len2) |
| 70 | minlen = len2; |
| 71 | |
| 72 | cmp = memcmp(name1, name2, minlen); |
| 73 | if (cmp == 0) |
| 74 | cmp = len1 - len2; |
| 75 | return cmp; |
| 76 | } |
| 77 | |
| 78 | static struct ctl_table *find_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 79 | struct ctl_dir *dir, const char *name, int namelen) |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 80 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 81 | struct ctl_table_set *set = dir->header.set; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 82 | struct ctl_table_header *head; |
| 83 | struct ctl_table *entry; |
| 84 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 85 | list_for_each_entry(head, &set->list, ctl_entry) { |
| 86 | if (head->unregistering) |
| 87 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 88 | if (head->parent != dir) |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 89 | continue; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 90 | for (entry = head->ctl_table; entry->procname; entry++) { |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 91 | const char *procname = entry->procname; |
| 92 | if (namecmp(procname, strlen(procname), name, namelen) == 0) { |
| 93 | *phead = head; |
| 94 | return entry; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 101 | static void init_header(struct ctl_table_header *head, |
| 102 | struct ctl_table_root *root, struct ctl_table_set *set, |
| 103 | struct ctl_table *table) |
| 104 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 105 | head->ctl_table = table; |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 106 | head->ctl_table_arg = table; |
| 107 | INIT_LIST_HEAD(&head->ctl_entry); |
| 108 | head->used = 0; |
| 109 | head->count = 1; |
| 110 | head->nreg = 1; |
| 111 | head->unregistering = NULL; |
| 112 | head->root = root; |
| 113 | head->set = set; |
| 114 | head->parent = NULL; |
| 115 | } |
| 116 | |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 117 | static void erase_header(struct ctl_table_header *head) |
| 118 | { |
| 119 | list_del_init(&head->ctl_entry); |
| 120 | } |
| 121 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 122 | static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 123 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 124 | int err; |
| 125 | |
| 126 | dir->header.nreg++; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 127 | header->parent = dir; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 128 | err = insert_links(header); |
| 129 | if (err) |
| 130 | goto fail_links; |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 131 | list_add_tail(&header->ctl_entry, &header->set->list); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 132 | return 0; |
| 133 | fail_links: |
| 134 | header->parent = NULL; |
| 135 | drop_sysctl_table(&dir->header); |
| 136 | return err; |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 139 | /* called under sysctl_lock */ |
| 140 | static int use_table(struct ctl_table_header *p) |
| 141 | { |
| 142 | if (unlikely(p->unregistering)) |
| 143 | return 0; |
| 144 | p->used++; |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /* called under sysctl_lock */ |
| 149 | static void unuse_table(struct ctl_table_header *p) |
| 150 | { |
| 151 | if (!--p->used) |
| 152 | if (unlikely(p->unregistering)) |
| 153 | complete(p->unregistering); |
| 154 | } |
| 155 | |
| 156 | /* called under sysctl_lock, will reacquire if has to wait */ |
| 157 | static void start_unregistering(struct ctl_table_header *p) |
| 158 | { |
| 159 | /* |
| 160 | * if p->used is 0, nobody will ever touch that entry again; |
| 161 | * we'll eliminate all paths to it before dropping sysctl_lock |
| 162 | */ |
| 163 | if (unlikely(p->used)) { |
| 164 | struct completion wait; |
| 165 | init_completion(&wait); |
| 166 | p->unregistering = &wait; |
| 167 | spin_unlock(&sysctl_lock); |
| 168 | wait_for_completion(&wait); |
| 169 | spin_lock(&sysctl_lock); |
| 170 | } else { |
| 171 | /* anything non-NULL; we'll never dereference it */ |
| 172 | p->unregistering = ERR_PTR(-EINVAL); |
| 173 | } |
| 174 | /* |
| 175 | * do not remove from the list until nobody holds it; walking the |
| 176 | * list in do_sysctl() relies on that. |
| 177 | */ |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 178 | erase_header(p); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void sysctl_head_get(struct ctl_table_header *head) |
| 182 | { |
| 183 | spin_lock(&sysctl_lock); |
| 184 | head->count++; |
| 185 | spin_unlock(&sysctl_lock); |
| 186 | } |
| 187 | |
| 188 | void sysctl_head_put(struct ctl_table_header *head) |
| 189 | { |
| 190 | spin_lock(&sysctl_lock); |
| 191 | if (!--head->count) |
| 192 | kfree_rcu(head, rcu); |
| 193 | spin_unlock(&sysctl_lock); |
| 194 | } |
| 195 | |
| 196 | static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) |
| 197 | { |
| 198 | if (!head) |
| 199 | BUG(); |
| 200 | spin_lock(&sysctl_lock); |
| 201 | if (!use_table(head)) |
| 202 | head = ERR_PTR(-ENOENT); |
| 203 | spin_unlock(&sysctl_lock); |
| 204 | return head; |
| 205 | } |
| 206 | |
| 207 | static void sysctl_head_finish(struct ctl_table_header *head) |
| 208 | { |
| 209 | if (!head) |
| 210 | return; |
| 211 | spin_lock(&sysctl_lock); |
| 212 | unuse_table(head); |
| 213 | spin_unlock(&sysctl_lock); |
| 214 | } |
| 215 | |
| 216 | static struct ctl_table_set * |
| 217 | lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) |
| 218 | { |
| 219 | struct ctl_table_set *set = &root->default_set; |
| 220 | if (root->lookup) |
| 221 | set = root->lookup(root, namespaces); |
| 222 | return set; |
| 223 | } |
| 224 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 225 | static struct ctl_table *lookup_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 226 | struct ctl_dir *dir, |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 227 | const char *name, int namelen) |
| 228 | { |
| 229 | struct ctl_table_header *head; |
| 230 | struct ctl_table *entry; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 231 | |
| 232 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 233 | entry = find_entry(&head, dir, name, namelen); |
| 234 | if (entry && use_table(head)) |
| 235 | *phead = head; |
| 236 | else |
| 237 | entry = NULL; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 238 | spin_unlock(&sysctl_lock); |
| 239 | return entry; |
| 240 | } |
| 241 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 242 | static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 243 | struct list_head *tmp) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 244 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 245 | struct ctl_table_set *set = dir->header.set; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 246 | struct ctl_table_header *head; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 247 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 248 | for (tmp = tmp->next; tmp != &set->list; tmp = tmp->next) { |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 249 | head = list_entry(tmp, struct ctl_table_header, ctl_entry); |
| 250 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 251 | if (head->parent != dir || |
| 252 | !head->ctl_table->procname || |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 253 | !use_table(head)) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 254 | continue; |
| 255 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 256 | return head; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 257 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 258 | return NULL; |
| 259 | } |
| 260 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 261 | static void first_entry(struct ctl_dir *dir, |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 262 | struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 263 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 264 | struct ctl_table_header *head; |
| 265 | struct ctl_table *entry = NULL; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 266 | |
| 267 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 268 | head = next_usable_entry(dir, &dir->header.set->list); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 269 | spin_unlock(&sysctl_lock); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 270 | if (head) |
| 271 | entry = head->ctl_table; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 272 | *phead = head; |
| 273 | *pentry = entry; |
| 274 | } |
| 275 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 276 | static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 277 | { |
| 278 | struct ctl_table_header *head = *phead; |
| 279 | struct ctl_table *entry = *pentry; |
| 280 | |
| 281 | entry++; |
| 282 | if (!entry->procname) { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 283 | spin_lock(&sysctl_lock); |
| 284 | unuse_table(head); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 285 | head = next_usable_entry(head->parent, &head->ctl_entry); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 286 | spin_unlock(&sysctl_lock); |
| 287 | if (head) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 288 | entry = head->ctl_table; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 289 | } |
| 290 | *phead = head; |
| 291 | *pentry = entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void register_sysctl_root(struct ctl_table_root *root) |
| 295 | { |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
| 299 | * sysctl_perm does NOT grant the superuser all rights automatically, because |
| 300 | * some sysctl variables are readonly even to root. |
| 301 | */ |
| 302 | |
| 303 | static int test_perm(int mode, int op) |
| 304 | { |
| 305 | if (!current_euid()) |
| 306 | mode >>= 6; |
| 307 | else if (in_egroup_p(0)) |
| 308 | mode >>= 3; |
| 309 | if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) |
| 310 | return 0; |
| 311 | return -EACCES; |
| 312 | } |
| 313 | |
| 314 | static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) |
| 315 | { |
| 316 | int mode; |
| 317 | |
| 318 | if (root->permissions) |
| 319 | mode = root->permissions(root, current->nsproxy, table); |
| 320 | else |
| 321 | mode = table->mode; |
| 322 | |
| 323 | return test_perm(mode, op); |
| 324 | } |
| 325 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 326 | static struct inode *proc_sys_make_inode(struct super_block *sb, |
| 327 | struct ctl_table_header *head, struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 328 | { |
| 329 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 330 | struct proc_inode *ei; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 331 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 332 | inode = new_inode(sb); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 333 | if (!inode) |
| 334 | goto out; |
| 335 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 336 | inode->i_ino = get_next_ino(); |
| 337 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 338 | sysctl_head_get(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 339 | ei = PROC_I(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 340 | ei->sysctl = head; |
| 341 | ei->sysctl_entry = table; |
| 342 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 343 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 344 | inode->i_mode = table->mode; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 345 | if (!S_ISDIR(table->mode)) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 346 | inode->i_mode |= S_IFREG; |
| 347 | inode->i_op = &proc_sys_inode_operations; |
| 348 | inode->i_fop = &proc_sys_file_operations; |
| 349 | } else { |
| 350 | inode->i_mode |= S_IFDIR; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 351 | inode->i_op = &proc_sys_dir_operations; |
| 352 | inode->i_fop = &proc_sys_dir_file_operations; |
| 353 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 354 | out: |
| 355 | return inode; |
| 356 | } |
| 357 | |
Adrian Bunk | 8132436 | 2008-10-03 00:33:54 +0400 | [diff] [blame] | 358 | static struct ctl_table_header *grab_header(struct inode *inode) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 359 | { |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 360 | struct ctl_table_header *head = PROC_I(inode)->sysctl; |
| 361 | if (!head) |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 362 | head = &sysctl_table_root.default_set.dir.header; |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 363 | return sysctl_head_grab(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, |
| 367 | struct nameidata *nd) |
| 368 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 369 | struct ctl_table_header *head = grab_header(dir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 370 | struct ctl_table_header *h = NULL; |
| 371 | struct qstr *name = &dentry->d_name; |
| 372 | struct ctl_table *p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 373 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 374 | struct dentry *err = ERR_PTR(-ENOENT); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 375 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 376 | int ret; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 377 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 378 | if (IS_ERR(head)) |
| 379 | return ERR_CAST(head); |
| 380 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 381 | ctl_dir = container_of(head, struct ctl_dir, header); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 382 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 383 | p = lookup_entry(&h, ctl_dir, name->name, name->len); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 384 | if (!p) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 385 | goto out; |
| 386 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 387 | ret = sysctl_follow_link(&h, &p, current->nsproxy); |
| 388 | err = ERR_PTR(ret); |
| 389 | if (ret) |
| 390 | goto out; |
| 391 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 392 | err = ERR_PTR(-ENOMEM); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 393 | inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); |
| 394 | if (h) |
| 395 | sysctl_head_finish(h); |
| 396 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 397 | if (!inode) |
| 398 | goto out; |
| 399 | |
| 400 | err = NULL; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 401 | d_set_d_op(dentry, &proc_sys_dentry_operations); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 402 | d_add(dentry, inode); |
| 403 | |
| 404 | out: |
| 405 | sysctl_head_finish(head); |
| 406 | return err; |
| 407 | } |
| 408 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 409 | static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, |
| 410 | size_t count, loff_t *ppos, int write) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 411 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 412 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 413 | struct ctl_table_header *head = grab_header(inode); |
| 414 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
David Howells | 2a2da53 | 2007-10-25 15:27:40 +0100 | [diff] [blame] | 415 | ssize_t error; |
| 416 | size_t res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 417 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 418 | if (IS_ERR(head)) |
| 419 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * At this point we know that the sysctl was not unregistered |
| 423 | * and won't be until we finish. |
| 424 | */ |
| 425 | error = -EPERM; |
Pavel Emelyanov | d7321cd | 2008-04-29 01:02:44 -0700 | [diff] [blame] | 426 | if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 427 | goto out; |
| 428 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 429 | /* if that can happen at all, it should be -EINVAL, not -EISDIR */ |
| 430 | error = -EINVAL; |
| 431 | if (!table->proc_handler) |
| 432 | goto out; |
| 433 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 434 | /* careful: calling conventions are nasty here */ |
| 435 | res = count; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 436 | error = table->proc_handler(table, write, buf, &res, ppos); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 437 | if (!error) |
| 438 | error = res; |
| 439 | out: |
| 440 | sysctl_head_finish(head); |
| 441 | |
| 442 | return error; |
| 443 | } |
| 444 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 445 | static ssize_t proc_sys_read(struct file *filp, char __user *buf, |
| 446 | size_t count, loff_t *ppos) |
| 447 | { |
| 448 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); |
| 449 | } |
| 450 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 451 | static ssize_t proc_sys_write(struct file *filp, const char __user *buf, |
| 452 | size_t count, loff_t *ppos) |
| 453 | { |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 454 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 457 | static int proc_sys_open(struct inode *inode, struct file *filp) |
| 458 | { |
| 459 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 460 | |
| 461 | if (table->poll) |
| 462 | filp->private_data = proc_sys_poll_event(table->poll); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) |
| 468 | { |
| 469 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 470 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 471 | unsigned long event = (unsigned long)filp->private_data; |
| 472 | unsigned int ret = DEFAULT_POLLMASK; |
| 473 | |
| 474 | if (!table->proc_handler) |
| 475 | goto out; |
| 476 | |
| 477 | if (!table->poll) |
| 478 | goto out; |
| 479 | |
| 480 | poll_wait(filp, &table->poll->wait, wait); |
| 481 | |
| 482 | if (event != atomic_read(&table->poll->event)) { |
| 483 | filp->private_data = proc_sys_poll_event(table->poll); |
| 484 | ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; |
| 485 | } |
| 486 | |
| 487 | out: |
| 488 | return ret; |
| 489 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 490 | |
| 491 | static int proc_sys_fill_cache(struct file *filp, void *dirent, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 492 | filldir_t filldir, |
| 493 | struct ctl_table_header *head, |
| 494 | struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 495 | { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 496 | struct dentry *child, *dir = filp->f_path.dentry; |
| 497 | struct inode *inode; |
| 498 | struct qstr qname; |
| 499 | ino_t ino = 0; |
| 500 | unsigned type = DT_UNKNOWN; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 501 | |
| 502 | qname.name = table->procname; |
| 503 | qname.len = strlen(table->procname); |
| 504 | qname.hash = full_name_hash(qname.name, qname.len); |
| 505 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 506 | child = d_lookup(dir, &qname); |
| 507 | if (!child) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 508 | child = d_alloc(dir, &qname); |
| 509 | if (child) { |
| 510 | inode = proc_sys_make_inode(dir->d_sb, head, table); |
| 511 | if (!inode) { |
| 512 | dput(child); |
| 513 | return -ENOMEM; |
| 514 | } else { |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 515 | d_set_d_op(child, &proc_sys_dentry_operations); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 516 | d_add(child, inode); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 517 | } |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 518 | } else { |
| 519 | return -ENOMEM; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 522 | inode = child->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 523 | ino = inode->i_ino; |
| 524 | type = inode->i_mode >> 12; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 525 | dput(child); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 526 | return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); |
| 527 | } |
| 528 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 529 | static int proc_sys_link_fill_cache(struct file *filp, void *dirent, |
| 530 | filldir_t filldir, |
| 531 | struct ctl_table_header *head, |
| 532 | struct ctl_table *table) |
| 533 | { |
| 534 | int err, ret = 0; |
| 535 | head = sysctl_head_grab(head); |
| 536 | |
| 537 | /* It is not an error if we can not follow the link ignore it */ |
| 538 | err = sysctl_follow_link(&head, &table, current->nsproxy); |
| 539 | if (err) |
| 540 | goto out; |
| 541 | |
| 542 | ret = proc_sys_fill_cache(filp, dirent, filldir, head, table); |
| 543 | out: |
| 544 | sysctl_head_finish(head); |
| 545 | return ret; |
| 546 | } |
| 547 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 548 | static int scan(struct ctl_table_header *head, ctl_table *table, |
| 549 | unsigned long *pos, struct file *file, |
| 550 | void *dirent, filldir_t filldir) |
| 551 | { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 552 | int res; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 553 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 554 | if ((*pos)++ < file->f_pos) |
| 555 | return 0; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 556 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 557 | if (unlikely(S_ISLNK(table->mode))) |
| 558 | res = proc_sys_link_fill_cache(file, dirent, filldir, head, table); |
| 559 | else |
| 560 | res = proc_sys_fill_cache(file, dirent, filldir, head, table); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 561 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 562 | if (res == 0) |
| 563 | file->f_pos = *pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 564 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 565 | return res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 569 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 570 | struct dentry *dentry = filp->f_path.dentry; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 571 | struct inode *inode = dentry->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 572 | struct ctl_table_header *head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 573 | struct ctl_table_header *h = NULL; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 574 | struct ctl_table *entry; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 575 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 576 | unsigned long pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 577 | int ret = -EINVAL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 578 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 579 | if (IS_ERR(head)) |
| 580 | return PTR_ERR(head); |
| 581 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 582 | ctl_dir = container_of(head, struct ctl_dir, header); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 583 | |
| 584 | ret = 0; |
| 585 | /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ |
| 586 | if (filp->f_pos == 0) { |
| 587 | if (filldir(dirent, ".", 1, filp->f_pos, |
| 588 | inode->i_ino, DT_DIR) < 0) |
| 589 | goto out; |
| 590 | filp->f_pos++; |
| 591 | } |
| 592 | if (filp->f_pos == 1) { |
| 593 | if (filldir(dirent, "..", 2, filp->f_pos, |
| 594 | parent_ino(dentry), DT_DIR) < 0) |
| 595 | goto out; |
| 596 | filp->f_pos++; |
| 597 | } |
| 598 | pos = 2; |
| 599 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 600 | for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 601 | ret = scan(h, entry, &pos, filp, dirent, filldir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 602 | if (ret) { |
| 603 | sysctl_head_finish(h); |
| 604 | break; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | ret = 1; |
| 608 | out: |
| 609 | sysctl_head_finish(head); |
| 610 | return ret; |
| 611 | } |
| 612 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 613 | static int proc_sys_permission(struct inode *inode, int mask) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 614 | { |
| 615 | /* |
| 616 | * sysctl entries that are not writeable, |
| 617 | * are _NOT_ writeable, capabilities or not. |
| 618 | */ |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 619 | struct ctl_table_header *head; |
| 620 | struct ctl_table *table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 621 | int error; |
| 622 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 623 | /* Executable files are not allowed under /proc/sys/ */ |
| 624 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) |
| 625 | return -EACCES; |
| 626 | |
| 627 | head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 628 | if (IS_ERR(head)) |
| 629 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 630 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 631 | table = PROC_I(inode)->sysctl_entry; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 632 | if (!table) /* global root - r-xr-xr-x */ |
| 633 | error = mask & MAY_WRITE ? -EACCES : 0; |
| 634 | else /* Use the permissions on the sysctl table entry */ |
Al Viro | 1fc0f78 | 2011-06-20 18:59:02 -0400 | [diff] [blame] | 635 | error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 636 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 637 | sysctl_head_finish(head); |
| 638 | return error; |
| 639 | } |
| 640 | |
| 641 | static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) |
| 642 | { |
| 643 | struct inode *inode = dentry->d_inode; |
| 644 | int error; |
| 645 | |
| 646 | if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 647 | return -EPERM; |
| 648 | |
| 649 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 650 | if (error) |
| 651 | return error; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 652 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 653 | if ((attr->ia_valid & ATTR_SIZE) && |
| 654 | attr->ia_size != i_size_read(inode)) { |
| 655 | error = vmtruncate(inode, attr->ia_size); |
| 656 | if (error) |
| 657 | return error; |
| 658 | } |
| 659 | |
| 660 | setattr_copy(inode, attr); |
| 661 | mark_inode_dirty(inode); |
| 662 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 665 | static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 666 | { |
| 667 | struct inode *inode = dentry->d_inode; |
| 668 | struct ctl_table_header *head = grab_header(inode); |
| 669 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 670 | |
| 671 | if (IS_ERR(head)) |
| 672 | return PTR_ERR(head); |
| 673 | |
| 674 | generic_fillattr(inode, stat); |
| 675 | if (table) |
| 676 | stat->mode = (stat->mode & S_IFMT) | table->mode; |
| 677 | |
| 678 | sysctl_head_finish(head); |
| 679 | return 0; |
| 680 | } |
| 681 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 682 | static const struct file_operations proc_sys_file_operations = { |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 683 | .open = proc_sys_open, |
| 684 | .poll = proc_sys_poll, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 685 | .read = proc_sys_read, |
| 686 | .write = proc_sys_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 687 | .llseek = default_llseek, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | static const struct file_operations proc_sys_dir_file_operations = { |
Pavel Emelyanov | 887df07 | 2011-11-02 13:38:42 -0700 | [diff] [blame] | 691 | .read = generic_read_dir, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 692 | .readdir = proc_sys_readdir, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 693 | .llseek = generic_file_llseek, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 694 | }; |
| 695 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 696 | static const struct inode_operations proc_sys_inode_operations = { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 697 | .permission = proc_sys_permission, |
| 698 | .setattr = proc_sys_setattr, |
| 699 | .getattr = proc_sys_getattr, |
| 700 | }; |
| 701 | |
| 702 | static const struct inode_operations proc_sys_dir_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 703 | .lookup = proc_sys_lookup, |
| 704 | .permission = proc_sys_permission, |
| 705 | .setattr = proc_sys_setattr, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 706 | .getattr = proc_sys_getattr, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 707 | }; |
| 708 | |
| 709 | static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 710 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 711 | if (nd->flags & LOOKUP_RCU) |
| 712 | return -ECHILD; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 713 | return !PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 714 | } |
| 715 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 716 | static int proc_sys_delete(const struct dentry *dentry) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 717 | { |
| 718 | return !!PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 719 | } |
| 720 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 721 | static int sysctl_is_seen(struct ctl_table_header *p) |
| 722 | { |
| 723 | struct ctl_table_set *set = p->set; |
| 724 | int res; |
| 725 | spin_lock(&sysctl_lock); |
| 726 | if (p->unregistering) |
| 727 | res = 0; |
| 728 | else if (!set->is_seen) |
| 729 | res = 1; |
| 730 | else |
| 731 | res = set->is_seen(set); |
| 732 | spin_unlock(&sysctl_lock); |
| 733 | return res; |
| 734 | } |
| 735 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 736 | static int proc_sys_compare(const struct dentry *parent, |
| 737 | const struct inode *pinode, |
| 738 | const struct dentry *dentry, const struct inode *inode, |
| 739 | unsigned int len, const char *str, const struct qstr *name) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 740 | { |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 741 | struct ctl_table_header *head; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 742 | /* Although proc doesn't have negative dentries, rcu-walk means |
| 743 | * that inode here can be NULL */ |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 744 | /* AV: can it, indeed? */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 745 | if (!inode) |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 746 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 747 | if (name->len != len) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 748 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 749 | if (memcmp(name->name, str, len)) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 750 | return 1; |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 751 | head = rcu_dereference(PROC_I(inode)->sysctl); |
| 752 | return !head || !sysctl_is_seen(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 755 | static const struct dentry_operations proc_sys_dentry_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 756 | .d_revalidate = proc_sys_revalidate, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 757 | .d_delete = proc_sys_delete, |
| 758 | .d_compare = proc_sys_compare, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 759 | }; |
| 760 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 761 | static struct ctl_dir *find_subdir(struct ctl_dir *dir, |
| 762 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 763 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 764 | struct ctl_table_header *head; |
| 765 | struct ctl_table *entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 766 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 767 | entry = find_entry(&head, dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 768 | if (!entry) |
| 769 | return ERR_PTR(-ENOENT); |
| 770 | if (S_ISDIR(entry->mode)) |
| 771 | return container_of(head, struct ctl_dir, header); |
| 772 | return ERR_PTR(-ENOTDIR); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 775 | static struct ctl_dir *new_dir(struct ctl_table_set *set, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 776 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 777 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 778 | struct ctl_table *table; |
| 779 | struct ctl_dir *new; |
| 780 | char *new_name; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 781 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 782 | new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 + |
| 783 | namelen + 1, GFP_KERNEL); |
| 784 | if (!new) |
| 785 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 786 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 787 | table = (struct ctl_table *)(new + 1); |
| 788 | new_name = (char *)(table + 2); |
| 789 | memcpy(new_name, name, namelen); |
| 790 | new_name[namelen] = '\0'; |
| 791 | table[0].procname = new_name; |
| 792 | table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 793 | init_header(&new->header, set->dir.header.root, set, table); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 794 | |
| 795 | return new; |
| 796 | } |
| 797 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 798 | static struct ctl_dir *get_subdir(struct ctl_dir *dir, |
| 799 | const char *name, int namelen) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 800 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 801 | struct ctl_table_set *set = dir->header.set; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 802 | struct ctl_dir *subdir, *new = NULL; |
| 803 | |
| 804 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 805 | subdir = find_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 806 | if (!IS_ERR(subdir)) |
| 807 | goto found; |
| 808 | if (PTR_ERR(subdir) != -ENOENT) |
| 809 | goto failed; |
| 810 | |
| 811 | spin_unlock(&sysctl_lock); |
| 812 | new = new_dir(set, name, namelen); |
| 813 | spin_lock(&sysctl_lock); |
| 814 | subdir = ERR_PTR(-ENOMEM); |
| 815 | if (!new) |
| 816 | goto failed; |
| 817 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 818 | subdir = find_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 819 | if (!IS_ERR(subdir)) |
| 820 | goto found; |
| 821 | if (PTR_ERR(subdir) != -ENOENT) |
| 822 | goto failed; |
| 823 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 824 | if (insert_header(dir, &new->header)) |
| 825 | goto failed; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 826 | subdir = new; |
| 827 | found: |
| 828 | subdir->header.nreg++; |
| 829 | failed: |
| 830 | if (unlikely(IS_ERR(subdir))) { |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame] | 831 | printk(KERN_ERR "sysctl could not get directory: "); |
| 832 | sysctl_print_dir(dir); |
| 833 | printk(KERN_CONT "/%*.*s %ld\n", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 834 | namelen, namelen, name, PTR_ERR(subdir)); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 835 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 836 | drop_sysctl_table(&dir->header); |
| 837 | if (new) |
| 838 | drop_sysctl_table(&new->header); |
| 839 | spin_unlock(&sysctl_lock); |
| 840 | return subdir; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 843 | static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) |
| 844 | { |
| 845 | struct ctl_dir *parent; |
| 846 | const char *procname; |
| 847 | if (!dir->header.parent) |
| 848 | return &set->dir; |
| 849 | parent = xlate_dir(set, dir->header.parent); |
| 850 | if (IS_ERR(parent)) |
| 851 | return parent; |
| 852 | procname = dir->header.ctl_table[0].procname; |
| 853 | return find_subdir(parent, procname, strlen(procname)); |
| 854 | } |
| 855 | |
| 856 | static int sysctl_follow_link(struct ctl_table_header **phead, |
| 857 | struct ctl_table **pentry, struct nsproxy *namespaces) |
| 858 | { |
| 859 | struct ctl_table_header *head; |
| 860 | struct ctl_table_root *root; |
| 861 | struct ctl_table_set *set; |
| 862 | struct ctl_table *entry; |
| 863 | struct ctl_dir *dir; |
| 864 | int ret; |
| 865 | |
| 866 | /* Get out quickly if not a link */ |
| 867 | if (!S_ISLNK((*pentry)->mode)) |
| 868 | return 0; |
| 869 | |
| 870 | ret = 0; |
| 871 | spin_lock(&sysctl_lock); |
| 872 | root = (*pentry)->data; |
| 873 | set = lookup_header_set(root, namespaces); |
| 874 | dir = xlate_dir(set, (*phead)->parent); |
| 875 | if (IS_ERR(dir)) |
| 876 | ret = PTR_ERR(dir); |
| 877 | else { |
| 878 | const char *procname = (*pentry)->procname; |
| 879 | head = NULL; |
| 880 | entry = find_entry(&head, dir, procname, strlen(procname)); |
| 881 | ret = -ENOENT; |
| 882 | if (entry && use_table(head)) { |
| 883 | unuse_table(*phead); |
| 884 | *phead = head; |
| 885 | *pentry = entry; |
| 886 | ret = 0; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | spin_unlock(&sysctl_lock); |
| 891 | return ret; |
| 892 | } |
| 893 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 894 | static int sysctl_check_table_dups(const char *path, struct ctl_table *old, |
| 895 | struct ctl_table *table) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 896 | { |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 897 | struct ctl_table *entry, *test; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 898 | int error = 0; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 899 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 900 | for (entry = old; entry->procname; entry++) { |
| 901 | for (test = table; test->procname; test++) { |
| 902 | if (strcmp(entry->procname, test->procname) == 0) { |
| 903 | printk(KERN_ERR "sysctl duplicate entry: %s/%s\n", |
| 904 | path, test->procname); |
| 905 | error = -EEXIST; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 906 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 907 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 908 | } |
| 909 | return error; |
| 910 | } |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 911 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 912 | static int sysctl_check_dups(struct ctl_dir *dir, |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 913 | const char *path, struct ctl_table *table) |
| 914 | { |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 915 | struct ctl_table_set *set; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 916 | struct ctl_table_header *head; |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 917 | int error = 0; |
| 918 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 919 | set = dir->header.set; |
| 920 | list_for_each_entry(head, &set->list, ctl_entry) { |
| 921 | if (head->unregistering) |
| 922 | continue; |
| 923 | if (head->parent != dir) |
| 924 | continue; |
| 925 | error = sysctl_check_table_dups(path, head->ctl_table, table); |
| 926 | } |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 927 | return error; |
| 928 | } |
| 929 | |
| 930 | static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) |
| 931 | { |
| 932 | struct va_format vaf; |
| 933 | va_list args; |
| 934 | |
| 935 | va_start(args, fmt); |
| 936 | vaf.fmt = fmt; |
| 937 | vaf.va = &args; |
| 938 | |
| 939 | printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", |
| 940 | path, table->procname, &vaf); |
| 941 | |
| 942 | va_end(args); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | |
| 946 | static int sysctl_check_table(const char *path, struct ctl_table *table) |
| 947 | { |
| 948 | int err = 0; |
| 949 | for (; table->procname; table++) { |
| 950 | if (table->child) |
| 951 | err = sysctl_err(path, table, "Not a file"); |
| 952 | |
| 953 | if ((table->proc_handler == proc_dostring) || |
| 954 | (table->proc_handler == proc_dointvec) || |
| 955 | (table->proc_handler == proc_dointvec_minmax) || |
| 956 | (table->proc_handler == proc_dointvec_jiffies) || |
| 957 | (table->proc_handler == proc_dointvec_userhz_jiffies) || |
| 958 | (table->proc_handler == proc_dointvec_ms_jiffies) || |
| 959 | (table->proc_handler == proc_doulongvec_minmax) || |
| 960 | (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { |
| 961 | if (!table->data) |
| 962 | err = sysctl_err(path, table, "No data"); |
| 963 | if (!table->maxlen) |
| 964 | err = sysctl_err(path, table, "No maxlen"); |
| 965 | } |
| 966 | if (!table->proc_handler) |
| 967 | err = sysctl_err(path, table, "No proc_handler"); |
| 968 | |
| 969 | if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) |
| 970 | err = sysctl_err(path, table, "bogus .mode 0%o", |
| 971 | table->mode); |
| 972 | } |
| 973 | return err; |
| 974 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 975 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 976 | static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, |
| 977 | struct ctl_table_root *link_root) |
| 978 | { |
| 979 | struct ctl_table *link_table, *entry, *link; |
| 980 | struct ctl_table_header *links; |
| 981 | char *link_name; |
| 982 | int nr_entries, name_bytes; |
| 983 | |
| 984 | name_bytes = 0; |
| 985 | nr_entries = 0; |
| 986 | for (entry = table; entry->procname; entry++) { |
| 987 | nr_entries++; |
| 988 | name_bytes += strlen(entry->procname) + 1; |
| 989 | } |
| 990 | |
| 991 | links = kzalloc(sizeof(struct ctl_table_header) + |
| 992 | sizeof(struct ctl_table)*(nr_entries + 1) + |
| 993 | name_bytes, |
| 994 | GFP_KERNEL); |
| 995 | |
| 996 | if (!links) |
| 997 | return NULL; |
| 998 | |
| 999 | link_table = (struct ctl_table *)(links + 1); |
| 1000 | link_name = (char *)&link_table[nr_entries + 1]; |
| 1001 | |
| 1002 | for (link = link_table, entry = table; entry->procname; link++, entry++) { |
| 1003 | int len = strlen(entry->procname) + 1; |
| 1004 | memcpy(link_name, entry->procname, len); |
| 1005 | link->procname = link_name; |
| 1006 | link->mode = S_IFLNK|S_IRWXUGO; |
| 1007 | link->data = link_root; |
| 1008 | link_name += len; |
| 1009 | } |
| 1010 | init_header(links, dir->header.root, dir->header.set, link_table); |
| 1011 | links->nreg = nr_entries; |
| 1012 | |
| 1013 | return links; |
| 1014 | } |
| 1015 | |
| 1016 | static bool get_links(struct ctl_dir *dir, |
| 1017 | struct ctl_table *table, struct ctl_table_root *link_root) |
| 1018 | { |
| 1019 | struct ctl_table_header *head; |
| 1020 | struct ctl_table *entry, *link; |
| 1021 | |
| 1022 | /* Are there links available for every entry in table? */ |
| 1023 | for (entry = table; entry->procname; entry++) { |
| 1024 | const char *procname = entry->procname; |
| 1025 | link = find_entry(&head, dir, procname, strlen(procname)); |
| 1026 | if (!link) |
| 1027 | return false; |
| 1028 | if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) |
| 1029 | continue; |
| 1030 | if (S_ISLNK(link->mode) && (link->data == link_root)) |
| 1031 | continue; |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | /* The checks passed. Increase the registration count on the links */ |
| 1036 | for (entry = table; entry->procname; entry++) { |
| 1037 | const char *procname = entry->procname; |
| 1038 | link = find_entry(&head, dir, procname, strlen(procname)); |
| 1039 | head->nreg++; |
| 1040 | } |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | static int insert_links(struct ctl_table_header *head) |
| 1045 | { |
| 1046 | struct ctl_table_set *root_set = &sysctl_table_root.default_set; |
| 1047 | struct ctl_dir *core_parent = NULL; |
| 1048 | struct ctl_table_header *links; |
| 1049 | int err; |
| 1050 | |
| 1051 | if (head->set == root_set) |
| 1052 | return 0; |
| 1053 | |
| 1054 | core_parent = xlate_dir(root_set, head->parent); |
| 1055 | if (IS_ERR(core_parent)) |
| 1056 | return 0; |
| 1057 | |
| 1058 | if (get_links(core_parent, head->ctl_table, head->root)) |
| 1059 | return 0; |
| 1060 | |
| 1061 | core_parent->header.nreg++; |
| 1062 | spin_unlock(&sysctl_lock); |
| 1063 | |
| 1064 | links = new_links(core_parent, head->ctl_table, head->root); |
| 1065 | |
| 1066 | spin_lock(&sysctl_lock); |
| 1067 | err = -ENOMEM; |
| 1068 | if (!links) |
| 1069 | goto out; |
| 1070 | |
| 1071 | err = 0; |
| 1072 | if (get_links(core_parent, head->ctl_table, head->root)) { |
| 1073 | kfree(links); |
| 1074 | goto out; |
| 1075 | } |
| 1076 | |
| 1077 | err = insert_header(core_parent, links); |
| 1078 | if (err) |
| 1079 | kfree(links); |
| 1080 | out: |
| 1081 | drop_sysctl_table(&core_parent->header); |
| 1082 | return err; |
| 1083 | } |
| 1084 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1085 | /** |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1086 | * __register_sysctl_table - register a leaf sysctl table |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1087 | * @set: Sysctl tree to register on |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1088 | * @path: The path to the directory the sysctl table is in. |
| 1089 | * @table: the top-level table structure |
| 1090 | * |
| 1091 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1092 | * array. A completely 0 filled entry terminates the table. |
| 1093 | * |
| 1094 | * The members of the &struct ctl_table structure are used as follows: |
| 1095 | * |
| 1096 | * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not |
| 1097 | * enter a sysctl file |
| 1098 | * |
| 1099 | * data - a pointer to data for use by proc_handler |
| 1100 | * |
| 1101 | * maxlen - the maximum size in bytes of the data |
| 1102 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1103 | * mode - the file permissions for the /proc/sys file |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1104 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1105 | * child - must be %NULL. |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1106 | * |
| 1107 | * proc_handler - the text handler routine (described below) |
| 1108 | * |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1109 | * extra1, extra2 - extra pointers usable by the proc handler routines |
| 1110 | * |
| 1111 | * Leaf nodes in the sysctl tree will be represented by a single file |
| 1112 | * under /proc; non-leaf nodes will be represented by directories. |
| 1113 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1114 | * There must be a proc_handler routine for any terminal nodes. |
| 1115 | * Several default handlers are available to cover common cases - |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1116 | * |
| 1117 | * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), |
| 1118 | * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), |
| 1119 | * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() |
| 1120 | * |
| 1121 | * It is the handler's job to read the input buffer from user memory |
| 1122 | * and process it. The handler should return 0 on success. |
| 1123 | * |
| 1124 | * This routine returns %NULL on a failure to register, and a pointer |
| 1125 | * to the table header on success. |
| 1126 | */ |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1127 | struct ctl_table_header *__register_sysctl_table( |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1128 | struct ctl_table_set *set, |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1129 | const char *path, struct ctl_table *table) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1130 | { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1131 | struct ctl_table_root *root = set->dir.header.root; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1132 | struct ctl_table_header *links = NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1133 | struct ctl_table_header *header; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1134 | const char *name, *nextname; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1135 | struct ctl_dir *dir; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1136 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1137 | header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1138 | if (!header) |
| 1139 | return NULL; |
| 1140 | |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1141 | init_header(header, root, set, table); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1142 | if (sysctl_check_table(path, table)) |
| 1143 | goto fail; |
Eric W. Biederman | 8d6ecfc | 2012-01-06 11:55:30 -0800 | [diff] [blame] | 1144 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1145 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1146 | dir = &set->dir; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1147 | dir->header.nreg++; |
| 1148 | spin_unlock(&sysctl_lock); |
| 1149 | |
| 1150 | /* Find the directory for the ctl_table */ |
| 1151 | for (name = path; name; name = nextname) { |
| 1152 | int namelen; |
| 1153 | nextname = strchr(name, '/'); |
| 1154 | if (nextname) { |
| 1155 | namelen = nextname - name; |
| 1156 | nextname++; |
| 1157 | } else { |
| 1158 | namelen = strlen(name); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1159 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1160 | if (namelen == 0) |
| 1161 | continue; |
| 1162 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1163 | dir = get_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1164 | if (IS_ERR(dir)) |
| 1165 | goto fail; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1166 | } |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1167 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1168 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1169 | if (sysctl_check_dups(dir, path, table)) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1170 | goto fail_put_dir_locked; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1171 | |
| 1172 | if (insert_header(dir, header)) |
| 1173 | goto fail_put_dir_locked; |
| 1174 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1175 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1176 | spin_unlock(&sysctl_lock); |
| 1177 | |
| 1178 | return header; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1179 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1180 | fail_put_dir_locked: |
| 1181 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1182 | spin_unlock(&sysctl_lock); |
| 1183 | fail: |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1184 | kfree(links); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1185 | kfree(header); |
| 1186 | dump_stack(); |
| 1187 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1190 | static char *append_path(const char *path, char *pos, const char *name) |
| 1191 | { |
| 1192 | int namelen; |
| 1193 | namelen = strlen(name); |
| 1194 | if (((pos - path) + namelen + 2) >= PATH_MAX) |
| 1195 | return NULL; |
| 1196 | memcpy(pos, name, namelen); |
| 1197 | pos[namelen] = '/'; |
| 1198 | pos[namelen + 1] = '\0'; |
| 1199 | pos += namelen + 1; |
| 1200 | return pos; |
| 1201 | } |
| 1202 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1203 | static int count_subheaders(struct ctl_table *table) |
| 1204 | { |
| 1205 | int has_files = 0; |
| 1206 | int nr_subheaders = 0; |
| 1207 | struct ctl_table *entry; |
| 1208 | |
| 1209 | /* special case: no directory and empty directory */ |
| 1210 | if (!table || !table->procname) |
| 1211 | return 1; |
| 1212 | |
| 1213 | for (entry = table; entry->procname; entry++) { |
| 1214 | if (entry->child) |
| 1215 | nr_subheaders += count_subheaders(entry->child); |
| 1216 | else |
| 1217 | has_files = 1; |
| 1218 | } |
| 1219 | return nr_subheaders + has_files; |
| 1220 | } |
| 1221 | |
| 1222 | static int register_leaf_sysctl_tables(const char *path, char *pos, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1223 | struct ctl_table_header ***subheader, struct ctl_table_set *set, |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1224 | struct ctl_table *table) |
| 1225 | { |
| 1226 | struct ctl_table *ctl_table_arg = NULL; |
| 1227 | struct ctl_table *entry, *files; |
| 1228 | int nr_files = 0; |
| 1229 | int nr_dirs = 0; |
| 1230 | int err = -ENOMEM; |
| 1231 | |
| 1232 | for (entry = table; entry->procname; entry++) { |
| 1233 | if (entry->child) |
| 1234 | nr_dirs++; |
| 1235 | else |
| 1236 | nr_files++; |
| 1237 | } |
| 1238 | |
| 1239 | files = table; |
| 1240 | /* If there are mixed files and directories we need a new table */ |
| 1241 | if (nr_dirs && nr_files) { |
| 1242 | struct ctl_table *new; |
| 1243 | files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), |
| 1244 | GFP_KERNEL); |
| 1245 | if (!files) |
| 1246 | goto out; |
| 1247 | |
| 1248 | ctl_table_arg = files; |
| 1249 | for (new = files, entry = table; entry->procname; entry++) { |
| 1250 | if (entry->child) |
| 1251 | continue; |
| 1252 | *new = *entry; |
| 1253 | new++; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | /* Register everything except a directory full of subdirectories */ |
| 1258 | if (nr_files || !nr_dirs) { |
| 1259 | struct ctl_table_header *header; |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1260 | header = __register_sysctl_table(set, path, files); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1261 | if (!header) { |
| 1262 | kfree(ctl_table_arg); |
| 1263 | goto out; |
| 1264 | } |
| 1265 | |
| 1266 | /* Remember if we need to free the file table */ |
| 1267 | header->ctl_table_arg = ctl_table_arg; |
| 1268 | **subheader = header; |
| 1269 | (*subheader)++; |
| 1270 | } |
| 1271 | |
| 1272 | /* Recurse into the subdirectories. */ |
| 1273 | for (entry = table; entry->procname; entry++) { |
| 1274 | char *child_pos; |
| 1275 | |
| 1276 | if (!entry->child) |
| 1277 | continue; |
| 1278 | |
| 1279 | err = -ENAMETOOLONG; |
| 1280 | child_pos = append_path(path, pos, entry->procname); |
| 1281 | if (!child_pos) |
| 1282 | goto out; |
| 1283 | |
| 1284 | err = register_leaf_sysctl_tables(path, child_pos, subheader, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1285 | set, entry->child); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1286 | pos[0] = '\0'; |
| 1287 | if (err) |
| 1288 | goto out; |
| 1289 | } |
| 1290 | err = 0; |
| 1291 | out: |
| 1292 | /* On failure our caller will unregister all registered subheaders */ |
| 1293 | return err; |
| 1294 | } |
| 1295 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1296 | /** |
| 1297 | * __register_sysctl_paths - register a sysctl table hierarchy |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1298 | * @set: Sysctl tree to register on |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1299 | * @path: The path to the directory the sysctl table is in. |
| 1300 | * @table: the top-level table structure |
| 1301 | * |
| 1302 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1303 | * array. A completely 0 filled entry terminates the table. |
| 1304 | * |
| 1305 | * See __register_sysctl_table for more details. |
| 1306 | */ |
| 1307 | struct ctl_table_header *__register_sysctl_paths( |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1308 | struct ctl_table_set *set, |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1309 | const struct ctl_path *path, struct ctl_table *table) |
| 1310 | { |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1311 | struct ctl_table *ctl_table_arg = table; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1312 | int nr_subheaders = count_subheaders(table); |
| 1313 | struct ctl_table_header *header = NULL, **subheaders, **subheader; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1314 | const struct ctl_path *component; |
| 1315 | char *new_path, *pos; |
| 1316 | |
| 1317 | pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1318 | if (!new_path) |
| 1319 | return NULL; |
| 1320 | |
| 1321 | pos[0] = '\0'; |
| 1322 | for (component = path; component->procname; component++) { |
| 1323 | pos = append_path(new_path, pos, component->procname); |
| 1324 | if (!pos) |
| 1325 | goto out; |
| 1326 | } |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1327 | while (table->procname && table->child && !table[1].procname) { |
| 1328 | pos = append_path(new_path, pos, table->procname); |
| 1329 | if (!pos) |
| 1330 | goto out; |
| 1331 | table = table->child; |
| 1332 | } |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1333 | if (nr_subheaders == 1) { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1334 | header = __register_sysctl_table(set, new_path, table); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1335 | if (header) |
| 1336 | header->ctl_table_arg = ctl_table_arg; |
| 1337 | } else { |
| 1338 | header = kzalloc(sizeof(*header) + |
| 1339 | sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); |
| 1340 | if (!header) |
| 1341 | goto out; |
| 1342 | |
| 1343 | subheaders = (struct ctl_table_header **) (header + 1); |
| 1344 | subheader = subheaders; |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1345 | header->ctl_table_arg = ctl_table_arg; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1346 | |
| 1347 | if (register_leaf_sysctl_tables(new_path, pos, &subheader, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1348 | set, table)) |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1349 | goto err_register_leaves; |
| 1350 | } |
| 1351 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1352 | out: |
| 1353 | kfree(new_path); |
| 1354 | return header; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1355 | |
| 1356 | err_register_leaves: |
| 1357 | while (subheader > subheaders) { |
| 1358 | struct ctl_table_header *subh = *(--subheader); |
| 1359 | struct ctl_table *table = subh->ctl_table_arg; |
| 1360 | unregister_sysctl_table(subh); |
| 1361 | kfree(table); |
| 1362 | } |
| 1363 | kfree(header); |
| 1364 | header = NULL; |
| 1365 | goto out; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1368 | /** |
| 1369 | * register_sysctl_table_path - register a sysctl table hierarchy |
| 1370 | * @path: The path to the directory the sysctl table is in. |
| 1371 | * @table: the top-level table structure |
| 1372 | * |
| 1373 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1374 | * array. A completely 0 filled entry terminates the table. |
| 1375 | * |
| 1376 | * See __register_sysctl_paths for more details. |
| 1377 | */ |
| 1378 | struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, |
| 1379 | struct ctl_table *table) |
| 1380 | { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame^] | 1381 | return __register_sysctl_paths(&sysctl_table_root.default_set, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1382 | path, table); |
| 1383 | } |
| 1384 | EXPORT_SYMBOL(register_sysctl_paths); |
| 1385 | |
| 1386 | /** |
| 1387 | * register_sysctl_table - register a sysctl table hierarchy |
| 1388 | * @table: the top-level table structure |
| 1389 | * |
| 1390 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1391 | * array. A completely 0 filled entry terminates the table. |
| 1392 | * |
| 1393 | * See register_sysctl_paths for more details. |
| 1394 | */ |
| 1395 | struct ctl_table_header *register_sysctl_table(struct ctl_table *table) |
| 1396 | { |
| 1397 | static const struct ctl_path null_path[] = { {} }; |
| 1398 | |
| 1399 | return register_sysctl_paths(null_path, table); |
| 1400 | } |
| 1401 | EXPORT_SYMBOL(register_sysctl_table); |
| 1402 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1403 | static void put_links(struct ctl_table_header *header) |
| 1404 | { |
| 1405 | struct ctl_table_set *root_set = &sysctl_table_root.default_set; |
| 1406 | struct ctl_table_root *root = header->root; |
| 1407 | struct ctl_dir *parent = header->parent; |
| 1408 | struct ctl_dir *core_parent; |
| 1409 | struct ctl_table *entry; |
| 1410 | |
| 1411 | if (header->set == root_set) |
| 1412 | return; |
| 1413 | |
| 1414 | core_parent = xlate_dir(root_set, parent); |
| 1415 | if (IS_ERR(core_parent)) |
| 1416 | return; |
| 1417 | |
| 1418 | for (entry = header->ctl_table; entry->procname; entry++) { |
| 1419 | struct ctl_table_header *link_head; |
| 1420 | struct ctl_table *link; |
| 1421 | const char *name = entry->procname; |
| 1422 | |
| 1423 | link = find_entry(&link_head, core_parent, name, strlen(name)); |
| 1424 | if (link && |
| 1425 | ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || |
| 1426 | (S_ISLNK(link->mode) && (link->data == root)))) { |
| 1427 | drop_sysctl_table(link_head); |
| 1428 | } |
| 1429 | else { |
| 1430 | printk(KERN_ERR "sysctl link missing during unregister: "); |
| 1431 | sysctl_print_dir(parent); |
| 1432 | printk(KERN_CONT "/%s\n", name); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1437 | static void drop_sysctl_table(struct ctl_table_header *header) |
| 1438 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1439 | struct ctl_dir *parent = header->parent; |
| 1440 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1441 | if (--header->nreg) |
| 1442 | return; |
| 1443 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1444 | put_links(header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1445 | start_unregistering(header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1446 | if (!--header->count) |
| 1447 | kfree_rcu(header, rcu); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1448 | |
| 1449 | if (parent) |
| 1450 | drop_sysctl_table(&parent->header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1453 | /** |
| 1454 | * unregister_sysctl_table - unregister a sysctl table hierarchy |
| 1455 | * @header: the header returned from register_sysctl_table |
| 1456 | * |
| 1457 | * Unregisters the sysctl table and all children. proc entries may not |
| 1458 | * actually be removed until they are no longer used by anyone. |
| 1459 | */ |
| 1460 | void unregister_sysctl_table(struct ctl_table_header * header) |
| 1461 | { |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1462 | int nr_subheaders; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1463 | might_sleep(); |
| 1464 | |
| 1465 | if (header == NULL) |
| 1466 | return; |
| 1467 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1468 | nr_subheaders = count_subheaders(header->ctl_table_arg); |
| 1469 | if (unlikely(nr_subheaders > 1)) { |
| 1470 | struct ctl_table_header **subheaders; |
| 1471 | int i; |
| 1472 | |
| 1473 | subheaders = (struct ctl_table_header **)(header + 1); |
| 1474 | for (i = nr_subheaders -1; i >= 0; i--) { |
| 1475 | struct ctl_table_header *subh = subheaders[i]; |
| 1476 | struct ctl_table *table = subh->ctl_table_arg; |
| 1477 | unregister_sysctl_table(subh); |
| 1478 | kfree(table); |
| 1479 | } |
| 1480 | kfree(header); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1484 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1485 | drop_sysctl_table(header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1486 | spin_unlock(&sysctl_lock); |
| 1487 | } |
| 1488 | EXPORT_SYMBOL(unregister_sysctl_table); |
| 1489 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1490 | void setup_sysctl_set(struct ctl_table_set *set, |
Eric W. Biederman | 9eb47c2 | 2012-01-22 21:26:00 -0800 | [diff] [blame] | 1491 | struct ctl_table_root *root, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1492 | int (*is_seen)(struct ctl_table_set *)) |
| 1493 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1494 | memset(set, sizeof(*set), 0); |
| 1495 | INIT_LIST_HEAD(&set->list); |
| 1496 | set->is_seen = is_seen; |
| 1497 | init_header(&set->dir.header, root, set, root_table); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Eric W. Biederman | 97324cd | 2012-01-09 22:19:13 -0800 | [diff] [blame] | 1500 | void retire_sysctl_set(struct ctl_table_set *set) |
| 1501 | { |
| 1502 | WARN_ON(!list_empty(&set->list)); |
| 1503 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1504 | |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 1505 | int __init proc_sys_init(void) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1506 | { |
Alexey Dobriyan | e167523 | 2008-10-03 00:23:32 +0400 | [diff] [blame] | 1507 | struct proc_dir_entry *proc_sys_root; |
| 1508 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1509 | proc_sys_root = proc_mkdir("sys", NULL); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 1510 | proc_sys_root->proc_iops = &proc_sys_dir_operations; |
| 1511 | proc_sys_root->proc_fops = &proc_sys_dir_file_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1512 | proc_sys_root->nlink = 0; |
Eric W. Biederman | de4e83bd | 2012-01-06 03:34:20 -0800 | [diff] [blame] | 1513 | |
| 1514 | return sysctl_init(); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1515 | } |