-
Notifications
You must be signed in to change notification settings - Fork 5
/
glib.c
424 lines (374 loc) · 11.5 KB
/
glib.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
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
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
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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
static gchar *exe = NULL;
static void set_gerror (GError **error, GbrInitError errcode);
/** Initialize the BinReloc library (for applications).
*
* This function must be called before using any other BinReloc functions.
* It attempts to locate the application's canonical filename.
*
* @note If you want to use BinReloc for a library, then you should call
* gbr_init_lib() instead.
* @note Initialization failure is not fatal. BinReloc functions will just
* fallback to the supplied default path.
*
* @param error If BinReloc failed to initialize, then the error report will
* be stored in this variable. Set to NULL if you don't want an
* error report. See the #GbrInitError for a list of error
* codes.
*
* @returns TRUE on success, FALSE if BinReloc failed to initialize.
*/
gboolean
gbr_init (GError **error)
{
GbrInitError errcode = 0;
/* Shut up compiler warning about uninitialized variable. */
errcode = GBR_INIT_ERROR_NOMEM;
/* Locate the application's filename. */
exe = _br_find_exe (&errcode);
if (exe != NULL)
/* Success! */
return TRUE;
else {
/* Failed :-( */
set_gerror (error, errcode);
return FALSE;
}
}
/** Initialize the BinReloc library (for libraries).
*
* This function must be called before using any other BinReloc functions.
* It attempts to locate the calling library's canonical filename.
*
* @note The BinReloc source code MUST be included in your library, or this
* function won't work correctly.
* @note Initialization failure is not fatal. BinReloc functions will just
* fallback to the supplied default path.
*
* @returns TRUE on success, FALSE if a filename cannot be found.
*/
gboolean
gbr_init_lib (GError **error)
{
GbrInitError errcode = 0;
/* Shut up compiler warning about uninitialized variable. */
errcode = GBR_INIT_ERROR_NOMEM;
exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
if (exe != NULL)
/* Success! */
return TRUE;
else {
/* Failed :-( */
set_gerror (error, errcode);
return exe != NULL;
}
}
static void
set_gerror (GError **error, GbrInitError errcode)
{
gchar *error_message;
if (error == NULL)
return;
switch (errcode) {
case GBR_INIT_ERROR_NOMEM:
error_message = "Cannot allocate memory.";
break;
case GBR_INIT_ERROR_OPEN_MAPS:
error_message = "Unable to open /proc/self/maps for reading.";
break;
case GBR_INIT_ERROR_READ_MAPS:
error_message = "Unable to read from /proc/self/maps.";
break;
case GBR_INIT_ERROR_INVALID_MAPS:
error_message = "The file format of /proc/self/maps is invalid.";
break;
case GBR_INIT_ERROR_DISABLED:
error_message = "Binary relocation support is disabled.";
break;
default:
error_message = "Unknown error.";
break;
};
g_set_error (error, g_quark_from_static_string ("GBinReloc"),
errcode, "%s", error_message);
}
/** Find the canonical filename of the current application.
*
* @param default_exe A default filename which will be used as fallback.
* @returns A string containing the application's canonical filename,
* which must be freed when no longer necessary. If BinReloc is
* not initialized, or if the initialization function failed,
* then a copy of default_exe will be returned. If default_exe
* is NULL, then NULL will be returned.
*/
gchar *
gbr_find_exe (const gchar *default_exe)
{
if (exe == NULL) {
/* BinReloc is not initialized. */
if (default_exe != NULL)
return g_strdup (default_exe);
else
return NULL;
}
return g_strdup (exe);
}
/** Locate the directory in which the current application is installed.
*
* The prefix is generated by the following pseudo-code evaluation:
* \code
* dirname(exename)
* \endcode
*
* @param default_dir A default directory which will used as fallback.
* @return A string containing the directory, which must be freed when no
* longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_dir
* will be returned. If default_dir is NULL, then NULL will be
* returned.
*/
gchar *
gbr_find_exe_dir (const gchar *default_dir)
{
if (exe == NULL) {
/* BinReloc not initialized. */
if (default_dir != NULL)
return g_strdup (default_dir);
else
return NULL;
}
return g_path_get_dirname (exe);
}
/** Locate the prefix in which the current application is installed.
*
* The prefix is generated by the following pseudo-code evaluation:
* \code
* dirname(dirname(exename))
* \endcode
*
* @param default_prefix A default prefix which will used as fallback.
* @return A string containing the prefix, which must be freed when no
* longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_prefix
* will be returned. If default_prefix is NULL, then NULL will be
* returned.
*/
gchar *
gbr_find_prefix (const gchar *default_prefix)
{
gchar *dir1, *dir2;
if (exe == NULL) {
/* BinReloc not initialized. */
if (default_prefix != NULL)
return g_strdup (default_prefix);
else
return NULL;
}
dir1 = g_path_get_dirname (exe);
dir2 = g_path_get_dirname (dir1);
g_free (dir1);
return dir2;
}
/** Locate the application's binary folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/bin"
* \endcode
*
* @param default_bin_dir A default path which will used as fallback.
* @return A string containing the bin folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_bin_dir will
* be returned. If default_bin_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_bin_dir (const gchar *default_bin_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_bin_dir != NULL)
return g_strdup (default_bin_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "bin", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's superuser binary folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/sbin"
* \endcode
*
* @param default_sbin_dir A default path which will used as fallback.
* @return A string containing the sbin folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_sbin_dir will
* be returned. If default_bin_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_sbin_dir (const gchar *default_sbin_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_sbin_dir != NULL)
return g_strdup (default_sbin_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "sbin", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's data folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/share"
* \endcode
*
* @param default_data_dir A default path which will used as fallback.
* @return A string containing the data folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_data_dir
* will be returned. If default_data_dir is NULL, then NULL will be
* returned.
*/
gchar *
gbr_find_data_dir (const gchar *default_data_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_data_dir != NULL)
return g_strdup (default_data_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "share", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's localization folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/share/locale"
* \endcode
*
* @param default_locale_dir A default path which will used as fallback.
* @return A string containing the localization folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_locale_dir will be returned.
* If default_locale_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_locale_dir (const gchar *default_locale_dir)
{
gchar *data_dir, *dir;
data_dir = gbr_find_data_dir (NULL);
if (data_dir == NULL) {
/* BinReloc not initialized. */
if (default_locale_dir != NULL)
return g_strdup (default_locale_dir);
else
return NULL;
}
dir = g_build_filename (data_dir, "locale", NULL);
g_free (data_dir);
return dir;
}
/** Locate the application's library folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/lib"
* \endcode
*
* @param default_lib_dir A default path which will used as fallback.
* @return A string containing the library folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_lib_dir will be returned.
* If default_lib_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_lib_dir (const gchar *default_lib_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_lib_dir != NULL)
return g_strdup (default_lib_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "lib", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's libexec folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/libexec"
* \endcode
*
* @param default_libexec_dir A default path which will used as fallback.
* @return A string containing the libexec folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the initialization
* function failed, then a copy of default_libexec_dir will be returned.
* If default_libexec_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_libexec_dir (const gchar *default_libexec_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_libexec_dir != NULL)
return g_strdup (default_libexec_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "libexec", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's configuration files folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/etc"
* \endcode
*
* @param default_etc_dir A default path which will used as fallback.
* @return A string containing the etc folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the initialization
* function failed, then a copy of default_etc_dir will be returned.
* If default_etc_dir is NULL, then NULL will be returned.
*/
gchar *
gbr_find_etc_dir (const gchar *default_etc_dir)
{
gchar *prefix, *dir;
prefix = gbr_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_etc_dir != NULL)
return g_strdup (default_etc_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "etc", NULL);
g_free (prefix);
return dir;
}