Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2021-06-20 16:44:19 -04:00
|
|
|
* Copyright (c) 2012-2017, 2021 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* descriptor.cpp
|
|
|
|
* A file descriptor.
|
|
|
|
*/
|
2012-08-07 18:19:44 -04:00
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
#include <sys/socket.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sys/types.h>
|
2017-01-09 17:40:56 -05:00
|
|
|
#include <sys/uio.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2014-05-07 08:14:38 -04:00
|
|
|
#include <fsmarshall-msg.h>
|
2017-01-09 17:40:56 -05:00
|
|
|
#include <limits.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/dirent.h>
|
|
|
|
#include <sortix/fcntl.h>
|
2017-01-09 17:40:56 -05:00
|
|
|
#ifndef IOV_MAX
|
|
|
|
#include <sortix/limits.h>
|
|
|
|
#endif
|
2014-05-07 08:14:38 -04:00
|
|
|
#include <sortix/mount.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/seek.h>
|
|
|
|
#include <sortix/stat.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
#include <sortix/kernel/copy.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/descriptor.h>
|
|
|
|
#include <sortix/kernel/fsfunc.h>
|
|
|
|
#include <sortix/kernel/inode.h>
|
|
|
|
#include <sortix/kernel/ioctx.h>
|
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/kthread.h>
|
|
|
|
#include <sortix/kernel/process.h>
|
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include <sortix/kernel/string.h>
|
|
|
|
#include <sortix/kernel/vnode.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
|
2013-03-21 10:26:08 -04:00
|
|
|
// Flags for the various base modes to open a file in.
|
2014-10-26 19:17:34 -04:00
|
|
|
static const int ACCESS_FLAGS = O_READ | O_WRITE | O_EXEC | O_SEARCH;
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Flags that only make sense at open time.
|
2014-10-26 19:17:34 -04:00
|
|
|
static const int OPEN_FLAGS = O_CREATE | O_DIRECTORY | O_EXCL | O_TRUNC |
|
|
|
|
O_NOFOLLOW | O_SYMLINK_NOFOLLOW | O_NOCTTY |
|
|
|
|
O_TTY_INIT;
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Flags that only make sense for descriptors.
|
2014-10-26 19:17:34 -04:00
|
|
|
static const int DESCRIPTOR_FLAGS = O_APPEND | O_NONBLOCK;
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2016-08-19 09:46:39 -04:00
|
|
|
// Let the ioctx_t force bits like O_NONBLOCK and otherwise use the dflags of
|
|
|
|
// the current file descriptor. This allows the caller to do non-blocking reads
|
|
|
|
// without calls to fcntl that may be racy with respect to other threads.
|
|
|
|
static int ContextFlags(int ctx_dflags, int desc_dflags)
|
|
|
|
{
|
|
|
|
return desc_dflags | (ctx_dflags & DESCRIPTOR_FLAGS);
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:41:31 -04:00
|
|
|
int LinkInodeInDir(ioctx_t* ctx,
|
|
|
|
Ref<Descriptor> dir,
|
|
|
|
const char* name,
|
|
|
|
Ref<Inode> inode)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
|
|
|
Ref<Vnode> vnode(new Vnode(inode, Ref<Vnode>(), 0, 0));
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !vnode )
|
2015-09-20 15:41:31 -04:00
|
|
|
return -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> desc(new Descriptor(Ref<Vnode>(vnode), 0));
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !desc )
|
2015-09-20 15:41:31 -04:00
|
|
|
return -1;
|
|
|
|
return dir->link(ctx, name, desc);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2014-10-26 19:17:34 -04:00
|
|
|
Ref<Descriptor> OpenDirContainingPath(ioctx_t* ctx,
|
|
|
|
Ref<Descriptor> from,
|
|
|
|
const char* path,
|
|
|
|
char** final_ptr)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !path[0] )
|
|
|
|
return errno = EINVAL, Ref<Descriptor>();
|
2012-08-07 18:19:44 -04:00
|
|
|
char* dirpath;
|
|
|
|
char* final;
|
|
|
|
if ( !SplitFinalElem(path, &dirpath, &final) )
|
|
|
|
return Ref<Descriptor>();
|
|
|
|
// TODO: Removing trailing slashes in final may not the right thing.
|
2014-10-26 19:17:34 -04:00
|
|
|
size_t final_length = strlen(final);
|
|
|
|
while ( final_length && final[final_length-1] == '/' )
|
|
|
|
final[--final_length] = 0;
|
|
|
|
// There is room for a single character as final is not the empty string.
|
|
|
|
if ( !final_length )
|
|
|
|
{
|
|
|
|
final[0] = '.';
|
2012-08-07 18:19:44 -04:00
|
|
|
final[1] = '\0';
|
2014-10-26 19:17:34 -04:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !dirpath[0] )
|
|
|
|
{
|
|
|
|
delete[] dirpath;
|
2014-10-26 19:17:34 -04:00
|
|
|
*final_ptr = final;
|
2012-08-07 18:19:44 -04:00
|
|
|
return from;
|
|
|
|
}
|
2013-03-21 10:26:08 -04:00
|
|
|
Ref<Descriptor> ret = from->open(ctx, dirpath, O_READ | O_DIRECTORY, 0);
|
2012-08-07 18:19:44 -04:00
|
|
|
delete[] dirpath;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !ret )
|
|
|
|
return delete[] final, Ref<Descriptor>();
|
|
|
|
*final_ptr = final;
|
2012-08-07 18:19:44 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:12:07 -04:00
|
|
|
size_t TruncateIOVec(struct iovec* iov, int iovcnt, off_t limit)
|
|
|
|
{
|
|
|
|
assert(0 <= iovcnt);
|
|
|
|
assert(0 <= limit);
|
|
|
|
off_t left = limit;
|
|
|
|
if ( (uintmax_t) SSIZE_MAX <= (uintmax_t) left )
|
|
|
|
left = SSIZE_MAX;
|
|
|
|
size_t requested = 0;
|
|
|
|
for ( int i = 0; i < iovcnt; i++ )
|
|
|
|
{
|
|
|
|
size_t request = iov[i].iov_len;
|
|
|
|
if ( __builtin_add_overflow(requested, request, &requested) )
|
|
|
|
requested = SIZE_MAX;
|
|
|
|
if ( left == 0 )
|
|
|
|
iov[i].iov_len = 0;
|
|
|
|
else if ( (uintmax_t) left < (uintmax_t) request )
|
|
|
|
{
|
|
|
|
iov[i].iov_len = left;
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
left -= request;
|
|
|
|
}
|
|
|
|
return requested;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
// TODO: Add security checks.
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
Descriptor::Descriptor()
|
|
|
|
{
|
|
|
|
current_offset_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
this->vnode = Ref<Vnode>(NULL);
|
|
|
|
this->ino = 0;
|
|
|
|
this->dev = 0;
|
|
|
|
this->type = 0;
|
|
|
|
this->dflags = 0;
|
|
|
|
checked_seekable = false;
|
|
|
|
seekable = false /* unused */;
|
|
|
|
current_offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Descriptor::Descriptor(Ref<Vnode> vnode, int dflags)
|
|
|
|
{
|
2014-10-26 19:17:34 -04:00
|
|
|
current_offset_lock = KTHREAD_MUTEX_INITIALIZER;
|
2014-05-07 08:14:38 -04:00
|
|
|
this->vnode = Ref<Vnode>(NULL);
|
|
|
|
this->ino = 0;
|
|
|
|
this->dev = 0;
|
|
|
|
this->type = 0;
|
|
|
|
this->dflags = 0;
|
|
|
|
checked_seekable = false;
|
|
|
|
seekable = false /* unused */;
|
|
|
|
current_offset = 0;
|
|
|
|
LateConstruct(vnode, dflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Descriptor::LateConstruct(Ref<Vnode> vnode, int dflags)
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
this->vnode = vnode;
|
|
|
|
this->ino = vnode->ino;
|
|
|
|
this->dev = vnode->dev;
|
|
|
|
this->type = vnode->type;
|
|
|
|
this->dflags = dflags;
|
|
|
|
}
|
|
|
|
|
|
|
|
Descriptor::~Descriptor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:50:49 -05:00
|
|
|
bool Descriptor::SetFlags(int new_dflags)
|
|
|
|
{
|
|
|
|
// TODO: Hmm, there is race condition between changing the flags here and
|
|
|
|
// the code that uses the flags below. We could add a lock, but that
|
|
|
|
// would kinda prevent concurrency on the same file descriptor. Since
|
|
|
|
// the chances of this becoming a problem is rather slim (but could
|
|
|
|
// happen!), we'll do the unsafe thing for now. (See below also)
|
2014-01-16 14:44:18 -05:00
|
|
|
dflags = (dflags & ~DESCRIPTOR_FLAGS) | (new_dflags & DESCRIPTOR_FLAGS);
|
2013-01-14 08:50:49 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::GetFlags()
|
|
|
|
{
|
|
|
|
// TODO: The race condition also applies here if the variable can change.
|
|
|
|
return dflags;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> Descriptor::Fork()
|
|
|
|
{
|
2015-05-13 13:22:28 -04:00
|
|
|
ScopedLock lock(¤t_offset_lock);
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> ret(new Descriptor(vnode, dflags));
|
|
|
|
if ( !ret )
|
|
|
|
return Ref<Descriptor>();
|
2014-10-26 19:17:34 -04:00
|
|
|
ret->current_offset = current_offset;
|
2012-08-07 18:19:44 -04:00
|
|
|
ret->checked_seekable = checked_seekable;
|
|
|
|
ret->seekable = seekable;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Descriptor::IsSeekable()
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
return false;
|
2014-10-26 19:17:34 -04:00
|
|
|
ScopedLock lock(¤t_offset_lock);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !checked_seekable )
|
|
|
|
{
|
2014-09-23 16:55:13 -04:00
|
|
|
int saved_errno = errno;
|
2012-08-07 18:19:44 -04:00
|
|
|
ioctx_t ctx; SetupKernelIOCtx(&ctx);
|
2017-10-26 11:12:07 -04:00
|
|
|
seekable = S_ISDIR(vnode->type) || 0 <= vnode->lseek(&ctx, 0, SEEK_END);
|
2012-08-07 18:19:44 -04:00
|
|
|
checked_seekable = true;
|
2014-09-23 16:55:13 -04:00
|
|
|
errno = saved_errno;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
return seekable;
|
|
|
|
}
|
|
|
|
|
2021-12-28 05:40:09 -05:00
|
|
|
bool Descriptor::pass()
|
|
|
|
{
|
|
|
|
return vnode->pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Descriptor::unpass()
|
|
|
|
{
|
|
|
|
vnode->unpass();
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int Descriptor::sync(ioctx_t* ctx)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
// TODO: Possible denial-of-service attack if someone opens the file without
|
|
|
|
// that much rights and just syncs it a whole lot and slows down the
|
|
|
|
// system as a whole.
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->sync(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::stat(ioctx_t* ctx, struct stat* st)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
// TODO: Possible information leak if not O_READ | O_WRITE and the caller
|
|
|
|
// is told about the file size.
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->stat(ctx, st);
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:53:18 -05:00
|
|
|
int Descriptor::statvfs(ioctx_t* ctx, struct statvfs* stvfs)
|
|
|
|
{
|
|
|
|
// TODO: Possible information leak if not O_READ | O_WRITE and the caller
|
|
|
|
// is told about the file size.
|
|
|
|
return vnode->statvfs(ctx, stvfs);
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int Descriptor::chmod(ioctx_t* ctx, mode_t mode)
|
|
|
|
{
|
2018-09-01 10:37:17 -04:00
|
|
|
// TODO: Regardless of dflags, check if the user/group can chmod.
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->chmod(ctx, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::chown(ioctx_t* ctx, uid_t owner, gid_t group)
|
|
|
|
{
|
2018-09-01 10:37:17 -04:00
|
|
|
// TODO: Regardless of dflags, check if the user/group can chown.
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->chown(ctx, owner, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::truncate(ioctx_t* ctx, off_t length)
|
|
|
|
{
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( length < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_WRITE) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->truncate(ctx, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t Descriptor::lseek(ioctx_t* ctx, off_t offset, int whence)
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
{
|
|
|
|
if ( whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !IsSeekable() )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = ESPIPE, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
|
|
|
|
ScopedLock lock(¤t_offset_lock);
|
2017-10-26 11:12:07 -04:00
|
|
|
|
|
|
|
// TODO: Possible information leak to let someone without O_READ | O_WRITE
|
|
|
|
// seek the file and get information about data holes.
|
2012-08-07 18:19:44 -04:00
|
|
|
off_t reloff;
|
|
|
|
if ( whence == SEEK_SET )
|
|
|
|
reloff = 0;
|
|
|
|
else if ( whence == SEEK_CUR )
|
2014-10-26 19:17:34 -04:00
|
|
|
reloff = current_offset;
|
2012-08-07 18:19:44 -04:00
|
|
|
else if ( whence == SEEK_END )
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( (reloff = vnode->lseek(ctx, 0, SEEK_END)) < 0 )
|
2012-08-07 18:19:44 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t new_offset;
|
|
|
|
if ( __builtin_add_overflow(reloff, offset, &new_offset) )
|
2012-08-07 18:19:44 -04:00
|
|
|
return errno = EOVERFLOW, -1;
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( new_offset < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
|
2017-10-26 11:12:07 -04:00
|
|
|
return current_offset = new_offset;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Descriptor::read(ioctx_t* ctx, uint8_t* buf, size_t count)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_READ) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-10-26 11:12:07 -04:00
|
|
|
ssize_t result;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !IsSeekable() )
|
2017-10-26 11:12:07 -04:00
|
|
|
result = vnode->read(ctx, buf, count);
|
|
|
|
else
|
2016-08-19 09:46:39 -04:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
ScopedLock lock(¤t_offset_lock);
|
|
|
|
off_t available = OFF_MAX - current_offset;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
|
|
|
count = available;
|
|
|
|
result = vnode->pread(ctx, buf, count, current_offset);
|
|
|
|
if ( 0 < result &&
|
|
|
|
__builtin_add_overflow(current_offset, result, ¤t_offset) )
|
|
|
|
current_offset = OFF_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
}
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
2017-10-26 11:12:07 -04:00
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::readv(ioctx_t* ctx, const struct iovec* iov_ptr, int iovcnt)
|
|
|
|
{
|
|
|
|
if ( !(dflags & O_READ) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( iovcnt < 0 || IOV_MAX < iovcnt )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
struct iovec* iov = new struct iovec[iovcnt];
|
|
|
|
if ( !iov )
|
|
|
|
return -1;
|
|
|
|
size_t iov_size = sizeof(struct iovec) * iovcnt;
|
|
|
|
if ( !ctx->copy_from_src(iov, iov_ptr, iov_size) )
|
|
|
|
return delete[] iov, -1;
|
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-10-26 11:12:07 -04:00
|
|
|
ssize_t result = -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( !IsSeekable() )
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( SSIZE_MAX < TruncateIOVec(iov, iovcnt, SSIZE_MAX) )
|
|
|
|
errno = EINVAL;
|
|
|
|
else
|
|
|
|
result = vnode->readv(ctx, iov, iovcnt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ScopedLock lock(¤t_offset_lock);
|
|
|
|
off_t available = OFF_MAX - current_offset;
|
|
|
|
if ( SSIZE_MAX < TruncateIOVec(iov, iovcnt, available) )
|
|
|
|
errno = EINVAL;
|
|
|
|
else
|
|
|
|
result = vnode->preadv(ctx, iov, iovcnt, current_offset);
|
|
|
|
if ( 0 < result &&
|
|
|
|
__builtin_add_overflow(current_offset, result, ¤t_offset) )
|
|
|
|
current_offset = OFF_MAX;
|
2017-01-09 17:40:56 -05:00
|
|
|
}
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
delete[] iov;
|
2017-10-26 11:12:07 -04:00
|
|
|
return result;
|
2017-01-09 17:40:56 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t Descriptor::pread(ioctx_t* ctx, uint8_t* buf, size_t count, off_t off)
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
return read(ctx, buf, count);
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_READ) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !IsSeekable() )
|
|
|
|
return errno = ESPIPE, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( off < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t available = OFF_MAX - off;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
|
|
|
count = available;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
ssize_t result = vnode->pread(ctx, buf, count, off);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::preadv(ioctx_t* ctx, const struct iovec* iov_ptr,
|
|
|
|
int iovcnt, off_t off)
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
return readv(ctx, iov_ptr, iovcnt);
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( !(dflags & O_READ) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !IsSeekable() )
|
|
|
|
return errno = ESPIPE, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( off < 0 || iovcnt < 0 || IOV_MAX < iovcnt )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
struct iovec* iov = new struct iovec[iovcnt];
|
|
|
|
if ( !iov )
|
|
|
|
return -1;
|
|
|
|
size_t iov_size = sizeof(struct iovec) * iovcnt;
|
|
|
|
if ( !ctx->copy_from_src(iov, iov_ptr, iov_size) )
|
|
|
|
return delete[] iov, -1;
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t available = OFF_MAX - off;
|
|
|
|
if ( SSIZE_MAX < TruncateIOVec(iov, iovcnt, available) )
|
|
|
|
return delete[] iov, errno = EINVAL, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
ssize_t result = vnode->preadv(ctx, iov, iovcnt, off);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
delete[] iov;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t Descriptor::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_WRITE) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-10-26 11:12:07 -04:00
|
|
|
ssize_t result;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !IsSeekable() )
|
2017-10-26 11:12:07 -04:00
|
|
|
result = vnode->write(ctx, buf, count);
|
|
|
|
else
|
2016-08-19 09:46:39 -04:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
ScopedLock lock(¤t_offset_lock);
|
|
|
|
if ( ctx->dflags & O_APPEND )
|
2016-08-19 09:46:39 -04:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t end = vnode->lseek(ctx, 0, SEEK_END);
|
|
|
|
if ( 0 <= end )
|
|
|
|
current_offset = end;
|
|
|
|
}
|
|
|
|
if ( current_offset == OFF_MAX && count )
|
|
|
|
{
|
|
|
|
errno = EFBIG;
|
|
|
|
result = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
off_t available = OFF_MAX - current_offset;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
|
|
|
count = available;
|
|
|
|
result = vnode->pwrite(ctx, buf, count, current_offset);
|
|
|
|
if ( 0 < result &&
|
|
|
|
__builtin_add_overflow(current_offset, result,
|
|
|
|
¤t_offset) )
|
|
|
|
current_offset = OFF_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
}
|
2014-10-26 19:17:34 -04:00
|
|
|
}
|
2016-08-19 09:46:39 -04:00
|
|
|
ctx->dflags = old_ctx_dflags;
|
2017-10-26 11:12:07 -04:00
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::writev(ioctx_t* ctx, const struct iovec* iov_ptr,
|
|
|
|
int iovcnt)
|
|
|
|
{
|
|
|
|
if ( !(dflags & O_WRITE) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( iovcnt < 0 || IOV_MAX < iovcnt )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
struct iovec* iov = new struct iovec[iovcnt];
|
|
|
|
if ( !iov )
|
|
|
|
return -1;
|
|
|
|
size_t iov_size = sizeof(struct iovec) * iovcnt;
|
|
|
|
if ( !ctx->copy_from_src(iov, iov_ptr, iov_size) )
|
|
|
|
return delete[] iov, -1;
|
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-10-26 11:12:07 -04:00
|
|
|
ssize_t result = -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( !IsSeekable() )
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( SSIZE_MAX < TruncateIOVec(iov, iovcnt, SSIZE_MAX) )
|
|
|
|
errno = EINVAL;
|
|
|
|
else
|
|
|
|
result = vnode->writev(ctx, iov, iovcnt);
|
2017-01-09 17:40:56 -05:00
|
|
|
}
|
2017-10-26 11:12:07 -04:00
|
|
|
else
|
2017-01-09 17:40:56 -05:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
ScopedLock lock(¤t_offset_lock);
|
|
|
|
if ( ctx->dflags & O_APPEND )
|
2017-01-09 17:40:56 -05:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t end = vnode->lseek(ctx, 0, SEEK_END);
|
|
|
|
if ( 0 <= end )
|
|
|
|
current_offset = end;
|
|
|
|
}
|
|
|
|
off_t available = OFF_MAX - current_offset;
|
|
|
|
size_t count = TruncateIOVec(iov, iovcnt, available);
|
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
errno = EINVAL;
|
|
|
|
else if ( current_offset == OFF_MAX && count )
|
|
|
|
errno = EFBIG;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = vnode->pwritev(ctx, iov, iovcnt, current_offset);
|
|
|
|
if ( 0 < result &&
|
|
|
|
__builtin_add_overflow(current_offset, result,
|
|
|
|
¤t_offset) )
|
|
|
|
current_offset = OFF_MAX;
|
2017-01-09 17:40:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
delete[] iov;
|
2017-10-26 11:12:07 -04:00
|
|
|
return result;
|
2017-01-09 17:40:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Descriptor::pwrite(ioctx_t* ctx, const uint8_t* buf, size_t count,
|
|
|
|
off_t off)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
return write(ctx, buf, count);
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_WRITE) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !IsSeekable() )
|
|
|
|
return errno = ESPIPE, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( off < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( off == OFF_MAX && count )
|
|
|
|
return errno = EFBIG, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t available = OFF_MAX - off;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
|
|
|
count = available;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
ssize_t result = vnode->pwrite(ctx, buf, count, off);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::pwritev(ioctx_t* ctx, const struct iovec* iov_ptr,
|
|
|
|
int iovcnt, off_t off)
|
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( S_ISCHR(type) )
|
|
|
|
return writev(ctx, iov_ptr, iovcnt);
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( !(dflags & O_WRITE) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !IsSeekable() )
|
|
|
|
return errno = ESPIPE, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( off < 0 || iovcnt < 0 || IOV_MAX < iovcnt )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
struct iovec* iov = new struct iovec[iovcnt];
|
|
|
|
if ( !iov )
|
|
|
|
return -1;
|
|
|
|
size_t iov_size = sizeof(struct iovec) * iovcnt;
|
|
|
|
if ( !ctx->copy_from_src(iov, iov_ptr, iov_size) )
|
|
|
|
return delete[] iov, -1;
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t available = OFF_MAX - off;
|
|
|
|
size_t count = TruncateIOVec(iov, iovcnt, available);
|
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
return delete[] iov, errno = EINVAL, -1;
|
|
|
|
if ( off == OFF_MAX && count != 0 )
|
|
|
|
return delete[] iov, errno = EFBIG, -1;
|
2017-01-09 17:40:56 -05:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
ssize_t result = vnode->pwritev(ctx, iov, iovcnt, off);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
delete[] iov;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:29:37 -05:00
|
|
|
static inline bool valid_utimens_timespec(struct timespec ts)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2021-06-20 16:44:19 -04:00
|
|
|
return (0 <= ts.tv_nsec && ts.tv_nsec < 1000000000) ||
|
2016-02-24 10:29:37 -05:00
|
|
|
ts.tv_nsec == UTIME_NOW ||
|
|
|
|
ts.tv_nsec == UTIME_OMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::utimens(ioctx_t* ctx, const struct timespec* user_times)
|
|
|
|
{
|
|
|
|
struct timespec times[2];
|
|
|
|
if ( !user_times )
|
|
|
|
{
|
|
|
|
times[0].tv_sec = 0;
|
|
|
|
times[0].tv_nsec = UTIME_NOW;
|
|
|
|
times[1].tv_sec = 0;
|
|
|
|
times[1].tv_nsec = UTIME_NOW;
|
|
|
|
}
|
|
|
|
else if ( !ctx->copy_from_src(×, user_times, sizeof(times)) )
|
|
|
|
return -1;
|
|
|
|
if ( !valid_utimens_timespec(times[0]) ||
|
|
|
|
!valid_utimens_timespec(times[1]) )
|
|
|
|
return errno = EINVAL;
|
2018-09-01 10:37:17 -04:00
|
|
|
// TODO: Regardless of dflags, check if the user/group can utimens.
|
2016-02-24 10:29:37 -05:00
|
|
|
return vnode->utimens(ctx, times);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::isatty(ioctx_t* ctx)
|
|
|
|
{
|
|
|
|
return vnode->isatty(ctx);
|
|
|
|
}
|
|
|
|
|
2014-10-26 19:17:34 -04:00
|
|
|
ssize_t Descriptor::readdirents(ioctx_t* ctx,
|
2015-11-19 20:57:09 -05:00
|
|
|
struct dirent* dirent,
|
|
|
|
size_t size)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
// TODO: COMPATIBILITY HACK: Traditionally, you can open a directory with
|
|
|
|
// O_RDONLY and pass it to fdopendir and then use it, which doesn't
|
|
|
|
// set the needed O_SEARCH flag! I think some software even do it with
|
|
|
|
// write permissions! Currently, we just let you search the directory
|
|
|
|
// if you opened with any of the O_SEARCH, O_READ or O_WRITE flags.
|
|
|
|
// A better solution would be to make fdopendir try to add the
|
|
|
|
// O_SEARCH flag to the file descriptor. Or perhaps just recheck the
|
|
|
|
// permissions to search (execute) the directory manually every time,
|
|
|
|
// though that is less pure. Unfortunately, POSIX is pretty vague on
|
|
|
|
// how O_SEARCH should be interpreted and most existing Unix systems
|
|
|
|
// such as Linux doesn't even have that flag! And how about combining
|
|
|
|
// it with the O_EXEC flag - POSIX allows that and it makes sense
|
|
|
|
// because the execute bit on directories control search permission.
|
|
|
|
if ( !(dflags & (O_SEARCH | O_READ | O_WRITE)) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < size )
|
|
|
|
size = SSIZE_MAX;
|
|
|
|
if ( size < sizeof(*dirent) )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
ScopedLock lock(¤t_offset_lock);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( current_offset == OFF_MAX && size )
|
|
|
|
return 0;
|
2015-11-19 20:57:09 -05:00
|
|
|
ssize_t ret = vnode->readdirents(ctx, dirent, size, current_offset);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( 0 < ret &&
|
|
|
|
__builtin_add_overflow(current_offset, 1, ¤t_offset) )
|
|
|
|
current_offset = OFF_MAX;
|
2012-08-07 18:19:44 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-21 10:26:08 -04:00
|
|
|
static bool IsSaneFlagModeCombination(int flags, mode_t /*mode*/)
|
|
|
|
{
|
2013-09-27 08:44:52 -04:00
|
|
|
// It doesn't make sense to pass O_CREATE or O_TRUNC when attempting to open
|
2013-03-21 10:26:08 -04:00
|
|
|
// a directory. We also reject O_TRUNC | O_DIRECTORY early to prevent
|
|
|
|
// opening a directory, attempting to truncate it, and then aborting with an
|
|
|
|
// error because a directory was opened.
|
2013-09-27 08:44:52 -04:00
|
|
|
if ( (flags & (O_CREATE | O_TRUNC)) && (flags & (O_DIRECTORY)) )
|
2018-04-18 00:37:49 -04:00
|
|
|
return false;
|
|
|
|
// POSIX: "The result of using O_TRUNC without either O_RDWR or
|
|
|
|
// O_WRONLY is undefined."
|
|
|
|
if ( (flags & O_TRUNC) && !(flags & O_WRITE) )
|
|
|
|
return false;
|
2013-03-21 10:26:08 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsLastPathElement(const char* elem)
|
|
|
|
{
|
|
|
|
while ( !(*elem == '/' || *elem == '\0') )
|
|
|
|
elem++;
|
|
|
|
while ( *elem == '/' )
|
|
|
|
elem++;
|
|
|
|
return *elem == '\0';
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> Descriptor::open(ioctx_t* ctx, const char* filename, int flags,
|
|
|
|
mode_t mode)
|
|
|
|
{
|
2013-05-16 16:03:15 -04:00
|
|
|
Process* process = CurrentProcess();
|
|
|
|
kthread_mutex_lock(&process->idlock);
|
|
|
|
mode &= ~process->umask;
|
|
|
|
kthread_mutex_unlock(&process->idlock);
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !filename[0] )
|
|
|
|
return errno = ENOENT, Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Reject some non-sensical flag combinations early.
|
|
|
|
if ( !IsSaneFlagModeCombination(flags, mode) )
|
2012-08-07 18:19:44 -04:00
|
|
|
return errno = EINVAL, Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2014-09-22 11:35:54 -04:00
|
|
|
char* filename_mine = NULL;
|
|
|
|
|
|
|
|
size_t symlink_iteration = 0;
|
|
|
|
const size_t MAX_SYMLINK_ITERATION = 20;
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> desc(this);
|
|
|
|
while ( filename[0] )
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
// Reaching a slash in the path means that the caller intended what came
|
|
|
|
// before to be a directory, stop the open call if it isn't.
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( filename[0] == '/' )
|
|
|
|
{
|
|
|
|
if ( !S_ISDIR(desc->type) )
|
2014-09-22 11:35:54 -04:00
|
|
|
return delete[] filename_mine, errno = ENOTDIR, Ref<Descriptor>();
|
2012-08-07 18:19:44 -04:00
|
|
|
filename++;
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Cut out the next path element from the input string.
|
2012-08-07 18:19:44 -04:00
|
|
|
size_t slashpos = strcspn(filename, "/");
|
|
|
|
char* elem = String::Substring(filename, 0, slashpos);
|
|
|
|
if ( !elem )
|
2014-09-22 11:35:54 -04:00
|
|
|
return delete[] filename_mine, Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Decide how to open the next element in the path.
|
|
|
|
bool lastelem = IsLastPathElement(filename);
|
|
|
|
int open_flags = lastelem ? flags : O_READ | O_DIRECTORY;
|
2012-08-07 18:19:44 -04:00
|
|
|
mode_t open_mode = lastelem ? mode : 0;
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Open the next element in the path.
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> next = desc->open_elem(ctx, elem, open_flags, open_mode);
|
|
|
|
delete[] elem;
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !next )
|
2014-09-22 11:35:54 -04:00
|
|
|
return delete[] filename_mine, Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
filename += slashpos;
|
2014-09-22 11:35:54 -04:00
|
|
|
|
|
|
|
bool want_the_symlink_itself = lastelem && (flags & O_SYMLINK_NOFOLLOW);
|
|
|
|
if ( S_ISLNK(next->type) && !want_the_symlink_itself )
|
|
|
|
{
|
|
|
|
if ( (flags & O_NOFOLLOW) && lastelem )
|
|
|
|
return delete[] filename_mine, errno = ELOOP, Ref<Descriptor>();
|
|
|
|
|
|
|
|
if ( symlink_iteration++ == MAX_SYMLINK_ITERATION )
|
|
|
|
return delete[] filename_mine, errno = ELOOP, Ref<Descriptor>();
|
|
|
|
|
|
|
|
ioctx_t kctx;
|
|
|
|
SetupKernelIOCtx(&kctx);
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if ( next->stat(&kctx, &st) < 0 )
|
|
|
|
return delete[] filename_mine, Ref<Descriptor>();
|
|
|
|
assert(0 <= st.st_size);
|
|
|
|
|
|
|
|
if ( (uintmax_t) SIZE_MAX <= (uintmax_t) st.st_size )
|
|
|
|
return delete[] filename_mine, Ref<Descriptor>();
|
|
|
|
|
|
|
|
size_t linkpath_length = (size_t) st.st_size;
|
|
|
|
char* linkpath = new char[linkpath_length + 1];
|
|
|
|
if ( !linkpath )
|
|
|
|
return delete[] filename_mine, Ref<Descriptor>();
|
|
|
|
|
|
|
|
ssize_t linkpath_ret = next->readlink(&kctx, linkpath, linkpath_length);
|
|
|
|
if ( linkpath_ret < 0 )
|
|
|
|
return delete[] linkpath, delete[] filename_mine, Ref<Descriptor>();
|
|
|
|
linkpath[linkpath_length] = '\0';
|
|
|
|
|
|
|
|
linkpath_length = strlen(linkpath);
|
|
|
|
if ( linkpath_length == 0 )
|
|
|
|
return delete[] linkpath, delete[] filename_mine,
|
|
|
|
errno = ENOENT, Ref<Descriptor>();
|
|
|
|
bool link_from_root = linkpath[0] == '/';
|
|
|
|
|
|
|
|
// Either filename is the empty string or starts with a slash.
|
|
|
|
size_t filename_length = strlen(filename);
|
|
|
|
// TODO: Avoid overflow here.
|
|
|
|
size_t new_filename_length = linkpath_length + filename_length;
|
|
|
|
char* new_filename = new char[new_filename_length + 1];
|
|
|
|
if ( !new_filename )
|
|
|
|
return delete[] linkpath, delete[] filename_mine,
|
|
|
|
errno = ENOENT, Ref<Descriptor>();
|
|
|
|
stpcpy(stpcpy(new_filename, linkpath), filename);
|
|
|
|
delete[] filename_mine;
|
|
|
|
filename = filename_mine = new_filename;
|
|
|
|
|
|
|
|
if ( link_from_root )
|
|
|
|
desc = CurrentProcess()->GetRoot();
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = next;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2014-09-22 11:35:54 -04:00
|
|
|
delete[] filename_mine;
|
|
|
|
|
2018-04-18 00:37:49 -04:00
|
|
|
// Abort if the user tries to write to an existing directory.
|
|
|
|
if ( S_ISDIR(desc->type) )
|
|
|
|
{
|
|
|
|
if ( flags & (O_CREATE | O_TRUNC | O_WRITE) )
|
|
|
|
return errno = EISDIR, Ref<Descriptor>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the file if requested.
|
|
|
|
if ( (flags & O_TRUNC) && S_ISREG(desc->type) )
|
|
|
|
{
|
|
|
|
assert(flags & O_WRITE); // IsSaneFlagModeCombination
|
|
|
|
if ( desc->truncate(ctx, 0) < 0 )
|
|
|
|
return Ref<Descriptor>();
|
|
|
|
}
|
|
|
|
|
2013-03-21 10:26:08 -04:00
|
|
|
// Abort the open if the user wanted a directory but this wasn't.
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( flags & O_DIRECTORY && !S_ISDIR(desc->type) )
|
|
|
|
return errno = ENOTDIR, Ref<Descriptor>();
|
|
|
|
|
|
|
|
// TODO: The new file descriptor may not be opened with the correct
|
|
|
|
// permissions in the below case!
|
|
|
|
// If the path only contains slashes, we'll get outselves back, be sure to
|
|
|
|
// get ourselves back.
|
|
|
|
return desc == this ? Fork() : desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Descriptor> Descriptor::open_elem(ioctx_t* ctx, const char* filename,
|
|
|
|
int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
assert(!strchr(filename, '/'));
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Verify that at least one of the base access modes are being used.
|
|
|
|
if ( !(flags & ACCESS_FLAGS) )
|
|
|
|
return errno = EINVAL, Ref<Descriptor>();
|
|
|
|
|
|
|
|
// Filter away flags that only make sense for descriptors.
|
|
|
|
int retvnode_flags = flags & ~DESCRIPTOR_FLAGS;
|
|
|
|
Ref<Vnode> retvnode = vnode->open(ctx, filename, retvnode_flags, mode);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !retvnode )
|
|
|
|
return Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
|
|
|
// Filter away flags that only made sense at during the open call.
|
|
|
|
int ret_flags = flags & ~OPEN_FLAGS;
|
|
|
|
Ref<Descriptor> ret(new Descriptor(retvnode, ret_flags));
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !ret )
|
|
|
|
return Ref<Descriptor>();
|
2013-03-21 10:26:08 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::mkdir(ioctx_t* ctx, const char* filename, mode_t mode)
|
|
|
|
{
|
2013-05-16 16:03:15 -04:00
|
|
|
Process* process = CurrentProcess();
|
|
|
|
kthread_mutex_lock(&process->idlock);
|
|
|
|
mode &= ~process->umask;
|
|
|
|
kthread_mutex_unlock(&process->idlock);
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
char* final;
|
|
|
|
Ref<Descriptor> dir = OpenDirContainingPath(ctx, Ref<Descriptor>(this),
|
|
|
|
filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return -1;
|
|
|
|
int ret = dir->vnode->mkdir(ctx, final, mode);
|
|
|
|
delete[] final;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::link(ioctx_t* ctx, const char* filename, Ref<Descriptor> node)
|
|
|
|
{
|
|
|
|
char* final;
|
|
|
|
Ref<Descriptor> dir = OpenDirContainingPath(ctx, Ref<Descriptor>(this),
|
|
|
|
filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return -1;
|
|
|
|
int ret = dir->vnode->link(ctx, final, node->vnode);
|
|
|
|
delete[] final;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:49:31 -04:00
|
|
|
int Descriptor::unlinkat(ioctx_t* ctx, const char* filename, int flags)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2015-05-13 12:49:31 -04:00
|
|
|
if ( flags & ~(AT_REMOVEFILE | AT_REMOVEDIR) )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
if ( !(flags & (AT_REMOVEFILE | AT_REMOVEDIR)) )
|
|
|
|
flags |= AT_REMOVEFILE;
|
2012-08-07 18:19:44 -04:00
|
|
|
char* final;
|
|
|
|
Ref<Descriptor> dir = OpenDirContainingPath(ctx, Ref<Descriptor>(this),
|
|
|
|
filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return -1;
|
2015-05-13 12:49:31 -04:00
|
|
|
int ret = -1;
|
|
|
|
if ( ret < 0 && (flags & AT_REMOVEFILE) )
|
|
|
|
ret = dir->vnode->unlink(ctx, final);
|
|
|
|
if ( ret < 0 && (flags & AT_REMOVEDIR) )
|
|
|
|
ret = dir->vnode->rmdir(ctx, final);
|
2012-08-07 18:19:44 -04:00
|
|
|
delete[] final;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::symlink(ioctx_t* ctx, const char* oldname, const char* filename)
|
|
|
|
{
|
|
|
|
char* final;
|
|
|
|
Ref<Descriptor> dir = OpenDirContainingPath(ctx, Ref<Descriptor>(this),
|
|
|
|
filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return -1;
|
|
|
|
int ret = dir->vnode->symlink(ctx, oldname, final);
|
|
|
|
delete[] final;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-20 10:19:07 -05:00
|
|
|
int Descriptor::rename_here(ioctx_t* ctx, Ref<Descriptor> from,
|
|
|
|
const char* oldpath, const char* newpath)
|
|
|
|
{
|
|
|
|
char* olddir_elem;
|
|
|
|
char* newdir_elem;
|
|
|
|
Ref<Descriptor> olddir = OpenDirContainingPath(ctx, from, oldpath,
|
|
|
|
&olddir_elem);
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !olddir )
|
|
|
|
return -1;
|
2012-12-20 10:19:07 -05:00
|
|
|
|
|
|
|
Ref<Descriptor> newdir = OpenDirContainingPath(ctx, Ref<Descriptor>(this),
|
|
|
|
newpath, &newdir_elem);
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( !newdir )
|
|
|
|
return delete[] olddir_elem, -1;
|
2012-12-20 10:19:07 -05:00
|
|
|
|
|
|
|
int ret = newdir->vnode->rename_here(ctx, olddir->vnode, olddir_elem,
|
|
|
|
newdir_elem);
|
|
|
|
|
|
|
|
delete[] newdir_elem;
|
|
|
|
delete[] olddir_elem;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t Descriptor::readlink(ioctx_t* ctx, char* buf, size_t bufsize)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
if ( !(dflags & O_READ) )
|
2017-10-26 11:12:07 -04:00
|
|
|
return errno = EBADF, -1;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < bufsize )
|
|
|
|
bufsize = SSIZE_MAX;
|
2012-08-07 18:19:44 -04:00
|
|
|
return vnode->readlink(ctx, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:55:05 -05:00
|
|
|
int Descriptor::tcgetwincurpos(ioctx_t* ctx, struct wincurpos* wcp)
|
|
|
|
{
|
|
|
|
return vnode->tcgetwincurpos(ctx, wcp);
|
|
|
|
}
|
|
|
|
|
2016-11-15 15:08:41 -05:00
|
|
|
int Descriptor::ioctl(ioctx_t* ctx, int cmd, uintptr_t arg)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2016-11-15 15:08:41 -05:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
int result = vnode->ioctl(ctx, cmd, arg);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 20:18:07 -04:00
|
|
|
int Descriptor::tcsetpgrp(ioctx_t* ctx, pid_t pgid)
|
|
|
|
{
|
|
|
|
return vnode->tcsetpgrp(ctx, pgid);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t Descriptor::tcgetpgrp(ioctx_t* ctx)
|
|
|
|
{
|
|
|
|
return vnode->tcgetpgrp(ctx);
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int Descriptor::settermmode(ioctx_t* ctx, unsigned mode)
|
|
|
|
{
|
|
|
|
return vnode->settermmode(ctx, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::gettermmode(ioctx_t* ctx, unsigned* mode)
|
|
|
|
{
|
|
|
|
return vnode->gettermmode(ctx, mode);
|
|
|
|
}
|
|
|
|
|
2012-12-29 17:09:09 -05:00
|
|
|
int Descriptor::poll(ioctx_t* ctx, PollNode* node)
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
// TODO: Perhaps deny polling against some kind of events if this
|
|
|
|
// descriptor's dflags would reject doing these operations?
|
2012-12-29 17:09:09 -05:00
|
|
|
return vnode->poll(ctx, node);
|
|
|
|
}
|
|
|
|
|
2017-02-25 11:00:24 -05:00
|
|
|
Ref<Descriptor> Descriptor::accept4(ioctx_t* ctx, uint8_t* addr,
|
|
|
|
size_t* addrlen, int flags)
|
2013-03-19 17:40:37 -04:00
|
|
|
{
|
2018-04-29 07:32:29 -04:00
|
|
|
if ( flags & ~(SOCK_NONBLOCK) )
|
|
|
|
return errno = EINVAL, Ref<Descriptor>();
|
|
|
|
int new_dflags = O_READ | O_WRITE;
|
|
|
|
if ( flags & SOCK_NONBLOCK )
|
|
|
|
new_dflags |= O_NONBLOCK;
|
|
|
|
flags &= ~(SOCK_NONBLOCK);
|
2017-02-25 11:00:24 -05:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
Ref<Vnode> retvnode = vnode->accept4(ctx, addr, addrlen, flags);
|
2013-03-19 17:40:37 -04:00
|
|
|
if ( !retvnode )
|
|
|
|
return Ref<Descriptor>();
|
2017-02-25 11:00:24 -05:00
|
|
|
ctx->dflags = old_ctx_dflags;
|
2018-04-29 07:32:29 -04:00
|
|
|
return Ref<Descriptor>(new Descriptor(retvnode, new_dflags));
|
2013-03-19 17:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::bind(ioctx_t* ctx, const uint8_t* addr, size_t addrlen)
|
|
|
|
{
|
|
|
|
return vnode->bind(ctx, addr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::connect(ioctx_t* ctx, const uint8_t* addr, size_t addrlen)
|
|
|
|
{
|
2022-12-04 18:35:21 -05:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
int result = vnode->connect(ctx, addr, addrlen);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2013-03-19 17:40:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::listen(ioctx_t* ctx, int backlog)
|
|
|
|
{
|
|
|
|
return vnode->listen(ctx, backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Descriptor::recv(ioctx_t* ctx, uint8_t* buf, size_t count, int flags)
|
|
|
|
{
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( SIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( flags & MSG_DONTWAIT )
|
|
|
|
ctx->dflags |= O_NONBLOCK;
|
|
|
|
flags &= ~MSG_DONTWAIT;
|
2016-08-19 09:46:39 -04:00
|
|
|
ssize_t result = vnode->recv(ctx, buf, count, flags);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2013-03-19 17:40:37 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::recvmsg(ioctx_t* ctx, struct msghdr* msg, int flags)
|
|
|
|
{
|
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
if ( flags & MSG_DONTWAIT )
|
|
|
|
ctx->dflags |= O_NONBLOCK;
|
|
|
|
flags &= ~MSG_DONTWAIT;
|
|
|
|
ssize_t result = vnode->recvmsg(ctx, msg, flags);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:40:37 -04:00
|
|
|
ssize_t Descriptor::send(ioctx_t* ctx, const uint8_t* buf, size_t count, int flags)
|
|
|
|
{
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( SIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2016-08-19 09:46:39 -04:00
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
2017-01-09 17:40:56 -05:00
|
|
|
if ( flags & MSG_DONTWAIT )
|
|
|
|
ctx->dflags |= O_NONBLOCK;
|
|
|
|
flags &= ~MSG_DONTWAIT;
|
2016-08-19 09:46:39 -04:00
|
|
|
ssize_t result = vnode->send(ctx, buf, count, flags);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
2013-03-19 17:40:37 -04:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:40:56 -05:00
|
|
|
ssize_t Descriptor::sendmsg(ioctx_t* ctx, const struct msghdr* msg, int flags)
|
|
|
|
{
|
|
|
|
int old_ctx_dflags = ctx->dflags;
|
|
|
|
ctx->dflags = ContextFlags(old_ctx_dflags, dflags);
|
|
|
|
if ( flags & MSG_DONTWAIT )
|
|
|
|
ctx->dflags |= O_NONBLOCK;
|
|
|
|
flags &= ~MSG_DONTWAIT;
|
|
|
|
ssize_t result = vnode->sendmsg(ctx, msg, flags);
|
|
|
|
ctx->dflags = old_ctx_dflags;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-28 11:10:08 -05:00
|
|
|
int Descriptor::getsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
void* option_value, size_t* option_size_ptr)
|
|
|
|
{
|
|
|
|
return vnode->getsockopt(ctx, level, option_name, option_value, option_size_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::setsockopt(ioctx_t* ctx, int level, int option_name,
|
|
|
|
const void* option_value, size_t option_size)
|
|
|
|
{
|
|
|
|
return vnode->setsockopt(ctx, level, option_name, option_value, option_size);
|
|
|
|
}
|
|
|
|
|
2014-05-05 15:36:40 -04:00
|
|
|
ssize_t Descriptor::tcgetblob(ioctx_t* ctx, const char* name, void* buffer, size_t count)
|
|
|
|
{
|
|
|
|
if ( name && !name[0] )
|
|
|
|
name = NULL;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < count )
|
|
|
|
count = SSIZE_MAX;
|
2014-05-05 15:36:40 -04:00
|
|
|
return vnode->tcgetblob(ctx, name, buffer, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Descriptor::tcsetblob(ioctx_t* ctx, const char* name, const void* buffer, size_t count)
|
|
|
|
{
|
|
|
|
if ( name && !name[0] )
|
|
|
|
name = NULL;
|
2014-10-26 19:17:34 -04:00
|
|
|
if ( SSIZE_MAX < count )
|
2014-05-05 15:36:40 -04:00
|
|
|
return errno = EFBIG, -1;
|
|
|
|
return vnode->tcsetblob(ctx, name, buffer, count);
|
|
|
|
}
|
|
|
|
|
2014-05-07 08:14:38 -04:00
|
|
|
int Descriptor::unmount(ioctx_t* ctx, const char* filename, int flags)
|
|
|
|
{
|
|
|
|
if ( flags & ~(UNMOUNT_FORCE | UNMOUNT_DETACH | UNMOUNT_NOFOLLOW) )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
int subflags = flags & ~(UNMOUNT_NOFOLLOW);
|
|
|
|
char* final;
|
|
|
|
// TODO: This may follow a symlink when not supposed to!
|
|
|
|
Ref<Descriptor> dir =
|
|
|
|
OpenDirContainingPath(ctx, Ref<Descriptor>(this), filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return -1;
|
|
|
|
if ( !(flags & UNMOUNT_NOFOLLOW) )
|
|
|
|
{
|
|
|
|
// TODO: Potentially follow a symlink here!
|
|
|
|
}
|
|
|
|
int ret = dir->vnode->unmount(ctx, final, subflags);
|
|
|
|
delete[] final;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::fsm_fsbind(ioctx_t* ctx, Ref<Descriptor> target, int flags)
|
|
|
|
{
|
|
|
|
return vnode->fsm_fsbind(ctx, target->vnode, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Descriptor> Descriptor::fsm_mount(ioctx_t* ctx,
|
|
|
|
const char* filename,
|
|
|
|
const struct stat* rootst,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
if ( flags & ~(FSM_MOUNT_NOFOLLOW | FSM_MOUNT_NONBLOCK) )
|
|
|
|
return errno = EINVAL, Ref<Descriptor>(NULL);
|
|
|
|
int result_dflags = O_READ | O_WRITE;
|
|
|
|
if ( flags & FSM_MOUNT_NOFOLLOW ) result_dflags |= O_NONBLOCK;
|
|
|
|
int subflags = flags & ~(FSM_MOUNT_NOFOLLOW | FSM_MOUNT_NONBLOCK);
|
|
|
|
char* final;
|
|
|
|
// TODO: This may follow a symlink when not supposed to!
|
|
|
|
Ref<Descriptor> dir =
|
|
|
|
OpenDirContainingPath(ctx, Ref<Descriptor>(this), filename, &final);
|
|
|
|
if ( !dir )
|
|
|
|
return errno = EINVAL, Ref<Descriptor>(NULL);
|
|
|
|
if ( !(flags & FSM_MOUNT_NOFOLLOW) )
|
|
|
|
{
|
|
|
|
// TODO: Potentially follow a symlink here!
|
|
|
|
}
|
|
|
|
Ref<Descriptor> result(new Descriptor());
|
|
|
|
if ( !result )
|
|
|
|
return Ref<Descriptor>(NULL);
|
|
|
|
Ref<Vnode> result_vnode = dir->vnode->fsm_mount(ctx, final, rootst, subflags);
|
|
|
|
delete[] final;
|
|
|
|
if ( !result_vnode )
|
|
|
|
return Ref<Descriptor>(NULL);
|
|
|
|
result->LateConstruct(result_vnode, result_dflags);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-23 14:56:07 -05:00
|
|
|
int Descriptor::tcdrain(ioctx_t* ctx)
|
|
|
|
{
|
|
|
|
return vnode->tcdrain(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::tcflow(ioctx_t* ctx, int action)
|
|
|
|
{
|
|
|
|
return vnode->tcflow(ctx, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::tcflush(ioctx_t* ctx, int queue_selector)
|
|
|
|
{
|
|
|
|
return vnode->tcflush(ctx, queue_selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::tcgetattr(ioctx_t* ctx, struct termios* tio)
|
|
|
|
{
|
|
|
|
return vnode->tcgetattr(ctx, tio);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t Descriptor::tcgetsid(ioctx_t* ctx)
|
|
|
|
{
|
|
|
|
return vnode->tcgetsid(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::tcsendbreak(ioctx_t* ctx, int duration)
|
|
|
|
{
|
|
|
|
return vnode->tcsendbreak(ctx, duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::tcsetattr(ioctx_t* ctx, int actions, const struct termios* tio)
|
|
|
|
{
|
|
|
|
return vnode->tcsetattr(ctx, actions, tio);
|
|
|
|
}
|
|
|
|
|
2016-08-06 09:44:37 -04:00
|
|
|
int Descriptor::shutdown(ioctx_t* ctx, int how)
|
|
|
|
{
|
|
|
|
if ( how & ~(SHUT_RD | SHUT_WR) )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
return vnode->shutdown(ctx, how);
|
|
|
|
}
|
|
|
|
|
2017-02-23 14:12:26 -05:00
|
|
|
int Descriptor::getpeername(ioctx_t* ctx, uint8_t* addr, size_t* addrsize)
|
|
|
|
{
|
|
|
|
return vnode->getpeername(ctx, addr, addrsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Descriptor::getsockname(ioctx_t* ctx, uint8_t* addr, size_t* addrsize)
|
|
|
|
{
|
|
|
|
return vnode->getsockname(ctx, addr, addrsize);
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
} // namespace Sortix
|