Index: native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc |
diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc |
index 8d5655ab59945c927ea65be2633d1b825051bcaf..dbc5e955ac2dd6a57113b7889b195b22a4d11cea 100644 |
--- a/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc |
+++ b/native_client_sdk/src/libraries/nacl_io/kernel_proxy.cc |
@@ -37,7 +37,8 @@ |
#define USR_ID 1002 |
#define GRP_ID 1003 |
-KernelProxy::KernelProxy() : dev_(0), ppapi_(NULL) {} |
+KernelProxy::KernelProxy() : dev_(0), ppapi_(NULL) { |
+} |
KernelProxy::~KernelProxy() { |
// Clean up the MountFactories. |
@@ -52,7 +53,6 @@ KernelProxy::~KernelProxy() { |
void KernelProxy::Init(PepperInterface* ppapi) { |
ppapi_ = ppapi; |
- cwd_ = "/"; |
dev_ = 1; |
factories_["memfs"] = new TypedMountFactory<MountMem>; |
@@ -74,18 +74,42 @@ void KernelProxy::Init(PepperInterface* ppapi) { |
open("/dev/stderr", O_WRONLY); |
} |
-int KernelProxy::open(const char* path, int oflags) { |
+int KernelProxy::open_resource(const char* path) { |
+ ScopedMount mnt; |
Path rel; |
- ScopedMount mnt; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
+ if (error) { |
+ errno = error; |
+ return -1; |
+ } |
+ |
+ ScopedMountNode node; |
+ error = mnt->OpenResource(rel, &node); |
+ if (error) { |
+ // OpenResource failed, try Open(). |
+ error = mnt->Open(rel, O_RDONLY, &node); |
+ if (error) { |
+ errno = error; |
+ return -1; |
+ } |
+ } |
+ |
+ ScopedKernelHandle handle(new KernelHandle(mnt, node)); |
+ error = handle->Init(O_RDONLY); |
if (error) { |
errno = error; |
return -1; |
} |
+ return AllocateFD(handle); |
+} |
+ |
+int KernelProxy::open(const char* path, int oflags) { |
+ ScopedMount mnt; |
ScopedMountNode node; |
- error = mnt->Open(rel, oflags, &node); |
+ |
+ Error error = AcquireMountAndNode(path, oflags, &mnt, &node); |
if (error) { |
errno = error; |
return -1; |
@@ -141,19 +165,30 @@ int KernelProxy::dup2(int oldfd, int newfd) { |
return newfd; |
} |
+int KernelProxy::chdir(const char* path) { |
+ Error error = SetCWD(path); |
+ if (error) { |
+ errno = error; |
+ return -1; |
+ } |
+ return 0; |
+} |
+ |
char* KernelProxy::getcwd(char* buf, size_t size) { |
- AutoLock lock(&process_lock_); |
+ std::string cwd = GetCWD(); |
+ |
if (size <= 0) { |
errno = EINVAL; |
return NULL; |
} |
+ |
// If size is 0, allocate as much as we need. |
if (size == 0) { |
- size = cwd_.size() + 1; |
+ size = cwd.size() + 1; |
} |
// Verify the buffer is large enough |
- if (size <= cwd_.size()) { |
+ if (size <= cwd.size()) { |
errno = ERANGE; |
return NULL; |
} |
@@ -163,7 +198,7 @@ char* KernelProxy::getcwd(char* buf, size_t size) { |
buf = static_cast<char*>(malloc(size)); |
} |
- strcpy(buf, cwd_.c_str()); |
+ strcpy(buf, cwd.c_str()); |
return buf; |
} |
@@ -204,7 +239,8 @@ int KernelProxy::utime(const char* filename, const struct utimbuf* times) { |
int KernelProxy::mkdir(const char* path, mode_t mode) { |
ScopedMount mnt; |
Path rel; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
if (error) { |
errno = error; |
return -1; |
@@ -222,7 +258,8 @@ int KernelProxy::mkdir(const char* path, mode_t mode) { |
int KernelProxy::rmdir(const char* path) { |
ScopedMount mnt; |
Path rel; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
if (error) { |
errno = error; |
return -1; |
@@ -247,41 +284,13 @@ int KernelProxy::stat(const char* path, struct stat* buf) { |
return result; |
} |
-int KernelProxy::chdir(const char* path) { |
- struct stat statbuf; |
- if (stat(path, &statbuf) == -1) |
- return -1; |
- |
- bool is_dir = (statbuf.st_mode & S_IFDIR) != 0; |
- if (!is_dir) { |
- errno = ENOTDIR; |
- return -1; |
- } |
- |
- AutoLock lock(&process_lock_); |
- cwd_ = GetAbsPathLocked(path).Join(); |
- return 0; |
-} |
int KernelProxy::mount(const char* source, |
const char* target, |
const char* filesystemtype, |
unsigned long mountflags, |
const void* data) { |
- // See if it's already mounted |
- std::string abs_targ; |
- |
- // Scope this lock to prevent holding both process and kernel locks |
- { |
- AutoLock lock(&process_lock_); |
- abs_targ = GetAbsPathLocked(target).Join(); |
- } |
- |
- AutoLock lock(&kernel_lock_); |
- if (mounts_.find(abs_targ) != mounts_.end()) { |
- errno = EBUSY; |
- return -1; |
- } |
+ std::string abs_path = GetAbsParts(target).Join(); |
// Find a factory of that type |
MountFactoryMap_t::iterator factory = factories_.find(filesystemtype); |
@@ -290,9 +299,10 @@ int KernelProxy::mount(const char* source, |
return -1; |
} |
+ // Create a map of settings |
StringMap_t smap; |
smap["SOURCE"] = source; |
- smap["TARGET"] = abs_targ; |
+ smap["TARGET"] = abs_path; |
if (data) { |
char* str = strdup(static_cast<const char*>(data)); |
@@ -311,8 +321,14 @@ int KernelProxy::mount(const char* source, |
free(str); |
} |
- Error error = |
- factory->second->CreateMount(dev_++, smap, ppapi_, &mounts_[abs_targ]); |
+ ScopedMount mnt; |
+ Error error = factory->second->CreateMount(dev_++, smap, ppapi_, &mnt); |
+ if (error) { |
+ errno = error; |
+ return -1; |
+ } |
+ |
+ error = AttachMountAtPath(mnt, abs_path); |
if (error) { |
errno = error; |
return -1; |
@@ -322,28 +338,11 @@ int KernelProxy::mount(const char* source, |
} |
int KernelProxy::umount(const char* path) { |
- Path abs_path; |
- |
- // Scope this lock to prevent holding both process and kernel locks |
- { |
- AutoLock lock(&process_lock_); |
- abs_path = GetAbsPathLocked(path); |
- } |
- |
- AutoLock lock(&kernel_lock_); |
- MountMap_t::iterator it = mounts_.find(abs_path.Join()); |
- |
- if (mounts_.end() == it) { |
- errno = EINVAL; |
- return -1; |
- } |
- |
- if (it->second->RefCount() != 1) { |
- errno = EBUSY; |
+ Error error = DetachMountAtPath(path); |
+ if (error) { |
+ errno = error; |
return -1; |
} |
- |
- mounts_.erase(it); |
return 0; |
} |
@@ -519,7 +518,8 @@ off_t KernelProxy::lseek(int fd, off_t offset, int whence) { |
int KernelProxy::unlink(const char* path) { |
ScopedMount mnt; |
Path rel; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
if (error) { |
errno = error; |
return -1; |
@@ -537,7 +537,8 @@ int KernelProxy::unlink(const char* path) { |
int KernelProxy::remove(const char* path) { |
ScopedMount mnt; |
Path rel; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
if (error) { |
errno = error; |
return -1; |
@@ -565,10 +566,10 @@ int KernelProxy::fchmod(int fd, int mode) { |
} |
int KernelProxy::access(const char* path, int amode) { |
+ ScopedMount mnt; |
Path rel; |
- ScopedMount mnt; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
+ Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
if (error) { |
errno = error; |
return -1; |
@@ -656,33 +657,3 @@ int KernelProxy::munmap(void* addr, size_t length) { |
return 0; |
} |
-int KernelProxy::open_resource(const char* path) { |
- ScopedMount mnt; |
- Path rel; |
- Error error = AcquireMountAndPath(path, &mnt, &rel); |
- if (error) { |
- errno = error; |
- return -1; |
- } |
- |
- ScopedMountNode node; |
- error = mnt->OpenResource(rel, &node); |
- if (error) { |
- // OpenResource failed, try Open(). |
- error = mnt->Open(rel, O_RDONLY, &node); |
- if (error) { |
- errno = error; |
- return -1; |
- } |
- } |
- |
- ScopedKernelHandle handle(new KernelHandle(mnt, node)); |
- error = handle->Init(O_RDONLY); |
- if (error) { |
- errno = error; |
- return -1; |
- } |
- |
- return AllocateFD(handle); |
-} |
- |