13 #ifndef __BINRELOC_C__
14 #define __BINRELOC_C__
17 #ifdef ENABLE_BINRELOC
18 #include <sys/types.h>
21 #ifdef MAC_INTEGRATION
22 #include <gtkmacintegration/gtkosxapplication.h>
40 _br_find_exe (Gnc_GbrInitError *error)
42 #ifndef ENABLE_BINRELOC
44 *error = GNC_GBR_INIT_ERROR_DISABLED;
59 prefix = g_win32_get_package_installation_directory_of_module (NULL);
60 result = g_build_filename (prefix,
65 #elif defined MAC_INTEGRATION
66 gchar *path = gtkosx_application_get_executable_path();
67 g_print (
"Application Path %s\n", path);
70 char *path, *path2, *line, *result;
77 if (
sizeof (path) > SSIZE_MAX)
78 buf_size = SSIZE_MAX - 1;
80 buf_size = PATH_MAX - 1;
81 path = (
char *) g_try_malloc (buf_size);
86 *error = GNC_GBR_INIT_ERROR_NOMEM;
89 path2 = (
char *) g_try_malloc (buf_size);
94 *error = GNC_GBR_INIT_ERROR_NOMEM;
99 strncpy (path2,
"/proc/self/exe", buf_size - 1);
105 size = readlink (path2, path, buf_size - 1);
118 i = stat (path, &stat_buf);
127 if (!S_ISLNK (stat_buf.st_mode))
135 strncpy (path, path2, buf_size - 1);
142 buf_size = PATH_MAX + 128;
143 line = (
char *) g_try_realloc (path, buf_size);
149 *error = GNC_GBR_INIT_ERROR_NOMEM;
153 f = fopen (
"/proc/self/maps",
"r");
158 *error = GNC_GBR_INIT_ERROR_OPEN_MAPS;
163 result = fgets (line, (
int) buf_size, f);
169 *error = GNC_GBR_INIT_ERROR_READ_MAPS;
174 buf_size = strlen (line);
181 *error = GNC_GBR_INIT_ERROR_INVALID_MAPS;
184 if (line[buf_size - 1] == 10)
185 line[buf_size - 1] = 0;
188 path = strchr (line,
'/');
191 if (strstr (line,
" r-xp ") == NULL || path == NULL)
196 *error = GNC_GBR_INIT_ERROR_INVALID_MAPS;
200 path = g_strdup (path);
210 static gchar *exe = NULL;
212 static void set_gerror (GError **error, Gnc_GbrInitError errcode);
215 void gnc_gbr_set_exe (
const gchar* default_exe)
221 if (default_exe != NULL)
222 exe = g_strdup(default_exe);
242 gnc_gbr_init (GError **error)
244 Gnc_GbrInitError errcode = 0;
247 exe = _br_find_exe (&errcode);
254 set_gerror (error, errcode);
261 set_gerror (GError **error, Gnc_GbrInitError errcode)
263 gchar *error_message;
270 case GNC_GBR_INIT_ERROR_NOMEM:
271 error_message =
"Cannot allocate memory.";
273 case GNC_GBR_INIT_ERROR_OPEN_MAPS:
274 error_message =
"Unable to open /proc/self/maps for reading.";
276 case GNC_GBR_INIT_ERROR_READ_MAPS:
277 error_message =
"Unable to read from /proc/self/maps.";
279 case GNC_GBR_INIT_ERROR_INVALID_MAPS:
280 error_message =
"The file format of /proc/self/maps is invalid.";
282 case GNC_GBR_INIT_ERROR_DISABLED:
283 error_message =
"Binary relocation support is disabled.";
286 error_message =
"Unknown error.";
289 g_set_error (error, g_quark_from_static_string (
"GBinReloc"),
290 errcode,
"%s", error_message);
304 gnc_gbr_find_exe (
const gchar *default_exe)
309 if (default_exe != NULL)
310 return g_strdup (default_exe);
314 return g_strdup (exe);
333 gnc_gbr_find_exe_dir (
const gchar *default_dir)
338 if (default_dir != NULL)
339 return g_strdup (default_dir);
344 return g_path_get_dirname (exe);
363 gnc_gbr_find_prefix (
const gchar *default_prefix)
365 #if defined ENABLE_BINRELOC && defined MAC_INTEGRATION
366 gchar *
id = gtkosx_application_get_bundle_id ();
367 gchar *path = gtkosx_application_get_resource_path ();
370 gchar *dirname = g_path_get_dirname (path);
383 if (default_prefix != NULL)
384 return g_strdup (default_prefix);
388 dir1 = g_path_get_dirname (exe);
389 dir2 = g_path_get_dirname (dir1);
392 #endif //ENABLE_BINRELOC && defined MAC_INTEGRATION
410 gnc_gbr_find_bin_dir (
const gchar *default_bin_dir)
413 prefix = gnc_gbr_find_prefix (NULL);
417 if (default_bin_dir != NULL)
418 return g_strdup (default_bin_dir);
423 dir = g_build_filename (prefix,
"bin", NULL);
443 gnc_gbr_find_sbin_dir (
const gchar *default_sbin_dir)
447 prefix = gnc_gbr_find_prefix (NULL);
451 if (default_sbin_dir != NULL)
452 return g_strdup (default_sbin_dir);
457 dir = g_build_filename (prefix,
"sbin", NULL);
478 gnc_gbr_find_data_dir (
const gchar *default_data_dir)
482 prefix = gnc_gbr_find_prefix (NULL);
486 if (default_data_dir != NULL)
487 return g_strdup (default_data_dir);
492 dir = g_build_filename (prefix,
"share", NULL);
512 gnc_gbr_find_lib_dir (
const gchar *default_lib_dir)
516 prefix = gnc_gbr_find_prefix (NULL);
520 if (default_lib_dir != NULL)
521 return g_strdup (default_lib_dir);
526 dir = g_build_filename (prefix,
"lib", NULL);
546 gnc_gbr_find_libexec_dir (
const gchar *default_libexec_dir)
550 prefix = gnc_gbr_find_prefix (NULL);
554 if (default_libexec_dir != NULL)
555 return g_strdup (default_libexec_dir);
560 dir = g_build_filename (prefix,
"libexec", NULL);
580 gnc_gbr_find_etc_dir (
const gchar *default_etc_dir)
584 prefix = gnc_gbr_find_prefix (NULL);
588 if (default_etc_dir != NULL)
589 return g_strdup (default_etc_dir);
594 dir = g_build_filename (prefix,
"etc", NULL);