4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/proc_fs.h>
7 #include <linux/security.h>
8 #include <linux/namei.h>
11 static const struct dentry_operations proc_sys_dentry_operations;
12 static const struct file_operations proc_sys_file_operations;
13 static const struct inode_operations proc_sys_inode_operations;
14 static const struct file_operations proc_sys_dir_file_operations;
15 static const struct inode_operations proc_sys_dir_operations;
17 static struct inode *proc_sys_make_inode(struct super_block *sb,
18 struct ctl_table_header *head, struct ctl_table *table)
21 struct proc_inode *ei;
23 inode = new_inode(sb);
27 inode->i_ino = get_next_ino();
29 sysctl_head_get(head);
32 ei->sysctl_entry = table;
34 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
35 inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
36 inode->i_mode = table->mode;
38 inode->i_mode |= S_IFREG;
39 inode->i_op = &proc_sys_inode_operations;
40 inode->i_fop = &proc_sys_file_operations;
42 inode->i_mode |= S_IFDIR;
44 inode->i_op = &proc_sys_dir_operations;
45 inode->i_fop = &proc_sys_dir_file_operations;
51 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
54 for ( ; p->procname; p++) {
59 len = strlen(p->procname);
63 if (memcmp(p->procname, name->name, len) != 0)
72 static struct ctl_table_header *grab_header(struct inode *inode)
74 if (PROC_I(inode)->sysctl)
75 return sysctl_head_grab(PROC_I(inode)->sysctl);
77 return sysctl_head_next(NULL);
80 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
83 struct ctl_table_header *head = grab_header(dir);
84 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
85 struct ctl_table_header *h = NULL;
86 struct qstr *name = &dentry->d_name;
89 struct dentry *err = ERR_PTR(-ENOENT);
92 return ERR_CAST(head);
94 if (table && !table->child) {
99 table = table ? table->child : head->ctl_table;
101 p = find_in_table(table, name);
103 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
104 if (h->attached_to != table)
106 p = find_in_table(h->attached_by, name);
115 err = ERR_PTR(-ENOMEM);
116 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
118 sysctl_head_finish(h);
124 d_set_d_op(dentry, &proc_sys_dentry_operations);
125 d_add(dentry, inode);
128 sysctl_head_finish(head);
132 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
133 size_t count, loff_t *ppos, int write)
135 struct inode *inode = filp->f_path.dentry->d_inode;
136 struct ctl_table_header *head = grab_header(inode);
137 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
142 return PTR_ERR(head);
145 * At this point we know that the sysctl was not unregistered
146 * and won't be until we finish.
149 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
152 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
154 if (!table->proc_handler)
157 /* careful: calling conventions are nasty here */
159 error = table->proc_handler(table, write, buf, &res, ppos);
163 sysctl_head_finish(head);
168 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
169 size_t count, loff_t *ppos)
171 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
174 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
175 size_t count, loff_t *ppos)
177 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
181 static int proc_sys_fill_cache(struct file *filp, void *dirent,
183 struct ctl_table_header *head,
184 struct ctl_table *table)
186 struct dentry *child, *dir = filp->f_path.dentry;
190 unsigned type = DT_UNKNOWN;
192 qname.name = table->procname;
193 qname.len = strlen(table->procname);
194 qname.hash = full_name_hash(qname.name, qname.len);
196 child = d_lookup(dir, &qname);
198 child = d_alloc(dir, &qname);
200 inode = proc_sys_make_inode(dir->d_sb, head, table);
205 d_set_d_op(child, &proc_sys_dentry_operations);
212 inode = child->d_inode;
214 type = inode->i_mode >> 12;
216 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
219 static int scan(struct ctl_table_header *head, ctl_table *table,
220 unsigned long *pos, struct file *file,
221 void *dirent, filldir_t filldir)
224 for (; table->procname; table++, (*pos)++) {
227 /* Can't do anything without a proc name */
228 if (!table->procname)
231 if (*pos < file->f_pos)
234 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
238 file->f_pos = *pos + 1;
243 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
245 struct dentry *dentry = filp->f_path.dentry;
246 struct inode *inode = dentry->d_inode;
247 struct ctl_table_header *head = grab_header(inode);
248 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
249 struct ctl_table_header *h = NULL;
254 return PTR_ERR(head);
256 if (table && !table->child) {
261 table = table ? table->child : head->ctl_table;
264 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
265 if (filp->f_pos == 0) {
266 if (filldir(dirent, ".", 1, filp->f_pos,
267 inode->i_ino, DT_DIR) < 0)
271 if (filp->f_pos == 1) {
272 if (filldir(dirent, "..", 2, filp->f_pos,
273 parent_ino(dentry), DT_DIR) < 0)
279 ret = scan(head, table, &pos, filp, dirent, filldir);
283 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
284 if (h->attached_to != table)
286 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
288 sysctl_head_finish(h);
294 sysctl_head_finish(head);
298 static int proc_sys_permission(struct inode *inode, int mask,unsigned int flags)
301 * sysctl entries that are not writeable,
302 * are _NOT_ writeable, capabilities or not.
304 struct ctl_table_header *head;
305 struct ctl_table *table;
308 if (flags & IPERM_FLAG_RCU)
311 /* Executable files are not allowed under /proc/sys/ */
312 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
315 head = grab_header(inode);
317 return PTR_ERR(head);
319 table = PROC_I(inode)->sysctl_entry;
320 if (!table) /* global root - r-xr-xr-x */
321 error = mask & MAY_WRITE ? -EACCES : 0;
322 else /* Use the permissions on the sysctl table entry */
323 error = sysctl_perm(head->root, table, mask);
325 sysctl_head_finish(head);
329 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
331 struct inode *inode = dentry->d_inode;
334 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
337 error = inode_change_ok(inode, attr);
341 if ((attr->ia_valid & ATTR_SIZE) &&
342 attr->ia_size != i_size_read(inode)) {
343 error = vmtruncate(inode, attr->ia_size);
348 setattr_copy(inode, attr);
349 mark_inode_dirty(inode);
353 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
355 struct inode *inode = dentry->d_inode;
356 struct ctl_table_header *head = grab_header(inode);
357 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
360 return PTR_ERR(head);
362 generic_fillattr(inode, stat);
364 stat->mode = (stat->mode & S_IFMT) | table->mode;
366 sysctl_head_finish(head);
370 static const struct file_operations proc_sys_file_operations = {
371 .read = proc_sys_read,
372 .write = proc_sys_write,
373 .llseek = default_llseek,
376 static const struct file_operations proc_sys_dir_file_operations = {
377 .readdir = proc_sys_readdir,
378 .llseek = generic_file_llseek,
381 static const struct inode_operations proc_sys_inode_operations = {
382 .permission = proc_sys_permission,
383 .setattr = proc_sys_setattr,
384 .getattr = proc_sys_getattr,
387 static const struct inode_operations proc_sys_dir_operations = {
388 .lookup = proc_sys_lookup,
389 .permission = proc_sys_permission,
390 .setattr = proc_sys_setattr,
391 .getattr = proc_sys_getattr,
394 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
396 if (nd->flags & LOOKUP_RCU)
398 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
401 static int proc_sys_delete(const struct dentry *dentry)
403 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
406 static int proc_sys_compare(const struct dentry *parent,
407 const struct inode *pinode,
408 const struct dentry *dentry, const struct inode *inode,
409 unsigned int len, const char *str, const struct qstr *name)
411 /* Although proc doesn't have negative dentries, rcu-walk means
412 * that inode here can be NULL */
415 if (name->len != len)
417 if (memcmp(name->name, str, len))
419 return !sysctl_is_seen(PROC_I(inode)->sysctl);
422 static const struct dentry_operations proc_sys_dentry_operations = {
423 .d_revalidate = proc_sys_revalidate,
424 .d_delete = proc_sys_delete,
425 .d_compare = proc_sys_compare,
428 int __init proc_sys_init(void)
430 struct proc_dir_entry *proc_sys_root;
432 proc_sys_root = proc_mkdir("sys", NULL);
433 proc_sys_root->proc_iops = &proc_sys_dir_operations;
434 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
435 proc_sys_root->nlink = 0;