Explorar o código

system/BConnection: Add option to take ownership of the pipe_fd.

Ambroz Bizjak %!s(int64=11) %!d(string=hai) anos
pai
achega
f844123ab2

+ 1 - 1
examples/stdin_input.c

@@ -107,7 +107,7 @@ int main ()
     }
     
     // init BConnection object backed by the stdin fd
-    if (!BConnection_Init(&pipe_con, BConnection_source_pipe(0), &reactor, NULL, connection_handler)) {
+    if (!BConnection_Init(&pipe_con, BConnection_source_pipe(0, 0), &reactor, NULL, connection_handler)) {
         fprintf(stderr, "BConnection_Init failed\n");
         goto fail3;
     }

+ 2 - 2
ncd/modules/sys_start_process.c

@@ -754,7 +754,7 @@ static void read_pipe_func_new (void *vo, NCDModuleInst *i, const struct NCDModu
     }
     
     // init connection
-    if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->read_fd), i->params->iparams->reactor, o, read_pipe_connection_handler)) {
+    if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->read_fd, 0), i->params->iparams->reactor, o, read_pipe_connection_handler)) {
         ModuleLog(i, BLOG_ERROR, "BConnection_Init failed");
         goto fail0;
     }
@@ -1022,7 +1022,7 @@ static void write_pipe_func_new (void *vo, NCDModuleInst *i, const struct NCDMod
     }
     
     // init connection
-    if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->write_fd), i->params->iparams->reactor, o, write_pipe_connection_handler)) {
+    if (!BConnection_Init(&o->connection, BConnection_source_pipe(pinst->write_fd, 0), i->params->iparams->reactor, o, write_pipe_connection_handler)) {
         ModuleLog(i, BLOG_ERROR, "BConnection_Init failed");
         goto fail0;
     }

+ 6 - 3
system/BConnection.h

@@ -238,6 +238,7 @@ struct BConnection_source {
 #ifndef BADVPN_USE_WINAPI
         struct {
             int pipefd;
+            int close_it;
         } pipe;
 #endif
     } u;
@@ -261,11 +262,12 @@ static struct BConnection_source BConnection_source_connector (BConnector *conne
 }
 
 #ifndef BADVPN_USE_WINAPI
-static struct BConnection_source BConnection_source_pipe (int pipefd)
+static struct BConnection_source BConnection_source_pipe (int pipefd, int close_it)
 {
     struct BConnection_source s;
     s.type = BCONNECTION_SOURCE_TYPE_PIPE;
     s.u.pipe.pipefd = pipefd;
+    s.u.pipe.close_it = close_it;
     return s;
 }
 #endif
@@ -313,9 +315,10 @@ typedef void (*BConnection_handler) (void *user, int event);
  *                 Uses a connection establised with {@link BConnector}. Must be called from the job
  *                 closure of the connector's {@link BConnector_handler}, the handler must be reporting
  *                 successful connection, and must be the first attempt for this handler invocation.
- *               - BCONNECTION_SOURCE_PIPE(int)
+ *               - BCONNECTION_SOURCE_PIPE(int pipefd, int close_it)
  *                 On Unix-like systems, uses the provided file descriptor. The file descriptor number must
- *                 be >=0.
+ *                 be >=0. If close_it is true, the connector will take responsibility of closing the
+ *                 pipefd. Note that it will be closed even when this Init fails.
  * @param reactor reactor we live in
  * @param user argument to handler
  * @param handler handler called when an error occurs or the receive end of the connection was closed

+ 1 - 1
system/BConnection_unix.c

@@ -786,7 +786,7 @@ int BConnection_Init (BConnection *o, struct BConnection_source source, BReactor
         case BCONNECTION_SOURCE_TYPE_PIPE: {
             // use user-provided fd
             o->fd = source.u.pipe.pipefd;
-            o->close_fd = 0;
+            o->close_fd = !!source.u.pipe.close_it;
             
             // set non-blocking
             if (!badvpn_set_nonblocking(o->fd)) {

+ 1 - 1
system/BInputProcess.c

@@ -105,7 +105,7 @@ int BInputProcess_Init (BInputProcess *o, BReactor *reactor, BProcessManager *ma
     }
     
     // init pipe connection
-    if (!BConnection_Init(&o->pipe_con, BConnection_source_pipe(pipefds[0]), o->reactor, o, (BConnection_handler)connection_handler)) {
+    if (!BConnection_Init(&o->pipe_con, BConnection_source_pipe(pipefds[0], 0), o->reactor, o, (BConnection_handler)connection_handler)) {
         BLog(BLOG_ERROR, "BConnection_Init failed");
         goto fail1;
     }