4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/sched.h>
10 #include <linux/namei.h>
14 static const struct dentry_operations proc_sys_dentry_operations;
15 static const struct file_operations proc_sys_file_operations;
16 static const struct inode_operations proc_sys_inode_operations;
17 static const struct file_operations proc_sys_dir_file_operations;
18 static const struct inode_operations proc_sys_dir_operations;
20 void proc_sys_poll_notify(struct ctl_table_poll *poll)
25 atomic_inc(&poll->event);
26 wake_up_interruptible(&poll->wait);
29 static struct inode *proc_sys_make_inode(struct super_block *sb,
30 struct ctl_table_header *head, struct ctl_table *table)
33 struct proc_inode *ei;
35 inode = new_inode(sb);
39 inode->i_ino = get_next_ino();
41 sysctl_head_get(head);
44 ei->sysctl_entry = table;
46 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
47 inode->i_mode = table->mode;
49 inode->i_mode |= S_IFREG;
50 inode->i_op = &proc_sys_inode_operations;
51 inode->i_fop = &proc_sys_file_operations;
53 inode->i_mode |= S_IFDIR;
55 inode->i_op = &proc_sys_dir_operations;
56 inode->i_fop = &proc_sys_dir_file_operations;
62 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
65 for ( ; p->procname; p++) {
70 len = strlen(p->procname);
74 if (memcmp(p->procname, name->name, len) != 0)
83 static struct ctl_table_header *grab_header(struct inode *inode)
85 if (PROC_I(inode)->sysctl)
86 return sysctl_head_grab(PROC_I(inode)->sysctl);
88 return sysctl_head_next(NULL);
91 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
94 struct ctl_table_header *head = grab_header(dir);
95 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
96 struct ctl_table_header *h = NULL;
97 struct qstr *name = &dentry->d_name;
100 struct dentry *err = ERR_PTR(-ENOENT);
103 return ERR_CAST(head);
105 if (table && !table->child) {
110 table = table ? table->child : head->ctl_table;
112 p = find_in_table(table, name);
114 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
115 if (h->attached_to != table)
117 p = find_in_table(h->attached_by, name);
126 err = ERR_PTR(-ENOMEM);
127 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
129 sysctl_head_finish(h);
135 d_set_d_op(dentry, &proc_sys_dentry_operations);
136 d_add(dentry, inode);
139 sysctl_head_finish(head);
143 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
144 size_t count, loff_t *ppos, int write)
146 struct inode *inode = filp->f_path.dentry->d_inode;
147 struct ctl_table_header *head = grab_header(inode);
148 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
153 return PTR_ERR(head);
156 * At this point we know that the sysctl was not unregistered
157 * and won't be until we finish.
160 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
163 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
165 if (!table->proc_handler)
168 /* careful: calling conventions are nasty here */
170 error = table->proc_handler(table, write, buf, &res, ppos);
174 sysctl_head_finish(head);
179 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
180 size_t count, loff_t *ppos)
182 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
185 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
186 size_t count, loff_t *ppos)
188 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
191 static int proc_sys_open(struct inode *inode, struct file *filp)
193 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
196 filp->private_data = proc_sys_poll_event(table->poll);
201 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
203 struct inode *inode = filp->f_path.dentry->d_inode;
204 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
205 unsigned long event = (unsigned long)filp->private_data;
206 unsigned int ret = DEFAULT_POLLMASK;
208 if (!table->proc_handler)
214 poll_wait(filp, &table->poll->wait, wait);
216 if (event != atomic_read(&table->poll->event)) {
217 filp->private_data = proc_sys_poll_event(table->poll);
218 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
225 static int proc_sys_fill_cache(struct file *filp, void *dirent,
227 struct ctl_table_header *head,
228 struct ctl_table *table)
230 struct dentry *child, *dir = filp->f_path.dentry;
234 unsigned type = DT_UNKNOWN;
236 qname.name = table->procname;
237 qname.len = strlen(table->procname);
238 qname.hash = full_name_hash(qname.name, qname.len);
240 child = d_lookup(dir, &qname);
242 child = d_alloc(dir, &qname);
244 inode = proc_sys_make_inode(dir->d_sb, head, table);
249 d_set_d_op(child, &proc_sys_dentry_operations);
256 inode = child->d_inode;
258 type = inode->i_mode >> 12;
260 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
263 static int scan(struct ctl_table_header *head, ctl_table *table,
264 unsigned long *pos, struct file *file,
265 void *dirent, filldir_t filldir)
268 for (; table->procname; table++, (*pos)++) {
271 /* Can't do anything without a proc name */
272 if (!table->procname)
275 if (*pos < file->f_pos)
278 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
282 file->f_pos = *pos + 1;
287 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
289 struct dentry *dentry = filp->f_path.dentry;
290 struct inode *inode = dentry->d_inode;
291 struct ctl_table_header *head = grab_header(inode);
292 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
293 struct ctl_table_header *h = NULL;
298 return PTR_ERR(head);
300 if (table && !table->child) {
305 table = table ? table->child : head->ctl_table;
308 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
309 if (filp->f_pos == 0) {
310 if (filldir(dirent, ".", 1, filp->f_pos,
311 inode->i_ino, DT_DIR) < 0)
315 if (filp->f_pos == 1) {
316 if (filldir(dirent, "..", 2, filp->f_pos,
317 parent_ino(dentry), DT_DIR) < 0)
323 ret = scan(head, table, &pos, filp, dirent, filldir);
327 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
328 if (h->attached_to != table)
330 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
332 sysctl_head_finish(h);
338 sysctl_head_finish(head);
342 static int proc_sys_permission(struct inode *inode, int mask)
345 * sysctl entries that are not writeable,
346 * are _NOT_ writeable, capabilities or not.
348 struct ctl_table_header *head;
349 struct ctl_table *table;
352 /* Executable files are not allowed under /proc/sys/ */
353 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
356 head = grab_header(inode);
358 return PTR_ERR(head);
360 table = PROC_I(inode)->sysctl_entry;
361 if (!table) /* global root - r-xr-xr-x */
362 error = mask & MAY_WRITE ? -EACCES : 0;
363 else /* Use the permissions on the sysctl table entry */
364 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
366 sysctl_head_finish(head);
370 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
372 struct inode *inode = dentry->d_inode;
375 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
378 error = inode_change_ok(inode, attr);
382 if ((attr->ia_valid & ATTR_SIZE) &&
383 attr->ia_size != i_size_read(inode)) {
384 error = vmtruncate(inode, attr->ia_size);
389 setattr_copy(inode, attr);
390 mark_inode_dirty(inode);
394 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
396 struct inode *inode = dentry->d_inode;
397 struct ctl_table_header *head = grab_header(inode);
398 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
401 return PTR_ERR(head);
403 generic_fillattr(inode, stat);
405 stat->mode = (stat->mode & S_IFMT) | table->mode;
407 sysctl_head_finish(head);
411 static const struct file_operations proc_sys_file_operations = {
412 .open = proc_sys_open,
413 .poll = proc_sys_poll,
414 .read = proc_sys_read,
415 .write = proc_sys_write,
416 .llseek = default_llseek,
419 static const struct file_operations proc_sys_dir_file_operations = {
420 .read = generic_read_dir,
421 .readdir = proc_sys_readdir,
422 .llseek = generic_file_llseek,
425 static const struct inode_operations proc_sys_inode_operations = {
426 .permission = proc_sys_permission,
427 .setattr = proc_sys_setattr,
428 .getattr = proc_sys_getattr,
431 static const struct inode_operations proc_sys_dir_operations = {
432 .lookup = proc_sys_lookup,
433 .permission = proc_sys_permission,
434 .setattr = proc_sys_setattr,
435 .getattr = proc_sys_getattr,
438 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
440 if (nd->flags & LOOKUP_RCU)
442 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
445 static int proc_sys_delete(const struct dentry *dentry)
447 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
450 static int proc_sys_compare(const struct dentry *parent,
451 const struct inode *pinode,
452 const struct dentry *dentry, const struct inode *inode,
453 unsigned int len, const char *str, const struct qstr *name)
455 struct ctl_table_header *head;
456 /* Although proc doesn't have negative dentries, rcu-walk means
457 * that inode here can be NULL */
458 /* AV: can it, indeed? */
461 if (name->len != len)
463 if (memcmp(name->name, str, len))
465 head = rcu_dereference(PROC_I(inode)->sysctl);
466 return !head || !sysctl_is_seen(head);
469 static const struct dentry_operations proc_sys_dentry_operations = {
470 .d_revalidate = proc_sys_revalidate,
471 .d_delete = proc_sys_delete,
472 .d_compare = proc_sys_compare,
475 int __init proc_sys_init(void)
477 struct proc_dir_entry *proc_sys_root;
479 proc_sys_root = proc_mkdir("sys", NULL);
480 proc_sys_root->proc_iops = &proc_sys_dir_operations;
481 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
482 proc_sys_root->nlink = 0;