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> |
| 6 | #include <linux/proc_fs.h> |
| 7 | #include <linux/security.h> |
| 8 | #include "internal.h" |
| 9 | |
| 10 | static struct dentry_operations proc_sys_dentry_operations; |
| 11 | static const struct file_operations proc_sys_file_operations; |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 12 | static const struct inode_operations proc_sys_inode_operations; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 13 | static const struct file_operations proc_sys_dir_file_operations; |
| 14 | static const struct inode_operations proc_sys_dir_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 15 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 16 | static struct inode *proc_sys_make_inode(struct super_block *sb, |
| 17 | struct ctl_table_header *head, struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 18 | { |
| 19 | struct inode *inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 20 | struct proc_inode *ei; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 21 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 22 | inode = new_inode(sb); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 23 | if (!inode) |
| 24 | goto out; |
| 25 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 26 | sysctl_head_get(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 27 | ei = PROC_I(inode); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 28 | ei->sysctl = head; |
| 29 | ei->sysctl_entry = table; |
| 30 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 31 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Eric W. Biederman | 86a71dbd | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 32 | inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */ |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 33 | inode->i_mode = table->mode; |
Al Viro | 5c06fe7 | 2008-11-16 22:19:10 +0000 | [diff] [blame] | 34 | inode->i_uid = inode->i_gid = 0; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 35 | if (!table->child) { |
| 36 | inode->i_mode |= S_IFREG; |
| 37 | inode->i_op = &proc_sys_inode_operations; |
| 38 | inode->i_fop = &proc_sys_file_operations; |
| 39 | } else { |
| 40 | inode->i_mode |= S_IFDIR; |
| 41 | inode->i_nlink = 0; |
| 42 | inode->i_op = &proc_sys_dir_operations; |
| 43 | inode->i_fop = &proc_sys_dir_file_operations; |
| 44 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 45 | out: |
| 46 | return inode; |
| 47 | } |
| 48 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 49 | static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 50 | { |
| 51 | int len; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 52 | for ( ; p->ctl_name || p->procname; p++) { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 53 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 54 | if (!p->procname) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 55 | continue; |
| 56 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 57 | len = strlen(p->procname); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 58 | if (len != name->len) |
| 59 | continue; |
| 60 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 61 | if (memcmp(p->procname, name->name, len) != 0) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 62 | continue; |
| 63 | |
| 64 | /* I have a match */ |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 65 | return p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Adrian Bunk | 8132436 | 2008-10-03 00:33:54 +0400 | [diff] [blame] | 70 | static struct ctl_table_header *grab_header(struct inode *inode) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 71 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 72 | if (PROC_I(inode)->sysctl) |
| 73 | return sysctl_head_grab(PROC_I(inode)->sysctl); |
| 74 | else |
| 75 | return sysctl_head_next(NULL); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, |
| 79 | struct nameidata *nd) |
| 80 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 81 | struct ctl_table_header *head = grab_header(dir); |
| 82 | struct ctl_table *table = PROC_I(dir)->sysctl_entry; |
| 83 | struct ctl_table_header *h = NULL; |
| 84 | struct qstr *name = &dentry->d_name; |
| 85 | struct ctl_table *p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 86 | struct inode *inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 87 | struct dentry *err = ERR_PTR(-ENOENT); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 88 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 89 | if (IS_ERR(head)) |
| 90 | return ERR_CAST(head); |
| 91 | |
| 92 | if (table && !table->child) { |
| 93 | WARN_ON(1); |
| 94 | goto out; |
| 95 | } |
| 96 | |
| 97 | table = table ? table->child : head->ctl_table; |
| 98 | |
| 99 | p = find_in_table(table, name); |
| 100 | if (!p) { |
| 101 | for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { |
| 102 | if (h->attached_to != table) |
| 103 | continue; |
| 104 | p = find_in_table(h->attached_by, name); |
| 105 | if (p) |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!p) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 111 | goto out; |
| 112 | |
| 113 | err = ERR_PTR(-ENOMEM); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 114 | inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); |
| 115 | if (h) |
| 116 | sysctl_head_finish(h); |
| 117 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 118 | if (!inode) |
| 119 | goto out; |
| 120 | |
| 121 | err = NULL; |
| 122 | dentry->d_op = &proc_sys_dentry_operations; |
| 123 | d_add(dentry, inode); |
| 124 | |
| 125 | out: |
| 126 | sysctl_head_finish(head); |
| 127 | return err; |
| 128 | } |
| 129 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 130 | static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, |
| 131 | size_t count, loff_t *ppos, int write) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 132 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 133 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 134 | struct ctl_table_header *head = grab_header(inode); |
| 135 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
David Howells | 2a2da53 | 2007-10-25 15:27:40 +0100 | [diff] [blame] | 136 | ssize_t error; |
| 137 | size_t res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 138 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 139 | if (IS_ERR(head)) |
| 140 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * At this point we know that the sysctl was not unregistered |
| 144 | * and won't be until we finish. |
| 145 | */ |
| 146 | error = -EPERM; |
Pavel Emelyanov | d7321cd | 2008-04-29 01:02:44 -0700 | [diff] [blame] | 147 | if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 148 | goto out; |
| 149 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 150 | /* if that can happen at all, it should be -EINVAL, not -EISDIR */ |
| 151 | error = -EINVAL; |
| 152 | if (!table->proc_handler) |
| 153 | goto out; |
| 154 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 155 | /* careful: calling conventions are nasty here */ |
| 156 | res = count; |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 157 | error = table->proc_handler(table, write, filp, buf, &res, ppos); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 158 | if (!error) |
| 159 | error = res; |
| 160 | out: |
| 161 | sysctl_head_finish(head); |
| 162 | |
| 163 | return error; |
| 164 | } |
| 165 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 166 | static ssize_t proc_sys_read(struct file *filp, char __user *buf, |
| 167 | size_t count, loff_t *ppos) |
| 168 | { |
| 169 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); |
| 170 | } |
| 171 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 172 | static ssize_t proc_sys_write(struct file *filp, const char __user *buf, |
| 173 | size_t count, loff_t *ppos) |
| 174 | { |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 175 | 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] | 176 | } |
| 177 | |
| 178 | |
| 179 | static int proc_sys_fill_cache(struct file *filp, void *dirent, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 180 | filldir_t filldir, |
| 181 | struct ctl_table_header *head, |
| 182 | struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 183 | { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 184 | struct dentry *child, *dir = filp->f_path.dentry; |
| 185 | struct inode *inode; |
| 186 | struct qstr qname; |
| 187 | ino_t ino = 0; |
| 188 | unsigned type = DT_UNKNOWN; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 189 | |
| 190 | qname.name = table->procname; |
| 191 | qname.len = strlen(table->procname); |
| 192 | qname.hash = full_name_hash(qname.name, qname.len); |
| 193 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 194 | child = d_lookup(dir, &qname); |
| 195 | if (!child) { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 196 | child = d_alloc(dir, &qname); |
| 197 | if (child) { |
| 198 | inode = proc_sys_make_inode(dir->d_sb, head, table); |
| 199 | if (!inode) { |
| 200 | dput(child); |
| 201 | return -ENOMEM; |
| 202 | } else { |
| 203 | child->d_op = &proc_sys_dentry_operations; |
| 204 | d_add(child, inode); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 205 | } |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 206 | } else { |
| 207 | return -ENOMEM; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 210 | inode = child->d_inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 211 | ino = inode->i_ino; |
| 212 | type = inode->i_mode >> 12; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 213 | dput(child); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 214 | return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); |
| 215 | } |
| 216 | |
| 217 | static int scan(struct ctl_table_header *head, ctl_table *table, |
| 218 | unsigned long *pos, struct file *file, |
| 219 | void *dirent, filldir_t filldir) |
| 220 | { |
| 221 | |
| 222 | for (; table->ctl_name || table->procname; table++, (*pos)++) { |
| 223 | int res; |
| 224 | |
| 225 | /* Can't do anything without a proc name */ |
| 226 | if (!table->procname) |
| 227 | continue; |
| 228 | |
| 229 | if (*pos < file->f_pos) |
| 230 | continue; |
| 231 | |
| 232 | res = proc_sys_fill_cache(file, dirent, filldir, head, table); |
| 233 | if (res) |
| 234 | return res; |
| 235 | |
| 236 | file->f_pos = *pos + 1; |
| 237 | } |
| 238 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 242 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 243 | struct dentry *dentry = filp->f_path.dentry; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 244 | struct inode *inode = dentry->d_inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 245 | struct ctl_table_header *head = grab_header(inode); |
| 246 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 247 | struct ctl_table_header *h = NULL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 248 | unsigned long pos; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 249 | int ret = -EINVAL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 250 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 251 | if (IS_ERR(head)) |
| 252 | return PTR_ERR(head); |
| 253 | |
| 254 | if (table && !table->child) { |
| 255 | WARN_ON(1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 256 | goto out; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | table = table ? table->child : head->ctl_table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 260 | |
| 261 | ret = 0; |
| 262 | /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ |
| 263 | if (filp->f_pos == 0) { |
| 264 | if (filldir(dirent, ".", 1, filp->f_pos, |
| 265 | inode->i_ino, DT_DIR) < 0) |
| 266 | goto out; |
| 267 | filp->f_pos++; |
| 268 | } |
| 269 | if (filp->f_pos == 1) { |
| 270 | if (filldir(dirent, "..", 2, filp->f_pos, |
| 271 | parent_ino(dentry), DT_DIR) < 0) |
| 272 | goto out; |
| 273 | filp->f_pos++; |
| 274 | } |
| 275 | pos = 2; |
| 276 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 277 | ret = scan(head, table, &pos, filp, dirent, filldir); |
| 278 | if (ret) |
| 279 | goto out; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 280 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 281 | for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { |
| 282 | if (h->attached_to != table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 283 | continue; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 284 | ret = scan(h, h->attached_by, &pos, filp, dirent, filldir); |
| 285 | if (ret) { |
| 286 | sysctl_head_finish(h); |
| 287 | break; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | ret = 1; |
| 291 | out: |
| 292 | sysctl_head_finish(head); |
| 293 | return ret; |
| 294 | } |
| 295 | |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 296 | static int proc_sys_permission(struct inode *inode, int mask) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 297 | { |
| 298 | /* |
| 299 | * sysctl entries that are not writeable, |
| 300 | * are _NOT_ writeable, capabilities or not. |
| 301 | */ |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 302 | struct ctl_table_header *head; |
| 303 | struct ctl_table *table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 304 | int error; |
| 305 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 306 | /* Executable files are not allowed under /proc/sys/ */ |
| 307 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) |
| 308 | return -EACCES; |
| 309 | |
| 310 | head = grab_header(inode); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 311 | if (IS_ERR(head)) |
| 312 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 313 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 314 | table = PROC_I(inode)->sysctl_entry; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 315 | if (!table) /* global root - r-xr-xr-x */ |
| 316 | error = mask & MAY_WRITE ? -EACCES : 0; |
| 317 | else /* Use the permissions on the sysctl table entry */ |
| 318 | error = sysctl_perm(head->root, table, mask); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 319 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 320 | sysctl_head_finish(head); |
| 321 | return error; |
| 322 | } |
| 323 | |
| 324 | static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) |
| 325 | { |
| 326 | struct inode *inode = dentry->d_inode; |
| 327 | int error; |
| 328 | |
| 329 | if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 330 | return -EPERM; |
| 331 | |
| 332 | error = inode_change_ok(inode, attr); |
John Johansen | 9d0633c | 2007-05-08 00:29:44 -0700 | [diff] [blame] | 333 | if (!error) |
| 334 | error = inode_setattr(inode, attr); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 335 | |
| 336 | return error; |
| 337 | } |
| 338 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 339 | static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 340 | { |
| 341 | struct inode *inode = dentry->d_inode; |
| 342 | struct ctl_table_header *head = grab_header(inode); |
| 343 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 344 | |
| 345 | if (IS_ERR(head)) |
| 346 | return PTR_ERR(head); |
| 347 | |
| 348 | generic_fillattr(inode, stat); |
| 349 | if (table) |
| 350 | stat->mode = (stat->mode & S_IFMT) | table->mode; |
| 351 | |
| 352 | sysctl_head_finish(head); |
| 353 | return 0; |
| 354 | } |
| 355 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 356 | static const struct file_operations proc_sys_file_operations = { |
| 357 | .read = proc_sys_read, |
| 358 | .write = proc_sys_write, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | static const struct file_operations proc_sys_dir_file_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 362 | .readdir = proc_sys_readdir, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 363 | .llseek = generic_file_llseek, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 366 | static const struct inode_operations proc_sys_inode_operations = { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 367 | .permission = proc_sys_permission, |
| 368 | .setattr = proc_sys_setattr, |
| 369 | .getattr = proc_sys_getattr, |
| 370 | }; |
| 371 | |
| 372 | static const struct inode_operations proc_sys_dir_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 373 | .lookup = proc_sys_lookup, |
| 374 | .permission = proc_sys_permission, |
| 375 | .setattr = proc_sys_setattr, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 376 | .getattr = proc_sys_getattr, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 380 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 381 | return !PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 382 | } |
| 383 | |
| 384 | static int proc_sys_delete(struct dentry *dentry) |
| 385 | { |
| 386 | return !!PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 387 | } |
| 388 | |
| 389 | static int proc_sys_compare(struct dentry *dir, struct qstr *qstr, |
| 390 | struct qstr *name) |
| 391 | { |
| 392 | struct dentry *dentry = container_of(qstr, struct dentry, d_name); |
| 393 | if (qstr->len != name->len) |
| 394 | return 1; |
| 395 | if (memcmp(qstr->name, name->name, name->len)) |
| 396 | return 1; |
| 397 | return !sysctl_is_seen(PROC_I(dentry->d_inode)->sysctl); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static struct dentry_operations proc_sys_dentry_operations = { |
| 401 | .d_revalidate = proc_sys_revalidate, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 402 | .d_delete = proc_sys_delete, |
| 403 | .d_compare = proc_sys_compare, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 404 | }; |
| 405 | |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 406 | int __init proc_sys_init(void) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 407 | { |
Alexey Dobriyan | e167523 | 2008-10-03 00:23:32 +0400 | [diff] [blame] | 408 | struct proc_dir_entry *proc_sys_root; |
| 409 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 410 | proc_sys_root = proc_mkdir("sys", NULL); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 411 | proc_sys_root->proc_iops = &proc_sys_dir_operations; |
| 412 | proc_sys_root->proc_fops = &proc_sys_dir_file_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 413 | proc_sys_root->nlink = 0; |
| 414 | return 0; |
| 415 | } |