root/fs/pipe.c

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

DEFINITIONS

This source file includes following definitions.
  1. pipe_read
  2. pipe_write
  3. sys_pipe
  4. pipe_ioctl

   1 /*
   2  *  linux/fs/pipe.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #include <signal.h>
   8 #include <errno.h>
   9 #include <termios.h>
  10 #include <fcntl.h>
  11 
  12 #include <linux/sched.h>
  13 #include <asm/segment.h>
  14 #include <linux/kernel.h>
  15 
  16 int pipe_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  17 {
  18         int chars, size, read = 0;
  19 
  20         if (!(filp->f_flags & O_NONBLOCK))
  21                 while (!PIPE_SIZE(*inode)) {
  22                         wake_up(& PIPE_WRITE_WAIT(*inode));
  23                         if (inode->i_count != 2) /* are there any writers? */
  24                                 return 0;
  25                         if (current->signal & ~current->blocked)
  26                                 return -ERESTARTSYS;
  27                         interruptible_sleep_on(& PIPE_READ_WAIT(*inode));
  28                 }
  29         while (count>0 && (size = PIPE_SIZE(*inode))) {
  30                 chars = PAGE_SIZE-PIPE_TAIL(*inode);
  31                 if (chars > count)
  32                         chars = count;
  33                 if (chars > size)
  34                         chars = size;
  35                 count -= chars;
  36                 read += chars;
  37                 size = PIPE_TAIL(*inode);
  38                 PIPE_TAIL(*inode) += chars;
  39                 PIPE_TAIL(*inode) &= (PAGE_SIZE-1);
  40                 while (chars-->0)
  41                         put_fs_byte(((char *)inode->i_size)[size++],buf++);
  42         }
  43         wake_up(& PIPE_WRITE_WAIT(*inode));
  44         return read?read:-EAGAIN;
  45 }
  46         
  47 int pipe_write(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         int chars, size, written = 0;
  50 
  51         while (count>0) {
  52                 while (!(size=(PAGE_SIZE-1)-PIPE_SIZE(*inode))) {
  53                         wake_up(& PIPE_READ_WAIT(*inode));
  54                         if (inode->i_count != 2) { /* no readers */
  55                                 current->signal |= (1<<(SIGPIPE-1));
  56                                 return written?written:-EINTR;
  57                         }
  58                         if (current->signal & ~current->blocked)
  59                                 return written?written:-EINTR;
  60                         interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
  61                 }
  62                 chars = PAGE_SIZE-PIPE_HEAD(*inode);
  63                 if (chars > count)
  64                         chars = count;
  65                 if (chars > size)
  66                         chars = size;
  67                 count -= chars;
  68                 written += chars;
  69                 size = PIPE_HEAD(*inode);
  70                 PIPE_HEAD(*inode) += chars;
  71                 PIPE_HEAD(*inode) &= (PAGE_SIZE-1);
  72                 while (chars-->0)
  73                         ((char *)inode->i_size)[size++]=get_fs_byte(buf++);
  74         }
  75         wake_up(& PIPE_READ_WAIT(*inode));
  76         return written;
  77 }
  78 
  79 int sys_pipe(unsigned long * fildes)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81         struct inode * inode;
  82         struct file * f[2];
  83         int fd[2];
  84         int i,j;
  85 
  86         j=0;
  87         for(i=0;j<2 && i<NR_FILE;i++)
  88                 if (!file_table[i].f_count)
  89                         (f[j++]=i+file_table)->f_count++;
  90         if (j==1)
  91                 f[0]->f_count=0;
  92         if (j<2)
  93                 return -1;
  94         j=0;
  95         for(i=0;j<2 && i<NR_OPEN;i++)
  96                 if (!current->filp[i]) {
  97                         current->filp[ fd[j]=i ] = f[j];
  98                         j++;
  99                 }
 100         if (j==1)
 101                 current->filp[fd[0]]=NULL;
 102         if (j<2) {
 103                 f[0]->f_count=f[1]->f_count=0;
 104                 return -1;
 105         }
 106         if (!(inode=get_pipe_inode())) {
 107                 current->filp[fd[0]] =
 108                         current->filp[fd[1]] = NULL;
 109                 f[0]->f_count = f[1]->f_count = 0;
 110                 return -1;
 111         }
 112         f[0]->f_inode = f[1]->f_inode = inode;
 113         f[0]->f_pos = f[1]->f_pos = 0;
 114         f[0]->f_mode = 1;               /* read */
 115         f[1]->f_mode = 2;               /* write */
 116         put_fs_long(fd[0],0+fildes);
 117         put_fs_long(fd[1],1+fildes);
 118         return 0;
 119 }
 120 
 121 int pipe_ioctl(struct inode *pino, int cmd, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         switch (cmd) {
 124                 case FIONREAD:
 125                         verify_area((void *) arg,4);
 126                         put_fs_long(PIPE_SIZE(*pino),(unsigned long *) arg);
 127                         return 0;
 128                 default:
 129                         return -EINVAL;
 130         }
 131 }

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