Skip to content

Commit

Permalink
more wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Sep 12, 2024
1 parent a1cd578 commit 8f516c5
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 66 deletions.
46 changes: 0 additions & 46 deletions patches/pg_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,52 +1162,6 @@ extern void AsyncPostgresSingleUserMain(int single_argc, char *single_argv[], co
#include "../postgresql/src/interfaces/libpq/pqexpbuffer.c"
#define fsync_pgdata(...)




#include <stdio.h> // FILE+fprintf
extern FILE* IDB_PIPE_FP;
extern FILE* SOCKET_FILE;
extern int SOCKET_DATA;
extern int IDB_STAGE;




static int
ends_with(const char *str, const char *suffix)
{
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}

FILE *pg_popen(const char *command, const char *type) {
if ( ends_with(command,"-V") || (IDB_STAGE>1)) {
fprintf(stderr,"# wasi-popen[%s] STUB\n", command);
return stderr;
}

if (!IDB_STAGE) {
fprintf(stderr,"# wasi-popen[%s] (BOOT)\n", command);
IDB_PIPE_FP = fopen( IDB_PIPE_BOOT, "w");
IDB_STAGE = 1;
} else {
fprintf(stderr,"# wasi-popen[%s] (SINGLE)\n", command);
IDB_PIPE_FP = fopen( IDB_PIPE_SINGLE, "w");
IDB_STAGE = 2;
}

return IDB_PIPE_FP;

}
//void __SIG_IGN(int) {}
//void __SIG_ERR(int) {}

#include "../postgresql/src/bin/initdb/initdb.c"


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- postgresql/src/include/port/emscripten.h
+++ postgresql-wasm/src/include/port/emscripten.h
@@ -0,0 +1,52 @@
@@ -0,0 +1,50 @@
+/* src/include/port/emscripten.h */
+
+#ifndef I_EMSCRIPTEN
Expand All @@ -12,8 +12,6 @@
+
+#include "/tmp/pglite/include/wasm_common.h"
+
+#include <stdbool.h>
+extern PGDLLIMPORT bool fe_utils_quote_all_identifiers;
+
+#define BOOT_END_MARK "build indices"
+#define FD_BUFFER_MAX 16384
Expand Down
27 changes: 27 additions & 0 deletions patches/postgresql-wasm-16.4/src-bin-pg_dump-parallel.c.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--- postgresql/src/bin/pg_dump/parallel.c
+++ postgresql-wasm/src/bin/pg_dump/parallel.c
@@ -49,7 +49,7 @@
* The pstate->te[] entry for each worker is valid when it's in WRKR_WORKING
* state, and must be NULL in other states.
*/
-
+#define PG_DUMP_PARALLEL
#include "postgres_fe.h"

#ifndef WIN32
@@ -445,6 +445,7 @@
static void
WaitForTerminatingWorkers(ParallelState *pstate)
{
+#if !defined(__wasi__)
while (!HasEveryWorkerTerminated(pstate))
{
ParallelSlot *slot = NULL;
@@ -504,6 +505,7 @@
slot->workerStatus = WRKR_TERMINATED;
pstate->te[j] = NULL;
}
+#endif /* __wasi__ */
}


18 changes: 18 additions & 0 deletions patches/postgresql-wasm-16.4/src-bin-pg_upgrade-parallel.c.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--- postgresql/src/bin/pg_upgrade/parallel.c
+++ postgresql-wasm/src/bin/pg_upgrade/parallel.c
@@ -277,6 +277,7 @@
bool
reap_child(bool wait_for_child)
{
+#if !defined(__wasi__)
#ifndef WIN32
int work_status;
pid_t child;
@@ -336,6 +337,6 @@

/* do this after job has been removed */
parallel_jobs--;
-
+#endif /* __wasi__ */
return true;
}
21 changes: 21 additions & 0 deletions patches/postgresql-wasm-16.4/src-bin-psql-command.c.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- postgresql/src/bin/psql/command.c
+++ postgresql-wasm/src/bin/psql/command.c
@@ -5146,6 +5146,10 @@
static bool
do_watch(PQExpBuffer query_buf, double sleep, int iter)
{
+#if defined(__wasi__)
+ pg_log_error("#5150 wasi: could not set timer");
+ return false;
+#else
long sleep_ms = (long) (sleep * 1000);
printQueryOpt myopt = pset.popt;
const char *strftime_fmt;
@@ -5384,6 +5388,7 @@

pg_free(title);
return (res >= 0);
+#endif /* __wasi__ */
}

/*
10 changes: 10 additions & 0 deletions patches/postgresql-wasm-16.4/src-fe_utils-print.c.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- postgresql/src/fe_utils/print.c
+++ postgresql-wasm/src/fe_utils/print.c
@@ -15,6 +15,7 @@
*
*-------------------------------------------------------------------------
*/
+#define FE_UTILS_PRINT
#include "postgres_fe.h"

#include <limits.h>
24 changes: 17 additions & 7 deletions patches/postgresql-wasm-16.4/src-port-pthread_barrier_wait.c.diff
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
int
pthread_barrier_init(pthread_barrier_t *barrier, const void *attr, int count)
{
@@ -33,7 +33,9 @@

@@ -75,3 +75,20 @@
pthread_mutex_destroy(&barrier->mutex);
return 0;
}
-
+#else
+// int pthread_barrier_init(pthread_barrier_t *__restrict, const pthread_barrierattr_t *__restrict, unsigned);
+int
+pthread_barrier_init(pthread_barrier_t *__restrict barrier, const pthread_barrierattr_t *__restrict attr, unsigned count) {
+ return 0;
+}
+
+int
+pthread_barrier_wait(pthread_barrier_t *barrier) {
+ return 0;
+}
+
+int
+pthread_barrier_destroy(pthread_barrier_t *barrier) {
+ return 0;
+}
+#endif
int
pthread_barrier_wait(pthread_barrier_t *barrier)
{
+
22 changes: 13 additions & 9 deletions patches/postgresql-wasm/src-include-port-wasi.h.diff
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
--- postgresql/src/include/port/wasi.h
+++ postgresql-wasm/src/include/port/wasi.h
@@ -0,0 +1,267 @@
@@ -0,0 +1,271 @@
+#ifndef I_WASI
+#define I_WASI
+
+/*
+#if defined(HAVE_SETSID)
+#undef HAVE_SETSID
+#endif
+
+#if defined(HAVE_GETRLIMIT)
+#undef HAVE_GETRLIMIT
+#endif
+
+
+*/
+
+#define PLATFORM_DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC
+
Expand Down Expand Up @@ -158,11 +157,6 @@
+ return -1;
+}
+
+static int
+system_wasi(const char *command) {
+ fprintf(stderr, "# 164: system('%s')\n", command);
+ return -1;
+}
+
+static const char *gai_strerror_msg = "# 168: gai_strerror_msg";
+static const char *
Expand Down Expand Up @@ -252,6 +246,8 @@
+
+
+
+#define system(command) system_wasi(command)
+extern int system_wasi(const char *command);
+
+
+
Expand All @@ -263,7 +259,15 @@
+}
+
+
+// HACK TODO FIXME !
+
+// #define getpwuid(...) (NULL)
+
+#if defined(PG_INITDB) || defined(FE_UTILS_PRINT) || defined(PG_DUMP_PARALLEL)
+static void
+__SIG_IGN(int param) {
+}
+#endif
+
+
+#endif // I_WASI
Expand Down
5 changes: 4 additions & 1 deletion patches/postgresql-wasm/src-include-port-wasm_common.h.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- postgresql/src/include/port/wasm_common.h
+++ postgresql-wasm/src/include/port/wasm_common.h
@@ -0,0 +1,308 @@
@@ -0,0 +1,311 @@
+#define WAIT_USE_POLL 1
+
+#define HAVE_LINUX_EIDRM_BUG
Expand Down Expand Up @@ -79,6 +79,9 @@
+extern const char *progname;
+#endif
+
+// exported in ./src/fe_utils/string_utils.c
+#include <stdbool.h>
+extern PGDLLIMPORT bool fe_utils_quote_all_identifiers;
+
+extern int pg_char_to_encoding_private(const char *name);
+extern const char *pg_encoding_to_char_private(int encoding);
Expand Down
63 changes: 63 additions & 0 deletions patches/wasi_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,66 @@ unsigned int alarm(unsigned int seconds) {
}








