2.2.0-final
[davej-history.git] / fs / proc / inode.c
blob44c4916f8598a8984b3708ba3a0ad707a3364da3
1 /*
2 * linux/fs/proc/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/sched.h>
8 #include <linux/proc_fs.h>
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/string.h>
12 #include <linux/stat.h>
13 #include <linux/file.h>
14 #include <linux/locks.h>
15 #include <linux/limits.h>
16 #include <linux/config.h>
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
21 extern void free_proc_entry(struct proc_dir_entry *);
23 struct proc_dir_entry * de_get(struct proc_dir_entry *de)
25 if (de)
26 de->count++;
27 return de;
31 * Decrements the use count and checks for deferred deletion.
33 void de_put(struct proc_dir_entry *de)
35 if (de) {
36 if (!de->count) {
37 printk("de_put: entry %s already free!\n", de->name);
38 return;
41 if (!--de->count) {
42 if (de->deleted) {
43 printk("de_put: deferred delete of %s\n",
44 de->name);
45 free_proc_entry(de);
51 static void proc_put_inode(struct inode *inode)
53 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
54 if ((inode->i_ino >= PROC_OPENPROM_FIRST) &&
55 (inode->i_ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM) &&
56 proc_openprom_use)
57 (*proc_openprom_use)(inode, 0);
58 #endif
60 * Kill off unused inodes ... VFS will unhash and
61 * delete the inode if we set i_nlink to zero.
63 if (inode->i_count == 1)
64 inode->i_nlink = 0;
68 * Decrement the use count of the proc_dir_entry.
70 static void proc_delete_inode(struct inode *inode)
72 struct proc_dir_entry *de = inode->u.generic_ip;
74 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
75 if ((inode->i_ino >= PROC_OPENPROM_FIRST) &&
76 (inode->i_ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM))
77 return;
78 #endif
80 if (de) {
82 * Call the fill_inode hook to release module counts.
84 if (de->fill_inode)
85 de->fill_inode(inode, 0);
86 de_put(de);
90 static struct super_operations proc_sops = {
91 proc_read_inode,
92 proc_write_inode,
93 proc_put_inode,
94 proc_delete_inode, /* delete_inode(struct inode *) */
95 NULL,
96 NULL,
97 NULL,
98 proc_statfs,
99 NULL
103 static int parse_options(char *options,uid_t *uid,gid_t *gid)
105 char *this_char,*value;
107 *uid = current->uid;
108 *gid = current->gid;
109 if (!options) return 1;
110 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
111 if ((value = strchr(this_char,'=')) != NULL)
112 *value++ = 0;
113 if (!strcmp(this_char,"uid")) {
114 if (!value || !*value)
115 return 0;
116 *uid = simple_strtoul(value,&value,0);
117 if (*value)
118 return 0;
120 else if (!strcmp(this_char,"gid")) {
121 if (!value || !*value)
122 return 0;
123 *gid = simple_strtoul(value,&value,0);
124 if (*value)
125 return 0;
127 else return 1;
129 return 1;
133 * The standard rules, copied from fs/namei.c:permission().
135 static int standard_permission(struct inode *inode, int mask)
137 int mode = inode->i_mode;
139 if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
140 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
141 return -EROFS; /* Nobody gets write access to a read-only fs */
142 else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
143 return -EACCES; /* Nobody gets write access to an immutable file */
144 else if (current->fsuid == inode->i_uid)
145 mode >>= 6;
146 else if (in_group_p(inode->i_gid))
147 mode >>= 3;
148 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
149 return 0;
150 /* read and search access */
151 if ((mask == S_IROTH) ||
152 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
153 if (capable(CAP_DAC_READ_SEARCH))
154 return 0;
155 return -EACCES;
159 * Set up permission rules for processes looking at other processes.
160 * You're not allowed to see a process unless it has the same or more
161 * restricted root than your own. This prevents a chrooted processes
162 * from escaping through the /proc entries of less restricted
163 * processes, and thus allows /proc to be safely mounted in a chrooted
164 * area.
166 * Note that root (uid 0) doesn't get permission for this either,
167 * since chroot is stronger than root.
169 * XXX TODO: use the dentry mechanism to make off-limits procs simply
170 * invisible rather than denied? Does each namespace root get its own
171 * dentry tree?
173 * This also applies the default permissions checks, as it only adds
174 * restrictions.
176 * Jeremy Fitzhardinge <jeremy@zip.com.au>
178 int proc_permission(struct inode *inode, int mask)
180 struct task_struct *p;
181 unsigned long ino = inode->i_ino;
182 unsigned long pid;
183 struct dentry *de, *base;
185 if (standard_permission(inode, mask) != 0)
186 return -EACCES;
189 * Find the root of the processes being examined (if any).
190 * XXX Surely there's a better way of doing this?
192 if (ino >= PROC_OPENPROM_FIRST &&
193 ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
194 return 0; /* already allowed */
196 pid = ino >> 16;
197 if (pid == 0)
198 return 0; /* already allowed */
200 de = NULL;
201 base = current->fs->root;
203 read_lock(&tasklist_lock);
204 p = find_task_by_pid(pid);
206 if (p && p->fs)
207 de = p->fs->root;
208 read_unlock(&tasklist_lock); /* FIXME! */
210 if (p == NULL)
211 return -EACCES; /* ENOENT? */
213 if (de == NULL)
215 /* kswapd and bdflush don't have proper root or cwd... */
216 return -EACCES;
219 /* XXX locking? */
220 for(;;)
222 struct dentry *parent;
224 if (de == base)
225 return 0; /* already allowed */
227 de = de->d_covers;
228 parent = de->d_parent;
230 if (de == parent)
231 break;
233 de = parent;
236 return -EACCES; /* incompatible roots */
239 struct inode * proc_get_inode(struct super_block * sb, int ino,
240 struct proc_dir_entry * de)
242 struct inode * inode;
245 * Increment the use count so the dir entry can't disappear.
247 de_get(de);
248 #if 1
249 /* shouldn't ever happen */
250 if (de && de->deleted)
251 printk("proc_iget: using deleted entry %s, count=%d\n", de->name, de->count);
252 #endif
254 inode = iget(sb, ino);
255 if (!inode)
256 goto out_fail;
258 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
259 if ((inode->i_ino >= PROC_OPENPROM_FIRST)
260 && (inode->i_ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
261 && proc_openprom_use)
262 (*proc_openprom_use)(inode, 1);
263 #endif
264 /* N.B. How can this test ever fail?? */
265 if (inode->i_sb != sb)
266 printk("proc_get_inode: inode fubar\n");
268 inode->u.generic_ip = (void *) de;
269 if (de) {
270 if (de->mode) {
271 inode->i_mode = de->mode;
272 inode->i_uid = de->uid;
273 inode->i_gid = de->gid;
275 if (de->size)
276 inode->i_size = de->size;
277 if (de->ops)
278 inode->i_op = de->ops;
279 if (de->nlink)
280 inode->i_nlink = de->nlink;
282 * The fill_inode routine should use this call
283 * to increment module counts, if necessary.
285 if (de->fill_inode)
286 de->fill_inode(inode, 1);
289 * Fixup the root inode's nlink value
291 if (inode->i_ino == PROC_ROOT_INO) {
292 struct task_struct *p;
294 read_lock(&tasklist_lock);
295 for_each_task(p) {
296 if (p->pid)
297 inode->i_nlink++;
299 read_unlock(&tasklist_lock);
301 out:
302 return inode;
304 out_fail:
305 de_put(de);
306 goto out;
309 struct super_block *proc_read_super(struct super_block *s,void *data,
310 int silent)
312 struct inode * root_inode;
314 lock_super(s);
315 s->s_blocksize = 1024;
316 s->s_blocksize_bits = 10;
317 s->s_magic = PROC_SUPER_MAGIC;
318 s->s_op = &proc_sops;
319 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
320 if (!root_inode)
321 goto out_no_root;
322 s->s_root = d_alloc_root(root_inode, NULL);
323 if (!s->s_root)
324 goto out_no_root;
325 parse_options(data, &root_inode->i_uid, &root_inode->i_gid);
326 unlock_super(s);
327 return s;
329 out_no_root:
330 printk("proc_read_super: get root inode failed\n");
331 iput(root_inode);
332 s->s_dev = 0;
333 unlock_super(s);
334 return NULL;
337 int proc_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
339 struct statfs tmp;
341 tmp.f_type = PROC_SUPER_MAGIC;
342 tmp.f_bsize = PAGE_SIZE/sizeof(long);
343 tmp.f_blocks = 0;
344 tmp.f_bfree = 0;
345 tmp.f_bavail = 0;
346 tmp.f_files = 0;
347 tmp.f_ffree = 0;
348 tmp.f_namelen = NAME_MAX;
349 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
352 void proc_read_inode(struct inode * inode)
354 unsigned long ino, pid;
355 struct task_struct * p;
357 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
358 inode->i_blocks = 0;
359 inode->i_blksize = 1024;
360 ino = inode->i_ino;
361 if (ino >= PROC_OPENPROM_FIRST &&
362 ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
363 goto out;
364 inode->i_op = NULL;
365 inode->i_mode = 0;
366 inode->i_uid = 0;
367 inode->i_gid = 0;
368 inode->i_nlink = 1;
369 inode->i_size = 0;
371 pid = ino >> 16;
372 if (!pid)
373 goto out;
375 read_lock(&tasklist_lock);
376 p = find_task_by_pid(pid);
377 if (!p)
378 goto out_unlock;
380 ino &= 0x0000ffff;
381 if (ino == PROC_PID_INO || p->dumpable) {
382 inode->i_uid = p->euid;
383 inode->i_gid = p->egid;
385 if (ino & PROC_PID_FD_DIR) {
386 struct file * file;
387 ino &= 0x7fff;
388 file = fcheck_task(p, ino);
389 if (!file)
390 goto out_unlock;
392 inode->i_op = &proc_link_inode_operations;
393 inode->i_size = 64;
394 inode->i_mode = S_IFLNK;
395 if (file->f_mode & 1)
396 inode->i_mode |= S_IRUSR | S_IXUSR;
397 if (file->f_mode & 2)
398 inode->i_mode |= S_IWUSR | S_IXUSR;
400 out_unlock:
401 /* Defer unlocking until we're done with the task */
402 read_unlock(&tasklist_lock);
404 out:
405 return;
408 void proc_write_inode(struct inode * inode)