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 };
  58 
  59 #define NR_ROOT_DIRENTRY ((sizeof (root_dir))/(sizeof (root_dir[0])))
  60 
  61 static int proc_lookuproot(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  62         struct inode ** result)
  63 {
  64         unsigned int pid, c;
  65         int i, ino;
  66 
  67         *result = NULL;
  68         if (!dir)
  69                 return -ENOENT;
  70         if (!S_ISDIR(dir->i_mode)) {
  71                 iput(dir);
  72                 return -ENOENT;
  73         }
  74         i = NR_ROOT_DIRENTRY;
  75         while (i-- > 0 && !proc_match(len,name,root_dir+i))
  76                 /* nothing */;
  77         if (i >= 0) {
  78                 ino = root_dir[i].low_ino;
  79                 if (ino == 1) {
  80                         *result = dir;
  81                         return 0;
  82                 }
  83         } else {
  84                 pid = 0;
  85                 while (len-- > 0) {
  86                         c = get_fs_byte(name) - '0';
  87                         name++;
  88                         if (c > 9) {
  89                                 pid = 0;
  90                                 break;
  91                         }
  92                         pid *= 10;
  93                         pid += c;
  94                         if (pid & 0xffff0000) {
  95                                 pid = 0;
  96                                 break;
  97                         }
  98                 }
  99                 for (i = 0 ; i < NR_TASKS ; i++)
 100                         if (task[i] && task[i]->pid == pid)
 101                                 break;
 102                 if (!pid || i >= NR_TASKS) {
 103                         iput(dir);
 104                         return -ENOENT;
 105                 }
 106                 ino = (pid << 16) + 2;
 107         }
 108         if (!(*result = iget(dir->i_sb,ino))) {
 109                 iput(dir);
 110                 return -ENOENT;
 111         }
 112         iput(dir);
 113         return 0;
 114 }
 115 
 116 static int proc_readroot(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 117         struct dirent * dirent, int count)
 118 {
 119         struct task_struct * p;
 120         unsigned int nr,pid;
 121         int i,j;
 122 
 123         if (!inode || !S_ISDIR(inode->i_mode))
 124                 return -EBADF;
 125 repeat:
 126         nr = filp->f_pos;
 127         if (nr < NR_ROOT_DIRENTRY) {
 128                 struct proc_dir_entry * de = root_dir + nr;
 129 
 130                 filp->f_pos++;
 131                 i = de->namelen;
 132                 put_fs_long(de->low_ino, &dirent->d_ino);
 133                 put_fs_word(i,&dirent->d_reclen);
 134                 put_fs_byte(0,i+dirent->d_name);
 135                 j = i;
 136                 while (i--)
 137                         put_fs_byte(de->name[i], i+dirent->d_name);
 138                 return j;
 139         }
 140         nr -= NR_ROOT_DIRENTRY;
 141         if (nr >= NR_TASKS)
 142                 return 0;
 143         filp->f_pos++;
 144         p = task[nr];
 145         if (!p || !(pid = p->pid))
 146                 goto repeat;
 147         if (pid & 0xffff0000)
 148                 goto repeat;
 149         j = 10;
 150         i = 1;
 151         while (pid >= j) {
 152                 j *= 10;
 153                 i++;
 154         }
 155         j = i;
 156         put_fs_long((pid << 16)+2, &dirent->d_ino);
 157         put_fs_word(i, &dirent->d_reclen);
 158         put_fs_byte(0, i+dirent->d_name);
 159         while (i--) {
 160                 put_fs_byte('0'+(pid % 10), i+dirent->d_name);
 161                 pid /= 10;
 162         }
 163         return j;
 164 }

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