root/fs/proc/fd.c

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

DEFINITIONS

This source file includes following definitions.
  1. proc_lookupfd
  2. proc_readfd

   1 /*
   2  *  linux/fs/proc/fd.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc fd 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_readfd(struct inode *, struct file *, struct dirent *, int);
  17 static int proc_lookupfd(struct inode *,const char *,int,struct inode **);
  18 
  19 static struct file_operations proc_fd_operations = {
  20         NULL,                   /* lseek - default */
  21         NULL,                   /* read - bad */
  22         NULL,                   /* write - bad */
  23         proc_readfd,            /* 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_fd_inode_operations = {
  35         &proc_fd_operations,    /* default base directory file-ops */
  36         NULL,                   /* create */
  37         proc_lookupfd,          /* 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 int proc_lookupfd(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  53         struct inode ** result)
  54 {
  55         unsigned int ino, pid, fd, c;
  56         struct task_struct * p;
  57         struct super_block * sb;
  58         int i;
  59 
  60         *result = NULL;
  61         ino = dir->i_ino;
  62         pid = ino >> 16;
  63         ino &= 0x0000ffff;
  64         ino -= 7;
  65         if (!dir)
  66                 return -ENOENT;
  67         sb = dir->i_sb;
  68         if (!pid || ino > 1 || !S_ISDIR(dir->i_mode)) {
  69                 iput(dir);
  70                 return -ENOENT;
  71         }
  72         if (!len || (get_fs_byte(name) == '.' && (len == 1 ||
  73             (get_fs_byte(name+1) == '.' && len == 2)))) {
  74                 if (len < 2) {
  75                         *result = dir;
  76                         return 0;
  77                 }
  78                 if (!(*result = iget(sb,(pid << 16)+2))) {
  79                         iput(dir);
  80                         return -ENOENT;
  81                 }
  82                 iput(dir);
  83                 return 0;
  84         }
  85         iput(dir);
  86         fd = 0;
  87         while (len-- > 0) {
  88                 c = get_fs_byte(name) - '0';
  89                 name++;
  90                 if (c > 9) {
  91                         fd = 0xfffff;
  92                         break;
  93                 }
  94                 fd *= 10;
  95                 fd += c;
  96                 if (fd & 0xffff0000) {
  97                         fd = 0xfffff;
  98                         break;
  99                 }
 100         }
 101         for (i = 0 ; i < NR_TASKS ; i++)
 102                 if ((p = task[i]) && p->pid == pid)
 103                         break;
 104         if (!pid || i >= NR_TASKS)
 105                 return -ENOENT;
 106         if (!ino) {
 107                 if (fd >= NR_OPEN || !p->filp[fd] || !p->filp[fd]->f_inode)
 108                         return -ENOENT;
 109                 ino = (pid << 16) + 0x100 + fd;
 110         } else {
 111                 if (fd >= p->numlibraries)
 112                         return -ENOENT;
 113                 ino = (pid << 16) + 0x200 + fd;
 114         }
 115         if (!(*result = iget(sb,ino)))
 116                 return -ENOENT;
 117         return 0;
 118 }
 119 
 120 static int proc_readfd(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 121         struct dirent * dirent, int count)
 122 {
 123         struct task_struct * p;
 124         unsigned int fd, pid, ino;
 125         int i,j;
 126 
 127         if (!inode || !S_ISDIR(inode->i_mode))
 128                 return -EBADF;
 129         ino = inode->i_ino;
 130         pid = ino >> 16;
 131         ino &= 0x0000ffff;
 132         ino -= 7;
 133         if (ino > 1)
 134                 return 0;
 135         while (1) {
 136                 fd = filp->f_pos;
 137                 filp->f_pos++;
 138                 if (fd < 2) {
 139                         i = j = fd+1;
 140                         if (!fd)
 141                                 fd = inode->i_ino;
 142                         else
 143                                 fd = (inode->i_ino & 0xffff0000) | 2;
 144                         put_fs_long(fd, &dirent->d_ino);
 145                         put_fs_word(i, &dirent->d_reclen);
 146                         put_fs_byte(0, i+dirent->d_name);
 147                         while (i--)
 148                                 put_fs_byte('.', i+dirent->d_name);
 149                         return j;
 150                 }
 151                 fd -= 2;
 152                 for (i = 1 ; i < NR_TASKS ; i++)
 153                         if ((p = task[i]) && p->pid == pid)
 154                                 break;
 155                 if (i >= NR_TASKS)
 156                         return 0;
 157                 if (!ino) {
 158                         if (fd >= NR_OPEN)
 159                                 break;
 160                         if (!p->filp[fd] || !p->filp[fd]->f_inode)
 161                                 continue;
 162                 } else
 163                         if (fd >= p->numlibraries)
 164                                 break;
 165                 j = 10;
 166                 i = 1;
 167                 while (fd >= j) {
 168                         j *= 10;
 169                         i++;
 170                 }
 171                 j = i;
 172                 if (!ino)
 173                         ino = (pid << 16) + 0x100 + fd;
 174                 else
 175                         ino = (pid << 16) + 0x200 + fd;
 176                 put_fs_long(ino, &dirent->d_ino);
 177                 put_fs_word(i, &dirent->d_reclen);
 178                 put_fs_byte(0, i+dirent->d_name);
 179                 while (i--) {
 180                         put_fs_byte('0'+(fd % 10), i+dirent->d_name);
 181                         fd /= 10;
 182                 }
 183                 return j;
 184         }
 185         return 0;
 186 }

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