diff options
author | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-24 21:43:12 +0000 |
---|---|---|
committer | sbc@chromium.org <sbc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-24 21:43:12 +0000 |
commit | 98ffd9fd0303b3c4ff47439f607a491b2aceac07 (patch) | |
tree | c7b54da0c32306f5ec2199c6d5f8ae04eb3c9fd0 | |
parent | 114571b355d34bc8459f10775255032c2b86105e (diff) | |
download | chromium_src-98ffd9fd0303b3c4ff47439f607a491b2aceac07.zip chromium_src-98ffd9fd0303b3c4ff47439f607a491b2aceac07.tar.gz chromium_src-98ffd9fd0303b3c4ff47439f607a491b2aceac07.tar.bz2 |
[NaCl SDK] nacl_io: Remove duplication between passthroughfs and devfs.
They both had implementations of RealNode.
Also update examples/tutorial/testing so that it shuts down
the NaCl module correctly.
TEST=trybots
R=binji@chromium.org
Review URL: https://codereview.chromium.org/414993002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285382 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 193 insertions, 174 deletions
diff --git a/native_client_sdk/src/examples/tutorial/testing/example.js b/native_client_sdk/src/examples/tutorial/testing/example.js index 627c5ca..b51b988 100644 --- a/native_client_sdk/src/examples/tutorial/testing/example.js +++ b/native_client_sdk/src/examples/tutorial/testing/example.js @@ -45,9 +45,14 @@ function handleMessage(event) { var msg = event.data; var firstColon = msg.indexOf(':'); var cmd = msg.substr(0, firstColon); + + if (cmd == 'testend') { + event.srcElement.postMessage({'testend' : ''}); + return; + } + var cmdFunctionName = cmd + 'Command'; var cmdFunction = window[cmdFunctionName]; - if (typeof(cmdFunction) !== 'function') { console.log('Unknown command: ' + cmd); console.log(' message: ' + msg); diff --git a/native_client_sdk/src/examples/tutorial/testing/index.html b/native_client_sdk/src/examples/tutorial/testing/index.html index 4fd2383..37fcdd8 100644 --- a/native_client_sdk/src/examples/tutorial/testing/index.html +++ b/native_client_sdk/src/examples/tutorial/testing/index.html @@ -17,7 +17,7 @@ .failed { background-color: #f00; } </style> </head> -<body {{attrs}}> +<body data-attrs="PS_EXIT_MESSAGE=testend" {{attrs}}> <h1>{{title}}</h1> <div id="listener"></div> <h2>Status: <code id="statusField">NO-STATUS</code></h2> diff --git a/native_client_sdk/src/libraries/nacl_io/devfs/dev_fs.cc b/native_client_sdk/src/libraries/nacl_io/devfs/dev_fs.cc index efe8055..6dd7642 100644 --- a/native_client_sdk/src/libraries/nacl_io/devfs/dev_fs.cc +++ b/native_client_sdk/src/libraries/nacl_io/devfs/dev_fs.cc @@ -20,6 +20,7 @@ #include "nacl_io/kernel_wrap_real.h" #include "nacl_io/node.h" #include "nacl_io/osunistd.h" +#include "nacl_io/passthroughfs/real_node.h" #include "nacl_io/pepper_interface.h" #include "sdk_util/auto_lock.h" @@ -33,24 +34,6 @@ namespace nacl_io { namespace { -class RealNode : public Node { - public: - RealNode(Filesystem* filesystem, int fd); - - virtual Error Read(const HandleAttr& attr, - void* buf, - size_t count, - int* out_bytes); - virtual Error Write(const HandleAttr& attr, - const void* buf, - size_t count, - int* out_bytes); - virtual Error GetStat(struct stat* stat); - - protected: - int fd_; -}; - class NullNode : public CharNode { public: explicit NullNode(Filesystem* filesystem) : CharNode(filesystem) {} @@ -125,44 +108,6 @@ class FsNode : public Node { Filesystem* other_fs_; }; -RealNode::RealNode(Filesystem* filesystem, int fd) : Node(filesystem), fd_(fd) { - SetType(S_IFCHR); -} - -Error RealNode::Read(const HandleAttr& attr, - void* buf, - size_t count, - int* out_bytes) { - *out_bytes = 0; - - size_t readcnt; - int err = _real_read(fd_, buf, count, &readcnt); - if (err) - return err; - - *out_bytes = static_cast<int>(readcnt); - return 0; -} - -Error RealNode::Write(const HandleAttr& attr, - const void* buf, - size_t count, - int* out_bytes) { - *out_bytes = 0; - - size_t writecnt; - int err = _real_write(fd_, buf, count, &writecnt); - if (err) - return err; - - *out_bytes = static_cast<int>(writecnt); - return 0; -} - -Error RealNode::GetStat(struct stat* stat) { - return _real_fstat(fd_, stat); -} - Error NullNode::Read(const HandleAttr& attr, void* buf, size_t count, diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc index af3ecca..1236e5d 100644 --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc @@ -46,6 +46,7 @@ EXTERN_C_BEGIN __libnacl_irt_##group.name = (typeof(REAL(name)))WRAP(name); extern void __libnacl_irt_dev_filename_init(void); +extern void __libnacl_irt_dev_fdio_init(void); extern struct nacl_irt_basic __libnacl_irt_basic; extern struct nacl_irt_fdio __libnacl_irt_fdio; @@ -261,13 +262,14 @@ static void assign_real_pointers() { static bool assigned = false; if (!assigned) { __libnacl_irt_dev_filename_init(); + __libnacl_irt_dev_fdio_init(); EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) assigned = true; } } -#define CHECK_REAL(func) \ - if (!REAL(func)) \ +#define CHECK_REAL(func) \ + if (!REAL(func)) \ assign_real_pointers(); // "real" functions, i.e. the unwrapped original functions. @@ -289,6 +291,12 @@ int _real_fstat(int fd, struct stat* buf) { int _real_isatty(int fd, int* result) { CHECK_REAL(isatty); + // The real isatty function can be NULL (for example if we are running + // withing chrome). + if (REAL(isatty) == NULL) { + *result = 0; + return ENOTTY; + } return REAL(isatty)(fd, result); } diff --git a/native_client_sdk/src/libraries/nacl_io/library.dsc b/native_client_sdk/src/libraries/nacl_io/library.dsc index d8630dd..a0f398b 100644 --- a/native_client_sdk/src/libraries/nacl_io/library.dsc +++ b/native_client_sdk/src/libraries/nacl_io/library.dsc @@ -48,6 +48,7 @@ "nacl_io.cc", "node.cc", "passthroughfs/passthrough_fs.cc", + "passthroughfs/real_node.cc", "path.cc", "pepper_interface.cc", "pepper_interface_delegate.cc", @@ -213,6 +214,7 @@ "osunistd.h", "osutime.h", "passthroughfs/passthrough_fs.h", + "passthroughfs/real_node.h", "path.h", "pepper_interface_delegate.h", "pepper_interface_dummy.h", diff --git a/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.cc b/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.cc index 52c5f96..93dbf18 100644 --- a/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.cc +++ b/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.cc @@ -8,120 +8,10 @@ #include "nacl_io/kernel_handle.h" #include "nacl_io/kernel_wrap_real.h" +#include "nacl_io/passthroughfs/real_node.h" namespace nacl_io { -class PassthroughFsNode : public Node { - public: - explicit PassthroughFsNode(Filesystem* filesystem, int real_fd) - : Node(filesystem), real_fd_(real_fd) {} - - protected: - virtual Error Init(int flags) { return 0; } - - virtual void Destroy() { - if (real_fd_) - _real_close(real_fd_); - real_fd_ = 0; - } - - public: - // Normal read/write operations on a file - virtual Error Read(const HandleAttr& attr, - void* buf, - size_t count, - int* out_bytes) { - *out_bytes = 0; - - int64_t new_offset; - int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset); - if (err) - return err; - - size_t nread; - err = _real_read(real_fd_, buf, count, &nread); - if (err) - return err; - - *out_bytes = static_cast<int>(nread); - return 0; - } - - virtual Error Write(const HandleAttr& attr, - const void* buf, - size_t count, - int* out_bytes) { - *out_bytes = 0; - - int64_t new_offset; - int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset); - if (err) - return err; - - size_t nwrote; - err = _real_write(real_fd_, buf, count, &nwrote); - if (err) - return err; - - *out_bytes = static_cast<int>(nwrote); - return 0; - } - - virtual Error FTruncate(off_t size) { - // TODO(binji): what to do here? - return ENOSYS; - } - - virtual Error GetDents(size_t offs, - struct dirent* pdir, - size_t count, - int* out_bytes) { - size_t nread; - int err = _real_getdents(real_fd_, pdir, count, &nread); - if (err) - return err; - return nread; - } - - virtual Error GetStat(struct stat* stat) { - int err = _real_fstat(real_fd_, stat); - if (err) - return err; - return 0; - } - - virtual Error Isatty() { -#ifdef __GLIBC__ - // isatty is not yet hooked up to the IRT interface under glibc. - return ENOTTY; -#else - int result = 0; - int err = _real_isatty(real_fd_, &result); - if (err) - return err; - return 0; -#endif - } - - Error MMap(void* addr, - size_t length, - int prot, - int flags, - size_t offset, - void** out_addr) { - *out_addr = addr; - int err = _real_mmap(out_addr, length, prot, flags, real_fd_, offset); - if (err) - return err; - return 0; - } - - private: - friend class PassthroughFs; - - int real_fd_; -}; - PassthroughFs::PassthroughFs() { } @@ -144,7 +34,7 @@ Error PassthroughFs::Open(const Path& path, int mode, ScopedNode* out_node) { if (error) return error; - out_node->reset(new PassthroughFsNode(this, real_fd)); + out_node->reset(new RealNode(this, real_fd, true)); return 0; } @@ -155,7 +45,7 @@ Error PassthroughFs::OpenResource(const Path& path, ScopedNode* out_node) { if (error) return error; - out_node->reset(new PassthroughFsNode(this, real_fd)); + out_node->reset(new RealNode(this, real_fd)); return 0; } diff --git a/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.h b/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.h index 498201b..a26ca71 100644 --- a/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.h +++ b/native_client_sdk/src/libraries/nacl_io/passthroughfs/passthrough_fs.h @@ -3,7 +3,6 @@ // found in the LICENSE file. #ifndef LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_ - #define LIBRARIES_NACL_IO_PASSTHROUGHFS_PASSTHROUGH_FS_H_ #include "nacl_io/filesystem.h" diff --git a/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.cc b/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.cc new file mode 100644 index 0000000..08a4e32 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.cc @@ -0,0 +1,118 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "nacl_io/passthroughfs/real_node.h" + +#include <errno.h> + +#include "nacl_io/kernel_handle.h" +#include "nacl_io/kernel_wrap_real.h" + +namespace nacl_io { +RealNode::RealNode(Filesystem* filesystem, int real_fd, bool close_on_destroy) + : Node(filesystem), + real_fd_(real_fd), + close_on_destroy_(close_on_destroy) +{ +} + +void RealNode::Destroy() { + if (close_on_destroy_) + _real_close(real_fd_); + real_fd_ = -1; +} + +// Normal read/write operations on a file +Error RealNode::Read(const HandleAttr& attr, + void* buf, + size_t count, + int* out_bytes) { + *out_bytes = 0; + + int64_t new_offset; + int err = _real_lseek(real_fd_, attr.offs, 0, &new_offset); + if (err && err != ESPIPE) + return err; + + size_t nread; + err = _real_read(real_fd_, buf, count, &nread); + if (err) + return err; + + *out_bytes = static_cast<int>(nread); + return 0; +} + +Error RealNode::Write(const HandleAttr& attr, + const void* buf, + size_t count, + int* out_bytes) { + //nacl_io_log("Real::Write\n"); + int err; + *out_bytes = 0; + + int64_t new_offset; + err = _real_lseek(real_fd_, attr.offs, 0, &new_offset); + if (err && err != ESPIPE) + return err; + + size_t nwrote; + err = _real_write(real_fd_, buf, count, &nwrote); + if (err) + return err; + + *out_bytes = static_cast<int>(nwrote); + return 0; +} + +Error RealNode::FTruncate(off_t size) { + // TODO(binji): what to do here? + return ENOSYS; +} + +Error RealNode::GetDents(size_t offs, + struct dirent* pdir, + size_t count, + int* out_bytes) { + size_t nread; + int err = _real_getdents(real_fd_, pdir, count, &nread); + if (err) + return err; + return nread; +} + +Error RealNode::GetStat(struct stat* stat) { + int err = _real_fstat(real_fd_, stat); + if (err) + return err; + return 0; +} + +Error RealNode::Isatty() { +#ifdef __GLIBC__ + // isatty is not yet hooked up to the IRT interface under glibc. + return ENOTTY; +#else + int result = 0; + int err = _real_isatty(real_fd_, &result); + if (err) + return err; + return 0; +#endif +} + +Error RealNode::MMap(void* addr, + size_t length, + int prot, + int flags, + size_t offset, + void** out_addr) { + *out_addr = addr; + int err = _real_mmap(out_addr, length, prot, flags, real_fd_, offset); + if (err) + return err; + return 0; +} + +} diff --git a/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.h b/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.h new file mode 100644 index 0000000..2bc3ac1 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_io/passthroughfs/real_node.h @@ -0,0 +1,52 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +#ifndef LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_ +#define LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_ + +#include "nacl_io/node.h" + +namespace nacl_io { + +class RealNode : public Node { + public: + RealNode(Filesystem* filesystem, int real_fd, bool close_on_destroy = false); + + protected: + virtual Error Init(int flags) { return 0; } + + virtual void Destroy(); + + public: + // Normal read/write operations on a file + virtual Error Read(const HandleAttr& attr, + void* buf, + size_t count, + int* out_bytes); + virtual Error Write(const HandleAttr& attr, + const void* buf, + size_t count, + int* out_bytes); + virtual Error FTruncate(off_t size); + virtual Error GetDents(size_t offs, + struct dirent* pdir, + size_t count, + int* out_bytes); + virtual Error GetStat(struct stat* stat); + virtual Error Isatty(); + virtual Error MMap(void* addr, + size_t length, + int prot, + int flags, + size_t offset, + void** out_addr); + + private: + int real_fd_; + bool close_on_destroy_; +}; + +} // namespace nacl_io + +#endif // LIBRARIES_NACL_IO_PASSTHROUGHFS_REAL_NODE_H_ |