Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.d
*.o
/strfry
/strfry.exe
/strfry-db/*.mdb
.vscode/*
/strfry-db-test*
Expand Down
16 changes: 11 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ export XLDFLAGS += $(BREW_LIBS)
endif
INCS += -Iexternal/negentropy/cpp

build/StrfryTemplates.h: $(shell find src/tmpls/ -type f -name '*.tmpl')
PERL5LIB=golpe/vendor/ perl golpe/external/templar/templar.pl src/tmpls/ strfrytmpl $@
build/StrfryTemplates.h: $(wildcard src/tmpls/*.tmpl)
perl -Igolpe/vendor golpe/external/templar/templar.pl src/tmpls/ strfrytmpl $@

src/apps/relay/RelayWebsocket.o: build/StrfryTemplates.h

ifeq ($(OS),Windows_NT)
TEST_BIN = build/subid_tests.exe
else
TEST_BIN = build/subid_tests
endif

.PHONY: test-subid
test-subid: build/subid_tests
build/subid_tests
test-subid: $(TEST_BIN)
$(TEST_BIN)

build/subid_tests: test/tests/SubIdTests.cpp build/golpe.h
$(TEST_BIN): test/SubIdTests.cpp build/golpe.h
$(CXX) $(CXXFLAGS) $(INCS) $< -o $@
30 changes: 30 additions & 0 deletions src/PluginEventSifter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@

#include <string.h>
#include <errno.h>
#ifndef _WIN32
#include <spawn.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#endif

#include <memory>

#ifndef _WIN32
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
#define st_mtim st_mtimespec
#endif
#endif

#ifndef _WIN32
#include "hoytech/stream.h"
#endif

#include "golpe.h"

#include "events.h"

#ifndef _WIN32
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
extern char **environ;
#elif defined(__APPLE__)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#endif
#endif



Expand All @@ -38,6 +46,26 @@ enum class PluginEventSifterResult {
};


#ifdef _WIN32

struct PluginEventSifter {
PluginEventSifterResult acceptEvent(const std::string &pluginCmd, const tao::json::value &evJson, EventSourceType sourceType, std::string_view sourceInfo, const Bytes32 &authed, std::string &okMsg) {
if (pluginCmd.size() == 0) {
return PluginEventSifterResult::Accept;
}

static bool warned = false;
if (!warned) {
LW << "Write policy plugins are not supported on Windows. All events are accepted by default.";
warned = true;
}

return PluginEventSifterResult::Accept;
}
};

#else

struct PluginEventSifter {
struct RunningPlugin {
pid_t pid;
Expand Down Expand Up @@ -198,3 +226,5 @@ struct PluginEventSifter {
running = make_unique<RunningPlugin>(pid, inPipe.extractFd(0), outPipe.extractFd(1), pluginCmd);
}
};

#endif
39 changes: 39 additions & 0 deletions src/apps/dbutils/cmd_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@

#include "WriterPipeline.h"

#ifdef _WIN32
#include <io.h>
inline ssize_t getline(char **lineptr, size_t *n, FILE *stream) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getline function is duplicated across import and upload. It should be shared somewhere.

if (lineptr == NULL || n == NULL || stream == NULL) {
errno = EINVAL;
return -1;
}
if (*lineptr == NULL || *n == 0) {
*n = 128;
*lineptr = (char *)malloc(*n);
if (*lineptr == NULL) {
return -1;
}
}
size_t count = 0;
int c;
while ((c = fgetc(stream)) != EOF) {
if (count + 1 >= *n) {
size_t new_len = *n * 2;
char *new_ptr = (char *)realloc(*lineptr, new_len);
if (new_ptr == NULL) {
return -1;
}
*lineptr = new_ptr;
*n = new_len;
}
(*lineptr)[count++] = c;
if (c == '\n') {
break;
}
}
if (count == 0 && c == EOF) {
return -1;
}
(*lineptr)[count] = '\0';
return count;
}
#endif


static const char USAGE[] =
R"(
Expand Down
39 changes: 39 additions & 0 deletions src/apps/mesh/cmd_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@

#include "golpe.h"

#ifdef _WIN32
#include <io.h>
inline ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
if (lineptr == NULL || n == NULL || stream == NULL) {
errno = EINVAL;
return -1;
}
if (*lineptr == NULL || *n == 0) {
*n = 128;
*lineptr = (char *)malloc(*n);
if (*lineptr == NULL) {
return -1;
}
}
size_t count = 0;
int c;
while ((c = fgetc(stream)) != EOF) {
if (count + 1 >= *n) {
size_t new_len = *n * 2;
char *new_ptr = (char *)realloc(*lineptr, new_len);
if (new_ptr == NULL) {
return -1;
}
*lineptr = new_ptr;
*n = new_len;
}
(*lineptr)[count++] = c;
if (c == '\n') {
break;
}
}
if (count == 0 && c == EOF) {
return -1;
}
(*lineptr)[count] = '\0';
return count;
}
#endif

#include "WSConnection.h"


Expand Down
8 changes: 8 additions & 0 deletions src/apps/relay/RelaySignalHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef _WIN32
#include <signal.h>
#endif

#include "RelayServer.h"


void RelayServer::runSignalHandler() {
setThreadName("signalHandler");

#ifndef _WIN32
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
Expand All @@ -22,4 +25,9 @@ void RelayServer::runSignalHandler() {
LW << "Got unexpected signal: " << sig;
}
}
#else
while (1) {
std::this_thread::sleep_for(std::chrono::seconds(1000));
}
#endif
}
4 changes: 4 additions & 0 deletions src/apps/relay/cmd_relay.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef _WIN32
#include <pthread.h>
#include <signal.h>
#endif

#include <docopt.h>

Expand Down Expand Up @@ -40,13 +42,15 @@ void cmd_relay(const std::vector<std::string> &subArgs) {
}

void RelayServer::run() {
#ifndef _WIN32
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
int s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0) throw herr("Unable to set sigmask: ", strerror(errno));
}
#endif

tpWebsocket.init("Websocket", 1, [this](auto &thr){
runWebsocket(thr);
Expand Down
8 changes: 7 additions & 1 deletion src/misc.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#ifdef __FreeBSD__
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <signal.h>
#endif
#include <stdio.h>

#include <algorithm>
#include <string>
Expand Down Expand Up @@ -122,10 +126,12 @@ uint64_t getDBVersion(lmdb::txn &txn) {


void exitOnSigPipe() {
#ifndef _WIN32
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_sigaction = [](int, siginfo_t*, void*){ ::exit(1); };
if (sigaction(SIGPIPE, &act, nullptr)) throw herr("couldn't run sigaction(): ", strerror(errno));
#endif
}


Expand Down
20 changes: 19 additions & 1 deletion src/onAppStartup.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#ifdef _WIN32
#include <ws2tcpip.h>
#include <io.h>
#define isatty _isatty
#define fileno _fileno
#else
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <iostream>

Expand All @@ -16,13 +23,22 @@


void onPreStartup(int argc, char **argv) {
#ifdef _WIN32
WSADATA wsaData;
int wsaResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (wsaResult != 0) {
std::cerr << "WSAStartup failed: " << wsaResult << std::endl;
::exit(1);
}
#else
if (getuid() == 0) {
if (isatty(fileno(stderr))) {
std::cerr << "\x1b[1;35mWARNING: Running as root is not recommended\x1b[0m" << std::endl;
} else {
std::cerr << "WARNING: Running as root is not recommended" << std::endl;
}
}
#endif

if (argc > 1) return;

Expand Down Expand Up @@ -78,6 +94,7 @@ static void dbCheck(lmdb::txn &txn, const std::string &cmd) {
}

static void setRLimits() {
#ifndef _WIN32
if (!cfg().relay__nofiles) return;
struct rlimit curr;

Expand All @@ -101,6 +118,7 @@ static void setRLimits() {
#endif

if (setrlimit(RLIMIT_NOFILE, &curr)) throw herr("Failed setting NOFILES limit to ", cfg().relay__nofiles, ": ", strerror(errno));
#endif
}


Expand Down
2 changes: 1 addition & 1 deletion strfry.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dbParams {
maxreaders = 256

# Size of mmap() to use when loading LMDB (default is 10TB, does *not* correspond to disk-space used) (restart required)
mapsize = 10995116277760
mapsize = 10737418240

# Disables read-ahead when accessing the LMDB mapping. Reduces IO activity when DB size is larger than RAM. (restart required)
noReadAhead = false
Expand Down
Loading