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

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