2013-01-30 14:33:13 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-08-14 12:24:40 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2014, 2015.
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
fs/user.cpp
|
|
|
|
User-space filesystem.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-16 11:58:16 -04:00
|
|
|
#include <timespec.h>
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
#include <sortix/dirent.h>
|
|
|
|
#include <sortix/fcntl.h>
|
|
|
|
#include <sortix/stat.h>
|
|
|
|
#include <sortix/termios.h>
|
2013-03-22 15:19:45 -04:00
|
|
|
#include <sortix/timespec.h>
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
#include <fsmarshall-msg.h>
|
|
|
|
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/descriptor.h>
|
2013-01-30 14:33:13 -05:00
|
|
|
#include <sortix/kernel/inode.h>
|
|
|
|
#include <sortix/kernel/ioctx.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/kthread.h>
|
2013-01-30 14:33:13 -05:00
|
|
|
#include <sortix/kernel/mtable.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>
|
2014-10-02 10:10:47 -04:00
|
|
|
#include <sortix/kernel/scheduler.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/syscall.h>
|
2014-10-02 10:10:47 -04:00
|
|
|
#include <sortix/kernel/thread.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/vnode.h>
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
namespace UserFS {
|
|
|
|
|
|
|
|
class ChannelDirection;
|
|
|
|
class Channel;
|
|
|
|
class ChannelNode;
|
|
|
|
class Server;
|
|
|
|
class ServerNode;
|
|
|
|
class Unode;
|
|
|
|
|
|
|
|
class ChannelDirection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChannelDirection();
|
|
|
|
~ChannelDirection();
|
|
|
|
size_t Send(ioctx_t* ctx, const void* ptr, size_t least, size_t max);
|
|
|
|
size_t Recv(ioctx_t* ctx, void* ptr, size_t least, size_t max);
|
|
|
|
void SendClose();
|
|
|
|
void RecvClose();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t BUFFER_SIZE = 8192;
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
|
|
size_t buffer_used;
|
|
|
|
size_t buffer_offset;
|
|
|
|
kthread_mutex_t transfer_lock;
|
|
|
|
kthread_cond_t not_empty;
|
|
|
|
kthread_cond_t not_full;
|
|
|
|
bool still_reading;
|
|
|
|
bool still_writing;
|
|
|
|
|
2014-10-02 10:10:47 -04:00
|
|
|
public:
|
|
|
|
uintptr_t sender_system_tid;
|
|
|
|
uintptr_t receiver_system_tid;
|
|
|
|
|
2013-01-30 14:33:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class Channel
|
|
|
|
{
|
|
|
|
public:
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel(ioctx_t* ioctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
~Channel();
|
|
|
|
|
2014-10-02 10:10:47 -04:00
|
|
|
public:
|
|
|
|
void InformSystemTids(uintptr_t client_tid, uintptr_t server_tid);
|
|
|
|
|
2013-01-30 14:33:13 -05:00
|
|
|
public:
|
|
|
|
bool KernelSend(ioctx_t* ctx, const void* ptr, size_t count)
|
|
|
|
{
|
|
|
|
return KernelSend(ctx, ptr, count, count) == count;
|
|
|
|
}
|
|
|
|
size_t KernelSend(ioctx_t* ctx, const void* ptr, size_t least, size_t max);
|
|
|
|
bool KernelRecv(ioctx_t* ctx, void* ptr, size_t count)
|
|
|
|
{
|
|
|
|
return KernelRecv(ctx, ptr, count, count) == count;
|
|
|
|
}
|
|
|
|
size_t KernelRecv(ioctx_t* ctx, void* ptr, size_t least, size_t max);
|
|
|
|
void KernelClose();
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool UserSend(ioctx_t* ctx, const void* ptr, size_t count)
|
|
|
|
{
|
|
|
|
return UserSend(ctx, ptr, count, count) == count;
|
|
|
|
}
|
|
|
|
size_t UserSend(ioctx_t* ctx, const void* ptr, size_t least, size_t max);
|
|
|
|
bool UserRecv(ioctx_t* ctx, void* ptr, size_t count)
|
|
|
|
{
|
|
|
|
return UserRecv(ctx, ptr, count, count) == count;
|
|
|
|
}
|
|
|
|
size_t UserRecv(ioctx_t* ctx, void* ptr, size_t least, size_t max);
|
|
|
|
void UserClose();
|
|
|
|
|
|
|
|
private:
|
|
|
|
kthread_mutex_t kernel_lock;
|
|
|
|
kthread_mutex_t user_lock;
|
|
|
|
kthread_mutex_t destruction_lock;
|
|
|
|
ChannelDirection from_kernel;
|
|
|
|
ChannelDirection from_user;
|
|
|
|
bool kernel_closed;
|
|
|
|
bool user_closed;
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
public:
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
|
2013-01-30 14:33:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelNode : public AbstractInode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChannelNode();
|
|
|
|
ChannelNode(Channel* channel);
|
2014-05-06 13:12:44 -04:00
|
|
|
virtual ~ChannelNode();
|
2013-01-30 14:33:13 -05:00
|
|
|
void Construct(Channel* channel);
|
|
|
|
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);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Channel* channel;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Server : public Refcountable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Server();
|
|
|
|
virtual ~Server();
|
2014-05-06 13:12:44 -04:00
|
|
|
void Disconnect();
|
2014-05-07 08:14:38 -04:00
|
|
|
void Unmount();
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* Connect(ioctx_t* ctx);
|
2014-05-07 08:14:38 -04:00
|
|
|
Channel* Accept();
|
2013-01-30 14:33:13 -05:00
|
|
|
Ref<Inode> BootstrapNode(ino_t ino, mode_t type);
|
|
|
|
Ref<Inode> OpenNode(ino_t ino, mode_t type);
|
|
|
|
|
|
|
|
private:
|
|
|
|
kthread_mutex_t connect_lock;
|
|
|
|
kthread_cond_t connecting_cond;
|
|
|
|
kthread_cond_t connectable_cond;
|
2014-10-02 10:10:47 -04:00
|
|
|
uintptr_t listener_system_tid;
|
|
|
|
uintptr_t connecter_system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
Channel* connecting;
|
2014-05-06 13:12:44 -04:00
|
|
|
bool disconnected;
|
2014-05-07 08:14:38 -04:00
|
|
|
bool unmounted;
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ServerNode : public AbstractInode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ServerNode(Ref<Server> server);
|
|
|
|
virtual ~ServerNode();
|
2014-05-07 08:14:38 -04:00
|
|
|
virtual Ref<Inode> accept(ioctx_t* ctx, uint8_t* addr, size_t* addrlen,
|
|
|
|
int flags);
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
Ref<Server> server;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Unode : public Inode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Unode(Ref<Server> server, ino_t ino, mode_t type);
|
|
|
|
virtual ~Unode();
|
|
|
|
virtual void linked();
|
|
|
|
virtual void unlinked();
|
|
|
|
virtual int sync(ioctx_t* ctx);
|
|
|
|
virtual int stat(ioctx_t* ctx, struct stat* st);
|
2014-01-20 18:53:18 -05:00
|
|
|
virtual int statvfs(ioctx_t* ctx, struct statvfs* stvfs);
|
2013-01-30 14:33:13 -05:00
|
|
|
virtual int chmod(ioctx_t* ctx, mode_t mode);
|
|
|
|
virtual int chown(ioctx_t* ctx, uid_t owner, gid_t group);
|
|
|
|
virtual int truncate(ioctx_t* ctx, off_t length);
|
|
|
|
virtual off_t lseek(ioctx_t* ctx, off_t offset, int whence);
|
|
|
|
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
|
|
|
|
virtual ssize_t pread(ioctx_t* ctx, uint8_t* buf, size_t count,
|
|
|
|
off_t off);
|
|
|
|
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
|
|
|
|
virtual ssize_t pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count,
|
|
|
|
off_t off);
|
2013-05-16 11:58:16 -04:00
|
|
|
virtual int utimens(ioctx_t* ctx, const struct timespec* atime,
|
|
|
|
const struct timespec* ctime,
|
|
|
|
const struct timespec* mtime);
|
2013-01-30 14:33:13 -05:00
|
|
|
virtual int isatty(ioctx_t* ctx);
|
|
|
|
virtual ssize_t readdirents(ioctx_t* ctx, struct kernel_dirent* dirent,
|
|
|
|
size_t size, off_t start, size_t maxcount);
|
|
|
|
virtual Ref<Inode> open(ioctx_t* ctx, const char* filename, int flags,
|
|
|
|
mode_t mode);
|
|
|
|
virtual int mkdir(ioctx_t* ctx, const char* filename, mode_t mode);
|
|
|
|
virtual int link(ioctx_t* ctx, const char* filename, Ref<Inode> node);
|
|
|
|
virtual int link_raw(ioctx_t* ctx, const char* filename, Ref<Inode> node);
|
|
|
|
virtual int unlink(ioctx_t* ctx, const char* filename);
|
|
|
|
virtual int unlink_raw(ioctx_t* ctx, const char* filename);
|
|
|
|
virtual int rmdir(ioctx_t* ctx, const char* filename);
|
|
|
|
virtual int rmdir_me(ioctx_t* ctx);
|
|
|
|
virtual int symlink(ioctx_t* ctx, const char* oldname,
|
|
|
|
const char* filename);
|
|
|
|
virtual ssize_t readlink(ioctx_t* ctx, char* buf, size_t bufsiz);
|
2013-12-20 15:55:05 -05:00
|
|
|
virtual int tcgetwincurpos(ioctx_t* ctx, struct wincurpos* wcp);
|
2013-01-30 14:33:13 -05:00
|
|
|
virtual int tcgetwinsize(ioctx_t* ctx, struct winsize* ws);
|
2013-06-11 20:18:07 -04:00
|
|
|
virtual int tcsetpgrp(ioctx_t* ctx, pid_t pgid);
|
|
|
|
virtual pid_t tcgetpgrp(ioctx_t* ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
virtual int settermmode(ioctx_t* ctx, unsigned mode);
|
|
|
|
virtual int gettermmode(ioctx_t* ctx, unsigned* mode);
|
2012-12-29 17:09:09 -05:00
|
|
|
virtual int poll(ioctx_t* ctx, PollNode* node);
|
2012-12-20 10:19:07 -05:00
|
|
|
virtual int rename_here(ioctx_t* ctx, Ref<Inode> from, const char* oldname,
|
|
|
|
const char* newname);
|
2013-03-19 17:40:37 -04:00
|
|
|
virtual Ref<Inode> accept(ioctx_t* ctx, uint8_t* addr, size_t* addrlen,
|
|
|
|
int flags);
|
|
|
|
virtual int bind(ioctx_t* ctx, const uint8_t* addr, size_t addrlen);
|
|
|
|
virtual int connect(ioctx_t* ctx, const uint8_t* addr, size_t addrlen);
|
|
|
|
virtual int listen(ioctx_t* ctx, int backlog);
|
|
|
|
virtual ssize_t recv(ioctx_t* ctx, uint8_t* buf, size_t count, int flags);
|
|
|
|
virtual ssize_t send(ioctx_t* ctx, const uint8_t* buf, size_t count,
|
|
|
|
int flags);
|
2014-02-28 11:10:08 -05:00
|
|
|
virtual int getsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
void* option_value, size_t* option_size_ptr);
|
|
|
|
virtual int setsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
const void* option_value, size_t option_size);
|
2014-05-05 15:36:40 -04:00
|
|
|
virtual ssize_t tcgetblob(ioctx_t* ctx, const char* name, void* buffer, size_t count);
|
|
|
|
virtual ssize_t tcsetblob(ioctx_t* ctx, const char* name, const void* buffer, size_t count);
|
2014-05-07 08:14:38 -04:00
|
|
|
virtual int unmounted(ioctx_t* ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool SendMessage(Channel* channel, size_t type, void* ptr, size_t size,
|
|
|
|
size_t extra = 0);
|
|
|
|
bool RecvMessage(Channel* channel, size_t type, void* ptr, size_t size);
|
|
|
|
void RecvError(Channel* channel);
|
|
|
|
bool RecvBoolean(Channel* channel);
|
|
|
|
void UnexpectedResponse(Channel* channel, struct fsm_msg_header* hdr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ioctx_t kctx;
|
|
|
|
Ref<Server> server;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Channel Directory.
|
|
|
|
//
|
|
|
|
|
|
|
|
ChannelDirection::ChannelDirection()
|
|
|
|
{
|
|
|
|
buffer_used = 0;
|
|
|
|
buffer_offset = 0;
|
|
|
|
transfer_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
not_empty = KTHREAD_COND_INITIALIZER;
|
|
|
|
not_full = KTHREAD_COND_INITIALIZER;
|
|
|
|
still_reading = true;
|
|
|
|
still_writing = true;
|
2014-10-02 10:10:47 -04:00
|
|
|
sender_system_tid = 0;
|
|
|
|
receiver_system_tid = 0;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ChannelDirection::~ChannelDirection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ChannelDirection::Send(ioctx_t* ctx, const void* ptr, size_t least, size_t max)
|
|
|
|
{
|
|
|
|
const uint8_t* src = (const uint8_t*) ptr;
|
|
|
|
size_t sofar = 0;
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = receiver_system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
ScopedLock inner_lock(&transfer_lock);
|
2014-10-02 10:10:47 -04:00
|
|
|
sender_system_tid = CurrentThread()->system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
if ( !still_reading )
|
2014-05-06 13:12:44 -04:00
|
|
|
return errno = ECONNRESET, sofar;
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( buffer_used < BUFFER_SIZE )
|
|
|
|
break;
|
|
|
|
if ( least <= sofar )
|
|
|
|
return sofar;
|
|
|
|
if ( !kthread_cond_wait_signal(¬_full, &transfer_lock) )
|
|
|
|
return errno = EINTR, sofar;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t use_offset = (buffer_offset + buffer_used) % BUFFER_SIZE;
|
|
|
|
size_t count = max - sofar;
|
|
|
|
size_t available_to_end = BUFFER_SIZE - use_offset;
|
|
|
|
size_t available = BUFFER_SIZE - buffer_used;
|
|
|
|
if ( available_to_end < available )
|
|
|
|
available = available_to_end;
|
|
|
|
if ( available < count )
|
|
|
|
count = available;
|
2014-09-30 11:42:24 -04:00
|
|
|
if ( !ctx->copy_from_src(buffer + use_offset, src + sofar, count) )
|
2013-01-30 14:33:13 -05:00
|
|
|
return sofar;
|
|
|
|
if ( !buffer_used )
|
|
|
|
kthread_cond_signal(¬_empty);
|
|
|
|
buffer_used += count;
|
|
|
|
sofar += count;
|
|
|
|
if ( sofar == max )
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ChannelDirection::Recv(ioctx_t* ctx, void* ptr, size_t least, size_t max)
|
|
|
|
{
|
|
|
|
uint8_t* dst = (uint8_t*) ptr;
|
|
|
|
size_t sofar = 0;
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = sender_system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
ScopedLock inner_lock(&transfer_lock);
|
2014-10-02 10:10:47 -04:00
|
|
|
receiver_system_tid = CurrentThread()->system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
if ( buffer_used )
|
|
|
|
break;
|
|
|
|
if ( least <= sofar )
|
|
|
|
return sofar;
|
|
|
|
if ( !still_writing )
|
2014-05-06 13:12:44 -04:00
|
|
|
return errno = ECONNRESET, sofar;
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !kthread_cond_wait_signal(¬_empty, &transfer_lock) )
|
|
|
|
return errno = EINTR, sofar;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t use_offset = buffer_offset;
|
|
|
|
size_t count = max - sofar;
|
|
|
|
size_t available_to_end = BUFFER_SIZE - use_offset;
|
|
|
|
size_t available = buffer_used;
|
|
|
|
if ( available_to_end < available )
|
|
|
|
available = available_to_end;
|
|
|
|
if ( available < count )
|
|
|
|
count = available;
|
2014-09-30 11:42:24 -04:00
|
|
|
if ( !ctx->copy_to_dest(dst + sofar, buffer + use_offset, count) )
|
2013-01-30 14:33:13 -05:00
|
|
|
return sofar;
|
|
|
|
if ( buffer_used == BUFFER_SIZE )
|
|
|
|
kthread_cond_signal(¬_full);
|
|
|
|
buffer_offset = (buffer_offset + count) % BUFFER_SIZE;
|
|
|
|
buffer_used -= count;
|
|
|
|
sofar += count;
|
|
|
|
if ( sofar == max )
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelDirection::SendClose()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&transfer_lock);
|
|
|
|
still_writing = false;
|
|
|
|
kthread_cond_signal(¬_empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelDirection::RecvClose()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&transfer_lock);
|
2014-09-30 16:44:53 -04:00
|
|
|
still_reading = false;
|
2013-01-30 14:33:13 -05:00
|
|
|
kthread_cond_signal(¬_full);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Channel.
|
|
|
|
//
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel::Channel(ioctx_t* ctx)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
|
|
|
kernel_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
user_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
destruction_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
user_closed = false;
|
|
|
|
kernel_closed = false;
|
2015-10-22 19:37:45 -04:00
|
|
|
uid = ctx ? ctx->uid : 0;
|
|
|
|
gid = ctx ? ctx->gid : 0;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Channel::~Channel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-02 10:10:47 -04:00
|
|
|
void Channel::InformSystemTids(uintptr_t client_tid, uintptr_t server_tid)
|
|
|
|
{
|
|
|
|
from_kernel.sender_system_tid = client_tid;
|
|
|
|
from_kernel.receiver_system_tid = server_tid;
|
|
|
|
from_user.sender_system_tid = server_tid;
|
|
|
|
from_user.receiver_system_tid = client_tid;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t Channel::KernelSend(ioctx_t* ctx, const void* ptr, size_t least,
|
|
|
|
size_t max)
|
|
|
|
{
|
|
|
|
ScopedLockSignal outer_lock(&kernel_lock);
|
|
|
|
if ( !outer_lock.IsAcquired() )
|
|
|
|
return errno = EINTR, 0;
|
|
|
|
size_t ret = from_kernel.Send(ctx, ptr, least, max);
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = 0;
|
|
|
|
Scheduler::ScheduleTrueThread();
|
2013-01-30 14:33:13 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Channel::KernelRecv(ioctx_t* ctx, void* ptr, size_t least, size_t max)
|
|
|
|
{
|
|
|
|
ScopedLockSignal outer_lock(&kernel_lock);
|
|
|
|
if ( !outer_lock.IsAcquired() )
|
|
|
|
return errno = EINTR, 0;
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = 0;
|
|
|
|
Scheduler::ScheduleTrueThread();
|
2013-01-30 14:33:13 -05:00
|
|
|
return from_user.Recv(ctx, ptr, least, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::KernelClose()
|
|
|
|
{
|
|
|
|
// No lock needed, this thread is the last to use this object as kernel.
|
|
|
|
from_kernel.SendClose();
|
|
|
|
from_user.RecvClose();
|
|
|
|
kthread_mutex_lock(&destruction_lock);
|
|
|
|
kernel_closed = true;
|
|
|
|
bool delete_this = user_closed;
|
|
|
|
kthread_mutex_unlock(&destruction_lock);
|
|
|
|
if ( delete_this )
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Channel::UserSend(ioctx_t* ctx, const void* ptr, size_t least,
|
|
|
|
size_t max)
|
|
|
|
{
|
|
|
|
ScopedLockSignal outer_lock(&user_lock);
|
|
|
|
if ( !outer_lock.IsAcquired() )
|
|
|
|
return errno = EINTR, 0;
|
2014-10-02 10:10:47 -04:00
|
|
|
size_t ret = from_user.Send(ctx, ptr, least, max);
|
|
|
|
CurrentThread()->yield_to_tid = 0;
|
|
|
|
Scheduler::ScheduleTrueThread();
|
|
|
|
return ret;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Channel::UserRecv(ioctx_t* ctx, void* ptr, size_t least, size_t max)
|
|
|
|
{
|
|
|
|
ScopedLockSignal outer_lock(&user_lock);
|
|
|
|
if ( !outer_lock.IsAcquired() )
|
|
|
|
return errno = EINTR, 0;
|
2014-10-02 10:10:47 -04:00
|
|
|
size_t ret = from_kernel.Recv(ctx, ptr, least, max);
|
|
|
|
CurrentThread()->yield_to_tid = 0;
|
|
|
|
Scheduler::ScheduleTrueThread();
|
|
|
|
return ret;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::UserClose()
|
|
|
|
{
|
|
|
|
// No lock needed, this thread is the last to use this object as user.
|
|
|
|
from_kernel.RecvClose();
|
|
|
|
from_user.SendClose();
|
|
|
|
kthread_mutex_lock(&destruction_lock);
|
|
|
|
user_closed = true;
|
|
|
|
bool delete_this = kernel_closed;
|
|
|
|
kthread_mutex_unlock(&destruction_lock);
|
|
|
|
if ( delete_this )
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of ChannelNode.
|
|
|
|
//
|
|
|
|
|
|
|
|
ChannelNode::ChannelNode()
|
|
|
|
{
|
|
|
|
channel = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelNode::ChannelNode(Channel* channel)
|
|
|
|
{
|
|
|
|
Construct(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelNode::~ChannelNode()
|
|
|
|
{
|
|
|
|
if ( channel )
|
|
|
|
channel->UserClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelNode::Construct(Channel* channel)
|
|
|
|
{
|
|
|
|
inode_type = INODE_TYPE_STREAM;
|
|
|
|
this->channel = channel;
|
|
|
|
this->type = S_IFCHR;
|
|
|
|
this->dev = (dev_t) this;
|
|
|
|
this->ino = 0;
|
|
|
|
// TODO: Set uid, gid, mode.
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ChannelNode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
return channel->UserRecv(ctx, buf, /*1*/ count, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ChannelNode::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
return channel->UserSend(ctx, buf, /*1*/ count, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Server.
|
|
|
|
//
|
|
|
|
|
|
|
|
Server::Server()
|
|
|
|
{
|
|
|
|
connect_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
connecting_cond = KTHREAD_COND_INITIALIZER;
|
|
|
|
connectable_cond = KTHREAD_COND_INITIALIZER;
|
2014-10-02 10:10:47 -04:00
|
|
|
listener_system_tid = 0;
|
2013-01-30 14:33:13 -05:00
|
|
|
connecting = NULL;
|
2014-05-06 13:12:44 -04:00
|
|
|
disconnected = false;
|
2014-05-07 08:14:38 -04:00
|
|
|
unmounted = false;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-06 13:12:44 -04:00
|
|
|
void Server::Disconnect()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&connect_lock);
|
|
|
|
disconnected = true;
|
|
|
|
kthread_cond_signal(&connectable_cond);
|
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
void Server::Unmount()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&connect_lock);
|
|
|
|
unmounted = true;
|
|
|
|
kthread_cond_signal(&connecting_cond);
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* Server::Connect(ioctx_t* ctx)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = new Channel(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return NULL;
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = listener_system_tid;
|
2013-01-30 14:33:13 -05:00
|
|
|
ScopedLock lock(&connect_lock);
|
2014-05-06 13:12:44 -04:00
|
|
|
while ( !disconnected && connecting )
|
2014-10-02 10:10:47 -04:00
|
|
|
{
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !kthread_cond_wait_signal(&connectable_cond, &connect_lock) )
|
|
|
|
{
|
2014-10-02 10:10:47 -04:00
|
|
|
CurrentThread()->yield_to_tid = 0;
|
2013-01-30 14:33:13 -05:00
|
|
|
delete channel;
|
|
|
|
return errno = EINTR, (Channel*) NULL;
|
|
|
|
}
|
2014-10-02 10:10:47 -04:00
|
|
|
}
|
|
|
|
CurrentThread()->yield_to_tid = 0;
|
2014-05-06 13:12:44 -04:00
|
|
|
if ( disconnected )
|
|
|
|
return delete channel, errno = ECONNREFUSED, (Channel*) NULL;
|
2014-10-02 10:10:47 -04:00
|
|
|
channel->InformSystemTids(CurrentThread()->system_tid, listener_system_tid);
|
2013-01-30 14:33:13 -05:00
|
|
|
connecting = channel;
|
|
|
|
kthread_cond_signal(&connecting_cond);
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
Channel* Server::Accept()
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
|
|
|
ScopedLock lock(&connect_lock);
|
2014-10-02 10:10:47 -04:00
|
|
|
listener_system_tid = CurrentThread()->system_tid;
|
2014-05-07 08:14:38 -04:00
|
|
|
while ( !connecting && !unmounted )
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !kthread_cond_wait_signal(&connecting_cond, &connect_lock) )
|
|
|
|
return errno = EINTR, (Channel*) NULL;
|
2014-05-07 08:14:38 -04:00
|
|
|
if ( unmounted )
|
|
|
|
return errno = ECONNRESET, (Channel*) NULL;
|
2013-01-30 14:33:13 -05:00
|
|
|
Channel* result = connecting;
|
|
|
|
connecting = NULL;
|
|
|
|
kthread_cond_signal(&connectable_cond);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Inode> Server::BootstrapNode(ino_t ino, mode_t type)
|
|
|
|
{
|
|
|
|
return Ref<Inode>(new Unode(Ref<Server>(this), ino, type));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Inode> Server::OpenNode(ino_t ino, mode_t type)
|
|
|
|
{
|
|
|
|
return BootstrapNode(ino, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of ServerNode.
|
|
|
|
//
|
|
|
|
|
|
|
|
ServerNode::ServerNode(Ref<Server> server)
|
|
|
|
{
|
|
|
|
inode_type = INODE_TYPE_UNKNOWN;
|
|
|
|
this->server = server;
|
2014-05-07 08:14:38 -04:00
|
|
|
this->type = S_IFSOCK;
|
2013-01-30 14:33:13 -05:00
|
|
|
this->dev = (dev_t) this;
|
|
|
|
this->ino = 0;
|
|
|
|
// TODO: Set uid, gid, mode.
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerNode::~ServerNode()
|
|
|
|
{
|
2014-05-06 13:12:44 -04:00
|
|
|
server->Disconnect();
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
Ref<Inode> ServerNode::accept(ioctx_t* ctx, uint8_t* addr, size_t* addrlen,
|
|
|
|
int flags)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2014-05-07 08:14:38 -04:00
|
|
|
(void) addr;
|
|
|
|
(void) flags;
|
|
|
|
size_t out_addrlen = 0;
|
|
|
|
if ( addrlen && !ctx->copy_to_dest(addrlen, &out_addrlen, sizeof(out_addrlen)) )
|
|
|
|
return Ref<Inode>(NULL);
|
|
|
|
Ref<ChannelNode> node(new ChannelNode);
|
|
|
|
if ( !node )
|
|
|
|
return Ref<Inode>(NULL);
|
|
|
|
Channel* channel = server->Accept();
|
|
|
|
if ( !channel )
|
|
|
|
return Ref<Inode>(NULL);
|
|
|
|
node->Construct(channel);
|
|
|
|
return node;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Unode.
|
|
|
|
//
|
|
|
|
|
|
|
|
Unode::Unode(Ref<Server> server, ino_t ino, mode_t type)
|
|
|
|
{
|
|
|
|
SetupKernelIOCtx(&kctx);
|
|
|
|
this->server = server;
|
|
|
|
this->ino = ino;
|
|
|
|
this->dev = (dev_t) server;
|
|
|
|
this->type = type;
|
2013-06-14 18:37:57 -04:00
|
|
|
|
|
|
|
// Let the remote know that the kernel is using this inode.
|
2015-08-14 12:24:40 -04:00
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
bool saved = thread->force_no_signals;
|
|
|
|
thread->force_no_signals = true;
|
|
|
|
thread->DoUpdatePendingSignal();
|
2015-10-22 19:37:45 -04:00
|
|
|
if ( Channel* channel = server->Connect(NULL) )
|
2013-06-14 18:37:57 -04:00
|
|
|
{
|
|
|
|
struct fsm_req_refer msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
SendMessage(channel, FSM_REQ_REFER, &msg, sizeof(msg));
|
|
|
|
channel->KernelClose();
|
|
|
|
}
|
2015-08-14 12:24:40 -04:00
|
|
|
thread->force_no_signals = saved;
|
|
|
|
thread->DoUpdatePendingSignal();
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Unode::~Unode()
|
|
|
|
{
|
2013-06-14 18:37:57 -04:00
|
|
|
// Let the remote know that the kernel is no longer using this inode.
|
2015-08-14 12:24:40 -04:00
|
|
|
Thread* thread = CurrentThread();
|
|
|
|
bool saved = thread->force_no_signals;
|
|
|
|
thread->force_no_signals = true;
|
|
|
|
thread->DoUpdatePendingSignal();
|
2015-10-22 19:37:45 -04:00
|
|
|
if ( Channel* channel = server->Connect(NULL) )
|
2013-06-14 18:37:57 -04:00
|
|
|
{
|
|
|
|
struct fsm_req_unref msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
SendMessage(channel, FSM_REQ_UNREF, &msg, sizeof(msg));
|
|
|
|
channel->KernelClose();
|
|
|
|
}
|
2015-08-14 12:24:40 -04:00
|
|
|
thread->force_no_signals = saved;
|
|
|
|
thread->DoUpdatePendingSignal();
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Unode::SendMessage(Channel* channel, size_t type, void* ptr, size_t size,
|
|
|
|
size_t extra)
|
|
|
|
{
|
|
|
|
struct fsm_msg_header hdr;
|
|
|
|
hdr.msgtype = type;
|
|
|
|
hdr.msgsize = size + extra;
|
2015-10-22 19:37:45 -04:00
|
|
|
hdr.uid = channel->uid;
|
|
|
|
hdr.gid = channel->gid;
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel->KernelSend(&kctx, &hdr, sizeof(hdr)) )
|
|
|
|
return false;
|
|
|
|
if ( !channel->KernelSend(&kctx, ptr, size) )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unode::RecvMessage(Channel* channel, size_t type, void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
struct fsm_msg_header resp_hdr;
|
|
|
|
if ( !channel->KernelRecv(&kctx, &resp_hdr, sizeof(resp_hdr)) )
|
|
|
|
return false;
|
|
|
|
if ( resp_hdr.msgtype != type )
|
|
|
|
{
|
|
|
|
UnexpectedResponse(channel, &resp_hdr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !ptr || !size || channel->KernelRecv(&kctx, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unode::RecvError(Channel* channel)
|
|
|
|
{
|
|
|
|
SetupKernelIOCtx(&kctx);
|
|
|
|
struct fsm_resp_error resp;
|
|
|
|
if ( channel->KernelRecv(&kctx, &resp, sizeof(resp)) )
|
|
|
|
errno = resp.errnum;
|
|
|
|
// In case of error, errno is set to that error.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unode::RecvBoolean(Channel* channel)
|
|
|
|
{
|
|
|
|
struct fsm_msg_header resp_hdr;
|
|
|
|
if ( !channel->KernelRecv(&kctx, &resp_hdr, sizeof(resp_hdr)) )
|
|
|
|
return false;
|
|
|
|
if ( resp_hdr.msgtype == FSM_RESP_SUCCESS )
|
|
|
|
return true;
|
|
|
|
UnexpectedResponse(channel, &resp_hdr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unode::UnexpectedResponse(Channel* channel, struct fsm_msg_header* hdr)
|
|
|
|
{
|
|
|
|
if ( hdr->msgtype == FSM_RESP_ERROR )
|
|
|
|
RecvError(channel);
|
|
|
|
else
|
|
|
|
errno = EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unode::linked()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unode::unlinked()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::sync(ioctx_t* ctx)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_sync msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_SYNC, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::stat(ioctx_t* ctx, struct stat* st)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_stat msg;
|
|
|
|
struct fsm_resp_stat resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_STAT, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_STAT, &resp, sizeof(resp)) &&
|
|
|
|
(resp.st.st_dev = (dev_t) server, true) &&
|
|
|
|
ctx->copy_to_dest(st, &resp.st, sizeof(*st)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:53:18 -05:00
|
|
|
int Unode::statvfs(ioctx_t* ctx, struct statvfs* stvfs)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2014-01-20 18:53:18 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_statvfs msg;
|
|
|
|
struct fsm_resp_statvfs resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_STATVFS, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_STATVFS, &resp, sizeof(resp)) &&
|
|
|
|
(resp.stvfs.f_fsid = (dev_t) server, true) &&
|
|
|
|
(resp.stvfs.f_flag |= ST_NOSUID, true) &&
|
|
|
|
ctx->copy_to_dest(stvfs, &resp.stvfs, sizeof(*stvfs)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::chmod(ioctx_t* ctx, mode_t mode)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_chmod msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.mode = mode;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_CHMOD, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::chown(ioctx_t* ctx, uid_t owner, gid_t group)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_chown msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.uid = owner;
|
|
|
|
msg.gid = group;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_CHOWN, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::truncate(ioctx_t* ctx, off_t length)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_truncate msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.size = length;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TRUNCATE, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
off_t Unode::lseek(ioctx_t* ctx, off_t offset, int whence)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
off_t ret = -1;
|
|
|
|
struct fsm_req_lseek msg;
|
|
|
|
struct fsm_resp_lseek resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.offset = offset;
|
|
|
|
msg.whence = whence;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_LSEEK, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_LSEEK, &resp, sizeof(resp)) )
|
|
|
|
ret = resp.offset;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
struct fsm_req_read msg;
|
|
|
|
struct fsm_resp_read resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.count = count;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_READ, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_READ, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( resp.count < count )
|
|
|
|
count = resp.count;
|
|
|
|
if ( channel->KernelRecv(ctx, buf, count) )
|
|
|
|
ret = (ssize_t) count;
|
|
|
|
}
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
struct fsm_req_pread msg;
|
|
|
|
struct fsm_resp_read resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.count = count;
|
|
|
|
msg.offset = off;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_PREAD, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_READ, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( resp.count < count )
|
|
|
|
count = resp.count;
|
|
|
|
if ( channel->KernelRecv(ctx, buf, count) )
|
|
|
|
ret = (ssize_t) count;
|
|
|
|
}
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_write msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.count = count;
|
|
|
|
struct fsm_msg_header hdr;
|
|
|
|
hdr.msgtype = FSM_REQ_WRITE;
|
|
|
|
hdr.msgsize = sizeof(msg) + count;
|
2015-10-22 19:37:45 -04:00
|
|
|
hdr.uid = ctx->uid;
|
|
|
|
hdr.gid = ctx->gid;
|
2013-01-30 14:33:13 -05:00
|
|
|
struct fsm_resp_write resp;
|
|
|
|
if ( channel->KernelSend(&kctx, &hdr, sizeof(hdr)) &&
|
|
|
|
channel->KernelSend(&kctx, &msg, sizeof(msg)) &&
|
|
|
|
channel->KernelSend(ctx, buf, count) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_WRITE, &resp, sizeof(resp)) )
|
|
|
|
ret = (ssize_t) resp.count;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count, off_t off)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
struct fsm_req_pwrite msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.count = count;
|
|
|
|
msg.offset = off;
|
|
|
|
struct fsm_msg_header hdr;
|
|
|
|
hdr.msgtype = FSM_REQ_PWRITE;
|
|
|
|
hdr.msgsize = sizeof(msg) + count;
|
2015-10-22 19:37:45 -04:00
|
|
|
hdr.uid = ctx->uid;
|
|
|
|
hdr.gid = ctx->gid;
|
2013-01-30 14:33:13 -05:00
|
|
|
struct fsm_resp_write resp;
|
|
|
|
if ( channel->KernelSend(&kctx, &hdr, sizeof(hdr)) &&
|
|
|
|
channel->KernelSend(&kctx, &msg, sizeof(msg)) &&
|
|
|
|
channel->KernelSend(ctx, buf, count) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_WRITE, &resp, sizeof(resp)) )
|
|
|
|
ret = (ssize_t) resp.count;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::utimens(ioctx_t* ctx,
|
2013-05-16 11:58:16 -04:00
|
|
|
const struct timespec* atime,
|
|
|
|
const struct timespec* /*ctime*/,
|
|
|
|
const struct timespec* mtime)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
2013-03-22 15:19:45 -04:00
|
|
|
struct fsm_req_utimens msg;
|
2013-01-30 14:33:13 -05:00
|
|
|
msg.ino = ino;
|
2013-05-16 11:58:16 -04:00
|
|
|
msg.times[0] = atime ? *atime : timespec_nul();
|
|
|
|
msg.times[1] = mtime ? *mtime : timespec_nul();
|
2013-03-22 15:19:45 -04:00
|
|
|
if ( SendMessage(channel, FSM_REQ_UTIMENS, &msg, sizeof(msg)) &&
|
2013-01-30 14:33:13 -05:00
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::isatty(ioctx_t* ctx)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return 0;
|
|
|
|
int ret = 0;
|
|
|
|
struct fsm_req_isatty msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_ISATTY, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 1;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::readdirents(ioctx_t* ctx, struct kernel_dirent* dirent,
|
|
|
|
size_t size, off_t start, size_t /*maxcount*/)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
struct fsm_req_readdirents msg;
|
|
|
|
struct fsm_resp_readdirents resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.rec_num = start;
|
|
|
|
errno = 0;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_READDIRENTS, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_READDIRENTS, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( !resp.namelen )
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
goto break_if;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct kernel_dirent entry;
|
|
|
|
entry.d_reclen = sizeof(entry) + resp.namelen + 1;
|
2014-01-03 17:21:43 -05:00
|
|
|
entry.d_nextoff = 0;
|
2014-01-03 16:09:36 -05:00
|
|
|
entry.d_namlen = resp.namelen;
|
2013-01-30 14:33:13 -05:00
|
|
|
entry.d_dev = (dev_t) server;
|
|
|
|
entry.d_ino = resp.ino;
|
2014-01-03 13:41:04 -05:00
|
|
|
entry.d_type = resp.type;
|
2013-01-30 14:33:13 -05:00
|
|
|
|
2014-09-30 11:42:24 -04:00
|
|
|
if ( !ctx->copy_to_dest(dirent, &entry, sizeof(entry)) )
|
2013-01-30 14:33:13 -05:00
|
|
|
goto break_if;
|
|
|
|
|
|
|
|
size_t needed = sizeof(*dirent) + resp.namelen + 1;
|
|
|
|
if ( size < needed && (errno = ERANGE) )
|
|
|
|
goto break_if;
|
|
|
|
|
|
|
|
uint8_t nul = 0;
|
|
|
|
if ( channel->KernelRecv(ctx, dirent->d_name, resp.namelen) &&
|
2014-09-30 11:42:24 -04:00
|
|
|
ctx->copy_to_dest(&dirent->d_name[resp.namelen], &nul, 1) )
|
2013-01-30 14:33:13 -05:00
|
|
|
ret = (ssize_t) needed;
|
|
|
|
} break_if:
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
Ref<Inode> Unode::open(ioctx_t* ctx, const char* filename, int flags,
|
2013-01-30 14:33:13 -05:00
|
|
|
mode_t mode)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return Ref<Inode>(NULL);
|
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
Ref<Inode> ret;
|
|
|
|
struct fsm_req_open msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.flags = flags;
|
|
|
|
msg.mode = mode;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
struct fsm_resp_open resp;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_OPEN, &msg, sizeof(msg), filenamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_OPEN, &resp, sizeof(resp)) )
|
|
|
|
ret = server->OpenNode(resp.ino, resp.type);
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::mkdir(ioctx_t* ctx, const char* filename, mode_t mode)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
2014-09-30 16:44:53 -04:00
|
|
|
return -1;
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_mkdir msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.mode = mode;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
struct fsm_resp_mkdir resp;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_MKDIR, &msg, sizeof(msg), filenamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_MKDIR, &resp, sizeof(resp)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::link(ioctx_t* ctx, const char* filename, Ref<Inode> node)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
|
|
|
if ( node->dev != this->dev )
|
|
|
|
return errno = EXDEV, -1;
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
2014-09-30 16:44:53 -04:00
|
|
|
return -1;
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_link msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.linkino = node->ino;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_LINK, &msg, sizeof(msg), filenamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::link_raw(ioctx_t* /*ctx*/, const char* /*filename*/, Ref<Inode> /*node*/)
|
|
|
|
{
|
|
|
|
return errno = EPERM, -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::unlink(ioctx_t* ctx, const char* filename)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
|
|
|
// TODO: Make sure the target is no longer used!
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
2014-09-30 16:44:53 -04:00
|
|
|
return -1;
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_unlink msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_UNLINK, &msg, sizeof(msg), filenamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::unlink_raw(ioctx_t* /*ctx*/, const char* /*filename*/)
|
|
|
|
{
|
|
|
|
return errno = EPERM, -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::rmdir(ioctx_t* ctx, const char* filename)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
|
|
|
// TODO: Make sure the target is no longer used!
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
2014-09-30 16:44:53 -04:00
|
|
|
return -1;
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_rmdir msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_RMDIR, &msg, sizeof(msg), filenamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::rmdir_me(ioctx_t* /*ctx*/)
|
|
|
|
{
|
|
|
|
return errno = EPERM, -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::symlink(ioctx_t* ctx, const char* oldname, const char* filename)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
2014-09-30 16:44:53 -04:00
|
|
|
return -1;
|
2013-01-30 14:33:13 -05:00
|
|
|
size_t oldnamelen = strlen(oldname);
|
|
|
|
size_t filenamelen = strlen(filename);
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_symlink msg;
|
|
|
|
msg.dirino = ino;
|
|
|
|
msg.targetlen = oldnamelen;
|
|
|
|
msg.namelen = filenamelen;
|
|
|
|
size_t extra = msg.targetlen + msg.namelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_SYMLINK, &msg, sizeof(msg), extra) &&
|
|
|
|
channel->KernelSend(&kctx, oldname, oldnamelen) &&
|
|
|
|
channel->KernelSend(&kctx, filename, filenamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::readlink(ioctx_t* ctx, char* buf, size_t bufsiz)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
struct fsm_req_readlink msg;
|
|
|
|
struct fsm_resp_readlink resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_READLINK, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_READLINK, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( resp.targetlen < bufsiz )
|
|
|
|
bufsiz = resp.targetlen;
|
|
|
|
if ( channel->KernelRecv(ctx, buf, bufsiz) )
|
|
|
|
ret = (ssize_t) bufsiz;
|
|
|
|
}
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:55:05 -05:00
|
|
|
int Unode::tcgetwincurpos(ioctx_t* ctx, struct wincurpos* wcp)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-12-20 15:55:05 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_tcgetwincurpos msg;
|
|
|
|
struct fsm_resp_tcgetwincurpos resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCGETWINCURPOS, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_TCGETWINCURPOS, &resp, sizeof(resp)) &&
|
|
|
|
ctx->copy_to_dest(wcp, &resp.pos, sizeof(*wcp)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:33:13 -05:00
|
|
|
int Unode::tcgetwinsize(ioctx_t* ctx, struct winsize* ws)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_tcgetwinsize msg;
|
|
|
|
struct fsm_resp_tcgetwinsize resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCGETWINSIZE, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_TCGETWINSIZE, &resp, sizeof(resp)) &&
|
|
|
|
ctx->copy_to_dest(ws, &resp.size, sizeof(*ws)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::tcsetpgrp(ioctx_t* ctx, pid_t pgid)
|
2013-06-11 20:18:07 -04:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-06-11 20:18:07 -04:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_tcsetpgrp msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.pgid = pgid;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCSETPGRP, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
pid_t Unode::tcgetpgrp(ioctx_t* ctx)
|
2013-06-11 20:18:07 -04:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-06-11 20:18:07 -04:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
pid_t ret = -1;
|
|
|
|
struct fsm_req_tcgetpgrp msg;
|
|
|
|
struct fsm_resp_tcgetpgrp resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCGETPGRP, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_TCGETPGRP, &resp, sizeof(resp)) )
|
|
|
|
ret = resp.pgid;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::settermmode(ioctx_t* ctx, unsigned mode)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_settermmode msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.termmode = mode;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_SETTERMMODE, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::gettermmode(ioctx_t* ctx, unsigned* mode)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2013-01-30 14:33:13 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_gettermmode msg;
|
|
|
|
struct fsm_resp_gettermmode resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_GETTERMMODE, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_GETTERMMODE, &resp, sizeof(resp)) &&
|
|
|
|
ctx->copy_to_dest(mode, &resp.termmode, sizeof(*mode)) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-29 17:09:09 -05:00
|
|
|
int Unode::poll(ioctx_t* /*ctx*/, PollNode* /*node*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSUP, -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:37:45 -04:00
|
|
|
int Unode::rename_here(ioctx_t* ctx, Ref<Inode> from, const char* oldname,
|
2012-12-20 10:19:07 -05:00
|
|
|
const char* newname)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2012-12-20 10:19:07 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_rename msg;
|
2014-10-04 18:36:52 -04:00
|
|
|
msg.olddirino = from->ino;
|
|
|
|
msg.newdirino = this->ino;
|
2012-12-20 10:19:07 -05:00
|
|
|
msg.oldnamelen = strlen(oldname);
|
|
|
|
msg.newnamelen = strlen(newname);
|
|
|
|
size_t extra = msg.oldnamelen + msg.newnamelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_RENAME, &msg, sizeof(msg), extra) &&
|
|
|
|
channel->KernelSend(&kctx, oldname, msg.oldnamelen) &&
|
|
|
|
channel->KernelSend(&kctx, newname, msg.newnamelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:40:37 -04:00
|
|
|
Ref<Inode> Unode::accept(ioctx_t* /*ctx*/, uint8_t* /*addr*/,
|
|
|
|
size_t* /*addrlen*/, int /*flags*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, Ref<Inode>();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::bind(ioctx_t* /*ctx*/, const uint8_t* /*addr*/, size_t /*addrlen*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::connect(ioctx_t* /*ctx*/, const uint8_t* /*addr*/, size_t /*addrlen*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::listen(ioctx_t* /*ctx*/, int /*backlog*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::recv(ioctx_t* /*ctx*/, uint8_t* /*buf*/, size_t /*count*/,
|
|
|
|
int /*flags*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::send(ioctx_t* /*ctx*/, const uint8_t* /*buf*/, size_t /*count*/,
|
|
|
|
int /*flags*/)
|
|
|
|
{
|
|
|
|
return errno = ENOTSOCK, -1;
|
|
|
|
}
|
|
|
|
|
2014-02-28 11:10:08 -05:00
|
|
|
int Unode::getsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
void* option_value, size_t* option_size_ptr)
|
|
|
|
{
|
|
|
|
size_t option_size;
|
|
|
|
if ( !ctx->copy_from_src(&option_size, option_size_ptr, sizeof(option_size)) )
|
|
|
|
return -1;
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2014-02-28 11:10:08 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_getsockopt msg;
|
|
|
|
struct fsm_resp_getsockopt resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.level = level;
|
|
|
|
msg.option_name = option_name;
|
|
|
|
msg.max_option_size = option_size;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_GETSOCKOPT, &msg, sizeof(msg)) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_GETSOCKOPT, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( resp.option_size < option_size )
|
|
|
|
option_size = resp.option_size;
|
|
|
|
if ( channel->KernelRecv(ctx, option_value, option_size) )
|
|
|
|
ret = 0;
|
|
|
|
if ( !ctx->copy_to_dest(option_size_ptr, &option_size, sizeof(option_size)) )
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Unode::setsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
const void* option_value, size_t option_size)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2014-02-28 11:10:08 -05:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
int ret = -1;
|
|
|
|
struct fsm_req_setsockopt msg;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.level = level;
|
|
|
|
msg.option_name = option_name;
|
|
|
|
msg.option_size = option_size;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_SETSOCKOPT, &msg, sizeof(msg)) &&
|
|
|
|
channel->KernelSend(ctx, option_value, option_size) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_SUCCESS, NULL, 0) )
|
|
|
|
ret = 0;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-05 15:36:40 -04:00
|
|
|
ssize_t Unode::tcgetblob(ioctx_t* ctx, const char* name, void* buffer, size_t count)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2014-05-05 15:36:40 -04:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
if ( !buffer )
|
|
|
|
count = SSIZE_MAX;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
size_t namelen = name ? strlen(name) : 0;
|
|
|
|
struct fsm_req_tcgetblob msg;
|
|
|
|
struct fsm_resp_tcgetblob resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.namelen = namelen;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCGETBLOB, &msg, sizeof(msg), namelen) &&
|
|
|
|
channel->KernelSend(&kctx, name, namelen) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_TCGETBLOB, &resp, sizeof(resp)) )
|
|
|
|
{
|
|
|
|
if ( resp.count < count )
|
|
|
|
count = resp.count;
|
|
|
|
if ( count < resp.count )
|
|
|
|
errno = ERANGE;
|
|
|
|
else if ( !buffer || channel->KernelRecv(ctx, buffer, count) )
|
|
|
|
ret = (ssize_t) count;
|
|
|
|
}
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Unode::tcsetblob(ioctx_t* ctx, const char* name, const void* buffer, size_t count)
|
|
|
|
{
|
2015-10-22 19:37:45 -04:00
|
|
|
Channel* channel = server->Connect(ctx);
|
2014-05-05 15:36:40 -04:00
|
|
|
if ( !channel )
|
|
|
|
return -1;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
size_t namelen = name ? strlen(name) : 0;
|
|
|
|
if ( SIZE_MAX - count < namelen )
|
|
|
|
return errno = EOVERFLOW, -1;
|
|
|
|
struct fsm_req_tcsetblob msg;
|
|
|
|
struct fsm_resp_tcsetblob resp;
|
|
|
|
msg.ino = ino;
|
|
|
|
msg.namelen = namelen;
|
|
|
|
msg.count = count;
|
|
|
|
if ( SendMessage(channel, FSM_REQ_TCSETBLOB, &msg, sizeof(msg), namelen + count) &&
|
|
|
|
channel->KernelSend(&kctx, name, namelen) &&
|
|
|
|
channel->KernelSend(ctx, buffer, count) &&
|
|
|
|
RecvMessage(channel, FSM_RESP_TCSETBLOB, &resp, sizeof(resp)) )
|
|
|
|
ret = (ssize_t) resp.count;
|
|
|
|
channel->KernelClose();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
int Unode::unmounted(ioctx_t* /*ctx*/)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2014-05-07 08:14:38 -04:00
|
|
|
server->Unmount();
|
|
|
|
return 0;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
bool Bootstrap(Ref<Inode>* out_root,
|
|
|
|
Ref<Inode>* out_server,
|
|
|
|
const struct stat* rootst)
|
2013-01-30 14:33:13 -05:00
|
|
|
{
|
2014-05-07 08:14:38 -04:00
|
|
|
Ref<Server> server(new Server());
|
|
|
|
if ( !server )
|
|
|
|
return false;
|
2013-01-30 14:33:13 -05:00
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
ino_t root_inode_ino = rootst->st_ino;
|
|
|
|
mode_t root_inode_type = rootst->st_mode & S_IFMT;
|
|
|
|
Ref<Inode> root_inode = server->OpenNode(root_inode_ino, root_inode_type);
|
|
|
|
if ( !root_inode )
|
|
|
|
return false;
|
2014-10-16 18:04:47 -04:00
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
Ref<ServerNode> server_inode(new ServerNode(server));
|
|
|
|
if ( !server_inode )
|
|
|
|
return false;
|
2014-10-16 18:04:47 -04:00
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
return *out_root = root_inode, *out_server = server_inode, true;
|
2013-01-30 14:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace UserFS
|
|
|
|
} // namespace Sortix
|