From d726a6cc3b96a95b24bcdbaa70b70b8402eb2ef0 Mon Sep 17 00:00:00 2001 From: niamtokik Date: Fri, 19 Jun 2020 15:29:05 +0000 Subject: [PATCH] set to erlang23, and can send a message. libuv is not thread safe, and it will be a bit... complicated to do what I want to do --- c_src/Makefile.openbsd | 6 +-- c_src/erluv.c | 112 ++++++++++++++++++++++++++++++++++++----- c_src/erluv.h | 7 +++ c_src/liberluv.c | 9 +++- 4 files changed, 118 insertions(+), 16 deletions(-) diff --git a/c_src/Makefile.openbsd b/c_src/Makefile.openbsd index a83d180..e67ec0c 100644 --- a/c_src/Makefile.openbsd +++ b/c_src/Makefile.openbsd @@ -2,8 +2,8 @@ # Makefile - OpenBSD pmake implementation ###################################################################### -ERLANG_VERSION ?= 22 -ERL_INTERFACE_VERSION ?= 3.13.2 +ERLANG_VERSION ?= 23 +ERL_INTERFACE_VERSION ?= 4.0 ERLANG_PATH = /usr/local/lib/erlang$(ERLANG_VERSION) @@ -15,7 +15,7 @@ INCLUDE_LIBS += -L$(ERLANG_PATH)/usr/lib INCLUDE_LIBS += -L/usr/local/lib/ INCLUDE_LIBS += -L/usr/lib -LIBS += -luv -lei -lerl_interface +LIBS += -luv -lei CFLAGS += -g $(INCLUDES) $(INCLUDE_LIBS) $(LIBS) LDFLAGS += $(INCLUDES) $(INCLUDE_LIBS) $(LIBS) diff --git a/c_src/erluv.c b/c_src/erluv.c index fbf166c..434cd69 100644 --- a/c_src/erluv.c +++ b/c_src/erluv.c @@ -1,11 +1,12 @@ /* copyright (c) 2020 Mathieu Kerjouan - * + * + * erluv -n NODE_NAME -t TARGET_NODE -c COOKIE -i ID + * */ #include #include #include #include -#include #include #include #include @@ -20,15 +21,102 @@ extern char cookie[]; extern char node_name[]; extern char node_target[]; -/* main function */ -int -main(int argc, char *argv[]) { - (void)init(); - ei_cnode ec; - ei_connect_init(&ec, node_name, cookie, creation); - int sockfd; - if ((sockfd = ei_connect(&ec, node_target)) < 0) - fprintf(stderr, "ERROR: ei_connect failed\n"); - for(;;); +void +usage() { + printf("usage: erluv ...\n"); +} + + +int +create_loop(uv_loop_t *loop) { + /* create the loop */ + loop = malloc(sizeof(uv_loop_t)); + uv_loop_init(loop); + return 0; +} + +int +close_loop(uv_loop_t *loop) { + uv_loop_close(uv_default_loop()); + free(loop); + return 0; +} + +void +process_on_exit(uv_process_t* p, int64_t exit_status, int term_signal) { + fprintf(stderr, "process %d exited\n", p->pid); +} + +int +process_new(uv_loop_t *loop) { + uv_process_t child_req = {0}; + uv_process_options_t options = {0}; + + char *name = "ls"; + char *args[3] = { "ls", "/", NULL }; + char *env[] = { NULL }; + + options.exit_cb = process_on_exit; + options.file = name; + options.args = args; + options.env = env; + + int r = uv_spawn(loop, &child_req, &options); + if (r) { + fprintf(stderr, "%s\n", uv_strerror(r)); + return 1; + } + else { + fprintf(stderr, "Launched process with ID %d\n", child_req.pid); + } + + return uv_run(loop, UV_RUN_DEFAULT); +} + +/* main function + * + */ +int +main(int argc, char *argv[]) { + /* init default variable */ + (void)init(); + + /* create the loop */ + /* + uv_loop_t *loop; + create_loop(loop); + uv_process_t child_req = {0}; + uv_process_options_t options = {0}; + uv_spawn(loop, &child_req, &options); + close_loop(loop); + */ + + /* connect to erlang node */ + ei_cnode ec; + ei_connect_init(&ec, node_name, cookie, creation); + + int sockfd; + if ((sockfd = ei_connect(&ec, node_target)) < 0) + fprintf(stderr, "ERROR: ei_connect failed\n"); + + erlang_pid pid; + ei_make_pid(&ec, &pid); + erlang_pid *p = ei_self(&ec); + printf("%d, %d, %d\n", p->num, p->serial, p->creation); + printf("%d, %d, %d\n", pid.num, pid.serial, pid.creation); + + char *message = "my_message"; + char *process_name = "ttt"; + + ei_x_buff x; + ei_x_new_with_version(&x); + ei_x_encode_string(&x, message); + ei_reg_send(&ec, sockfd, process_name, x.buff, x.index); + ei_x_free(&x); + //SleepEx(100); + shutdown(sockfd, 1); + close(sockfd); + + return 0; } diff --git a/c_src/erluv.h b/c_src/erluv.h index 011321a..e8b140b 100644 --- a/c_src/erluv.h +++ b/c_src/erluv.h @@ -3,6 +3,7 @@ */ #define COOKIE_SIZE 255 #define NODE_NAME_SIZE 255 +#define PROCESS_NAME 255 /* interfaces */ int init_cookie(void); @@ -10,3 +11,9 @@ int init_node_name(void); int init_id(void); int init_creation(void); void init(void); + +struct process_baton { + char *name; + char **arguments; + char **environment; +}; diff --git a/c_src/liberluv.c b/c_src/liberluv.c index 4a5ad2e..c5f7c1f 100644 --- a/c_src/liberluv.c +++ b/c_src/liberluv.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -111,3 +110,11 @@ init() { ei_init(); } + +/* process_create + * + */ +void +process_create() { + +}