95 lines
3.9 KiB
C++
95 lines
3.9 KiB
C++
// Host stand-in for <montauk/syscall.h>, for running smbfs and the SMB
|
|
// library's MontaukOS transport inside tests/smb/mount_harness.
|
|
#pragma once
|
|
#include <Api/Syscall.hpp>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#include <cerrno>
|
|
#include <cstdio>
|
|
#include <cstdint>
|
|
|
|
extern "C" {
|
|
int h_userfs_mount(const void* params);
|
|
int h_userfs_recv(int drive, void* req, void* data, uint32_t dataMax, uint64_t timeoutMs);
|
|
int h_userfs_reply(int drive, uint64_t id, int64_t result, const void* data, uint32_t len);
|
|
int h_userfs_unmount(int drive);
|
|
int h_userfs_info(int drive, void* out);
|
|
void h_print(const char* text);
|
|
[[noreturn]] void h_exit(int code);
|
|
void h_getargs(char* buf, uint64_t max);
|
|
uint64_t h_milliseconds();
|
|
}
|
|
|
|
namespace montauk {
|
|
inline int userfs_mount(const abi::UserFsMountParams* p) { return h_userfs_mount(p); }
|
|
inline int userfs_recv(int d, abi::UserFsRequest* r, void* data, uint32_t max, uint64_t t) {
|
|
return h_userfs_recv(d, r, data, max, t);
|
|
}
|
|
inline int userfs_reply(int d, uint64_t id, int64_t res, const void* data = nullptr, uint32_t len = 0) {
|
|
return h_userfs_reply(d, id, res, data, len);
|
|
}
|
|
inline int userfs_unmount(int d) { return h_userfs_unmount(d); }
|
|
inline int userfs_info(int d, abi::UserFsInfo* out) { return h_userfs_info(d, out); }
|
|
|
|
inline void print(const char* s) { h_print(s); }
|
|
inline int64_t write_log(const char* s) { fprintf(stderr, " %s\n", s); return 0; }
|
|
inline bool terminal_attached() { return false; }
|
|
[[noreturn]] inline void exit(int code = 0) { h_exit(code); }
|
|
inline int getargs(char* buf, uint64_t max) { h_getargs(buf, max); return 0; }
|
|
inline uint64_t get_milliseconds() { return h_milliseconds(); }
|
|
inline void sleep_ms(uint64_t ms) { usleep((useconds_t)ms * 1000); }
|
|
inline int64_t getrandom(void* buf, uint32_t len) {
|
|
FILE* f = fopen("/dev/urandom", "rb");
|
|
size_t n = f ? fread(buf, 1, len, f) : 0;
|
|
if (f) fclose(f);
|
|
return (int64_t)n;
|
|
}
|
|
|
|
// Sockets, with the MontaukOS conventions: recv returns 0 when nothing is
|
|
// waiting and a negative value once the peer has closed.
|
|
inline uint32_t resolve(const char* host) {
|
|
addrinfo hints{};
|
|
hints.ai_family = AF_INET;
|
|
addrinfo* res = nullptr;
|
|
if (getaddrinfo(host, nullptr, &hints, &res) != 0 || !res) return 0;
|
|
uint32_t ip = ((sockaddr_in*)res->ai_addr)->sin_addr.s_addr;
|
|
freeaddrinfo(res);
|
|
return ip;
|
|
}
|
|
inline int socket(int) { return ::socket(AF_INET, SOCK_STREAM, 0); }
|
|
inline int connect(int fd, uint32_t ip, uint16_t port) {
|
|
sockaddr_in a{};
|
|
a.sin_family = AF_INET;
|
|
a.sin_port = htons(port);
|
|
a.sin_addr.s_addr = ip;
|
|
return ::connect(fd, (sockaddr*)&a, sizeof(a));
|
|
}
|
|
inline int closesocket(int fd) { return ::close(fd); }
|
|
inline int send(int fd, const void* data, uint32_t len) {
|
|
ssize_t r = ::send(fd, data, len, MSG_NOSIGNAL);
|
|
return r < 0 ? -1 : (int)r;
|
|
}
|
|
inline int recv(int fd, void* buf, uint32_t len) {
|
|
ssize_t r = ::recv(fd, buf, len, MSG_DONTWAIT);
|
|
if (r > 0) return (int)r;
|
|
if (r == 0) return -1;
|
|
return (errno == EAGAIN || errno == EWOULDBLOCK) ? 0 : -1;
|
|
}
|
|
inline uint32_t wait_handle(int fd, uint32_t wanted, uint64_t timeoutMs = ~0ULL) {
|
|
pollfd p{fd, 0, 0};
|
|
if (wanted & abi::IPC_SIGNAL_READABLE) p.events |= POLLIN;
|
|
if (wanted & abi::IPC_SIGNAL_WRITABLE) p.events |= POLLOUT;
|
|
int ms = timeoutMs > 60000 ? 60000 : (int)timeoutMs;
|
|
if (::poll(&p, 1, ms) < 0) return (uint32_t)-1;
|
|
uint32_t out = 0;
|
|
if (p.revents & POLLIN) out |= abi::IPC_SIGNAL_READABLE;
|
|
if (p.revents & POLLOUT) out |= abi::IPC_SIGNAL_WRITABLE;
|
|
if (p.revents & (POLLHUP | POLLERR)) out |= abi::IPC_SIGNAL_PEER_CLOSED;
|
|
return out;
|
|
}
|
|
}
|