00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <dlfcn.h>
00023 #include <string.h>
00024 #include <malloc.h>
00025
00026 #include "preload.h"
00027
00028 typedef int (*entry_t)(int, char **);
00029
00030 int g_debugPrinting;
00031
00033 static void loadLibraries(const char * const libs[], unsigned int numLibs)
00034 {
00035 for (unsigned int i = 0; i < numLibs; i++)
00036 {
00037 const char * lib = libs[i];
00038 int len = strlen(lib);
00039 if (lib[0] != '#' && len > 1)
00040 {
00041 int flags = 0;
00042 int skipChar = 0;
00043
00044
00045 if (lib[0] == 'N')
00046 {
00047 skipChar = 1;
00048 flags = RTLD_NOW | RTLD_GLOBAL;
00049 }
00050
00051 else if (lib[0] == 'L')
00052 {
00053 skipChar = 1;
00054 flags = RTLD_LAZY | RTLD_GLOBAL;
00055 }
00056
00057 else if (lib[0] == 'D')
00058 {
00059 skipChar = 1;
00060 flags = RTLD_DEEPBIND | RTLD_GLOBAL;
00061 }
00062
00063 else
00064 {
00065 skipChar = 0;
00066 flags = RTLD_NOW | RTLD_GLOBAL;
00067 }
00068
00069
00070 dlerror();
00071
00072
00073 if (!dlopen(lib + skipChar, flags) && g_debugPrinting)
00074 {
00075 fprintf(stderr, "Warning: can't preload %s\n", lib + skipChar);
00076 }
00077 }
00078 }
00079 }
00080
00089 static int invokeLauncherLib(int argc, char ** argv)
00090 {
00091
00092 dlerror();
00093
00094 void * handle = dlopen(LAUNCHER_LIBRARY, RTLD_LAZY | RTLD_LOCAL);
00095 if (handle)
00096 {
00097 char * error = NULL;
00098
00099
00100 dlerror();
00101
00102
00103 entry_t entry = (entry_t)dlsym(handle, "main");
00104
00105
00106 if ((error = dlerror()) != NULL)
00107 {
00108 fprintf(stderr, "%s\n", error);
00109 dlclose(handle);
00110 return 0;
00111 }
00112
00113 entry(argc, argv);
00114 dlclose(handle);
00115
00116 return 1;
00117 }
00118 else
00119 {
00120 fprintf(stderr, "%s\n", dlerror());
00121 return 0;
00122 }
00123
00124 return 1;
00125 }
00126
00128 int main(int argc, char ** argv)
00129 {
00130
00131
00132 if (!getenv("DISPLAY"))
00133 {
00134 fprintf(stderr, "FATAL!!: DISPLAY environment variable not set.\n");
00135 return EXIT_FAILURE;
00136 }
00137
00138
00139 g_debugPrinting = 0;
00140
00141 int helpWanted = 0;
00142 for (int i = 1; i < argc; ++i)
00143 {
00144 if (strcmp(argv[i], "--debug") == 0)
00145 g_debugPrinting = 1;
00146
00147 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
00148 helpWanted = 1;
00149 }
00150
00151
00152
00153
00154
00155 setenv("TMPDIR", "/var/tmp", 0);
00156
00157
00158
00159
00160 char* s = NULL;
00161 if((s = getenv("MALLOC_TRIM_THRESHOLD_")))
00162 mallopt(M_TRIM_THRESHOLD, atoi(s));
00163 if((s = getenv("MALLOC_TOP_PAD_")))
00164 mallopt(M_TOP_PAD, atoi(s));
00165 if((s = getenv("MALLOC_PERTURB_")))
00166 mallopt(M_PERTURB, atoi(s));
00167 if((s = getenv("MALLOC_MMAP_THRESHOLD_")))
00168 mallopt(M_MMAP_THRESHOLD, atoi(s));
00169 if((s = getenv("MALLOC_MMAP_MAX_")))
00170 mallopt(M_MMAP_MAX, atoi(s));
00171
00172
00173 if (!helpWanted)
00174 loadLibraries(gLibs, sizeof(gLibs) / sizeof(char *));
00175
00176
00177 if (!invokeLauncherLib(argc, argv))
00178 {
00179 fprintf(stderr, "FATAL!!: Failed to load the launcher library\n");
00180 return EXIT_FAILURE;
00181 }
00182
00183 #ifdef WITH_COVERAGE
00184 __gcov_flush();
00185 #endif
00186
00187 return EXIT_SUCCESS;
00188 }