chiark / gitweb /
noip.c (do_implicit_bind): Handle `SAME' impbind entries properly.
[preload-hacks] / uopen.c
diff --git a/uopen.c b/uopen.c
index 206ec75f7bf75a2de4722e06b76791d9df0c5abd..22b09db5a15cc5cd6eec8cee11ed9c5f13750ddb 100644 (file)
--- a/uopen.c
+++ b/uopen.c
@@ -1,6 +1,34 @@
+/* -*-c-*-
+ *
+ * Allow open(2) to work on Unix-domain sockets
+ *
+ * (c) 2008 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the preload-hacks package.
+ *
+ * Preload-hacks are free software; you can redistribute it and/or modify
+ * them under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * Preload-hacks are distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with preload-hacks; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
 #define _GNU_SOURCE
 #undef sun
 
+/*----- Header files ------------------------------------------------------*/
+
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/un.h>
 
+#ifndef SUN_LEN
+#  define SUN_LEN (sun) \
+       (strlen((sun)->sun_path) + offsetof(struct sockaddr_un, sun_path))
+#endif
+
+/*----- Import the real versions of functions -----------------------------*/
+
+/* The list of functions to immport. */
 #define IMPORTS(_)                                                     \
   _(open, int, (const char *, int, ...))                               \
   _(open64, int, (const char *, int, ...))                             \
   _(fopen, FILE *, (const char *, const char *))                       \
   _(freopen, FILE *, (const char *, const char *, FILE *))
 
+/* Function pointers to set up. */
 #define DECL(imp, ret, args) static ret (*real_##imp) args;
 IMPORTS(DECL)
 #undef DECL
 
-static void setup(void) __attribute__((constructor));
+/* Import the system calls. */
 static void import(void)
 {
 #define IMPORT(imp, ret, args)                                         \
@@ -32,13 +69,21 @@ static void import(void)
   IMPORTS(IMPORT)
 #undef IMPORT
 }
+/*----- Utilities ---------------------------------------------------------*/
 
+/* Socket address casts */
 #define SA(sa) ((struct sockaddr *)(sa))
 
+/* Preservation of error status */
 #define PRESERVING_ERRNO(body) do {                                    \
   int _err = errno; { body } errno = _err;                             \
 } while (0)
 
+/*----- Connecting to Unix-domain sockets ---------------------------------*/
+
+/* If FN refers to a Unix-domain socket, connect to it, stash the socket
+ * (or -1 on error) in *FDP, and return 1.  Otherwise return 0.
+ */
 static int maybe_connect(const char *fn, int *fdp)
 {
   int fd;
@@ -63,6 +108,9 @@ static int maybe_connect(const char *fn, int *fdp)
   return (1);
 }
 
+/*----- Intercepted system calls ------------------------------------------*/
+
+/* Create an open(2)-like function OPEN. */
 #define OPEN_VENEER(open)                                              \
   int open(const char *fn, int how, ...)                               \
   {                                                                    \
@@ -78,6 +126,8 @@ static int maybe_connect(const char *fn, int *fdp)
     });                                                                        \
     return (fd);                                                       \
   }
+
+/* open(2) and open64(2). */
 OPEN_VENEER(open)
 OPEN_VENEER(open64)
 
@@ -110,9 +160,14 @@ FILE *freopen(const char *fn, const char *how, FILE *fp)
   return (fp);
 }
 
+/*----- Initialization ----------------------------------------------------*/
+
+static void setup(void) __attribute__((constructor));
 static void setup(void)
 {
   PRESERVING_ERRNO({
     import();
   });
 }
+
+/*----- That's all, folks -------------------------------------------------*/