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         NULL                    /* can't fsync */
  30 };
  31 
  32 /*
  33  * proc directories can do almost nothing..
  34  */
  35 struct inode_operations proc_fd_inode_operations = {
  36         &proc_fd_operations,    /* default base directory file-ops */
  37         NULL,                   /* create */
  38         proc_lookupfd,          /* lookup */
  39         NULL,                   /* link */
  40         NULL,                   /* unlink */
  41         NULL,                   /* symlink */
  42         NULL,                   /* mkdir */
  43         NULL,                   /* rmdir */
  44         NULL,                   /* mknod */
  45         NULL,                   /* rename */
  46         NULL,                   /* readlink */
  47         NULL,                   /* follow_link */
  48         NULL,                   /* bmap */
  49         NULL,                   /* truncate */
  50         NULL                    /* permission */
  51 };
  52 
  53 static int proc_lookupfd(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  54         struct inode ** result)
  55 {
  56         unsigned int ino, pid, fd, c;
  57         struct task_struct * p;
  58         struct super_block * sb;
  59         int i;
  60 
  61         *result = NULL;
  62         ino = dir->i_ino;
  63         pid = ino >> 16;
  64         ino &= 0x0000ffff;
  65         ino -= 7;
  66         if (!dir)
  67                 return -ENOENT;
  68         sb = dir->i_sb;
  69         if (!pid || ino || !S_ISDIR(dir->i_mode)) {
  70                 iput(dir);
  71                 return -ENOENT;
  72         }
  73         if (!len || (name[0] == '.' && (len == 1 ||
  74             (name[1] == '.' && len == 2)))) {
  75                 if (len < 2) {
  76                         *result = dir;
  77                         return 0;
  78                 }
  79                 if (!(*result = iget(sb,(pid << 16)+2))) {
  80                         iput(dir);
  81                         return -ENOENT;
  82                 }
  83                 iput(dir);
  84                 return 0;
  85         }
  86         iput(dir);
  87         fd = 0;
  88         while (len-- > 0) {
  89                 c = *name - '0';
  90                 name++;
  91                 if (c > 9) {
  92                         fd = 0xfffff;
  93                         break;
  94                 }
  95                 fd *= 10;
  96                 fd += c;
  97                 if (fd & 0xffff0000) {
  98                         fd = 0xfffff;
  99                         break;
 100                 }
 101         }
 102         for (i = 0 ; i < NR_TASKS ; i++)
 103                 if ((p = task[i]) && p->pid == pid)
 104                         break;
 105         if (!pid || i >= NR_TASKS)
 106                 return -ENOENT;
 107 
 108         if (fd >= NR_OPEN || !p->files->fd[fd] || !p->files->fd[fd]->f_inode)
 109           return -ENOENT;
 110 
 111         ino = (pid << 16) + 0x100 + fd;
 112 
 113         if (!(*result = iget(sb,ino)))
 114                 return -ENOENT;
 115         return 0;
 116 }
 117 
 118 static int proc_readfd(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 119         struct dirent * dirent, int count)
 120 {
 121         struct task_struct * p;
 122         unsigned int fd, pid, ino;
 123         int i,j;
 124 
 125         if (!inode || !S_ISDIR(inode->i_mode))
 126                 return -EBADF;
 127         ino = inode->i_ino;
 128         pid = ino >> 16;
 129         ino &= 0x0000ffff;
 130         ino -= 7;
 131         if (ino)
 132                 return 0;
 133         while (1) {
 134                 fd = filp->f_pos;
 135                 filp->f_pos++;
 136                 if (fd < 2) {
 137                         i = j = fd+1;
 138                         if (!fd)
 139                                 fd = inode->i_ino;
 140                         else
 141                                 fd = (inode->i_ino & 0xffff0000) | 2;
 142                         put_fs_long(fd, &dirent->d_ino);
 143                         put_fs_word(i, &dirent->d_reclen);
 144                         put_fs_byte(0, i+dirent->d_name);
 145                         while (i--)
 146                                 put_fs_byte('.', i+dirent->d_name);
 147                         return j;
 148                 }
 149                 fd -= 2;
 150                 for (i = 1 ; i < NR_TASKS ; i++)
 151                         if ((p = task[i]) && p->pid == pid)
 152                                 break;
 153                 if (i >= NR_TASKS)
 154                         return 0;
 155                 if (fd >= NR_OPEN)
 156                   break;
 157 
 158                 if (!p->files->fd[fd] || !p->files->fd[fd]->f_inode)
 159                   continue;
 160 
 161                 j = 10;
 162                 i = 1;
 163                 while (fd >= j) {
 164                         j *= 10;
 165                         i++;
 166                 }
 167                 j = i;
 168                 ino = (pid << 16) + 0x100 + fd;
 169 
 170                 put_fs_long(ino, &dirent->d_ino);
 171                 put_fs_word(i, &dirent->d_reclen);
 172                 put_fs_byte(0, i+dirent->d_name);
 173                 while (i--) {
 174                         put_fs_byte('0'+(fd % 10), i+dirent->d_name);
 175                         fd /= 10;
 176                 }
 177                 return j;
 178         }
 179         return 0;
 180 }

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