Selaa lähdekoodia

Add option for pipe creation

Emil Suleymanov 5 vuotta sitten
vanhempi
commit
dcbafb1979

+ 7 - 3
admin/angel/Core.c

@@ -150,6 +150,7 @@ static void sendResponse(String* error,
 }
 
 static void initSocket2(String* socketFullPath,
+                          bool attemptToCreate,
                           struct Context* ctx,
                           uint8_t addressPrefix,
                           struct Except* eh)
@@ -157,7 +158,7 @@ static void initSocket2(String* socketFullPath,
     Log_debug(ctx->logger, "Initializing socket: %s;", socketFullPath->bytes);
 
     struct Iface* rawSocketIf = SocketInterface_new(
-        socketFullPath->bytes, ctx->base, ctx->logger, NULL, ctx->alloc);
+        socketFullPath->bytes, attemptToCreate, ctx->base, ctx->logger, NULL, ctx->alloc);
 
     struct SocketWrapper* sw = SocketWrapper_new(ctx->alloc, ctx->logger);
     Iface_plumb(&sw->externalIf, rawSocketIf);
@@ -249,7 +250,9 @@ static void initSocket(Dict* args, void* vcontext, String* txid, struct Allocato
     struct Jmp jmp;
     Jmp_try(jmp) {
         String* socketFullPath = Dict_getStringC(args, "socketFullPath");
-        initSocket2(socketFullPath, ctx, AddressCalc_ADDRESS_PREFIX_BITS, &jmp.handler);
+        bool socketAttemptToCreate = *Dict_getIntC(args, "socketAttemptToCreate");
+        initSocket2(socketFullPath, socketAttemptToCreate, ctx, AddressCalc_ADDRESS_PREFIX_BITS,
+            &jmp.handler);
     } Jmp_catch {
         String* error = String_printf(requestAlloc, "Failed to configure socket [%s]",
             jmp.message);
@@ -372,7 +375,8 @@ void Core_init(struct Allocator* alloc,
 
     Admin_registerFunction("Core_initSocket", initSocket, ctx, true,
         ((struct Admin_FunctionArg[]) {
-            { .name = "socketFullPath", .required = 1, .type = "String" }
+            { .name = "socketFullPath", .required = 1, .type = "String" },
+            { .name = "socketAttemptToCreate", .required = 1, .type = "Int" }
         }), admin);
 }
 

+ 9 - 0
client/Configurator.c

@@ -344,7 +344,16 @@ static void socketInterface(Dict* ifaceConf, struct Allocator* tempAlloc, struct
         exit(1);
     }
 
+    // false by default
+    int64_t attemptToCreate = 0;
+
+    int64_t* socketAttemptToCreate = Dict_getIntC(ifaceConf, "socketAttemptToCreate");
+    if (socketAttemptToCreate && *socketAttemptToCreate) {
+        attemptToCreate = 1;
+    }
+
     Dict_putStringC(args, "socketFullPath", socketFullPath, tempAlloc);
+    Dict_putIntC(args, "socketAttemptToCreate", attemptToCreate, tempAlloc);
     rpcCall0(String_CONST("Core_initSocket"), args, ctx, tempAlloc, NULL, true);
 }
 

+ 2 - 1
interface/tuntap/SocketInterface.c

@@ -19,6 +19,7 @@
 #include "util/events/Pipe.h"
 
 struct Iface* SocketInterface_new(const char* socketFullPath,
+                                    bool attemptToCreate,
                                     struct EventBase* base,
                                     struct Log* logger,
                                     struct Except* eh,
@@ -26,7 +27,7 @@ struct Iface* SocketInterface_new(const char* socketFullPath,
 {
     Log_info(logger, "Initializing socket: %s;", socketFullPath);
 
-    struct Pipe* p = Pipe_namedConnect(socketFullPath, base, eh, alloc);
+    struct Pipe* p = Pipe_namedConnect(socketFullPath, attemptToCreate, base, eh, alloc);
 
     return &p->iface;
 }

+ 3 - 0
interface/tuntap/SocketInterface.h

@@ -22,6 +22,8 @@
 #include "util/Linker.h"
 Linker_require("interface/tuntap/SocketInterface.c");
 
+#include <stdbool.h>
+
 /**
  * Create a new SocketInterface.
  *
@@ -33,6 +35,7 @@ Linker_require("interface/tuntap/SocketInterface.c");
  * @return a Interface.
  */
 struct Iface* SocketInterface_new(const char* socketFullPath,
+                                    bool attemptToCreate,
                                     struct EventBase* base,
                                     struct Log* logger,
                                     struct Except* eh,

+ 3 - 0
util/events/Pipe.h

@@ -22,6 +22,8 @@
 #include "util/Linker.h"
 Linker_require("util/events/libuv/Pipe.c");
 
+#include <stdbool.h>
+
 struct Pipe;
 typedef void (* Pipe_callback)(struct Pipe* p, int status);
 
@@ -68,6 +70,7 @@ struct Pipe* Pipe_named(const char* path,
                         struct Allocator* userAlloc);
 
 struct Pipe* Pipe_namedConnect(const char* fullPath,
+                               bool attemptToCreate,
                                struct EventBase* eb,
                                struct Except* eh,
                                struct Allocator* userAlloc);

+ 14 - 0
util/events/libuv/Pipe.c

@@ -515,12 +515,26 @@ struct Pipe* Pipe_named(const char* path,
 }
 
 struct Pipe* Pipe_namedConnect(const char* fullPath,
+                               bool attemptToCreate,
                                struct EventBase* eb,
                                struct Except* eh,
                                struct Allocator* userAlloc)
 {
     struct Pipe_pvt* out = newPipeAny(eb, fullPath, eh, userAlloc);
 
+    if (attemptToCreate) {
+        // Attempt to create pipe.
+        int ret = uv_pipe_bind(&out->server, out->pub.fullName);
+        if (!ret) {
+            ret = uv_listen((uv_stream_t*) &out->server, 1, listenCallback);
+            if (ret) {
+                Except_throw(eh, "uv_listen() failed [%s] for pipe [%s]",
+                             uv_strerror(ret), out->pub.fullName);
+            }
+            return &out->pub;
+        }
+    }
+
     uv_connect_t* req = Allocator_malloc(out->alloc, sizeof(uv_connect_t));
     req->data = out;
     uv_pipe_connect(req, &out->peer, out->pub.fullName, connected);