#include <stdio.h> // FILE+fprintf
extern FILE* IDB_PIPE_FP;
extern FILE* SOCKET_FILE;
extern int SOCKET_DATA;
extern int IDB_STAGE;


static inline int
ends_with(const char *str, const char *suffix)
{
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}

FILE *pg_popen(const char *command, const char *type) {
if ( ends_with(command,"-V") || (IDB_STAGE>1)) {
fprintf(stderr,"# wasi-popen[%s] STUB\n", command);
return stderr;
}

if (!IDB_STAGE) {
fprintf(stderr,"# wasi-popen[%s] (BOOT)\n", command);
IDB_PIPE_FP = fopen( IDB_PIPE_BOOT, "w");
IDB_STAGE = 1;
} else {
fprintf(stderr,"# wasi-popen[%s] (SINGLE)\n", command);
IDB_PIPE_FP = fopen( IDB_PIPE_SINGLE, "w");
IDB_STAGE = 2;
}

return IDB_PIPE_FP;
}

int
system_wasi(const char *command) {
fprintf(stderr, "# 164: system('%s')\n", command);
return -1;
}

int pthread_mutex_lock(pthread_mutex_t *mutex) {
return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex) {
return 0;
}

void wait();

// present in share/wasi-sysroot/lib/wasm32-wasi/libwasi-emulated-signal.a(signal.o)
//void __SIG_IGN(int param) { }

0 comments on commit 8f516c5

Please sign in to comment.