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

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