override.c 5.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*****************************************************************************
 * override.c: overriden function calls for VLC media player
 *****************************************************************************
 * Copyright (C) 2010 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdbool.h>

void vlc_enable_override (void);

#if defined (__GNUC__) /* typeof and statement-expression */ \
 && (defined (__ELF__) && !defined (__sun__))
/* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
37
#include <pthread.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
38 39 40
#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
#endif
41

42 43 44 45 46 47 48 49 50 51 52 53 54
static bool override = false;

static void vlc_reset_override (void)
{
    override = false;
}

void vlc_enable_override (void)
{
    override = true;
    pthread_atfork (NULL, NULL, vlc_reset_override);
}

55 56 57
static void vlogbug (const char *level, const char *func, const char *fmt,
                     va_list ap)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
58 59 60 61 62 63
#ifdef HAVE_BACKTRACE
    const size_t framec = 8;
    void *framev[framec];

    backtrace (framev, framec);
#endif
64 65 66 67
    flockfile (stderr);
    fprintf (stderr, "%s: call to %s(", level, func);
    vfprintf (stderr, fmt, ap);
    fputs (")\n", stderr);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
68 69 70 71
    fflush (stderr);
#ifdef HAVE_BACKTRACE
    backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
#endif
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    funlockfile (stderr);
}

static void logbug (const char *level, const char *func, const char *fmt, ...)
{
    va_list ap;

    va_start (ap, fmt);
    vlogbug (level, func, fmt, ap);
    va_end (ap);
}

static void *getsym (const char *name)
{
    void *sym = dlsym (RTLD_NEXT, name);
    if (sym == NULL)
    {
        fprintf (stderr, "Cannot resolve symbol %s!\n", name);
        abort ();
    }
    return sym;
}

#define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
#define CALL(func, ...) \
    ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })


100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/*** Environment ***
 *
 * "Conforming multi-threaded applications shall not use the environ variable
 *  to access or modify any environment variable while any other thread is
 *  concurrently modifying any environment variable." -- POSIX.
 *
 * Some evil libraries modify the environment. We currently ignore the calls as
 * they could crash the process. This may cause funny behaviour though. */
int putenv (char *str)
{
    if (override)
    {
        LOG("Blocked", "\"%s\"", str);
        return 0;
    }
    return CALL(putenv, str);
}

int setenv (const char *name, const char *value, int overwrite)
{
    if (override)
    {
        LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
        return 0;
    }
    return CALL(setenv, name, value, overwrite);
}

int unsetenv (const char *name)
{
    if (override)
    {
        LOG("Blocked", "\"%s\"", name);
        return 0;
    }
    return CALL(unsetenv, name);
}


139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/*** Pseudo random numbers ***
 *
 * The C PRNG is not thread-safe (and generally sucks, the POSIX 48-bits PRNG
 * is much better as a reproducible non-secure PRNG). To work around this, we
 * force evil callers to serialize. This makes the call safe, but fails to
 * preserve reproducibility of the number sequence (which usually does not
 * matter).
 **/
static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;

void srand (unsigned int seed)
{
    pthread_mutex_lock (&prng_lock);
    LOG("Warning", "%d", seed);
    CALL(srand, seed);
    pthread_mutex_unlock (&prng_lock);
}

int rand (void)
{
    int ret;

    pthread_mutex_lock (&prng_lock);
    LOG("Warning", "");
    ret = CALL(rand);
    pthread_mutex_unlock (&prng_lock);
    return ret;
}


169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/** Signals **/
#include <signal.h>

void (*signal (int signum, void (*handler) (int))) (int)
{
    if (override)
    {
        const char *msg = "Error";

        if ((signum == SIGPIPE && handler == SIG_IGN)
         || (signum != SIGPIPE && handler == SIG_DFL))
            /* Same settings we already use */
            msg = "Warning";
        LOG(msg, "%d, %p", signum, handler);
    }
    return CALL(signal, signum, handler);
}

int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
{
    if (act != NULL)
        LOG("Error", "%d, %p, %p", signum, act, old);
    return CALL(sigaction, signum, act, old);
}


195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*** Xlib ****/
#ifdef HAVE_X11_XLIB_H
# include <X11/Xlib.h>

static pthread_mutex_t xlib_lock = PTHREAD_MUTEX_INITIALIZER;

int (*XSetErrorHandler (int (*handler) (Display *, XErrorEvent *)))
     (Display *, XErrorEvent *)
{
    if (override)
    {
        int (*ret) (Display *, XErrorEvent *);

        pthread_mutex_lock (&xlib_lock);
        LOG("Error", "%p", handler);
        ret = CALL(XSetErrorHandler, handler);
        pthread_mutex_unlock (&xlib_lock);
        return ret;
    }
    return CALL(XSetErrorHandler, handler);
}

int (*XSetIOErrorHandler (int (*handler) (Display *))) (Display *)
{
    if (override)
    {
        int (*ret) (Display *);

        pthread_mutex_lock (&xlib_lock);
        LOG("Error", "%p", handler);
        ret = CALL(XSetIOErrorHandler, handler);
        pthread_mutex_unlock (&xlib_lock);
        return ret;
    }
    return CALL(XSetIOErrorHandler, handler);
}
#endif
232 233 234 235 236
#else
static void vlc_enable_override (void)
{
}
#endif