Repository: mesos Updated Branches: refs/heads/master d10ac8e0b -> f47dfb726
Refactored stout os open/close/fcntl. Review: https://reviews.apache.org/r/26843 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f47dfb72 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f47dfb72 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f47dfb72 Branch: refs/heads/master Commit: f47dfb726fb8bfaf7dac468a3d98f40918e06196 Parents: d10ac8e Author: Jie Yu <[email protected]> Authored: Thu Oct 16 16:28:23 2014 -0700 Committer: Jie Yu <[email protected]> Committed: Thu Oct 16 16:52:13 2014 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/Makefile.am | 3 + .../3rdparty/stout/include/stout/os.hpp | 123 +------------------ .../3rdparty/stout/include/stout/os/close.hpp | 37 ++++++ .../3rdparty/stout/include/stout/os/fcntl.hpp | 86 +++++++++++++ .../3rdparty/stout/include/stout/os/open.hpp | 84 +++++++++++++ 5 files changed, 213 insertions(+), 120 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/f47dfb72/3rdparty/libprocess/3rdparty/stout/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am index d4a8ad4..d529013 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am @@ -37,12 +37,15 @@ nobase_include_HEADERS = \ stout/numify.hpp \ stout/option.hpp \ stout/os.hpp \ + stout/os/close.hpp \ stout/os/execenv.hpp \ stout/os/exists.hpp \ + stout/os/fcntl.hpp \ stout/os/fork.hpp \ stout/os/killtree.hpp \ stout/os/linux.hpp \ stout/os/ls.hpp \ + stout/os/open.hpp \ stout/os/osx.hpp \ stout/os/process.hpp \ stout/os/read.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/f47dfb72/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index c26bd2a..63bda7a 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -19,7 +19,6 @@ #endif #include <dirent.h> #include <errno.h> -#include <fcntl.h> #include <fts.h> #include <glob.h> #include <libgen.h> @@ -67,13 +66,16 @@ #include <stout/unreachable.hpp> #include <stout/version.hpp> +#include <stout/os/close.hpp> #include <stout/os/exists.hpp> +#include <stout/os/fcntl.hpp> #include <stout/os/fork.hpp> #include <stout/os/killtree.hpp> #ifdef __linux__ #include <stout/os/linux.hpp> #endif // __linux__ #include <stout/os/ls.hpp> +#include <stout/os/open.hpp> #ifdef __APPLE__ #include <stout/os/osx.hpp> #endif // __APPLE__ @@ -209,125 +211,6 @@ inline Try<bool> access(const std::string& path, int how) } -inline Try<Nothing> cloexec(int fd) -{ - int flags = ::fcntl(fd, F_GETFD); - - if (flags == -1) { - return ErrnoError(); - } - - if (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { - return ErrnoError(); - } - - return Nothing(); -} - - -inline Try<bool> isCloexec(int fd) -{ - int flags = ::fcntl(fd, F_GETFD); - - if (flags == -1) { - return ErrnoError(); - } - - return (flags & FD_CLOEXEC) != 0; -} - - -inline Try<Nothing> nonblock(int fd) -{ - int flags = ::fcntl(fd, F_GETFL); - - if (flags == -1) { - return ErrnoError(); - } - - if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { - return ErrnoError(); - } - - return Nothing(); -} - - -inline Try<bool> isNonblock(int fd) -{ - int flags = ::fcntl(fd, F_GETFL); - - if (flags == -1) { - return ErrnoError(); - } - - return (flags & O_NONBLOCK) != 0; -} - - -// For old systems that do not support O_CLOEXEC, we still want -// os::open to accept that flag so that we can simplify the code. -// TODO(jieyu): Move this along with nonblock/cloexec to a separate -// header 'stout/fcntl.hpp'. -#ifndef O_CLOEXEC -// Since we will define O_CLOEXEC if it is not yet defined, we use a -// special symbol to tell if the flag is truly unavailable or not. -#define O_CLOEXEC_UNDEFINED - -// NOTE: For backward compatibility concern, kernel usually does not -// change the constant values for symbols like O_CLOEXEC. -#if defined(__APPLE__) -// Copied from '/usr/include/sys/fcntl.h' -#define O_CLOEXEC 0x1000000 -#elif defined(__linux__) -// Copied from '/usr/include/asm-generic/fcntl.h'. -#define O_CLOEXEC 02000000 -#endif -#endif - - -inline Try<int> open(const std::string& path, int oflag, mode_t mode = 0) -{ -#ifdef O_CLOEXEC_UNDEFINED - // Before we passing oflag to ::open, we need to strip the O_CLOEXEC - // flag since it's not supported. - bool cloexec = false; - if ((oflag & O_CLOEXEC) != 0) { - oflag &= ~O_CLOEXEC; - cloexec = true; - } -#endif - - int fd = ::open(path.c_str(), oflag, mode); - - if (fd < 0) { - return ErrnoError(); - } - -#ifdef O_CLOEXEC_UNDEFINED - if (cloexec) { - Try<Nothing> result = os::cloexec(fd); - if (result.isError()) { - os::close(fd); - return Error("Failed to set cloexec: " + result.error()); - } - } -#endif - - return fd; -} - - -inline Try<Nothing> close(int fd) -{ - if (::close(fd) != 0) { - return ErrnoError(); - } - - return Nothing(); -} - - // Sets the access and modification times of 'path' to the current time. inline Try<Nothing> utime(const std::string& path) { http://git-wip-us.apache.org/repos/asf/mesos/blob/f47dfb72/3rdparty/libprocess/3rdparty/stout/include/stout/os/close.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/close.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/close.hpp new file mode 100644 index 0000000..fad82e8 --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/close.hpp @@ -0,0 +1,37 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STOUT_OS_CLOSE_HPP__ +#define __STOUT_OS_CLOSE_HPP__ + +#include <unistd.h> + +#include <stout/error.hpp> +#include <stout/nothing.hpp> +#include <stout/try.hpp> + +namespace os { + +inline Try<Nothing> close(int fd) +{ + if (::close(fd) != 0) { + return ErrnoError(); + } + + return Nothing(); +} + +} // namespace os { + +#endif // __STOUT_OS_CLOSE_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/f47dfb72/3rdparty/libprocess/3rdparty/stout/include/stout/os/fcntl.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/fcntl.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/fcntl.hpp new file mode 100644 index 0000000..bc8ffed --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/fcntl.hpp @@ -0,0 +1,86 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STOUT_OS_FCNTL_HPP__ +#define __STOUT_OS_FCNTL_HPP__ + +#include <fcntl.h> + +#include <stout/error.hpp> +#include <stout/nothing.hpp> +#include <stout/try.hpp> + +// TODO(jieyu): Consider introducing a general os::fcntl function +// which allows us to get/set multiple flags in one call. + +namespace os { + +inline Try<Nothing> cloexec(int fd) +{ + int flags = ::fcntl(fd, F_GETFD); + + if (flags == -1) { + return ErrnoError(); + } + + if (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { + return ErrnoError(); + } + + return Nothing(); +} + + +inline Try<bool> isCloexec(int fd) +{ + int flags = ::fcntl(fd, F_GETFD); + + if (flags == -1) { + return ErrnoError(); + } + + return (flags & FD_CLOEXEC) != 0; +} + + +inline Try<Nothing> nonblock(int fd) +{ + int flags = ::fcntl(fd, F_GETFL); + + if (flags == -1) { + return ErrnoError(); + } + + if (::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + return ErrnoError(); + } + + return Nothing(); +} + + +inline Try<bool> isNonblock(int fd) +{ + int flags = ::fcntl(fd, F_GETFL); + + if (flags == -1) { + return ErrnoError(); + } + + return (flags & O_NONBLOCK) != 0; +} + +} // namespace os { + +#endif // __STOUT_OS_FCNTL_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/f47dfb72/3rdparty/libprocess/3rdparty/stout/include/stout/os/open.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/open.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/open.hpp new file mode 100644 index 0000000..86949ec --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/open.hpp @@ -0,0 +1,84 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STOUT_OS_OPEN_HPP__ +#define __STOUT_OS_OPEN_HPP__ + +#include <sys/stat.h> +#include <sys/types.h> + +#include <string> + +#include <stout/error.hpp> +#include <stout/nothing.hpp> +#include <stout/try.hpp> + +#include <stout/os/close.hpp> +#include <stout/os/fcntl.hpp> + +namespace os { + +// For old systems that do not support O_CLOEXEC, we still want +// os::open to accept that flag so that we can simplify the code. +#ifndef O_CLOEXEC +// Since we will define O_CLOEXEC if it is not yet defined, we use a +// special symbol to tell if the flag is truly unavailable or not. +#define O_CLOEXEC_UNDEFINED + +// NOTE: For backward compatibility concern, kernel usually does not +// change the constant values for symbols like O_CLOEXEC. +#if defined(__APPLE__) +// Copied from '/usr/include/sys/fcntl.h' +#define O_CLOEXEC 0x1000000 +#elif defined(__linux__) +// Copied from '/usr/include/asm-generic/fcntl.h'. +#define O_CLOEXEC 02000000 +#endif +#endif + + +inline Try<int> open(const std::string& path, int oflag, mode_t mode = 0) +{ +#ifdef O_CLOEXEC_UNDEFINED + // Before we passing oflag to ::open, we need to strip the O_CLOEXEC + // flag since it's not supported. + bool cloexec = false; + if ((oflag & O_CLOEXEC) != 0) { + oflag &= ~O_CLOEXEC; + cloexec = true; + } +#endif + + int fd = ::open(path.c_str(), oflag, mode); + + if (fd < 0) { + return ErrnoError(); + } + +#ifdef O_CLOEXEC_UNDEFINED + if (cloexec) { + Try<Nothing> result = os::cloexec(fd); + if (result.isError()) { + os::close(fd); + return Error("Failed to set cloexec: " + result.error()); + } + } +#endif + + return fd; +} + +} // namespace os { + +#endif // __STOUT_OS_OPEN_HPP__
