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

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