root/fs/proc/base.c

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

DEFINITIONS

This source file includes following definitions.
  1. proc_match
  2. proc_lookupbase
  3. proc_readbase

   1 /*
   2  *  linux/fs/proc/base.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc base 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_readbase(struct inode *, struct file *, struct dirent *, int);
  17 static int proc_lookupbase(struct inode *,const char *,int,struct inode **);
  18 
  19 static struct file_operations proc_base_operations = {
  20         NULL,                   /* lseek - default */
  21         NULL,                   /* read - bad */
  22         NULL,                   /* write - bad */
  23         proc_readbase,          /* 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_base_inode_operations = {
  35         &proc_base_operations,  /* default base directory file-ops */
  36         NULL,                   /* create */
  37         proc_lookupbase,        /* 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 struct proc_dir_entry {
  52         unsigned short low_ino;
  53         unsigned short namelen;
  54         char * name;
  55 };
  56 
  57 static struct proc_dir_entry base_dir[] = {
  58         { 1,2,".." },
  59         { 2,1,"." },
  60         { 3,3,"mem" },
  61         { 4,3,"cwd" },
  62         { 5,4,"root" },
  63         { 6,3,"exe" },
  64         { 7,2,"fd" },
  65         { 8,3,"lib" }
  66 };
  67 
  68 #define NR_BASE_DIRENTRY ((sizeof (base_dir))/(sizeof (base_dir[0])))
  69 
  70 static int proc_match(int len,const char * name,struct proc_dir_entry * de)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72         register int same __asm__("ax");
  73 
  74         if (!de || !de->low_ino)
  75                 return 0;
  76         /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
  77         if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
  78                 return 1;
  79         if (de->namelen != len)
  80                 return 0;
  81         __asm__("cld\n\t"
  82                 "fs ; repe ; cmpsb\n\t"
  83                 "setz %%al"
  84                 :"=a" (same)
  85                 :"0" (0),"S" ((long) name),"D" ((long) de->name),"c" (len)
  86                 :"cx","di","si");
  87         return same;
  88 }
  89 
  90 static int proc_lookupbase(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  91         struct inode ** result)
  92 {
  93         unsigned int pid;
  94         int i, ino;
  95 
  96         *result = NULL;
  97         if (!dir)
  98                 return -ENOENT;
  99         if (!S_ISDIR(dir->i_mode)) {
 100                 iput(dir);
 101                 return -ENOENT;
 102         }
 103         ino = dir->i_ino;
 104         pid = ino >> 16;
 105         i = NR_BASE_DIRENTRY;
 106         while (i-- > 0 && !proc_match(len,name,base_dir+i))
 107                 /* nothing */;
 108         if (i < 0) {
 109                 iput(dir);
 110                 return -ENOENT;
 111         }
 112         if (base_dir[i].low_ino == 1)
 113                 ino = 1;
 114         else
 115                 ino = (pid << 16) + base_dir[i].low_ino;
 116         for (i = 0 ; i < NR_TASKS ; i++)
 117                 if (task[i] && task[i]->pid == pid)
 118                         break;
 119         if (!pid || i >= NR_TASKS) {
 120                 iput(dir);
 121                 return -ENOENT;
 122         }
 123         if (!(*result = iget(dir->i_sb,ino))) {
 124                 iput(dir);
 125                 return -ENOENT;
 126         }
 127         iput(dir);
 128         return 0;
 129 }
 130 
 131 static int proc_readbase(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 132         struct dirent * dirent, int count)
 133 {
 134         struct proc_dir_entry * de;
 135         unsigned int pid, ino;
 136         int i,j;
 137 
 138         if (!inode || !S_ISDIR(inode->i_mode))
 139                 return -EBADF;
 140         ino = inode->i_ino;
 141         pid = ino >> 16;
 142         for (i = 0 ; i < NR_TASKS ; i++)
 143                 if (task[i] && task[i]->pid == pid)
 144                         break;
 145         if (!pid || i >= NR_TASKS)
 146                 return 0;
 147         if (((unsigned) filp->f_pos) < NR_BASE_DIRENTRY) {
 148                 de = base_dir + filp->f_pos;
 149                 filp->f_pos++;
 150                 i = de->namelen;
 151                 ino = de->low_ino;
 152                 if (ino != 1)
 153                         ino |= (pid << 16);
 154                 put_fs_long(ino, &dirent->d_ino);
 155                 put_fs_word(i,&dirent->d_reclen);
 156                 put_fs_byte(0,i+dirent->d_name);
 157                 j = i;
 158                 while (i--)
 159                         put_fs_byte(de->name[i], i+dirent->d_name);
 160                 return j;
 161         }
 162         return 0;
 163 }

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