Skip to content

Commit

Permalink
fix: fix compiler errors on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 18, 2024
1 parent 16c19be commit cb7c212
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
33 changes: 33 additions & 0 deletions src/fork-result.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#ifndef FORK_RESULT_H
#define FORK_RESULT_H

#include <cstdint>
#include <string>

namespace cartesi {

/// Result of a fork
struct fork_result final {
std::string address;
uint32_t pid{};
};

} // namespace cartesi

#endif
2 changes: 1 addition & 1 deletion src/json-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "access-log.h"
#include "base64.h"
#include "bracket-note.h"
#include "fork-result.h"
#include "interpret.h"
#include "json-util.h"
#include "json.hpp"
#include "jsonrpc-connection.h"
#include "machine-config.h"
#include "machine-memory-range-descr.h"
#include "machine-merkle-tree.h"
Expand Down
2 changes: 1 addition & 1 deletion src/json-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

#include "access-log.h"
#include "bracket-note.h"
#include "fork-result.h"
#include "interpret.h"
#include "jsonrpc-connection.h"
#include "machine-config.h"
#include "machine-memory-range-descr.h"
#include "machine-merkle-tree.h"
Expand Down
7 changes: 1 addition & 6 deletions src/jsonrpc-connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@
#include <boost/container/static_vector.hpp>
#pragma GCC diagnostic pop

#include "fork-result.h"
#include "semantic-version.h"

namespace cartesi {

/// Result of a fork
struct fork_result final {
std::string address;
uint32_t pid{};
};

class jsonrpc_connection final {
public:
jsonrpc_connection(std::string remote_address, bool detach_server);
Expand Down
7 changes: 7 additions & 0 deletions src/jsonrpc-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ cm_error cm_jsonrpc_connect(const char *address, int detach_server, cm_jsonrpc_c
return cm_result_failure();
}

#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 All @@ -108,6 +109,7 @@ static std::string endpoint_to_string(const boost::asio::ip::tcp::endpoint &endp
ss << endpoint;
return ss.str();
}
#endif

cm_error cm_jsonrpc_spawn_server(const char *address, int detach_server, cm_jsonrpc_connection **con,
const char **bound_address, int32_t *pid) try {
Expand All @@ -131,6 +133,7 @@ cm_error cm_jsonrpc_spawn_server(const char *address, int detach_server, cm_json
if (pid == nullptr) {
throw std::invalid_argument("invalid pid output");
}
#ifdef HAVE_FORK
sigset_t mask{};
sigset_t omask{};
sigemptyset(&mask); // always returns 0
Expand Down Expand Up @@ -239,6 +242,10 @@ cm_error cm_jsonrpc_spawn_server(const char *address, int detach_server, cm_json
}
}
return cm_result_success(); // code never reaches here
#else
throw std::runtime_error{"spawn is unsupported in this platform"};

#endif
} catch (...) {
*con = nullptr;
*bound_address = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#include "access-log.h"
#include "base64.h"
#include "fork-result.h"
#include "interpret.h"
#include "json-util.h"
#include "jsonrpc-connection.h"
Expand Down
1 change: 1 addition & 0 deletions src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "access-log.h"
#include "base64.h"
#include "fork-result.h"
#include "interpret.h"
#include "json-util.h"
#include "json.hpp"
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__))
#define HAVE_FORK
#endif

#endif
30 changes: 23 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 @@ -92,14 +91,18 @@

#else // not _WIN32

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

#if defined(HAVE_SELECT)
#include <sys/select.h> // select
#endif

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

#define plat_write write
#define plat_mkdir mkdir

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([[maybe_unused]] 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(int newpgid) {
int os_double_fork_or_throw([[maybe_unused]] int newpgid) {
#ifdef HAVE_FORK
int fd[2] = {-1, -1};
struct sigaction chld_act {};
bool restore_sigchld = false;
Expand Down Expand Up @@ -926,9 +932,15 @@ int os_double_fork_or_throw(int newpgid) {
}
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(int newpgid, const char **err_msg) {
int os_double_fork([[maybe_unused]] int newpgid, [[maybe_unused]] const char **err_msg) {
#ifdef HAVE_FORK
static THREAD_LOCAL std::string error_storage;
try {
*err_msg = nullptr;
Expand All @@ -938,6 +950,10 @@ int os_double_fork(int newpgid, const char **err_msg) {
*err_msg = error_storage.c_str();
return -1;
}
#else
throw std::runtime_error{"fork() is unsupported in this platform"s};

#endif
}

} // namespace cartesi

0 comments on commit cb7c212

Please sign in to comment.