2012-08-02 08:34:36 -04:00
|
|
|
/*******************************************************************************
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-01-02 11:34:40 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
pipe.cpp
|
|
|
|
A device with a writing end and a reading end.
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-02 08:34:36 -04:00
|
|
|
*******************************************************************************/
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-01-02 11:34:40 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-03-21 10:26:08 -04:00
|
|
|
#include <sortix/fcntl.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/poll.h>
|
2013-01-02 11:34:40 -05:00
|
|
|
#include <sortix/signal.h>
|
|
|
|
#include <sortix/stat.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/copy.h>
|
|
|
|
#include <sortix/kernel/descriptor.h>
|
|
|
|
#include <sortix/kernel/dtable.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/inode.h>
|
|
|
|
#include <sortix/kernel/interlock.h>
|
|
|
|
#include <sortix/kernel/ioctx.h>
|
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/kthread.h>
|
2013-04-24 11:32:36 -04:00
|
|
|
#include <sortix/kernel/pipe.h>
|
|
|
|
#include <sortix/kernel/poll.h>
|
2013-05-12 18:41:30 -04:00
|
|
|
#include <sortix/kernel/process.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include <sortix/kernel/signal.h>
|
|
|
|
#include <sortix/kernel/syscall.h>
|
2013-05-12 18:41:30 -04:00
|
|
|
#include <sortix/kernel/thread.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/vnode.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
|
2011-11-16 02:37:29 -05:00
|
|
|
#include "pipe.h"
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
class PipeChannel
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
public:
|
|
|
|
PipeChannel(uint8_t* buffer, size_t buffersize);
|
|
|
|
~PipeChannel();
|
|
|
|
void CloseReading();
|
|
|
|
void CloseWriting();
|
|
|
|
void PerhapsShutdown();
|
|
|
|
ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
|
|
|
|
ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
|
2013-01-02 11:34:40 -05:00
|
|
|
int poll(ioctx_t* ctx, PollNode* node);
|
|
|
|
|
|
|
|
private:
|
|
|
|
short PollEventStatus();
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
private:
|
2013-01-02 11:34:40 -05:00
|
|
|
PollChannel poll_channel;
|
2012-08-07 18:19:44 -04:00
|
|
|
kthread_mutex_t pipelock;
|
|
|
|
kthread_cond_t readcond;
|
|
|
|
kthread_cond_t writecond;
|
|
|
|
uint8_t* buffer;
|
|
|
|
size_t bufferoffset;
|
|
|
|
size_t bufferused;
|
|
|
|
size_t buffersize;
|
|
|
|
bool anyreading;
|
|
|
|
bool anywriting;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
PipeChannel::PipeChannel(uint8_t* buffer, size_t buffersize)
|
|
|
|
{
|
|
|
|
pipelock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
readcond = KTHREAD_COND_INITIALIZER;
|
|
|
|
writecond = KTHREAD_COND_INITIALIZER;
|
|
|
|
this->buffer = buffer;
|
|
|
|
this->buffersize = buffersize;
|
|
|
|
bufferoffset = bufferused = 0;
|
2013-04-24 11:32:36 -04:00
|
|
|
anyreading = anywriting = true;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
PipeChannel::~PipeChannel()
|
|
|
|
{
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void PipeChannel::CloseReading()
|
|
|
|
{
|
|
|
|
anyreading = false;
|
|
|
|
kthread_cond_broadcast(&writecond);
|
|
|
|
PerhapsShutdown();
|
|
|
|
}
|
2012-03-01 08:31:12 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void PipeChannel::CloseWriting()
|
|
|
|
{
|
|
|
|
anywriting = false;
|
|
|
|
kthread_cond_broadcast(&readcond);
|
|
|
|
PerhapsShutdown();
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void PipeChannel::PerhapsShutdown()
|
|
|
|
{
|
|
|
|
kthread_mutex_lock(&pipelock);
|
2013-01-02 11:34:40 -05:00
|
|
|
poll_channel.Signal(PollEventStatus());
|
2012-08-07 18:19:44 -04:00
|
|
|
bool deleteme = !anyreading & !anywriting;
|
|
|
|
kthread_mutex_unlock(&pipelock);
|
|
|
|
if ( deleteme )
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t PipeChannel::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
ScopedLockSignal lock(&pipelock);
|
|
|
|
if ( !lock.IsAcquired() ) { errno = EINTR; return -1; }
|
|
|
|
while ( anywriting && !bufferused )
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2013-03-31 11:26:03 -04:00
|
|
|
if ( ctx->dflags & O_NONBLOCK )
|
|
|
|
return errno = EWOULDBLOCK, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !kthread_cond_wait_signal(&readcond, &pipelock) )
|
2012-08-02 08:34:36 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
errno = EINTR;
|
2012-08-02 08:34:36 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !bufferused && !anywriting ) { return 0; }
|
|
|
|
if ( bufferused < count ) { count = bufferused; }
|
|
|
|
size_t amount = count;
|
|
|
|
size_t linear = buffersize - bufferoffset;
|
|
|
|
if ( linear < amount ) { amount = linear; }
|
|
|
|
assert(amount);
|
|
|
|
ctx->copy_to_dest(buf, buffer + bufferoffset, amount);
|
|
|
|
bufferoffset = (bufferoffset + amount) % buffersize;
|
|
|
|
bufferused -= amount;
|
|
|
|
kthread_cond_broadcast(&writecond);
|
2013-01-02 11:34:40 -05:00
|
|
|
poll_channel.Signal(PollEventStatus());
|
2012-08-07 18:19:44 -04:00
|
|
|
return amount;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t PipeChannel::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
ScopedLockSignal lock(&pipelock);
|
|
|
|
if ( !lock.IsAcquired() ) { errno = EINTR; return -1; }
|
|
|
|
while ( anyreading && bufferused == buffersize )
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2013-03-31 11:26:03 -04:00
|
|
|
if ( ctx->dflags & O_NONBLOCK )
|
|
|
|
return errno = EWOULDBLOCK, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !kthread_cond_wait_signal(&writecond, &pipelock) )
|
|
|
|
{
|
|
|
|
errno = EINTR;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !anyreading )
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
CurrentThread()->DeliverSignal(SIGPIPE);
|
|
|
|
errno = EPIPE;
|
2011-11-16 02:37:29 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( buffersize - bufferused < count ) { count = buffersize - bufferused; }
|
|
|
|
size_t writeoffset = (bufferoffset + bufferused) % buffersize;
|
|
|
|
size_t amount = count;
|
|
|
|
size_t linear = buffersize - writeoffset;
|
|
|
|
if ( linear < amount ) { amount = linear; }
|
|
|
|
assert(amount);
|
|
|
|
ctx->copy_from_src(buffer + writeoffset, buf, amount);
|
|
|
|
bufferused += amount;
|
|
|
|
kthread_cond_broadcast(&readcond);
|
2013-01-02 11:34:40 -05:00
|
|
|
poll_channel.Signal(PollEventStatus());
|
2012-08-07 18:19:44 -04:00
|
|
|
return amount;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-01-02 11:34:40 -05:00
|
|
|
short PipeChannel::PollEventStatus()
|
|
|
|
{
|
|
|
|
short status = 0;
|
|
|
|
if ( !anywriting )
|
|
|
|
status |= POLLHUP;
|
|
|
|
if ( !anyreading )
|
|
|
|
status |= POLLERR;
|
|
|
|
if ( bufferused )
|
|
|
|
status |= POLLIN | POLLRDNORM;
|
|
|
|
if ( bufferused != buffersize )
|
|
|
|
status |= POLLOUT | POLLWRNORM;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PipeChannel::poll(ioctx_t* /*ctx*/, PollNode* node)
|
|
|
|
{
|
|
|
|
ScopedLockSignal lock(&pipelock);
|
|
|
|
short ret_status = PollEventStatus() & node->events;
|
|
|
|
if ( ret_status )
|
|
|
|
{
|
|
|
|
node->revents |= ret_status;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
poll_channel.Register(node);
|
|
|
|
return errno = EAGAIN, -1;
|
|
|
|
}
|
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
PipeEndpoint::PipeEndpoint()
|
|
|
|
{
|
|
|
|
channel = NULL;
|
|
|
|
reading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PipeEndpoint::~PipeEndpoint()
|
|
|
|
{
|
|
|
|
if ( channel )
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PipeEndpoint::Connect(PipeEndpoint* destination)
|
|
|
|
{
|
|
|
|
assert(!channel);
|
|
|
|
assert(!destination->channel);
|
|
|
|
const size_t BUFFER_SIZE = 4096;
|
|
|
|
size_t size = BUFFER_SIZE;
|
|
|
|
uint8_t* buffer = new uint8_t[size];
|
|
|
|
if ( !buffer )
|
|
|
|
return false;
|
|
|
|
destination->reading = !(reading = false);
|
|
|
|
if ( !(destination->channel = channel = new PipeChannel(buffer, size)) )
|
|
|
|
{
|
|
|
|
delete[] buffer;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PipeEndpoint::Disconnect()
|
|
|
|
{
|
|
|
|
assert(channel);
|
|
|
|
if ( reading )
|
|
|
|
channel->CloseReading();
|
|
|
|
else
|
|
|
|
channel->CloseWriting();
|
|
|
|
reading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t PipeEndpoint::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
if ( !reading ) { errno = EBADF; return -1; }
|
|
|
|
return channel->read(ctx, buf, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t PipeEndpoint::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
if ( reading ) { errno = EBADF; return -1; }
|
|
|
|
return channel->write(ctx, buf, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PipeEndpoint::poll(ioctx_t* ctx, PollNode* node)
|
|
|
|
{
|
|
|
|
return channel->poll(ctx, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PipeNode : public AbstractInode
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
|
|
|
public:
|
2013-04-24 11:32:36 -04:00
|
|
|
PipeNode(dev_t dev, uid_t owner, gid_t group, mode_t mode);
|
|
|
|
~PipeNode();
|
|
|
|
bool Connect(PipeNode* destination);
|
2012-08-07 18:19:44 -04:00
|
|
|
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
|
|
|
|
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
|
2013-01-02 11:34:40 -05:00
|
|
|
virtual int poll(ioctx_t* ctx, PollNode* node);
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
private:
|
2013-04-24 11:32:36 -04:00
|
|
|
PipeEndpoint endpoint;
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
bool PipeNode::Connect(PipeNode* destination)
|
|
|
|
{
|
|
|
|
return endpoint.Connect(&destination->endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
PipeNode::PipeNode(dev_t dev, uid_t owner, gid_t group, mode_t mode)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
|
|
|
inode_type = INODE_TYPE_STREAM;
|
|
|
|
this->dev = dev;
|
|
|
|
this->ino = (ino_t) this;
|
|
|
|
this->stat_uid = owner;
|
|
|
|
this->stat_gid = group;
|
|
|
|
this->type = S_IFCHR;
|
|
|
|
this->stat_mode = (mode & S_SETABLE) | this->type;
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
PipeNode::~PipeNode()
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
ssize_t PipeNode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-04-24 11:32:36 -04:00
|
|
|
return endpoint.read(ctx, buf, count);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
ssize_t PipeNode::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-04-24 11:32:36 -04:00
|
|
|
return endpoint.write(ctx, buf, count);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
int PipeNode::poll(ioctx_t* ctx, PollNode* node)
|
2013-01-02 11:34:40 -05:00
|
|
|
{
|
2013-04-24 11:32:36 -04:00
|
|
|
return endpoint.poll(ctx, node);
|
2013-01-02 11:34:40 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
namespace Pipe {
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
static int sys_pipe(int pipefd[2])
|
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
uid_t uid = process->uid;
|
|
|
|
uid_t gid = process->gid;
|
|
|
|
mode_t mode = 0600;
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
Ref<PipeNode> recv_inode(new PipeNode(0, uid, gid, mode));
|
|
|
|
if ( !recv_inode ) return -1;
|
|
|
|
Ref<PipeNode> send_inode(new PipeNode(0, uid, gid, mode));
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !send_inode ) return -1;
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-04-24 11:32:36 -04:00
|
|
|
if ( !send_inode->Connect(recv_inode.Get()) )
|
|
|
|
return -1;
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Vnode> recv_vnode(new Vnode(recv_inode, Ref<Vnode>(NULL), 0, 0));
|
|
|
|
Ref<Vnode> send_vnode(new Vnode(send_inode, Ref<Vnode>(NULL), 0, 0));
|
|
|
|
if ( !recv_vnode || !send_vnode ) return -1;
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2013-03-21 10:26:08 -04:00
|
|
|
Ref<Descriptor> recv_desc(new Descriptor(recv_vnode, O_READ));
|
|
|
|
Ref<Descriptor> send_desc(new Descriptor(send_vnode, O_WRITE));
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !recv_desc || !send_desc ) return -1;
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<DescriptorTable> dtable = process->GetDTable();
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int recv_index, send_index;
|
|
|
|
if ( 0 <= (recv_index = dtable->Allocate(recv_desc, 0)) )
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( 0 <= (send_index = dtable->Allocate(send_desc, 0)) )
|
2011-11-16 02:37:29 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
int ret[2] = { recv_index, send_index };
|
|
|
|
if ( CopyToUser(pipefd, ret, sizeof(ret)) )
|
|
|
|
return 0;
|
2011-11-16 02:37:29 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
dtable->Free(send_index);
|
2011-11-16 02:37:29 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
dtable->Free(recv_index);
|
2011-11-16 02:37:29 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
Syscall::Register(SYSCALL_PIPE, (void*) sys_pipe);
|
2011-11-16 02:37:29 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
} // namespace Pipe
|
|
|
|
} // namespace Sortix
|