X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/preload-hacks/blobdiff_plain/e4976bb0731d14a7d8bfa53067ec1d99e94617cb..97162e5b05244df7dee91813544cd2e1566d881c:/uopen.c diff --git a/uopen.c b/uopen.c index 206ec75..3c0d1d4 100644 --- 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 #include #include @@ -14,17 +42,21 @@ #include #include +/*----- 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 +64,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 +103,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 +121,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 +155,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 -------------------------------------------------*/