15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "nacl_io/pipe/pipe_node.h" 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <errno.h> 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <fcntl.h> 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <pthread.h> 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string.h> 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "nacl_io/ioctl.h" 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "nacl_io/kernel_handle.h" 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "nacl_io/pipe/pipe_event_emitter.h" 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace { 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const size_t kDefaultPipeSize = 512 * 1024; 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace nacl_io { 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PipeNode::PipeNode(Filesystem* fs) 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) : StreamNode(fs), pipe_(new PipeEventEmitter(kDefaultPipeSize)) { 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)PipeEventEmitter* PipeNode::GetEventEmitter() { 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return pipe_.get(); 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Error PipeNode::Read(const HandleAttr& attr, 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void* buf, 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) size_t count, 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int* out_bytes) { 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int ms = attr.IsBlocking() ? read_timeout_ : 0; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EventListenerLock wait(GetEventEmitter()); 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Error err = wait.WaitOnEvent(POLLIN, ms); 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (err == ETIMEDOUT) 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) err = EWOULDBLOCK; 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (err) 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return err; 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return GetEventEmitter()->Read_Locked(static_cast<char*>(buf), count, 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) out_bytes); 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Error PipeNode::Write(const HandleAttr& attr, 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const void* buf, 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) size_t count, 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int* out_bytes) { 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int ms = attr.IsBlocking() ? write_timeout_ : 0; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EventListenerLock wait(GetEventEmitter()); 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Error err = wait.WaitOnEvent(POLLOUT, ms); 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (err == ETIMEDOUT) 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) err = EWOULDBLOCK; 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (err) 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return err; 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return GetEventEmitter()->Write_Locked(static_cast<const char*>(buf), 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) count, out_bytes); 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace nacl_io 65