static ssize_t pipe_write(struct file *filp, const char *buf, size_t count, loff_t *ppos) { struct inode *inode = filp->f_dentry->d_inode; ssize_t free, written, ret; /* Seeks are not allowed on pipes. */ ret = -ESPIPE; written = 0; if (ppos != &filp->f_pos) goto out_nolock; /* Null write succeeds. */ ret = 0; if (count == 0) goto out_nolock; ret = -ERESTARTSYS; if (down_interruptible(PIPE_SEM(*inode))) goto out_nolock; /* No readers yields SIGPIPE. */ if (!PIPE_READERS(*inode)) goto sigpipe; /* If count <= PIPE_BUF, we have to make it atomic. */ free = (count <= PIPE_BUF ? count : 1); /* Wait, or check for, available space. */ if (filp->f_flags & O_NONBLOCK) { ret = -EAGAIN; if (PIPE_FREE(*inode) < free) goto out; } else { while (PIPE_FREE(*inode) < free) { PIPE_WAITING_WRITERS(*inode)++; pipe_wait(inode); PIPE_WAITING_WRITERS(*inode)--; ret = -ERESTARTSYS; if (signal_pending(current)) goto out; if (!PIPE_READERS(*inode)) goto sigpipe; } } /* Copy into available space. */ ret = -EFAULT; while (count > 0) { int space; char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode); ssize_t chars = PIPE_MAX_WCHUNK(*inode); if ((space = PIPE_FREE(*inode)) != 0) { if (chars > count) chars = count; if (chars > space) chars = space; if (copy_from_user(pipebuf, buf, chars)) goto out; written += chars; PIPE_LEN(*inode) += chars; count -= chars; buf += chars; space = PIPE_FREE(*inode); continue; } ret = written; if (filp->f_flags & O_NONBLOCK) break; do { /* * Synchronous wake-up: it knows that this process * is going to give up this CPU, so it doesn't have * to do idle reschedules. */ wake_up_interruptible_sync(PIPE_WAIT(*inode)); PIPE_WAITING_WRITERS(*inode)++; pipe_wait(inode); PIPE_WAITING_WRITERS(*inode)--; if (signal_pending(current)) goto out; if (!PIPE_READERS(*inode)) goto sigpipe; } while (!PIPE_FREE(*inode)); ret = -EFAULT; } /* Signal readers asynchronously that there is more data. */ wake_up_interruptible(PIPE_WAIT(*inode)); update_mctime(inode); out: up(PIPE_SEM(*inode)); out_nolock: if (written) ret = written; return ret; sigpipe: if (written) goto out; up(PIPE_SEM(*inode)); send_sig(SIGPIPE, current, 0); return -EPIPE; }
static ssize_t pipe_write(struct file * filp, const char * buf, size_t count, loff_t *ppos) { struct inode * inode = filp->f_dentry->d_inode; ssize_t chars = 0, free = 0, written = 0, err=0; char *pipebuf; if (ppos != &filp->f_pos) return -ESPIPE; if (!PIPE_READERS(*inode)) { /* no readers */ send_sig(SIGPIPE,current,0); return -EPIPE; } /* if count <= PIPE_BUF, we have to make it atomic */ if (count <= PIPE_BUF) free = count; else free = 1; /* can't do it atomically, wait for any free space */ up(&inode->i_sem); if (down_interruptible(&inode->i_atomic_write)) { down(&inode->i_sem); return -ERESTARTSYS; } while (count>0) { while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) { if (!PIPE_READERS(*inode)) { /* no readers */ send_sig(SIGPIPE,current,0); err = -EPIPE; goto errout; } if (signal_pending(current)) { err = -ERESTARTSYS; goto errout; } if (filp->f_flags & O_NONBLOCK) { err = -EAGAIN; goto errout; } interruptible_sleep_on(&PIPE_WAIT(*inode)); } PIPE_LOCK(*inode)++; while (count>0 && (free = PIPE_FREE(*inode))) { chars = PIPE_MAX_WCHUNK(*inode); if (chars > count) chars = count; if (chars > free) chars = free; pipebuf = PIPE_BASE(*inode)+PIPE_END(*inode); written += chars; PIPE_LEN(*inode) += chars; count -= chars; copy_from_user(pipebuf, buf, chars ); buf += chars; } PIPE_LOCK(*inode)--; wake_up_interruptible(&PIPE_WAIT(*inode)); free = 1; } inode->i_ctime = inode->i_mtime = CURRENT_TIME; mark_inode_dirty(inode); errout: up(&inode->i_atomic_write); down(&inode->i_sem); return written ? written : err; }
static ssize_t pipe_writev(struct file *filp, const struct iovec *_iov, unsigned long nr_segs, loff_t *ppos) { struct inode *inode = filp->f_dentry->d_inode; ssize_t ret; size_t min; int do_wakeup; struct iovec *iov = (struct iovec *)_iov; size_t total_len; total_len = iov_length(iov, nr_segs); /* Null write succeeds. */ if (unlikely(total_len == 0)) return 0; do_wakeup = 0; ret = 0; min = total_len; if (min > PIPE_BUF) min = 1; down(PIPE_SEM(*inode)); for (;;) { int free; if (!PIPE_READERS(*inode)) { send_sig(SIGPIPE, current, 0); if (!ret) ret = -EPIPE; break; } free = PIPE_FREE(*inode); if (free >= min) { /* transfer data */ ssize_t chars = PIPE_MAX_WCHUNK(*inode); char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode); /* Always wakeup, even if the copy fails. Otherwise * we lock up (O_NONBLOCK-)readers that sleep due to * syscall merging. */ do_wakeup = 1; if (chars > total_len) chars = total_len; if (chars > free) chars = free; if (pipe_iov_copy_from_user(pipebuf, iov, chars)) { if (!ret) ret = -EFAULT; break; } ret += chars; PIPE_LEN(*inode) += chars; total_len -= chars; if (!total_len) break; } if (PIPE_FREE(*inode) && ret) { /* handle cyclic data buffers */ min = 1; continue; } if (filp->f_flags & O_NONBLOCK) { if (!ret) ret = -EAGAIN; break; } if (signal_pending(current)) { if (!ret) ret = -ERESTARTSYS; break; } if (do_wakeup) { wake_up_interruptible_sync(PIPE_WAIT(*inode)); kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); do_wakeup = 0; } PIPE_WAITING_WRITERS(*inode)++; pipe_wait(inode); PIPE_WAITING_WRITERS(*inode)--; } up(PIPE_SEM(*inode)); if (do_wakeup) { wake_up_interruptible(PIPE_WAIT(*inode)); kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); } /* * Hack: we turn off atime updates for -RT kernels. * Who uses them on pipes anyway? */ #ifndef CONFIG_PREEMPT_RT if (ret > 0) inode_update_time(inode, 1); /* mtime and ctime */ #endif return ret; }
int PipeWrite(struct File* file,BYTE* buffer,DWORD size) { DWORD free = 0, chars; int written = 0; int err = 0; /* If we've got no readers on this pipe, let userspace know. */ if (PIPE_READERS(file) == 0) return -EPIPE; if (size <= PIPE_BUFFER_SIZE) free = size; else free = 1; while (size > 0) { while ((PIPE_FREE(file) < free || PIPE_LOCK(file))) { if (!PIPE_READERS(file) == 0) { err = -EPIPE; goto out; } if (file->flags & FILE_NONBLOCK) { goto out; } WAIT_ON(&PIPE_WAIT(file), PIPE_LOCK(file) == 0); } PIPE_LOCK(file)++; while (size > 0 && (free = PIPE_FREE(file))) { char* pipeBuf = PIPE_BUF(file) + PIPE_END(file); chars = PIPE_MAX_WCHUNK(file); if (chars > size) chars = size; if (chars > free) chars = size; /* Update counters. */ written += chars; PIPE_SIZE(file) += chars; size -= chars; memcpy(pipeBuf, buffer, chars); buffer += chars; } PIPE_LOCK(file)--; WakeUp(&PIPE_WAIT(file)); free = 1; } out: return (written > 0) ? (written) : (err); }