1 /*
2 * BinReloc - a library for creating relocatable executables
3 * Written by: Hongli Lai <h.lai@chello.nl>
4 * http://autopackage.org/
5 *
6 * This source code is public domain. You can relicense this code
7 * under whatever license you want.
8 *
9 * See http://autopackage.org/docs/binreloc/ for
10 * more information and how to use this.
11 */
12
13 #ifndef __BINRELOC_C__
14 #define __BINRELOC_C__
15
16 #ifdef ENABLE_BINRELOC
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #endif /* ENABLE_BINRELOC */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include <string.h>
25 #include "binreloc.h"
26
27 G_BEGIN_DECLS
28
29
30 /** @internal
31 * Find the canonical filename of the executable. Returns the filename
32 * (which must be freed) or NULL on error. If the parameter 'error' is
33 * not NULL, the error code will be stored there, if an error occurred.
34 */
35 static char *
_br_find_exe(GbrInitError * error)36 _br_find_exe (GbrInitError *error)
37 {
38 #ifndef ENABLE_BINRELOC
39 if (error)
40 *error = _GDA_GBR_INIT_ERROR_DISABLED;
41 return NULL;
42 #else
43 char *path, *path2, *line, *result;
44 size_t buf_size;
45 ssize_t size;
46 struct stat stat_buf;
47 FILE *f;
48
49 /* Read from /proc/self/exe (symlink) */
50 if (sizeof (path) > SSIZE_MAX)
51 buf_size = SSIZE_MAX - 1;
52 else
53 buf_size = PATH_MAX - 1;
54 path = (char *) g_try_malloc (buf_size);
55 if (path == NULL) {
56 /* Cannot allocate memory. */
57 if (error)
58 *error = _GDA_GBR_INIT_ERROR_NOMEM;
59 return NULL;
60 }
61 path2 = (char *) g_try_malloc (buf_size);
62 if (path2 == NULL) {
63 /* Cannot allocate memory. */
64 if (error)
65 *error = _GDA_GBR_INIT_ERROR_NOMEM;
66 g_free (path);
67 return NULL;
68 }
69
70 strncpy (path2, "/proc/self/exe", buf_size - 1);
71
72 while (1) {
73 int i;
74
75 size = readlink (path2, path, buf_size - 1); /* Flawfinder: ignore */
76 if (size == -1) {
77 /* Error. */
78 g_free (path2);
79 break;
80 }
81
82 /* readlink() success. */
83 path[size] = '\0';
84
85 /* Check whether the symlink's target is also a symlink.
86 * We want to get the final target. */
87 i = stat (path, &stat_buf);
88 if (i == -1) {
89 /* Error. */
90 g_free (path2);
91 break;
92 }
93
94 /* stat() success. */
95 if (!S_ISLNK (stat_buf.st_mode)) {
96 /* path is not a symlink. Done. */
97 g_free (path2);
98 return path;
99 }
100
101 /* path is a symlink. Continue loop and resolve this. */
102 strncpy (path, path2, buf_size - 1);
103 }
104
105
106 /* readlink() or stat() failed; this can happen when the program is
107 * running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
108
109 buf_size = PATH_MAX + 128;
110 line = (char *) g_try_realloc (path, buf_size);
111 if (line == NULL) {
112 /* Cannot allocate memory. */
113 g_free (path);
114 if (error)
115 *error = _GDA_GBR_INIT_ERROR_NOMEM;
116 return NULL;
117 }
118
119 f = fopen ("/proc/self/maps", "r"); /* Flawfinder: ignore */
120 if (f == NULL) {
121 g_free (line);
122 if (error)
123 *error = _GDA_GBR_INIT_ERROR_OPEN_MAPS;
124 return NULL;
125 }
126
127 /* The first entry should be the executable name. */
128 result = fgets (line, (int) buf_size, f);
129 if (result == NULL) {
130 fclose (f);
131 g_free (line);
132 if (error)
133 *error = _GDA_GBR_INIT_ERROR_READ_MAPS;
134 return NULL;
135 }
136
137 /* Get rid of newline character. */
138 buf_size = strlen (line);
139 if (buf_size <= 0) {
140 /* Huh? An empty string? */
141 fclose (f);
142 g_free (line);
143 if (error)
144 *error = _GDA_GBR_INIT_ERROR_INVALID_MAPS;
145 return NULL;
146 }
147 if (line[buf_size - 1] == 10)
148 line[buf_size - 1] = 0;
149
150 /* Extract the filename; it is always an absolute path. */
151 path = strchr (line, '/');
152
153 /* Sanity check. */
154 if (strstr (line, " r-xp ") == NULL || path == NULL) {
155 fclose (f);
156 g_free (line);
157 if (error)
158 *error = _GDA_GBR_INIT_ERROR_INVALID_MAPS;
159 return NULL;
160 }
161
162 path = g_strdup (path);
163 g_free (line);
164 fclose (f);
165 return path;
166 #endif /* ENABLE_BINRELOC */
167 }
168
169
170 /** @internal
171 * Find the canonical filename of the executable which owns symbol.
172 * Returns a filename which must be freed, or NULL on error.
173 */
174 static char *
_br_find_exe_for_symbol(G_GNUC_UNUSED const void * symbol,GbrInitError * error)175 _br_find_exe_for_symbol (
176 #ifndef ENABLE_BINRELOC
177 G_GNUC_UNUSED const void *symbol, GbrInitError *error)
178 #else
179 const void *symbol, GbrInitError *error)
180 #endif
181 {
182 #ifndef ENABLE_BINRELOC
183 if (error)
184 *error = _GDA_GBR_INIT_ERROR_DISABLED;
185 return (char *) NULL;
186 #else
187 #define SIZE PATH_MAX + 100
188 FILE *f;
189 size_t address_string_len;
190 char *address_string, line[SIZE], *found; /* Flawfinder: ignore */
191
192 if (symbol == NULL)
193 return (char *) NULL;
194
195 f = fopen ("/proc/self/maps", "r"); /* Flawfinder: ignore */
196 if (f == NULL)
197 return (char *) NULL;
198
199 address_string_len = 4;
200 address_string = (char *) g_try_malloc (address_string_len);
201 found = (char *) NULL;
202
203 while (!feof (f)) {
204 char *start_addr, *end_addr, *end_addr_end, *file;
205 void *start_addr_p, *end_addr_p;
206 size_t len;
207
208 if (fgets (line, SIZE, f) == NULL)
209 break;
210
211 /* Sanity check. */
212 if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
213 continue;
214
215 /* Parse line. */
216 start_addr = line;
217 end_addr = strchr (line, '-');
218 file = strchr (line, '/');
219
220 /* More sanity check. */
221 if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
222 continue;
223
224 end_addr[0] = '\0';
225 end_addr++;
226 end_addr_end = strchr (end_addr, ' ');
227 if (end_addr_end == NULL)
228 continue;
229
230 end_addr_end[0] = '\0';
231 len = strlen (file);
232 if (len == 0)
233 continue;
234 if (file[len - 1] == '\n')
235 file[len - 1] = '\0';
236
237 /* Get rid of "(deleted)" from the filename. */
238 len = strlen (file);
239 if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
240 file[len - 10] = '\0';
241
242 /* I don't know whether this can happen but better safe than sorry. */
243 len = strlen (start_addr);
244 if (len != strlen (end_addr))
245 continue;
246
247
248 /* Transform the addresses into a string in the form of 0xdeadbeef,
249 * then transform that into a pointer. */
250 if (address_string_len < len + 3) {
251 address_string_len = len + 3;
252 address_string = (char *) g_realloc (address_string, address_string_len);
253 }
254
255 memcpy (address_string, "0x", 2); /* Flawfinder: ignore */
256 memcpy (address_string + 2, start_addr, len); /* Flawfinder: ignore */
257 address_string[2 + len] = '\0';
258 sscanf (address_string, "%p", &start_addr_p);
259
260 memcpy (address_string, "0x", 2); /* Flawfinder: ignore */
261 memcpy (address_string + 2, end_addr, len); /* Flawfinder: ignore */
262 address_string[2 + len] = '\0';
263 sscanf (address_string, "%p", &end_addr_p);
264
265
266 if (symbol >= start_addr_p && symbol < end_addr_p) {
267 found = file;
268 break;
269 }
270 }
271
272 g_free (address_string);
273 fclose (f);
274
275 if (found == NULL)
276 return (char *) NULL;
277 else
278 return g_strdup (found);
279 #endif /* ENABLE_BINRELOC */
280 }
281
282
283 static gchar *exe = NULL;
284
285 static void set_gerror (GError **error, GbrInitError errcode);
286
287
288 /** Initialize the BinReloc library (for applications).
289 *
290 * This function must be called before using any other BinReloc functions.
291 * It attempts to locate the application's canonical filename.
292 *
293 * @note If you want to use BinReloc for a library, then you should call
294 * _gda_gbr_init_lib() instead.
295 *
296 * @param error If BinReloc failed to initialize, then the error report will
297 * be stored in this variable. Set to NULL if you don't want an
298 * error report. See the #GbrInitError for a list of error
299 * codes.
300 *
301 * @returns TRUE on success, FALSE if BinReloc failed to initialize.
302 */
303 gboolean
_gda_gbr_init(GError ** error)304 _gda_gbr_init (GError **error)
305 {
306 GbrInitError errcode;
307
308 /* Locate the application's filename. */
309 exe = _br_find_exe (&errcode);
310 if (exe != NULL)
311 /* Success! */
312 return TRUE;
313 else {
314 /* Failed :-( */
315 set_gerror (error, errcode);
316 return FALSE;
317 }
318 }
319
320
321 /** Initialize the BinReloc library (for libraries).
322 *
323 * This function must be called before using any other BinReloc functions.
324 * It attempts to locate the calling library's canonical filename.
325 *
326 * @note The BinReloc source code MUST be included in your library, or this
327 * function won't work correctly.
328 *
329 * @returns TRUE on success, FALSE if a filename cannot be found.
330 */
331 gboolean
_gda_gbr_init_lib(GError ** error)332 _gda_gbr_init_lib (GError **error)
333 {
334 GbrInitError errcode;
335
336 exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
337 if (exe != NULL)
338 /* Success! */
339 return TRUE;
340 else {
341 /* Failed :-( */
342 set_gerror (error, errcode);
343 return exe != NULL;
344 }
345 }
346
347
348 static void
set_gerror(GError ** error,GbrInitError errcode)349 set_gerror (GError **error, GbrInitError errcode)
350 {
351 gchar *error_message;
352
353 if (error == NULL)
354 return;
355
356 switch (errcode) {
357 case _GDA_GBR_INIT_ERROR_NOMEM:
358 error_message = "Cannot allocate memory.";
359 break;
360 case _GDA_GBR_INIT_ERROR_OPEN_MAPS:
361 error_message = "Unable to open /proc/self/maps for reading.";
362 break;
363 case _GDA_GBR_INIT_ERROR_READ_MAPS:
364 error_message = "Unable to read from /proc/self/maps.";
365 break;
366 case _GDA_GBR_INIT_ERROR_INVALID_MAPS:
367 error_message = "The file format of /proc/self/maps is invalid.";
368 break;
369 case _GDA_GBR_INIT_ERROR_DISABLED:
370 error_message = "Binary relocation support is disabled.";
371 break;
372 default:
373 error_message = "Unknown error.";
374 break;
375 };
376 g_set_error (error, g_quark_from_static_string ("GBinReloc"),
377 errcode, "%s", error_message);
378 }
379
380
381 /** Find the canonical filename of the current application.
382 *
383 * @param default_exe A default filename which will be used as fallback.
384 * @returns A string containing the application's canonical filename,
385 * which must be freed when no longer necessary. If BinReloc is
386 * not initialized, or if the initialization function failed,
387 * then a copy of default_exe will be returned. If default_exe
388 * is NULL, then NULL will be returned.
389 */
390 gchar *
_gda_gbr_find_exe(const gchar * default_exe)391 _gda_gbr_find_exe (const gchar *default_exe)
392 {
393 if (exe == NULL) {
394 /* BinReloc is not initialized. */
395 if (default_exe != NULL)
396 return g_strdup (default_exe);
397 else
398 return NULL;
399 }
400 return g_strdup (exe);
401 }
402
403
404 /** Locate the directory in which the current application is installed.
405 *
406 * The prefix is generated by the following pseudo-code evaluation:
407 * \code
408 * dirname(exename)
409 * \endcode
410 *
411 * @param default_dir A default directory which will used as fallback.
412 * @return A string containing the directory, which must be freed when no
413 * longer necessary. If BinReloc is not initialized, or if the
414 * initialization function failed, then a copy of default_dir
415 * will be returned. If default_dir is NULL, then NULL will be
416 * returned.
417 */
418 gchar *
_gda_gbr_find_exe_dir(const gchar * default_dir)419 _gda_gbr_find_exe_dir (const gchar *default_dir)
420 {
421 if (exe == NULL) {
422 /* BinReloc not initialized. */
423 if (default_dir != NULL)
424 return g_strdup (default_dir);
425 else
426 return NULL;
427 }
428
429 return g_path_get_dirname (exe);
430 }
431
432
433 /** Locate the prefix in which the current application is installed.
434 *
435 * The prefix is generated by the following pseudo-code evaluation:
436 * \code
437 * dirname(dirname(exename))
438 * \endcode
439 *
440 * @param default_prefix A default prefix which will used as fallback.
441 * @return A string containing the prefix, which must be freed when no
442 * longer necessary. If BinReloc is not initialized, or if the
443 * initialization function failed, then a copy of default_prefix
444 * will be returned. If default_prefix is NULL, then NULL will be
445 * returned.
446 */
447 gchar *
_gda_gbr_find_prefix(const gchar * default_prefix)448 _gda_gbr_find_prefix (const gchar *default_prefix)
449 {
450 gchar *dir1, *dir2;
451
452 if (exe == NULL) {
453 /* BinReloc not initialized. */
454 if (default_prefix != NULL)
455 return g_strdup (default_prefix);
456 else
457 return NULL;
458 }
459
460 dir1 = g_path_get_dirname (exe);
461 dir2 = g_path_get_dirname (dir1);
462 g_free (dir1);
463 return dir2;
464 }
465
466
467 /** Locate the application's binary folder.
468 *
469 * The path is generated by the following pseudo-code evaluation:
470 * \code
471 * prefix + "/bin"
472 * \endcode
473 *
474 * @param default_bin_dir A default path which will used as fallback.
475 * @return A string containing the bin folder's path, which must be freed when
476 * no longer necessary. If BinReloc is not initialized, or if the
477 * initialization function failed, then a copy of default_bin_dir will
478 * be returned. If default_bin_dir is NULL, then NULL will be returned.
479 */
480 gchar *
_gda_gbr_find_bin_dir(const gchar * default_bin_dir)481 _gda_gbr_find_bin_dir (const gchar *default_bin_dir)
482 {
483 gchar *prefix, *dir;
484
485 prefix = _gda_gbr_find_prefix (NULL);
486 if (prefix == NULL) {
487 /* BinReloc not initialized. */
488 if (default_bin_dir != NULL)
489 return g_strdup (default_bin_dir);
490 else
491 return NULL;
492 }
493
494 dir = g_build_filename (prefix, "bin", NULL);
495 g_free (prefix);
496 return dir;
497 }
498
499
500 /** Locate the application's superuser binary folder.
501 *
502 * The path is generated by the following pseudo-code evaluation:
503 * \code
504 * prefix + "/sbin"
505 * \endcode
506 *
507 * @param default_sbin_dir A default path which will used as fallback.
508 * @return A string containing the sbin folder's path, which must be freed when
509 * no longer necessary. If BinReloc is not initialized, or if the
510 * initialization function failed, then a copy of default_sbin_dir will
511 * be returned. If default_bin_dir is NULL, then NULL will be returned.
512 */
513 gchar *
_gda_gbr_find_sbin_dir(const gchar * default_sbin_dir)514 _gda_gbr_find_sbin_dir (const gchar *default_sbin_dir)
515 {
516 gchar *prefix, *dir;
517
518 prefix = _gda_gbr_find_prefix (NULL);
519 if (prefix == NULL) {
520 /* BinReloc not initialized. */
521 if (default_sbin_dir != NULL)
522 return g_strdup (default_sbin_dir);
523 else
524 return NULL;
525 }
526
527 dir = g_build_filename (prefix, "sbin", NULL);
528 g_free (prefix);
529 return dir;
530 }
531
532
533 /** Locate the application's data folder.
534 *
535 * The path is generated by the following pseudo-code evaluation:
536 * \code
537 * prefix + "/share"
538 * \endcode
539 *
540 * @param default_data_dir A default path which will used as fallback.
541 * @return A string containing the data folder's path, which must be freed when
542 * no longer necessary. If BinReloc is not initialized, or if the
543 * initialization function failed, then a copy of default_data_dir
544 * will be returned. If default_data_dir is NULL, then NULL will be
545 * returned.
546 */
547 gchar *
_gda_gbr_find_data_dir(const gchar * default_data_dir)548 _gda_gbr_find_data_dir (const gchar *default_data_dir)
549 {
550 gchar *prefix, *dir;
551
552 prefix = _gda_gbr_find_prefix (NULL);
553 if (prefix == NULL) {
554 /* BinReloc not initialized. */
555 if (default_data_dir != NULL)
556 return g_strdup (default_data_dir);
557 else
558 return NULL;
559 }
560
561 dir = g_build_filename (prefix, "share", NULL);
562 g_free (prefix);
563 return dir;
564 }
565
566
567 /** Locate the application's localization folder.
568 *
569 * The path is generated by the following pseudo-code evaluation:
570 * \code
571 * prefix + "/share/locale"
572 * \endcode
573 *
574 * @param default_locale_dir A default path which will used as fallback.
575 * @return A string containing the localization folder's path, which must be freed when
576 * no longer necessary. If BinReloc is not initialized, or if the
577 * initialization function failed, then a copy of default_locale_dir will be returned.
578 * If default_locale_dir is NULL, then NULL will be returned.
579 */
580 gchar *
_gda_gbr_find_locale_dir(const gchar * default_locale_dir)581 _gda_gbr_find_locale_dir (const gchar *default_locale_dir)
582 {
583 gchar *data_dir, *dir;
584
585 data_dir = _gda_gbr_find_data_dir (NULL);
586 if (data_dir == NULL) {
587 /* BinReloc not initialized. */
588 if (default_locale_dir != NULL)
589 return g_strdup (default_locale_dir);
590 else
591 return NULL;
592 }
593
594 dir = g_build_filename (data_dir, "locale", NULL);
595 g_free (data_dir);
596 return dir;
597 }
598
599
600 /** Locate the application's library folder.
601 *
602 * The path is generated by the following pseudo-code evaluation:
603 * \code
604 * prefix + "/lib"
605 * \endcode
606 *
607 * @param default_lib_dir A default path which will used as fallback.
608 * @return A string containing the library folder's path, which must be freed when
609 * no longer necessary. If BinReloc is not initialized, or if the
610 * initialization function failed, then a copy of default_lib_dir will be returned.
611 * If default_lib_dir is NULL, then NULL will be returned.
612 */
613 gchar *
_gda_gbr_find_lib_dir(const gchar * default_lib_dir)614 _gda_gbr_find_lib_dir (const gchar *default_lib_dir)
615 {
616 gchar *prefix, *dir;
617
618 prefix = _gda_gbr_find_prefix (NULL);
619 if (prefix == NULL) {
620 /* BinReloc not initialized. */
621 if (default_lib_dir != NULL)
622 return g_strdup (default_lib_dir);
623 else
624 return NULL;
625 }
626
627 dir = g_build_filename (prefix, "lib", NULL);
628 g_free (prefix);
629 return dir;
630 }
631
632
633 /** Locate the application's libexec folder.
634 *
635 * The path is generated by the following pseudo-code evaluation:
636 * \code
637 * prefix + "/libexec"
638 * \endcode
639 *
640 * @param default_libexec_dir A default path which will used as fallback.
641 * @return A string containing the libexec folder's path, which must be freed when
642 * no longer necessary. If BinReloc is not initialized, or if the initialization
643 * function failed, then a copy of default_libexec_dir will be returned.
644 * If default_libexec_dir is NULL, then NULL will be returned.
645 */
646 gchar *
_gda_gbr_find_libexec_dir(const gchar * default_libexec_dir)647 _gda_gbr_find_libexec_dir (const gchar *default_libexec_dir)
648 {
649 gchar *prefix, *dir;
650
651 prefix = _gda_gbr_find_prefix (NULL);
652 if (prefix == NULL) {
653 /* BinReloc not initialized. */
654 if (default_libexec_dir != NULL)
655 return g_strdup (default_libexec_dir);
656 else
657 return NULL;
658 }
659
660 dir = g_build_filename (prefix, "libexec", NULL);
661 g_free (prefix);
662 return dir;
663 }
664
665
666 /** Locate the application's configuration files folder.
667 *
668 * The path is generated by the following pseudo-code evaluation:
669 * \code
670 * prefix + "/etc"
671 * \endcode
672 *
673 * @param default_etc_dir A default path which will used as fallback.
674 * @return A string containing the etc folder's path, which must be freed when
675 * no longer necessary. If BinReloc is not initialized, or if the initialization
676 * function failed, then a copy of default_etc_dir will be returned.
677 * If default_etc_dir is NULL, then NULL will be returned.
678 */
679 gchar *
_gda_gbr_find_etc_dir(const gchar * default_etc_dir)680 _gda_gbr_find_etc_dir (const gchar *default_etc_dir)
681 {
682 gchar *prefix, *dir;
683
684 prefix = _gda_gbr_find_prefix (NULL);
685 if (prefix == NULL) {
686 /* BinReloc not initialized. */
687 if (default_etc_dir != NULL)
688 return g_strdup (default_etc_dir);
689 else
690 return NULL;
691 }
692
693 dir = g_build_filename (prefix, "etc", NULL);
694 g_free (prefix);
695 return dir;
696 }
697
698
699 G_END_DECLS
700
701 #endif /* __BINRELOC_C__ */
702