4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/proc_fs.h>
7 #include <linux/security.h>
10 static const struct dentry_operations proc_sys_dentry_operations;
11 static const struct file_operations proc_sys_file_operations;
12 static const struct inode_operations proc_sys_inode_operations;
13 static const struct file_operations proc_sys_dir_file_operations;
14 static const struct inode_operations proc_sys_dir_operations;
16 static struct inode *proc_sys_make_inode(struct super_block *sb,
17 struct ctl_table_header *head, struct ctl_table *table)
20 struct proc_inode *ei;
22 inode = new_inode(sb);
26 sysctl_head_get(head);
29 ei->sysctl_entry = table;
31 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
32 inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
33 inode->i_mode = table->mode;
35 inode->i_mode |= S_IFREG;
36 inode->i_op = &proc_sys_inode_operations;
37 inode->i_fop = &proc_sys_file_operations;
39 inode->i_mode |= S_IFDIR;
41 inode->i_op = &proc_sys_dir_operations;
42 inode->i_fop = &proc_sys_dir_file_operations;
48 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
51 for ( ; p->procname; p++) {
56 len = strlen(p->procname);
60 if (memcmp(p->procname, name->name, len) != 0)
69 static struct ctl_table_header *grab_header(struct inode *inode)
71 if (PROC_I(inode)->sysctl)
72 return sysctl_head_grab(PROC_I(inode)->sysctl);
74 return sysctl_head_next(NULL);
77 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
80 struct ctl_table_header *head = grab_header(dir);
81 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
82 struct ctl_table_header *h = NULL;
83 struct qstr *name = &dentry->d_name;
86 struct dentry *err = ERR_PTR(-ENOENT);
89 return ERR_CAST(head);
91 if (table && !table->child) {
96 table = table ? table->child : head->ctl_table;
98 p = find_in_table(table, name);
100 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
101 if (h->attached_to != table)
103 p = find_in_table(h->attached_by, name);
112 err = ERR_PTR(-ENOMEM);
113 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
115 sysctl_head_finish(h);
121 dentry->d_op = &proc_sys_dentry_operations;
122 d_add(dentry, inode);
125 sysctl_head_finish(head);
129 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
130 size_t count, loff_t *ppos, int write)
132 struct inode *inode = filp->f_path.dentry->d_inode;
133 struct ctl_table_header *head = grab_header(inode);
134 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
139 return PTR_ERR(head);
142 * At this point we know that the sysctl was not unregistered
143 * and won't be until we finish.
146 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
149 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
151 if (!table->proc_handler)
154 /* careful: calling conventions are nasty here */
156 error = table->proc_handler(table, write, buf, &res, ppos);
160 sysctl_head_finish(head);
165 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
166 size_t count, loff_t *ppos)
168 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
171 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
172 size_t count, loff_t *ppos)
174 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
178 static int proc_sys_fill_cache(struct file *filp, void *dirent,
180 struct ctl_table_header *head,
181 struct ctl_table *table)
183 struct dentry *child, *dir = filp->f_path.dentry;
187 unsigned type = DT_UNKNOWN;
189 qname.name = table->procname;
190 qname.len = strlen(table->procname);
191 qname.hash = full_name_hash(qname.name, qname.len);
193 child = d_lookup(dir, &qname);
195 child = d_alloc(dir, &qname);
197 inode = proc_sys_make_inode(dir->d_sb, head, table);
202 child->d_op = &proc_sys_dentry_operations;
209 inode = child->d_inode;
211 type = inode->i_mode >> 12;
213 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
216 static int scan(struct ctl_table_header *head, ctl_table *table,
217 unsigned long *pos, struct file *file,
218 void *dirent, filldir_t filldir)
221 for (; table->procname; table++, (*pos)++) {
224 /* Can't do anything without a proc name */
225 if (!table->procname)
228 if (*pos < file->f_pos)
231 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
235 file->f_pos = *pos + 1;
240 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
242 struct dentry *dentry = filp->f_path.dentry;
243 struct inode *inode = dentry->d_inode;
244 struct ctl_table_header *head = grab_header(inode);
245 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
246 struct ctl_table_header *h = NULL;
251 return PTR_ERR(head);
253 if (table && !table->child) {
258 table = table ? table->child : head->ctl_table;
261 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
262 if (filp->f_pos == 0) {
263 if (filldir(dirent, ".", 1, filp->f_pos,
264 inode->i_ino, DT_DIR) < 0)
268 if (filp->f_pos == 1) {
269 if (filldir(dirent, "..", 2, filp->f_pos,
270 parent_ino(dentry), DT_DIR) < 0)
276 ret = scan(head, table, &pos, filp, dirent, filldir);
280 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
281 if (h->attached_to != table)
283 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
285 sysctl_head_finish(h);
291 sysctl_head_finish(head);
295 static int proc_sys_permission(struct inode *inode, int mask)
298 * sysctl entries that are not writeable,
299 * are _NOT_ writeable, capabilities or not.
301 struct ctl_table_header *head;
302 struct ctl_table *table;
305 /* Executable files are not allowed under /proc/sys/ */
306 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
309 head = grab_header(inode);
311 return PTR_ERR(head);
313 table = PROC_I(inode)->sysctl_entry;
314 if (!table) /* global root - r-xr-xr-x */
315 error = mask & MAY_WRITE ? -EACCES : 0;
316 else /* Use the permissions on the sysctl table entry */
317 error = sysctl_perm(head->root, table, mask);
319 sysctl_head_finish(head);
323 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
325 struct inode *inode = dentry->d_inode;
328 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
331 error = inode_change_ok(inode, attr);
335 if ((attr->ia_valid & ATTR_SIZE) &&
336 attr->ia_size != i_size_read(inode)) {
337 error = vmtruncate(inode, attr->ia_size);
342 setattr_copy(inode, attr);
343 mark_inode_dirty(inode);
347 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
349 struct inode *inode = dentry->d_inode;
350 struct ctl_table_header *head = grab_header(inode);
351 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
354 return PTR_ERR(head);
356 generic_fillattr(inode, stat);
358 stat->mode = (stat->mode & S_IFMT) | table->mode;
360 sysctl_head_finish(head);
364 static const struct file_operations proc_sys_file_operations = {
365 .read = proc_sys_read,
366 .write = proc_sys_write,
367 .llseek = default_llseek,
370 static const struct file_operations proc_sys_dir_file_operations = {
371 .readdir = proc_sys_readdir,
372 .llseek = generic_file_llseek,
375 static const struct inode_operations proc_sys_inode_operations = {
376 .permission = proc_sys_permission,
377 .setattr = proc_sys_setattr,
378 .getattr = proc_sys_getattr,
381 static const struct inode_operations proc_sys_dir_operations = {
382 .lookup = proc_sys_lookup,
383 .permission = proc_sys_permission,
384 .setattr = proc_sys_setattr,
385 .getattr = proc_sys_getattr,
388 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
390 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
393 static int proc_sys_delete(struct dentry *dentry)
395 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
398 static int proc_sys_compare(struct dentry *dir, struct qstr *qstr,
401 struct dentry *dentry = container_of(qstr, struct dentry, d_name);
402 if (qstr->len != name->len)
404 if (memcmp(qstr->name, name->name, name->len))
406 return !sysctl_is_seen(PROC_I(dentry->d_inode)->sysctl);
409 static const struct dentry_operations proc_sys_dentry_operations = {
410 .d_revalidate = proc_sys_revalidate,
411 .d_delete = proc_sys_delete,
412 .d_compare = proc_sys_compare,
415 int __init proc_sys_init(void)
417 struct proc_dir_entry *proc_sys_root;
419 proc_sys_root = proc_mkdir("sys", NULL);
420 proc_sys_root->proc_iops = &proc_sys_dir_operations;
421 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
422 proc_sys_root->nlink = 0;