5#ifndef LINE_UTIL_PLATFORM_H
6#define LINE_UTIL_PLATFORM_H
45#include <netinet/in.h>
47#include <sys/socket.h>
49#include <sys/statvfs.h>
52#include <sys/utsname.h>
86bool stat_file(
const std::string& p, FileStat& out);
99bool rename_replace(
const std::string& from,
const std::string& to);
133 const char* base = std::getenv(
"TMPDIR");
134 return (base && *base) ? std::string(base) : std::string(
"/tmp");
139 const char* h = std::getenv(
"HOME");
140 return (h !=
nullptr && *h) ? std::string(h) : std::string();
145 return !p.empty() && ::stat(p.c_str(), &st) == 0 && S_ISREG(st.st_mode);
150 return !p.empty() && ::stat(p.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
156 return !p.empty() && ::stat(p.c_str(), &st) == 0 && ::access(p.c_str(), X_OK) == 0;
160 return !p.empty() && ::access(p.c_str(), W_OK) == 0;
165 if (p.empty() || ::stat(p.c_str(), &st) != 0)
return false;
166 out.
size =
static_cast<long long>(st.st_size);
167 out.
mtime =
static_cast<long long>(st.st_mtime);
168 out.
is_dir = S_ISDIR(st.st_mode);
169 out.
is_reg = S_ISREG(st.st_mode);
175 const char* raw = std::getenv(
"PATH");
176 if (raw ==
nullptr)
return std::string();
177 const std::string path(raw);
179 while (b <= path.size()) {
180 const std::size_t e = path.find(
':', b);
181 const std::string dir = path.substr(b, e == std::string::npos ? std::string::npos : e - b);
182 const std::string f = (dir.empty() ? std::string(
".") : dir) +
"/" + name;
184 if (e == std::string::npos)
break;
187 return std::string();
193 const ssize_t n = ::readlink(
"/proc/self/exe", buf,
sizeof(buf) - 1);
194 if (n <= 0)
return std::string();
195 return std::string(buf,
static_cast<std::size_t
>(n));
200 return ::getcwd(buf,
sizeof(buf)) !=
nullptr ? std::string(buf) : std::string();
206 return ::uname(&u) == 0 ? std::string(u.sysname) : std::string();
212 return ::uname(&u) == 0 ? std::string(u.machine) : std::string();
217 return ::mkdir(path.c_str(), 0700) == 0 || errno == EEXIST;
222 std::vector<char> buf(tmpl.begin(), tmpl.end());
224 if (::mkdtemp(&buf[0]) ==
nullptr)
return std::string();
225 return std::string(&buf[0]);
230 std::vector<char> buf(tmpl.begin(), tmpl.end());
232 const int fd = ::mkstemp(&buf[0]);
233 if (fd >= 0) tmpl.assign(&buf[0]);
237inline int dup_fd(
int fd) { return ::dup(fd); }
238inline int dup2_fd(
int from,
int to) { return ::dup2(from, to); }
240inline bool remove_file(
const std::string& p) { return ::unlink(p.c_str()) == 0; }
242 return ::rename(from.c_str(), to.c_str()) == 0;
247 DIR* d = ::opendir(dir.c_str());
250 while ((e = ::readdir(d)) !=
nullptr) {
251 const std::string n(e->d_name);
252 if (n ==
"." || n ==
"..")
continue;
253 const std::string child = dir +
"/" + n;
255 if (::lstat(child.c_str(), &st) == 0 && S_ISDIR(st.st_mode))
258 ::unlink(child.c_str());
262 ::rmdir(dir.c_str());
265inline long process_id() {
return static_cast<long>(::getpid()); }
270 if (::statvfs(path.c_str(), &vfs) != 0)
return -1;
271 const long long usable =
272 static_cast<long long>(vfs.f_bavail) *
static_cast<long long>(vfs.f_frsize);
273 return usable > 0 ? usable : -1;
278#if defined(_SC_AVPHYS_PAGES) && defined(_SC_PAGE_SIZE)
279 const long pages = ::sysconf(_SC_AVPHYS_PAGES);
280 const long page_size = ::sysconf(_SC_PAGE_SIZE);
281 if (pages > 0 && page_size > 0)
282 return static_cast<double>(pages) *
static_cast<double>(page_size);
290 clock_gettime(CLOCK_MONOTONIC, &ts);
291 return static_cast<long long>(ts.tv_sec) * 1000LL + ts.tv_nsec / 1000000L;
296 ts.tv_sec = millis / 1000;
297 ts.tv_nsec = (millis % 1000) * 1000000L;
298 ::nanosleep(&ts,
nullptr);
303 out = std::to_string(
static_cast<unsigned long>(::getuid())) +
":" +
304 std::to_string(
static_cast<unsigned long>(::getgid()));
310 if (argv.empty())
return false;
311 const pid_t pid = ::fork();
312 if (pid < 0)
return false;
315 if (::fork() != 0) ::_exit(0);
316 const int devnull = ::open(
"/dev/null", O_RDWR);
318 ::dup2(devnull, STDIN_FILENO);
319 ::dup2(devnull, STDOUT_FILENO);
320 ::dup2(devnull, STDERR_FILENO);
321 if (devnull > STDERR_FILENO) ::close(devnull);
323 std::vector<char*> raw;
324 for (std::size_t i = 0; i < argv.size(); ++i)
325 raw.push_back(
const_cast<char*
>(argv[i].c_str()));
326 raw.push_back(
nullptr);
327 ::execvp(raw[0], &raw[0]);
331 ::waitpid(pid, &status, 0);
332 return WIFEXITED(status) && WEXITSTATUS(status) == 0;
343 struct addrinfo hints;
344 std::memset(&hints, 0,
sizeof(hints));
345 hints.ai_family = AF_UNSPEC;
346 hints.ai_socktype = SOCK_STREAM;
347 struct addrinfo* res =
nullptr;
348 const std::string portStr = std::to_string(port);
349 if (::getaddrinfo(host.c_str(), portStr.c_str(), &hints, &res) != 0 || res ==
nullptr) {
354 lastError =
"connection refused";
355 for (
struct addrinfo* ai = res; ai !=
nullptr; ai = ai->ai_next) {
356 fd = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
357 if (fd < 0)
continue;
358 const int flags = ::fcntl(fd, F_GETFL, 0);
359 ::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
360 int rc = ::connect(fd, ai->ai_addr, ai->ai_addrlen);
361 if (rc != 0 && errno == EINPROGRESS) {
364 pfd.events = POLLOUT;
366 const int pr = ::poll(&pfd, 1, connectMillis > 0 ? connectMillis : -1);
369 socklen_t len =
sizeof(soerr);
370 if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &soerr, &len) == 0 && soerr == 0)
373 lastError = std::strerror(soerr);
374 }
else if (pr == 0) {
375 lastError =
"connect timed out";
378 lastError = std::strerror(errno);
381 }
else if (rc != 0) {
382 lastError = std::strerror(errno);
385 ::fcntl(fd, F_SETFL, flags);
393 return fd < 0 ? kInvalidSocket : static_cast<socket_t>(fd);
398 if (millis <= 0)
return;
400 tv.tv_sec = millis / 1000;
401 tv.tv_usec = (millis % 1000) * 1000;
402 const int fd =
static_cast<int>(s);
403 ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
404 ::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv));
415 struct addrinfo hints;
416 std::memset(&hints, 0,
sizeof(hints));
417 hints.ai_family = AF_UNSPEC;
418 hints.ai_socktype = SOCK_STREAM;
419 struct addrinfo* res =
nullptr;
420 if (::getaddrinfo(host.c_str(), port.c_str(), &hints, &res) != 0 || res ==
nullptr) {
425 for (
struct addrinfo* ai = res; ai !=
nullptr; ai = ai->ai_next) {
426 fd = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
427 if (fd < 0)
continue;
429 tv.tv_sec =
static_cast<long>(ioSeconds);
430 tv.tv_usec =
static_cast<long>((ioSeconds -
static_cast<double>(tv.tv_sec)) * 1e6);
431 ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
432 ::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv));
433 if (::connect(fd, ai->ai_addr, ai->ai_addrlen) == 0)
break;
439 return fd < 0 ? kInvalidSocket : static_cast<socket_t>(fd);
447 const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
453 ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes));
454 struct sockaddr_in addr;
455 std::memset(&addr, 0,
sizeof(addr));
456 addr.sin_family = AF_INET;
457 addr.sin_addr.s_addr = htonl(INADDR_ANY);
458 addr.sin_port = htons(
static_cast<uint16_t
>(port));
459 if (::bind(fd,
reinterpret_cast<struct sockaddr*
>(&addr),
sizeof(addr)) != 0) {
464 if (::listen(fd, backlog) != 0) {
473 const int c = ::accept(
static_cast<int>(listener),
nullptr,
nullptr);
474 return c < 0 ? kInvalidSocket : static_cast<socket_t>(c);
482 const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
487 struct sockaddr_in addr;
488 std::memset(&addr, 0,
sizeof(addr));
489 addr.sin_family = AF_INET;
490 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
492 if (::bind(fd,
reinterpret_cast<struct sockaddr*
>(&addr),
sizeof(addr)) != 0) {
497 socklen_t len =
sizeof(addr);
498 if (::getsockname(fd,
reinterpret_cast<struct sockaddr*
>(&addr), &len) != 0) {
500 stage =
"getsockname";
503 const int port = ntohs(addr.sin_port);
511 const int flags = MSG_NOSIGNAL;
515 return static_cast<long>(::send(
static_cast<int>(s), p, n, flags));
519 return static_cast<long>(::recv(
static_cast<int>(s), p, n, 0));
Conservation laws of a layered queueing network, enumerated from its structure.