root/fs/proc/root.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. proc_lookuproot
  2. proc_readroot

   1 /*
   2  *  linux/fs/proc/root.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc root directory handling functions
   7  */
   8 
   9 #include <asm/segment.h>
  10 
  11 #include <linux/errno.h>
  12 #include <linux/sched.h>
  13 #include <linux/proc_fs.h>
  14 #include <linux/stat.h>
  15 
  16 static int proc_readroot(struct inode *, struct file *, struct dirent *, int);
  17 static int proc_lookuproot(struct inode *,const char *,int,struct inode **);
  18 
  19 static struct file_operations proc_root_operations = {
  20         NULL,                   /* lseek - default */
  21         NULL,                   /* read - bad */
  22         NULL,                   /* write - bad */
  23         proc_readroot,          /* readdir */
  24         NULL,                   /* select - default */
  25         NULL,                   /* ioctl - default */
  26         NULL,                   /* mmap */
  27         NULL,                   /* no special open code */
  28         NULL,                   /* no special release code */
  29         NULL                    /* no fsync */
  30 };
  31 
  32 /*
  33  * proc directories can do almost nothing..
  34  */
  35 struct inode_operations proc_root_inode_operations = {
  36         &proc_root_operations,  /* default base directory file-ops */
  37         NULL,                   /* create */
  38         proc_lookuproot,        /* lookup */
  39         NULL,                   /* link */
  40         NULL,                   /* unlink */
  41         NULL,                   /* symlink */
  42         NULL,                   /* mkdir */
  43         NULL,                   /* rmdir */
  44         NULL,                   /* mknod */
  45         NULL,                   /* rename */
  46         NULL,                   /* readlink */
  47         NULL,                   /* follow_link */
  48         NULL,                   /* bmap */
  49         NULL,                   /* truncate */
  50         NULL                    /* permission */
  51 };
  52 
  53 static struct proc_dir_entry root_dir[] = {
  54         { 1,1,"." },
  55         { 1,2,".." },
  56         { 2,7,"loadavg" },
  57         { 3,6,"uptime" },
  58         { 4,7,"meminfo" },
  59         { 5,4,"kmsg" },
  60         { 6,7,"version" },
  61         { 7,4,"self" }  /* will change inode # */
  62 };
  63 
  64 #define NR_ROOT_DIRENTRY ((sizeof (root_dir))/(sizeof (root_dir[0])))
  65 
  66 static int proc_lookuproot(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  67         struct inode ** result)
  68 {
  69         unsigned int pid, c;
  70         int i, ino;
  71 
  72         *result = NULL;
  73         if (!dir)
  74                 return -ENOENT;
  75         if (!S_ISDIR(dir->i_mode)) {
  76                 iput(dir);
  77                 return -ENOENT;
  78         }
  79         i = NR_ROOT_DIRENTRY;
  80         while (i-- > 0 && !proc_match(len,name,root_dir+i))
  81                 /* nothing */;
  82         if (i >= 0) {
  83                 ino = root_dir[i].low_ino;
  84                 if (ino == 1) {
  85                         *result = dir;
  86                         return 0;
  87                 }
  88                 if (ino == 7) /* self modifying inode ... */
  89                         ino = (current->pid << 16) + 2;
  90         } else {
  91                 pid = 0;
  92                 while (len-- > 0) {
  93                         c = *name - '0';
  94                         name++;
  95                         if (c > 9) {
  96                                 pid = 0;
  97                                 break;
  98                         }
  99                         pid *= 10;
 100                         pid += c;
 101                         if (pid & 0xffff0000) {
 102                                 pid = 0;
 103                                 break;
 104                         }
 105                 }
 106                 for (i = 0 ; i < NR_TASKS ; i++)
 107                         if (task[i] && task[i]->pid == pid)
 108                                 break;
 109                 if (!pid || i >= NR_TASKS) {
 110                         iput(dir);
 111                         return -ENOENT;
 112                 }
 113                 ino = (pid << 16) + 2;
 114         }
 115         if (!(*result = iget(dir->i_sb,ino))) {
 116                 iput(dir);
 117                 return -ENOENT;
 118         }
 119         iput(dir);
 120         return 0;
 121 }
 122 
 123 static int proc_readroot(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 124         struct dirent * dirent, int count)
 125 {
 126         struct task_struct * p;
 127         unsigned int nr,pid;
 128         int i,j;
 129 
 130         if (!inode || !S_ISDIR(inode->i_mode))
 131                 return -EBADF;
 132 repeat:
 133         nr = filp->f_pos;
 134         if (nr < NR_ROOT_DIRENTRY) {
 135                 struct proc_dir_entry * de = root_dir + nr;
 136 
 137                 filp->f_pos++;
 138                 i = de->namelen;
 139                 put_fs_long(de->low_ino, &dirent->d_ino);
 140                 put_fs_word(i,&dirent->d_reclen);
 141                 put_fs_byte(0,i+dirent->d_name);
 142                 j = i;
 143                 while (i--)
 144                         put_fs_byte(de->name[i], i+dirent->d_name);
 145                 return j;
 146         }
 147         nr -= NR_ROOT_DIRENTRY;
 148         if (nr >= NR_TASKS)
 149                 return 0;
 150         filp->f_pos++;
 151         p = task[nr];
 152         if (!p || !(pid = p->pid))
 153                 goto repeat;
 154         if (pid & 0xffff0000)
 155                 goto repeat;
 156         j = 10;
 157         i = 1;
 158         while (pid >= j) {
 159                 j *= 10;
 160                 i++;
 161         }
 162         j = i;
 163         put_fs_long((pid << 16)+2, &dirent->d_ino);
 164         put_fs_word(i, &dirent->d_reclen);
 165         put_fs_byte(0, i+dirent->d_name);
 166         while (i--) {
 167                 put_fs_byte('0'+(pid % 10), i+dirent->d_name);
 168                 pid /= 10;
 169         }
 170         return j;
 171 }

/* [previous][next][first][last][top][bottom][index][help] */