Annotation of linux/fs/pipe.c, revision 1.1.1.5

1.1.1.2   root        1: /*
                      2:  *  linux/fs/pipe.c
                      3:  *
                      4:  *  (C) 1991  Linus Torvalds
                      5:  */
                      6: 
1.1       root        7: #include <signal.h>
1.1.1.3   root        8: #include <errno.h>
                      9: #include <termios.h>
1.1.1.4   root       10: #include <fcntl.h>
1.1       root       11: 
                     12: #include <linux/sched.h>
                     13: #include <asm/segment.h>
1.1.1.3   root       14: #include <linux/kernel.h>
1.1       root       15: 
1.1.1.4   root       16: int pipe_read(struct inode * inode, struct file * filp, char * buf, int count)
1.1       root       17: {
1.1.1.2   root       18:        int chars, size, read = 0;
1.1       root       19: 
1.1.1.4   root       20:        if (!(filp->f_flags & O_NONBLOCK))
                     21:                while (!PIPE_SIZE(*inode)) {
1.1.1.3   root       22:                        wake_up(& PIPE_WRITE_WAIT(*inode));
1.1.1.2   root       23:                        if (inode->i_count != 2) /* are there any writers? */
1.1.1.4   root       24:                                return 0;
1.1.1.3   root       25:                        if (current->signal & ~current->blocked)
1.1.1.4   root       26:                                return -ERESTARTSYS;
1.1.1.3   root       27:                        interruptible_sleep_on(& PIPE_READ_WAIT(*inode));
1.1.1.2   root       28:                }
1.1.1.4   root       29:        while (count>0 && (size = PIPE_SIZE(*inode))) {
1.1.1.2   root       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++);
1.1       root       42:        }
1.1.1.3   root       43:        wake_up(& PIPE_WRITE_WAIT(*inode));
1.1.1.5 ! root       44:        return read?read:-EAGAIN;
1.1       root       45: }
                     46:        
1.1.1.4   root       47: int pipe_write(struct inode * inode, struct file * filp, char * buf, int count)
1.1       root       48: {
1.1.1.2   root       49:        int chars, size, written = 0;
1.1       root       50: 
1.1.1.2   root       51:        while (count>0) {
                     52:                while (!(size=(PAGE_SIZE-1)-PIPE_SIZE(*inode))) {
1.1.1.3   root       53:                        wake_up(& PIPE_READ_WAIT(*inode));
1.1.1.2   root       54:                        if (inode->i_count != 2) { /* no readers */
1.1       root       55:                                current->signal |= (1<<(SIGPIPE-1));
1.1.1.4   root       56:                                return written?written:-EINTR;
1.1       root       57:                        }
1.1.1.4   root       58:                        if (current->signal & ~current->blocked)
                     59:                                return written?written:-EINTR;
                     60:                        interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
1.1       root       61:                }
1.1.1.2   root       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++);
1.1       root       74:        }
1.1.1.3   root       75:        wake_up(& PIPE_READ_WAIT(*inode));
1.1.1.2   root       76:        return written;
1.1       root       77: }
                     78: 
                     79: int sys_pipe(unsigned long * fildes)
                     80: {
1.1.1.4   root       81:        struct inode * inode;
1.1       root       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: }
1.1.1.3   root      120: 
1.1.1.4   root      121: int pipe_ioctl(struct inode *pino, int cmd, int arg)
1.1.1.3   root      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: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.