13 #ifndef __STOUT_POSIX_OS_HPP__ 14 #define __STOUT_POSIX_OS_HPP__ 18 #include <sys/loadavg.h> 19 #define dirfd(dir) ((dir)->d_fd) 21 #define NAME_MAX MAXNAMLEN 38 #include <sys/ioctl.h> 41 #include <linux/version.h> 42 #include <sys/sysinfo.h> 45 #include <sys/ioctl.h> 46 #include <sys/utsname.h> 145 if (child_pid == 0) {
147 }
else if (child_pid < 0) {
148 return ErrnoError(
"os::waitpid: Call to `waitpid` failed");
157 inline void setenv(
const std::string& key,
158 const std::string& value,
159 bool overwrite =
true)
161 ::setenv(key.c_str(), value.c_str(), overwrite ? 1 : 0);
183 char* value =
::getenv(key.c_str());
188 ::memset(value,
'\0', ::strlen(value));
206 const std::string&
path,
222 remaining.tv_sec =
static_cast<long>(duration.
secs());
224 static_cast<long>((duration -
Seconds(remaining.tv_sec)).
ns());
226 while (nanosleep(&remaining, &remaining) == -1) {
227 if (errno == EINTR) {
242 int status =
::glob(pattern.c_str(), GLOB_NOSORT,
nullptr, &g);
244 std::list<std::string> result;
247 if (status == GLOB_NOMATCH) {
254 for (
size_t i = 0; i < g.gl_pathc; ++i) {
255 result.push_back(g.gl_pathv[i]);
267 long cpus = sysconf(_SC_NPROCESSORS_ONLN);
283 if (getloadavg(loadArray, 3) == -1) {
284 return ErrnoError(
"Failed to determine system load averages");
288 load.
one = loadArray[0];
289 load.
five = loadArray[1];
301 if (::
uname(&name) < 0) {
322 }
else if (group.
isSome() && group.
get() < 0) {
323 return Error(
"Invalid group");
324 }
else if (session.
isSome() && session.
get() < 0) {
325 return Error(
"Invalid session");
335 if (group.
isSome() && group.
get() == 0) {
338 if (session.
isSome() && session.
get() == 0) {
342 std::set<pid_t> result;
349 result.insert(process.
pid);
352 result.insert(process.
pid);
355 result.insert(process.
pid);
367 os::shell(
"tar %s %s %s",
"-czf", archive.c_str(), path.c_str());
370 return Error(
"Failed to archive " + path +
": " + tarOut.
error());
385 int major, minor,
patch = 0;
399 if (::sscanf(info->
release.c_str(),
"%d.%d-%*s", &major, &minor) != 2) {
403 return Version(major, minor, patch);
415 while (::
dup2(oldFd, newFd) == -1) {
416 if (errno == EINTR) {
431 static std::mutex* mutex =
new std::mutex;
433 synchronized (mutex) {
434 const char* slavePath =
::ptsname(master);
435 if (slavePath ==
nullptr) {
445 if (ioctl(fd, TIOCSCTTY,
nullptr) == -1) {
458 unsigned short columns)
460 struct winsize winsize;
461 winsize.ws_row = rows;
462 winsize.ws_col = columns;
464 if (ioctl(fd, TIOCSWINSZ, &winsize) != 0) {
476 return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
481 #endif // __STOUT_POSIX_OS_HPP__ Try< Nothing > dup2(int oldFd, int newFd)
Definition: os.hpp:413
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Try< std::string > patch(const std::string &s, const Diff &diff)
Definition: svn.hpp:157
Definition: master.hpp:27
Try< Nothing > chmod(const std::string &path, int mode)
Definition: os.hpp:195
Try< Nothing > sleep(const Duration &duration)
Definition: os.hpp:219
const pid_t group
Definition: process.hpp:55
Try< std::list< Process > > processes()
Definition: os.hpp:184
Try< Nothing > mknod(const std::string &path, mode_t mode, dev_t dev)
Definition: os.hpp:205
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
const Option< pid_t > session
Definition: process.hpp:56
void setenv(const std::string &key, const std::string &value, bool overwrite=true)
Definition: os.hpp:157
void unsetenv(const std::string &key)
Definition: os.hpp:167
Definition: duration.hpp:32
std::string host_default_path()
Definition: os.hpp:474
bool isSome() const
Definition: option.hpp:116
std::string release
Definition: os.hpp:48
std::string version
Definition: os.hpp:49
Try< Load > loadavg()
Definition: os.hpp:280
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
int mode_t
Definition: windows.hpp:177
Try< dev_t > dev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:181
const pid_t pid
Definition: process.hpp:53
Try< Nothing > utime(const std::string &path)
Definition: utime.hpp:32
Try< long > cpus()
Definition: os.hpp:265
Definition: duration.hpp:207
Try< Nothing > tar(const std::string &path, const std::string &archive)
Definition: os.hpp:364
std::string hstrerror(int err)=delete
Try< Nothing > setctty(int fd)
Definition: os.hpp:443
const T & get() const &
Definition: option.hpp:119
Option< std::string > getenv(const std::string &key)
Definition: getenv.hpp:29
int random()
Definition: os.hpp:580
Try< Version > release()
Definition: os.hpp:378
double secs() const
Definition: duration.hpp:49
Result< pid_t > waitpid(pid_t pid, int *status, int options)
Definition: os.hpp:141
std::string sysname
Definition: os.hpp:46
Try< UTSInfo > uname()
Definition: os.hpp:297
static Try error(const E &e)
Definition: try.hpp:43
Try< std::string > shell(const std::string &fmt, const T &...t)
Definition: shell.hpp:49
bool isError() const
Definition: try.hpp:78
std::string nodename
Definition: os.hpp:47
Definition: executor.hpp:48
void eraseenv(const std::string &key)
Definition: os.hpp:181
Definition: version.hpp:41
double fifteen
Definition: os.hpp:28
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:168
bool isNone() const
Definition: option.hpp:117
Try< std::string > var()
Definition: os.hpp:407
Try< std::list< std::string > > glob(const std::string &pattern)
Definition: os.hpp:239
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62
double five
Definition: os.hpp:27
std::string machine
Definition: os.hpp:50
Try< Nothing > setWindowSize(int fd, unsigned short rows, unsigned short columns)
Definition: os.hpp:455
tm * gmtime_r(const time_t *timep, tm *result)
Definition: os.hpp:431
double one
Definition: os.hpp:26
Try< std::string > ptsname(int master)
Definition: os.hpp:426