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

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