lib/tsocket: add tdgram_bsd_existing_socket() helper function
authorStefan Metzmacher <metze@samba.org>
Thu, 21 May 2015 09:37:06 +0000 (11:37 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 12 Jun 2015 15:08:17 +0000 (17:08 +0200)
This is similar to tstream_bsd_existing_socket().
Both help to migrate strange code path to using the tstream or tdgram
abstractions.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/tsocket/tsocket.h
lib/tsocket/tsocket_bsd.c

index 6b0eef6d7999f7f454db256598ec27eddca70201..296c7c555ae907b51fd7854eff9e9ed3b239989a 100644 (file)
@@ -627,6 +627,48 @@ int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
 char *tsocket_address_unix_path(const struct tsocket_address *addr,
                                TALLOC_CTX *mem_ctx);
 
+#ifdef DOXYGEN
+/**
+ * @brief Wrap an existing file descriptors into the tdgram abstraction.
+ *
+ * You can use this function to wrap an existing file descriptors into the
+ * tdgram abstraction. After that you're not able to use this file descriptor
+ * for anything else. The file descriptor will be closed when the stream gets
+ * freed. If you still want to use the fd you have have to create a duplicate.
+ *
+ * @param[in]  mem_ctx  The talloc memory context to use.
+ *
+ * @param[in]  fd       The non blocking fd to use!
+ *
+ * @param[out] dgram    A pointer to store an allocated tdgram_context.
+ *
+ * @return              0 on success, -1 on error.
+ *
+ * Example:
+ * @code
+ *   fd2 = dup(fd);
+ *   rc = tdgram_bsd_existing_socket(mem_ctx, fd2, &tdgram);
+ *   if (rc < 0) {
+ *     return;
+ *   }
+ * @endcode
+ *
+ * @warning This is an internal function. You should read the code to fully
+ *          understand it if you plan to use it.
+ */
+int tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
+                              int fd,
+                              struct tdgram_context **dgram);
+#else
+int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
+                               int fd,
+                               struct tdgram_context **_dgram,
+                               const char *location);
+#define tdgram_bsd_existing_socket(mem_ctx, fd, dgram) \
+       _tdgram_bsd_existing_socket(mem_ctx, fd, dgram, \
+                                   __location__)
+#endif
+
 /**
  * @brief Request a syscall optimization for tdgram_recvfrom_send()
  *
index 5d8f80cc223496a159c415a848fd86982eda07e1..9ddbc061b94bf7db9ae999068f15edc406739857 100644 (file)
@@ -1388,6 +1388,30 @@ static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
        return 0;
 }
 
+int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
+                               int fd,
+                               struct tdgram_context **_dgram,
+                               const char *location)
+{
+       struct tdgram_context *dgram;
+       struct tdgram_bsd *bsds;
+
+       dgram = tdgram_context_create(mem_ctx,
+                                     &tdgram_bsd_ops,
+                                     &bsds,
+                                     struct tdgram_bsd,
+                                     location);
+       if (!dgram) {
+               return -1;
+       }
+       ZERO_STRUCTP(bsds);
+       bsds->fd = fd;
+       talloc_set_destructor(bsds, tdgram_bsd_destructor);
+
+       *_dgram = dgram;
+       return 0;
+}
+
 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
                            const struct tsocket_address *remote,
                            TALLOC_CTX *mem_ctx,