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

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