+/* -*-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 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 mLib; 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>
+/*----- 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) \
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;
return (1);
}
+/*----- Intercepted system calls ------------------------------------------*/
+
+/* Create an open(2)-like function OPEN. */
#define OPEN_VENEER(open) \
int open(const char *fn, int how, ...) \
{ \
}); \
return (fd); \
}
+
+/* open(2) and open64(2). */
OPEN_VENEER(open)
OPEN_VENEER(open64)
return (fp);
}
+/*----- Initialization ----------------------------------------------------*/
+
+static void setup(void) __attribute__((constructor));
static void setup(void)
{
PRESERVING_ERRNO({
import();
});
}
+
+/*----- That's all, folks -------------------------------------------------*/