Import 2.3.26pre2
[davej-history.git] / fs / proc / inode.c
bloba5596e4ee404ed9e8b17afb7edeff8e66458e147
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 struct super_block *proc_super_blocks = NULL;
92 static void proc_put_super(struct super_block *sb)
94 struct super_block **p = &proc_super_blocks;
95 while (*p != sb) {
96 if (!*p) /* should never happen */
97 return;
98 p = (struct super_block **)&(*p)->u.generic_sbp;
100 *p = (struct super_block *)(*p)->u.generic_sbp;
103 static struct super_operations proc_sops = {
104 proc_read_inode,
105 proc_write_inode,
106 proc_put_inode,
107 proc_delete_inode, /* delete_inode(struct inode *) */
108 NULL,
109 proc_put_super,
110 NULL,
111 proc_statfs,
112 NULL
116 static int parse_options(char *options,uid_t *uid,gid_t *gid)
118 char *this_char,*value;
120 *uid = current->uid;
121 *gid = current->gid;
122 if (!options) return 1;
123 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
124 if ((value = strchr(this_char,'=')) != NULL)
125 *value++ = 0;
126 if (!strcmp(this_char,"uid")) {
127 if (!value || !*value)
128 return 0;
129 *uid = simple_strtoul(value,&value,0);
130 if (*value)
131 return 0;
133 else if (!strcmp(this_char,"gid")) {
134 if (!value || !*value)
135 return 0;
136 *gid = simple_strtoul(value,&value,0);
137 if (*value)
138 return 0;
140 else return 1;
142 return 1;
146 * The standard rules, copied from fs/namei.c:permission().
148 static int standard_permission(struct inode *inode, int mask)
150 int mode = inode->i_mode;
152 if ((mask & S_IWOTH) && IS_RDONLY(inode) &&
153 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
154 return -EROFS; /* Nobody gets write access to a read-only fs */
155 else if ((mask & S_IWOTH) && IS_IMMUTABLE(inode))
156 return -EACCES; /* Nobody gets write access to an immutable file */
157 else if (current->fsuid == inode->i_uid)
158 mode >>= 6;
159 else if (in_group_p(inode->i_gid))
160 mode >>= 3;
161 if (((mode & mask & S_IRWXO) == mask) || capable(CAP_DAC_OVERRIDE))
162 return 0;
163 /* read and search access */
164 if ((mask == S_IROTH) ||
165 (S_ISDIR(mode) && !(mask & ~(S_IROTH | S_IXOTH))))
166 if (capable(CAP_DAC_READ_SEARCH))
167 return 0;
168 return -EACCES;
172 * Set up permission rules for processes looking at other processes.
173 * You're not allowed to see a process unless it has the same or more
174 * restricted root than your own. This prevents a chrooted processes
175 * from escaping through the /proc entries of less restricted
176 * processes, and thus allows /proc to be safely mounted in a chrooted
177 * area.
179 * Note that root (uid 0) doesn't get permission for this either,
180 * since chroot is stronger than root.
182 * XXX TODO: use the dentry mechanism to make off-limits procs simply
183 * invisible rather than denied? Does each namespace root get its own
184 * dentry tree?
186 * This also applies the default permissions checks, as it only adds
187 * restrictions.
189 * Jeremy Fitzhardinge <jeremy@zip.com.au>
191 int proc_permission(struct inode *inode, int mask)
193 struct task_struct *p;
194 unsigned long ino = inode->i_ino;
195 unsigned long pid;
196 struct dentry *de, *base;
198 if (standard_permission(inode, mask) != 0)
199 return -EACCES;
202 * Find the root of the processes being examined (if any).
203 * XXX Surely there's a better way of doing this?
205 if (ino >= PROC_OPENPROM_FIRST &&
206 ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
207 return 0; /* already allowed */
209 pid = ino >> 16;
210 if (pid == 0)
211 return 0; /* already allowed */
213 de = NULL;
214 base = current->fs->root;
216 read_lock(&tasklist_lock);
217 p = find_task_by_pid(pid);
219 if (p && p->fs)
220 de = p->fs->root;
221 read_unlock(&tasklist_lock); /* FIXME! */
223 if (p == NULL)
224 return -EACCES; /* ENOENT? */
226 if (de == NULL)
228 /* kswapd and bdflush don't have proper root or cwd... */
229 return -EACCES;
232 /* XXX locking? */
233 for(;;)
235 struct dentry *parent;
237 if (de == base)
238 return 0; /* already allowed */
240 de = de->d_covers;
241 parent = de->d_parent;
243 if (de == parent)
244 break;
246 de = parent;
249 return -EACCES; /* incompatible roots */
252 struct inode * proc_get_inode(struct super_block * sb, int ino,
253 struct proc_dir_entry * de)
255 struct inode * inode;
258 * Increment the use count so the dir entry can't disappear.
260 de_get(de);
261 #if 1
262 /* shouldn't ever happen */
263 if (de && de->deleted)
264 printk("proc_iget: using deleted entry %s, count=%d\n", de->name, de->count);
265 #endif
267 inode = iget(sb, ino);
268 if (!inode)
269 goto out_fail;
271 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
272 if ((inode->i_ino >= PROC_OPENPROM_FIRST)
273 && (inode->i_ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
274 && proc_openprom_use)
275 (*proc_openprom_use)(inode, 1);
276 #endif
277 /* N.B. How can this test ever fail?? */
278 if (inode->i_sb != sb)
279 printk("proc_get_inode: inode fubar\n");
281 inode->u.generic_ip = (void *) de;
282 if (de) {
283 if (de->mode) {
284 inode->i_mode = de->mode;
285 inode->i_uid = de->uid;
286 inode->i_gid = de->gid;
288 if (de->size)
289 inode->i_size = de->size;
290 if (de->ops)
291 inode->i_op = de->ops;
292 if (de->nlink)
293 inode->i_nlink = de->nlink;
295 * The fill_inode routine should use this call
296 * to increment module counts, if necessary.
298 if (de->fill_inode)
299 de->fill_inode(inode, 1);
302 * Fixup the root inode's nlink value
304 if (inode->i_ino == PROC_ROOT_INO) {
305 struct task_struct *p;
307 read_lock(&tasklist_lock);
308 for_each_task(p) {
309 if (p->pid)
310 inode->i_nlink++;
312 read_unlock(&tasklist_lock);
314 out:
315 return inode;
317 out_fail:
318 de_put(de);
319 goto out;
322 struct super_block *proc_read_super(struct super_block *s,void *data,
323 int silent)
325 struct inode * root_inode;
327 lock_super(s);
328 s->s_blocksize = 1024;
329 s->s_blocksize_bits = 10;
330 s->s_magic = PROC_SUPER_MAGIC;
331 s->s_op = &proc_sops;
332 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
333 if (!root_inode)
334 goto out_no_root;
335 s->s_root = d_alloc_root(root_inode);
336 if (!s->s_root)
337 goto out_no_root;
338 parse_options(data, &root_inode->i_uid, &root_inode->i_gid);
339 s->u.generic_sbp = (void*) proc_super_blocks;
340 proc_super_blocks = s;
341 unlock_super(s);
342 return s;
344 out_no_root:
345 printk("proc_read_super: get root inode failed\n");
346 iput(root_inode);
347 s->s_dev = 0;
348 unlock_super(s);
349 return NULL;
352 int proc_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
354 struct statfs tmp;
356 tmp.f_type = PROC_SUPER_MAGIC;
357 tmp.f_bsize = PAGE_SIZE/sizeof(long);
358 tmp.f_blocks = 0;
359 tmp.f_bfree = 0;
360 tmp.f_bavail = 0;
361 tmp.f_files = 0;
362 tmp.f_ffree = 0;
363 tmp.f_namelen = NAME_MAX;
364 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
367 void proc_read_inode(struct inode * inode)
369 unsigned long ino, pid;
370 struct task_struct * p;
372 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
373 inode->i_blocks = 0;
374 inode->i_blksize = 1024;
375 ino = inode->i_ino;
376 if (ino >= PROC_OPENPROM_FIRST &&
377 ino < PROC_OPENPROM_FIRST + PROC_NOPENPROM)
378 goto out;
379 inode->i_op = NULL;
380 inode->i_mode = 0;
381 inode->i_uid = 0;
382 inode->i_gid = 0;
383 inode->i_nlink = 1;
384 inode->i_size = 0;
386 pid = ino >> 16;
387 if (!pid)
388 goto out;
390 read_lock(&tasklist_lock);
391 p = find_task_by_pid(pid);
392 if (!p)
393 goto out_unlock;
395 ino &= 0x0000ffff;
396 if (ino == PROC_PID_INO || p->dumpable) {
397 inode->i_uid = p->euid;
398 inode->i_gid = p->egid;
400 if (ino & PROC_PID_FD_DIR) {
401 struct file * file;
402 ino &= 0x7fff;
403 if (!p->files) /* can we ever get here if that's the case? */
404 goto out_unlock;
405 read_lock(&p->files->file_lock);
406 file = fcheck_task(p, ino);
407 if (!file)
408 goto out_unlock2;
410 inode->i_op = &proc_link_inode_operations;
411 inode->i_size = 64;
412 inode->i_mode = S_IFLNK;
413 if (file->f_mode & 1)
414 inode->i_mode |= S_IRUSR | S_IXUSR;
415 if (file->f_mode & 2)
416 inode->i_mode |= S_IWUSR | S_IXUSR;
417 out_unlock2:
418 read_unlock(&p->files->file_lock);
420 out_unlock:
421 /* Defer unlocking until we're done with the task */
422 read_unlock(&tasklist_lock);
424 out:
425 return;
428 void proc_write_inode(struct inode * inode)