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