diff options
Diffstat (limited to 'src/os_installation.c')
-rw-r--r-- | src/os_installation.c | 455 |
1 files changed, 455 insertions, 0 deletions
diff --git a/src/os_installation.c b/src/os_installation.c new file mode 100644 index 00000000..4d9d86d3 --- /dev/null +++ b/src/os_installation.c | |||
@@ -0,0 +1,455 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2006, 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/os_installation.c | ||
23 | * @brief get paths used by the program; this code is almost the | ||
24 | * same as src/util/os_installation.c in libgnunetutil; however, | ||
25 | * it contains sublte changes to detect the installation path | ||
26 | * of gnunet-gtk (which may be different from the path for | ||
27 | * GNUnet itself) and also needs to be replicated anyway since | ||
28 | * some of the methods try to find the location of the binary | ||
29 | * of the test-code itself, which would never yield the | ||
30 | * correct result for gnunet-gtk if the code lives in libgnunetutil. | ||
31 | * @author Milan | ||
32 | * @author Christian Grothoff | ||
33 | */ | ||
34 | |||
35 | #ifdef __cplusplus | ||
36 | extern "C" | ||
37 | { | ||
38 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
39 | } | ||
40 | #endif | ||
41 | #endif | ||
42 | |||
43 | #include <sys/stat.h> | ||
44 | #include <stdlib.h> | ||
45 | #include <string.h> | ||
46 | #include <unistd.h> | ||
47 | |||
48 | #include "platform.h" | ||
49 | #include "gnunet_common.h" | ||
50 | #include "gnunet_configuration_lib.h" | ||
51 | #include "gnunet_disk_lib.h" | ||
52 | #include "gnunet_os_lib.h" | ||
53 | #if DARWIN | ||
54 | #include <mach-o/ldsyms.h> | ||
55 | #include <mach-o/dyld.h> | ||
56 | #endif | ||
57 | |||
58 | #if LINUX | ||
59 | /** | ||
60 | * Try to determine path by reading /proc/PID/exe | ||
61 | */ | ||
62 | static char * | ||
63 | get_path_from_proc_maps () | ||
64 | { | ||
65 | char fn[64]; | ||
66 | char line[1024]; | ||
67 | char dir[1024]; | ||
68 | FILE *f; | ||
69 | char *lgu; | ||
70 | |||
71 | GNUNET_snprintf (fn, | ||
72 | sizeof(fn), | ||
73 | "/proc/%u/maps", | ||
74 | getpid ()); | ||
75 | f = fopen (fn, "r"); | ||
76 | if (f == NULL) | ||
77 | return NULL; | ||
78 | while (NULL != fgets (line, sizeof(line), f)) | ||
79 | { | ||
80 | if ((1 == sscanf (line, | ||
81 | "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", | ||
82 | dir)) && | ||
83 | (NULL != (lgu = strstr (dir, "gnunet-gtk")))) | ||
84 | { | ||
85 | lgu[0] = '\0'; | ||
86 | fclose (f); | ||
87 | return GNUNET_strdup (dir); | ||
88 | } | ||
89 | } | ||
90 | fclose (f); | ||
91 | return NULL; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Try to determine path by reading /proc/PID/exe | ||
96 | */ | ||
97 | static char * | ||
98 | get_path_from_proc_exe () | ||
99 | { | ||
100 | char fn[64]; | ||
101 | char lnk[1024]; | ||
102 | ssize_t size; | ||
103 | |||
104 | GNUNET_snprintf (fn, | ||
105 | sizeof(fn), "/proc/%u/exe", getpid ()); | ||
106 | size = readlink (fn, lnk, sizeof (lnk)-1); | ||
107 | if (size <= 0) | ||
108 | { | ||
109 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); | ||
110 | return NULL; | ||
111 | } | ||
112 | GNUNET_assert (size < sizeof (lnk)); | ||
113 | lnk[size] = '\0'; | ||
114 | while ((lnk[size] != '/') && (size > 0)) | ||
115 | size--; | ||
116 | if ((size < 4) || (lnk[size - 4] != '/')) | ||
117 | { | ||
118 | /* not installed in "/bin/" -- binary path probably useless */ | ||
119 | return NULL; | ||
120 | } | ||
121 | lnk[size] = '\0'; | ||
122 | return GNUNET_strdup (lnk); | ||
123 | } | ||
124 | #endif | ||
125 | |||
126 | #if WINDOWS | ||
127 | /** | ||
128 | * Try to determine path with win32-specific function | ||
129 | */ | ||
130 | static char * | ||
131 | get_path_from_module_filename () | ||
132 | { | ||
133 | char path[4097]; | ||
134 | char *idx; | ||
135 | |||
136 | GetModuleFileName (NULL, path, sizeof(path)-1); | ||
137 | idx = path + strlen (path); | ||
138 | while ((idx > path) && (*idx != '\\') && (*idx != '/')) | ||
139 | idx--; | ||
140 | *idx = '\0'; | ||
141 | return GNUNET_strdup (path); | ||
142 | } | ||
143 | #endif | ||
144 | |||
145 | #if DARWIN | ||
146 | typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize); | ||
147 | |||
148 | static char * | ||
149 | get_path_from_NSGetExecutablePath () | ||
150 | { | ||
151 | static char zero = '\0'; | ||
152 | char *path; | ||
153 | size_t len; | ||
154 | MyNSGetExecutablePathProto func; | ||
155 | int ret; | ||
156 | |||
157 | path = NULL; | ||
158 | func = | ||
159 | (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); | ||
160 | if (!func) | ||
161 | return NULL; | ||
162 | path = &zero; | ||
163 | len = 0; | ||
164 | /* get the path len, including the trailing \0 */ | ||
165 | func (path, &len); | ||
166 | if (len == 0) | ||
167 | return NULL; | ||
168 | path = GNUNET_malloc (len); | ||
169 | ret = func (path, &len); | ||
170 | if (ret != 0) | ||
171 | { | ||
172 | GNUNET_free (path); | ||
173 | return NULL; | ||
174 | } | ||
175 | len = strlen (path); | ||
176 | while ((path[len] != '/') && (len > 0)) | ||
177 | len--; | ||
178 | path[len] = '\0'; | ||
179 | return path; | ||
180 | } | ||
181 | |||
182 | static char * | ||
183 | get_path_from_dyld_image () | ||
184 | { | ||
185 | const char *path; | ||
186 | char *p, *s; | ||
187 | int i; | ||
188 | int c; | ||
189 | |||
190 | p = NULL; | ||
191 | c = _dyld_image_count (); | ||
192 | for (i = 0; i < c; i++) | ||
193 | { | ||
194 | if (_dyld_get_image_header (i) == &_mh_dylib_header) | ||
195 | { | ||
196 | path = _dyld_get_image_name (i); | ||
197 | if (path != NULL && strlen (path) > 0) | ||
198 | { | ||
199 | p = strdup (path); | ||
200 | s = p + strlen (p); | ||
201 | while ((s > p) && (*s != '/')) | ||
202 | s--; | ||
203 | s++; | ||
204 | *s = '\0'; | ||
205 | } | ||
206 | break; | ||
207 | } | ||
208 | } | ||
209 | return p; | ||
210 | } | ||
211 | #endif | ||
212 | |||
213 | static char * | ||
214 | get_path_from_PATH () | ||
215 | { | ||
216 | char *path; | ||
217 | char *pos; | ||
218 | char *end; | ||
219 | char *buf; | ||
220 | const char *p; | ||
221 | |||
222 | p = getenv ("PATH"); | ||
223 | if (p == NULL) | ||
224 | return NULL; | ||
225 | path = GNUNET_strdup (p); /* because we write on it */ | ||
226 | buf = GNUNET_malloc (strlen (path) + 20); | ||
227 | pos = path; | ||
228 | |||
229 | while (NULL != (end = strchr (pos, ':'))) | ||
230 | { | ||
231 | *end = '\0'; | ||
232 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); | ||
233 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
234 | { | ||
235 | pos = GNUNET_strdup (pos); | ||
236 | GNUNET_free (buf); | ||
237 | GNUNET_free (path); | ||
238 | return pos; | ||
239 | } | ||
240 | pos = end + 1; | ||
241 | } | ||
242 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); | ||
243 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
244 | { | ||
245 | pos = GNUNET_strdup (pos); | ||
246 | GNUNET_free (buf); | ||
247 | GNUNET_free (path); | ||
248 | return pos; | ||
249 | } | ||
250 | GNUNET_free (buf); | ||
251 | GNUNET_free (path); | ||
252 | return NULL; | ||
253 | } | ||
254 | |||
255 | static char * | ||
256 | get_path_from_GNUNET_PREFIX () | ||
257 | { | ||
258 | const char *p; | ||
259 | |||
260 | p = getenv ("GNUNET_GTK_PREFIX"); | ||
261 | if (p != NULL) | ||
262 | return GNUNET_strdup (p); | ||
263 | p = getenv ("GNUNET_PREFIX"); | ||
264 | if (p != NULL) | ||
265 | return GNUNET_strdup (p); | ||
266 | return NULL; | ||
267 | } | ||
268 | |||
269 | /* | ||
270 | * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path | ||
271 | * @author Milan | ||
272 | * | ||
273 | * @return a pointer to the executable path, or NULL on error | ||
274 | */ | ||
275 | static char * | ||
276 | os_get_gnunet_path () | ||
277 | { | ||
278 | char *ret; | ||
279 | |||
280 | ret = get_path_from_GNUNET_PREFIX (); | ||
281 | if (ret != NULL) | ||
282 | return ret; | ||
283 | #if LINUX | ||
284 | ret = get_path_from_proc_maps (); | ||
285 | if (ret != NULL) | ||
286 | return ret; | ||
287 | ret = get_path_from_proc_exe (); | ||
288 | if (ret != NULL) | ||
289 | return ret; | ||
290 | #endif | ||
291 | #if WINDOWS | ||
292 | ret = get_path_from_module_filename (); | ||
293 | if (ret != NULL) | ||
294 | return ret; | ||
295 | #endif | ||
296 | #if DARWIN | ||
297 | ret = get_path_from_dyld_image (); | ||
298 | if (ret != NULL) | ||
299 | return ret; | ||
300 | ret = get_path_from_NSGetExecutablePath (); | ||
301 | if (ret != NULL) | ||
302 | return ret; | ||
303 | #endif | ||
304 | ret = get_path_from_PATH (); | ||
305 | if (ret != NULL) | ||
306 | return ret; | ||
307 | /* other attempts here */ | ||
308 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
309 | _ | ||
310 | ("Could not determine installation path for %s. Set `%s' environment variable.\n"), | ||
311 | "gnunet-gtk", | ||
312 | "GNUNET_GTK_PREFIX"); | ||
313 | return NULL; | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * @brief get the path to current app's bin/ | ||
318 | * @author Milan | ||
319 | * | ||
320 | * @return a pointer to the executable path, or NULL on error | ||
321 | */ | ||
322 | static char * | ||
323 | os_get_exec_path () | ||
324 | { | ||
325 | char *ret; | ||
326 | |||
327 | ret = NULL; | ||
328 | #if LINUX | ||
329 | ret = get_path_from_proc_exe (); | ||
330 | if (ret != NULL) | ||
331 | return ret; | ||
332 | #endif | ||
333 | #if WINDOWS | ||
334 | ret = get_path_from_module_filename (); | ||
335 | if (ret != NULL) | ||
336 | return ret; | ||
337 | #endif | ||
338 | #if DARWIN | ||
339 | ret = get_path_from_NSGetExecutablePath (); | ||
340 | if (ret != NULL) | ||
341 | return ret; | ||
342 | #endif | ||
343 | /* other attempts here */ | ||
344 | return ret; | ||
345 | } | ||
346 | |||
347 | |||
348 | |||
349 | /** | ||
350 | * @brief get the path to a specific GNUnet installation directory or, | ||
351 | * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory | ||
352 | * @author Milan | ||
353 | * @return a pointer to the dir path (to be freed by the caller) | ||
354 | */ | ||
355 | char * | ||
356 | GNUNET_GTK_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) | ||
357 | { | ||
358 | size_t n; | ||
359 | const char *dirname; | ||
360 | char *execpath = NULL; | ||
361 | char *tmp; | ||
362 | int isbasedir; | ||
363 | |||
364 | /* if wanted, try to get the current app's bin/ */ | ||
365 | if (dirkind == GNUNET_OS_IPK_SELF_PREFIX) | ||
366 | execpath = os_get_exec_path (); | ||
367 | |||
368 | /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some | ||
369 | * guess for the current app */ | ||
370 | if (execpath == NULL) | ||
371 | execpath = os_get_gnunet_path (); | ||
372 | |||
373 | if (execpath == NULL) | ||
374 | return NULL; | ||
375 | |||
376 | n = strlen (execpath); | ||
377 | if (n == 0) | ||
378 | { | ||
379 | /* should never happen, but better safe than sorry */ | ||
380 | GNUNET_free (execpath); | ||
381 | return NULL; | ||
382 | } | ||
383 | /* remove filename itself */ | ||
384 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) | ||
385 | execpath[--n] = '\0'; | ||
386 | |||
387 | isbasedir = 1; | ||
388 | if ((n > 5) && | ||
389 | ((0 == strcasecmp (&execpath[n - 5], "lib32")) || | ||
390 | (0 == strcasecmp (&execpath[n - 5], "lib64")))) | ||
391 | { | ||
392 | if (dirkind != GNUNET_OS_IPK_LIBDIR) | ||
393 | { | ||
394 | /* strip '/lib32' or '/lib64' */ | ||
395 | execpath[n - 5] = '\0'; | ||
396 | n -= 5; | ||
397 | } | ||
398 | else | ||
399 | isbasedir = 0; | ||
400 | } | ||
401 | else if ((n > 3) && | ||
402 | ((0 == strcasecmp (&execpath[n - 3], "bin")) || | ||
403 | (0 == strcasecmp (&execpath[n - 3], "lib")))) | ||
404 | { | ||
405 | /* strip '/bin' or '/lib' */ | ||
406 | execpath[n - 3] = '\0'; | ||
407 | n -= 3; | ||
408 | } | ||
409 | /* in case this was a directory named foo-bin, remove "foo-" */ | ||
410 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) | ||
411 | execpath[--n] = '\0'; | ||
412 | switch (dirkind) | ||
413 | { | ||
414 | case GNUNET_OS_IPK_PREFIX: | ||
415 | case GNUNET_OS_IPK_SELF_PREFIX: | ||
416 | dirname = DIR_SEPARATOR_STR; | ||
417 | break; | ||
418 | case GNUNET_OS_IPK_BINDIR: | ||
419 | dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; | ||
420 | break; | ||
421 | case GNUNET_OS_IPK_LIBDIR: | ||
422 | if (isbasedir) | ||
423 | dirname = | ||
424 | DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet-gtk" | ||
425 | DIR_SEPARATOR_STR; | ||
426 | else | ||
427 | dirname = DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR; | ||
428 | break; | ||
429 | case GNUNET_OS_IPK_DATADIR: | ||
430 | dirname = | ||
431 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet-gtk" | ||
432 | DIR_SEPARATOR_STR; | ||
433 | break; | ||
434 | case GNUNET_OS_IPK_LOCALEDIR: | ||
435 | dirname = | ||
436 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" | ||
437 | DIR_SEPARATOR_STR; | ||
438 | break; | ||
439 | default: | ||
440 | GNUNET_free (execpath); | ||
441 | return NULL; | ||
442 | } | ||
443 | tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); | ||
444 | sprintf (tmp, "%s%s", execpath, dirname); | ||
445 | GNUNET_free (execpath); | ||
446 | return tmp; | ||
447 | } | ||
448 | |||
449 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
450 | { | ||
451 | #endif | ||
452 | #ifdef __cplusplus | ||
453 | } | ||
454 | #endif | ||
455 | /* end of os_installation.c */ | ||