]> git.sesse.net Git - vlc/blob - bin/override.c
5098a7d59823e329a3c9e6469a72e1fdece1b84d
[vlc] / bin / override.c
1 /*****************************************************************************
2  * override.c: overriden function calls for VLC media player
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdbool.h>
26
27 void vlc_enable_override (void);
28
29 static bool override = false;
30
31 void vlc_enable_override (void)
32 {
33     override = true;
34 }
35
36 #if defined (__GNUC__) /* typeof and statement-expression */ \
37  && (defined (__ELF__) && !defined (__sun__))
38 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
39
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <dlfcn.h>
44
45 static void vlogbug (const char *level, const char *func, const char *fmt,
46                      va_list ap)
47 {
48     flockfile (stderr);
49     fprintf (stderr, "%s: call to %s(", level, func);
50     vfprintf (stderr, fmt, ap);
51     fputs (")\n", stderr);
52     funlockfile (stderr);
53 }
54
55 static void logbug (const char *level, const char *func, const char *fmt, ...)
56 {
57     va_list ap;
58
59     va_start (ap, fmt);
60     vlogbug (level, func, fmt, ap);
61     va_end (ap);
62 }
63
64 static void *getsym (const char *name)
65 {
66     void *sym = dlsym (RTLD_NEXT, name);
67     if (sym == NULL)
68     {
69         fprintf (stderr, "Cannot resolve symbol %s!\n", name);
70         abort ();
71     }
72     return sym;
73 }
74
75 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
76 #define CALL(func, ...) \
77     ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
78
79
80 /*** Environment ***
81  *
82  * "Conforming multi-threaded applications shall not use the environ variable
83  *  to access or modify any environment variable while any other thread is
84  *  concurrently modifying any environment variable." -- POSIX.
85  *
86  * Some evil libraries modify the environment. We currently ignore the calls as
87  * they could crash the process. This may cause funny behaviour though. */
88 int putenv (char *str)
89 {
90     if (override)
91     {
92         LOG("Blocked", "\"%s\"", str);
93         return 0;
94     }
95     return CALL(putenv, str);
96 }
97
98 int setenv (const char *name, const char *value, int overwrite)
99 {
100     if (override)
101     {
102         LOG("Blocked", "\"%s\", \"%s\", %d", name, value, overwrite);
103         return 0;
104     }
105     return CALL(setenv, name, value, overwrite);
106 }
107
108 int unsetenv (const char *name)
109 {
110     if (override)
111     {
112         LOG("Blocked", "\"%s\"", name);
113         return 0;
114     }
115     return CALL(unsetenv, name);
116 }
117
118
119 #endif /* __ELF__ */