Skip to content

Commit

Permalink
fix: fix compile errors for Windows and WASM targets
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Dec 9, 2024
1 parent 8bd3b0d commit 15fbd0d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,7 @@ static json jsonrpc_machine_is_empty_handler(const json &j, const std::shared_pt
/// \param j JSON request object
/// \param session HTTP session
/// \returns JSON response object
static json jsonrpc_emancipate_handler(const json &j, const std::shared_ptr<http_session> &session) {
(void) session;
static json jsonrpc_emancipate_handler(const json &j, const std::shared_ptr<http_session> & /*session*/) {
jsonrpc_check_no_params(j);
setpgid(0, 0);
return jsonrpc_response_ok(j);
Expand Down
18 changes: 16 additions & 2 deletions src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
#include <tuple>
#include <utility>

#include "os-features.h"

#ifdef HAVE_FORK
#include <sys/time.h>
#include <unistd.h>
#endif

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Expand Down Expand Up @@ -363,6 +367,8 @@ jsonrpc_virtual_machine::jsonrpc_virtual_machine(std::string address) :
check_server_version();
}

#ifdef HAVE_FORK

static boost::asio::ip::tcp::endpoint address_to_endpoint(const std::string &address) {
try {
const auto pos = address.find_last_of(':');
Expand Down Expand Up @@ -426,9 +432,9 @@ jsonrpc_virtual_machine::jsonrpc_virtual_machine(const std::string &address, for
if (grand_child == 0) { // grand-child and double-fork() succeeded
sigprocmask(SIG_SETMASK, &omask, nullptr);
char sigusr1[256] = "";
(void) snprintf(sigusr1, std::size(sigusr1), "--sigusr1=%d", ppid);
std::ignore = snprintf(sigusr1, std::size(sigusr1), "--sigusr1=%d", ppid);
char server_fd[256] = "";
(void) snprintf(server_fd, std::size(server_fd), "--server-fd=%d", a.native_handle());
std::ignore = snprintf(server_fd, std::size(server_fd), "--server-fd=%d", a.native_handle());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
char *args[] = {const_cast<char *>(bin), server_fd, sigusr1, nullptr};
if (execvp(bin, args) < 0) {
Expand Down Expand Up @@ -496,6 +502,14 @@ jsonrpc_virtual_machine::jsonrpc_virtual_machine(const std::string &address, for
}
}

#else

jsonrpc_virtual_machine::jsonrpc_virtual_machine(const std::string & /*address*/, fork_result & /*spawned*/) {
throw std::runtime_error{"fork() is unsupported in this platform"s};
}

#endif

void jsonrpc_virtual_machine::do_load(const std::string &directory, const machine_runtime_config &runtime) {
bool result = false;
jsonrpc_request(m_ioc, m_stream, m_address, "machine.load", std::tie(directory, runtime), result, m_timeout);
Expand Down
4 changes: 4 additions & 0 deletions src/os-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@
#define HAVE_USLEEP
#endif

#if !defined(NO_FORK) && (defined(__linux__) || defined(__unix__)) && !defined(__wasi__)
#define HAVE_FORK
#endif

#endif
31 changes: 24 additions & 7 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "unique-c-ptr.h"

#include <sys/time.h>
#include <sys/wait.h>

#ifdef HAVE_SIGACTION
#include <csignal>
Expand Down Expand Up @@ -93,7 +92,11 @@
#else // not _WIN32

#if defined(HAVE_TTY) || defined(HAVE_MMAP) || defined(HAVE_TERMIOS) || defined(HAVE_USLEEP)
#include <unistd.h> // write/read/close
#include <unistd.h> // write/read/close/usleep/fork
#endif

#if defined(HAVE_FORK)
#include <sys/wait.h> // waitpid
#endif

#if defined(HAVE_SELECT)
Expand Down Expand Up @@ -183,7 +186,7 @@ static void os_SIGWINCH_handler(int /*sig*/) {
}
#endif

bool os_update_tty_size(tty_state *s) {
bool os_update_tty_size([[maybe_unused]] tty_state *s) {
#ifdef HAVE_TTY
#if defined(HAVE_TERMIOS) && defined(HAVE_IOCTL)
winsize ws{};
Expand Down Expand Up @@ -383,7 +386,7 @@ void os_prepare_tty_select([[maybe_unused]] select_fd_sets *fds) {
#endif
}

bool os_poll_selected_tty(int select_ret, select_fd_sets *fds) {
bool os_poll_selected_tty([[maybe_unused]] int select_ret, [[maybe_unused]] select_fd_sets *fds) {
auto *s = get_state();
if (!s->initialized) { // We can't poll when TTY is not initialized
return false;
Expand Down Expand Up @@ -522,7 +525,7 @@ void os_putchars(const uint8_t *data, size_t len) {
}
}

int os_mkdir(const char *path, int mode) {
int os_mkdir(const char *path, [[maybe_unused]] int mode) {
#ifdef HAVE_MKDIR
return plat_mkdir(path, mode);
#else
Expand Down Expand Up @@ -781,17 +784,20 @@ void os_sleep_us(uint64_t timeout_us) {
#endif
}

#ifdef HAVE_FORK
static void sig_alrm(int /*unused*/) {
;
}
#endif

// this function forks and intermediate child, and the intermediate child forks a final child
// the intermediate child simply exits immediately
// the final child sends its pid to the parent via a pipe
// the parent returns the final child pid
// the final child returns 0
// on error, the parent throws and the final child does not return
int os_double_fork_or_throw(bool emancipate) {
int os_double_fork_or_throw([[maybe_unused]] bool emancipate) {
#ifdef HAVE_FORK
int fd[2] = {-1, -1};
struct sigaction chld_act {};
bool restore_sigchld = false;
Expand Down Expand Up @@ -927,9 +933,15 @@ int os_double_fork_or_throw(bool emancipate) {
}
throw; // rethrow so caller can see why we failed
}

#else
throw std::runtime_error{"fork() is unsupported in this platform"s};

#endif
}

int os_double_fork(bool emancipate, const char **err_msg) {
int os_double_fork([[maybe_unused]] bool emancipate, [[maybe_unused]] const char **err_msg) {
#ifdef HAVE_FORK
static THREAD_LOCAL std::string error_storage;
try {
*err_msg = nullptr;
Expand All @@ -939,6 +951,11 @@ int os_double_fork(bool emancipate, const char **err_msg) {
*err_msg = error_storage.c_str();
return -1;
}

#else
*err_msg = "fork() is unsupported in this platform";

#endif
}

} // namespace cartesi
7 changes: 6 additions & 1 deletion src/virtio-net-carrier-slirp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ virtio_net_carrier_slirp::virtio_net_carrier_slirp(const cartesi::virtio_net_use
slirp_cfg.vhost.s_addr = htonl(SLIRP_DEFAULT_IPV4_VHOST); // Host address/gateway
slirp_cfg.vdhcp_start.s_addr = htonl(SLIRP_DEFAULT_IPV4_VDHCP_START); // DHCP start address
slirp_cfg.vnameserver.s_addr = htonl(SLIRP_DEFAULT_IPV4_VNAMESERVER); // DNS server address
slirp_cfg.disable_dhcp = true;
// ??(edubart): Should all the above settings be configurable by the user?
// ??(edubart): Should we add support for IPv6? It is disabled by default.
// Configure required slirp callbacks
Expand All @@ -195,8 +194,11 @@ virtio_net_carrier_slirp::virtio_net_carrier_slirp(const cartesi::virtio_net_use
slirp_cbs.timer_new = slirp_timer_new;
slirp_cbs.timer_free = slirp_timer_free;
slirp_cbs.timer_mod = slirp_timer_mod;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
slirp_cbs.register_poll_fd = slirp_register_poll_fd;
slirp_cbs.unregister_poll_fd = slirp_unregister_poll_fd;
#pragma GCC diagnostic pop
slirp_cbs.notify = slirp_notify;

// Initialize slirp
Expand Down Expand Up @@ -292,7 +294,10 @@ void virtio_net_carrier_slirp::do_prepare_select(select_fd_sets *fds, uint64_t *
slirp_select_fds slirp_fds{&fds->maxfd, readfds, writefds, exceptfds};
const uint32_t initial_timeout_ms = *timeout_us / 1000;
uint32_t timeout_ms = initial_timeout_ms;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
slirp_pollfds_fill(slirp, &timeout_ms, slirp_add_poll_cb, &slirp_fds);
#pragma GCC diagnostic pop
if (initial_timeout_ms != timeout_ms) {
*timeout_us = static_cast<uint64_t>(timeout_ms) * 1000;
}
Expand Down

0 comments on commit 15fbd0d

Please sign in to comment.