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

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