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);
177 char * value =
::getenv(key.c_str());
182 ::memset(value,
'\0', ::strlen(value));
202 int result =
execvp(file, argv);
221 const std::string& path,
237 remaining.tv_sec =
static_cast<long>(duration.
secs());
239 static_cast<long>((duration -
Seconds(remaining.tv_sec)).ns());
241 while (nanosleep(&remaining, &remaining) == -1) {
242 if (errno == EINTR) {
257 int status =
::glob(pattern.c_str(), GLOB_NOSORT,
nullptr, &g);
259 std::list<std::string> result;
262 if (status == GLOB_NOMATCH) {
269 for (
size_t i = 0; i < g.gl_pathc; ++i) {
270 result.push_back(g.gl_pathv[i]);
282 long cpus = sysconf(_SC_NPROCESSORS_ONLN);
298 if (getloadavg(loadArray, 3) == -1) {
299 return ErrnoError(
"Failed to determine system load averages");
303 load.
one = loadArray[0];
304 load.
five = loadArray[1];
316 if (::
uname(&name) < 0) {
337 }
else if (group.
isSome() && group.
get() < 0) {
338 return Error(
"Invalid group");
339 }
else if (session.
isSome() && session.
get() < 0) {
340 return Error(
"Invalid session");
350 if (group.
isSome() && group.
get() == 0) {
353 if (session.
isSome() && session.
get() == 0) {
357 std::set<pid_t> result;
364 result.insert(process.
pid);
367 result.insert(process.
pid);
370 result.insert(process.
pid);
382 os::shell(
"tar %s %s %s",
"-czf", archive.c_str(), path.c_str());
385 return Error(
"Failed to archive " + path +
": " + tarOut.
error());
400 int major, minor,
patch = 0;
414 if (::sscanf(info.
get().
release.c_str(),
"%d.%d-%*s", &major, &minor) != 2) {
418 return Version(major, minor, patch);
430 while (::
dup2(oldFd, newFd) == -1) {
431 if (errno == EINTR) {
446 static std::mutex* mutex =
new std::mutex;
448 synchronized (mutex) {
449 const char* slavePath =
::ptsname(master);
450 if (slavePath ==
nullptr) {
460 if (ioctl(fd, TIOCSCTTY,
nullptr) == -1) {
473 unsigned short columns)
475 struct winsize winsize;
476 winsize.ws_row = rows;
477 winsize.ws_col = columns;
479 if (ioctl(fd, TIOCSWINSZ, &winsize) != 0) {
491 return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
496 #endif // __STOUT_POSIX_OS_HPP__
Try< Nothing > dup2(int oldFd, int newFd)
Definition: os.hpp:428
Definition: nothing.hpp:16
Definition: errorbase.hpp:35
Try< std::string > patch(const std::string &s, const Diff &diff)
Definition: svn.hpp:157
Try< Nothing > chmod(const std::string &path, int mode)
Definition: os.hpp:210
Try< Nothing > sleep(const Duration &duration)
Definition: os.hpp:234
const pid_t group
Definition: process.hpp:55
Try< std::list< Process > > processes()
Definition: os.hpp:182
Try< Nothing > mknod(const std::string &path, mode_t mode, dev_t dev)
Definition: os.hpp:220
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:49
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
Definition: result.hpp:40
std::string host_default_path()
Definition: os.hpp:489
char *** environmentp()
Definition: environment.hpp:88
bool isSome() const
Definition: option.hpp:115
std::string release
Definition: os.hpp:48
int execvp(const char *file, char *const argv[])
Definition: shell.hpp:192
std::string version
Definition: os.hpp:49
Try< Load > loadavg()
Definition: os.hpp:295
DWORD pid_t
Definition: windows.hpp:187
Definition: process.hpp:32
int mode_t
Definition: windows.hpp:183
Try< dev_t > dev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:139
const pid_t pid
Definition: process.hpp:53
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
Try< Nothing > utime(const std::string &path)
Definition: utime.hpp:32
Try< long > cpus()
Definition: os.hpp:280
Definition: duration.hpp:259
Try< Nothing > tar(const std::string &path, const std::string &archive)
Definition: os.hpp:379
std::string hstrerror(int err)=delete
Try< Nothing > setctty(int fd)
Definition: os.hpp:458
const T & get() const &
Definition: option.hpp:118
Option< std::string > getenv(const std::string &key)
Definition: getenv.hpp:29
int random()
Definition: os.hpp:538
Try< Version > release()
Definition: os.hpp:393
double secs() const
Definition: duration.hpp:101
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:312
static Try error(const E &e)
Definition: try.hpp:42
Try< std::string > shell(const std::string &fmt, const T &...t)
Runs a shell command with optional arguments.
Definition: shell.hpp:71
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
bool isError() const
Definition: try.hpp:71
std::string nodename
Definition: os.hpp:47
void eraseenv(const std::string &key)
Definition: os.hpp:175
int execvpe(const std::string &command, const std::vector< std::string > &argv, const std::map< std::string, std::string > &envp)
Definition: shell.hpp:394
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:126
bool isNone() const
Definition: option.hpp:116
Try< std::string > var()
Definition: os.hpp:422
Try< std::list< std::string > > glob(const std::string &pattern)
Definition: os.hpp:254
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62
const T & get() const
Definition: try.hpp:73
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:470
tm * gmtime_r(const time_t *timep, tm *result)
Definition: os.hpp:389
double one
Definition: os.hpp:26
Try< std::string > ptsname(int master)
Definition: os.hpp:441
char ** environment()
Definition: environment.hpp:66