|
|
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 root 10:
11: #include <linux/sched.h>
12: #include <linux/mm.h> /* for get_free_page */
13: #include <asm/segment.h>
1.1.1.3 ! root 14: #include <linux/kernel.h>
1.1 root 15:
16: int read_pipe(struct m_inode * inode, char * buf, int count)
17: {
1.1.1.2 root 18: int chars, size, read = 0;
1.1 root 19:
1.1.1.2 root 20: while (count>0) {
21: while (!(size=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? */
24: return read;
1.1.1.3 ! root 25: if (current->signal & ~current->blocked)
! 26: return read?read:-ERESTARTSYS;
! 27: interruptible_sleep_on(& PIPE_READ_WAIT(*inode));
1.1.1.2 root 28: }
29: chars = PAGE_SIZE-PIPE_TAIL(*inode);
30: if (chars > count)
31: chars = count;
32: if (chars > size)
33: chars = size;
34: count -= chars;
35: read += chars;
36: size = PIPE_TAIL(*inode);
37: PIPE_TAIL(*inode) += chars;
38: PIPE_TAIL(*inode) &= (PAGE_SIZE-1);
39: while (chars-->0)
40: put_fs_byte(((char *)inode->i_size)[size++],buf++);
1.1 root 41: }
1.1.1.3 ! root 42: wake_up(& PIPE_WRITE_WAIT(*inode));
1.1.1.2 root 43: return read;
1.1 root 44: }
45:
46: int write_pipe(struct m_inode * inode, char * buf, int count)
47: {
1.1.1.2 root 48: int chars, size, written = 0;
1.1 root 49:
1.1.1.2 root 50: while (count>0) {
51: while (!(size=(PAGE_SIZE-1)-PIPE_SIZE(*inode))) {
1.1.1.3 ! root 52: wake_up(& PIPE_READ_WAIT(*inode));
1.1.1.2 root 53: if (inode->i_count != 2) { /* no readers */
1.1 root 54: current->signal |= (1<<(SIGPIPE-1));
1.1.1.2 root 55: return written?written:-1;
1.1 root 56: }
1.1.1.3 ! root 57: sleep_on(& PIPE_WRITE_WAIT(*inode));
1.1 root 58: }
1.1.1.2 root 59: chars = PAGE_SIZE-PIPE_HEAD(*inode);
60: if (chars > count)
61: chars = count;
62: if (chars > size)
63: chars = size;
64: count -= chars;
65: written += chars;
66: size = PIPE_HEAD(*inode);
67: PIPE_HEAD(*inode) += chars;
68: PIPE_HEAD(*inode) &= (PAGE_SIZE-1);
69: while (chars-->0)
70: ((char *)inode->i_size)[size++]=get_fs_byte(buf++);
1.1 root 71: }
1.1.1.3 ! root 72: wake_up(& PIPE_READ_WAIT(*inode));
1.1.1.2 root 73: return written;
1.1 root 74: }
75:
76: int sys_pipe(unsigned long * fildes)
77: {
78: struct m_inode * inode;
79: struct file * f[2];
80: int fd[2];
81: int i,j;
82:
83: j=0;
84: for(i=0;j<2 && i<NR_FILE;i++)
85: if (!file_table[i].f_count)
86: (f[j++]=i+file_table)->f_count++;
87: if (j==1)
88: f[0]->f_count=0;
89: if (j<2)
90: return -1;
91: j=0;
92: for(i=0;j<2 && i<NR_OPEN;i++)
93: if (!current->filp[i]) {
94: current->filp[ fd[j]=i ] = f[j];
95: j++;
96: }
97: if (j==1)
98: current->filp[fd[0]]=NULL;
99: if (j<2) {
100: f[0]->f_count=f[1]->f_count=0;
101: return -1;
102: }
103: if (!(inode=get_pipe_inode())) {
104: current->filp[fd[0]] =
105: current->filp[fd[1]] = NULL;
106: f[0]->f_count = f[1]->f_count = 0;
107: return -1;
108: }
109: f[0]->f_inode = f[1]->f_inode = inode;
110: f[0]->f_pos = f[1]->f_pos = 0;
111: f[0]->f_mode = 1; /* read */
112: f[1]->f_mode = 2; /* write */
113: put_fs_long(fd[0],0+fildes);
114: put_fs_long(fd[1],1+fildes);
115: return 0;
116: }
1.1.1.3 ! root 117:
! 118: int pipe_ioctl(struct m_inode *pino, int cmd, int arg)
! 119: {
! 120: switch (cmd) {
! 121: case FIONREAD:
! 122: verify_area((void *) arg,4);
! 123: put_fs_long(PIPE_SIZE(*pino),(unsigned long *) arg);
! 124: return 0;
! 125: default:
! 126: return -EINVAL;
! 127: }
! 128: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.