2 This file is part of GNUnet.
3 (C) 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file src/util/os_installation.c
23 * @brief get paths used by the program
30 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_common.h"
42 #include "gnunet_configuration_lib.h"
43 #include "gnunet_disk_lib.h"
44 #include "gnunet_os_lib.h"
46 #include <mach-o/ldsyms.h>
47 #include <mach-o/dyld.h>
52 * Try to determine path by reading /proc/PID/exe
55 get_path_from_proc_maps ()
62 GNUNET_snprintf (fn, 64, "/proc/%u/maps", getpid ());
63 line = GNUNET_malloc (1024);
64 dir = GNUNET_malloc (1024);
68 while (NULL != fgets (line, 1024, f))
70 if ((1 == sscanf (line,
71 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
72 dir)) && (NULL != strstr (dir, "libgnunetutil")))
74 strstr (dir, "libgnunetutil")[0] = '\0';
88 * Try to determine path by reading /proc/PID/exe
91 get_path_from_proc_exe ()
97 GNUNET_snprintf (fn, 64, "/proc/%u/exe", getpid ());
98 lnk = GNUNET_malloc (1024);
99 size = readlink (fn, lnk, 1023);
100 if ((size == 0) || (size >= 1024))
102 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
107 while ((lnk[size] != '/') && (size > 0))
109 if ((size < 4) || (lnk[size - 4] != '/'))
111 /* not installed in "/bin/" -- binary path probably useless */
122 * Try to determine path with win32-specific function
125 get_path_from_module_filename ()
130 path = GNUNET_malloc (4097);
131 GetModuleFileName (NULL, path, 4096);
132 idx = path + strlen (path);
133 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
141 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
144 get_path_from_NSGetExecutablePath ()
146 static char zero = '\0';
149 MyNSGetExecutablePathProto func;
154 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
159 /* get the path len, including the trailing \0 */
163 path = GNUNET_malloc (len);
164 ret = func (path, &len);
171 while ((path[len] != '/') && (len > 0))
178 get_path_from_dyld_image ()
186 c = _dyld_image_count ();
187 for (i = 0; i < c; i++)
189 if (_dyld_get_image_header (i) == &_mh_dylib_header)
191 path = _dyld_get_image_name (i);
192 if (path != NULL && strlen (path) > 0)
196 while ((s > p) && (*s != '/'))
209 get_path_from_PATH ()
220 path = GNUNET_strdup (p); /* because we write on it */
221 buf = GNUNET_malloc (strlen (path) + 20);
224 while (NULL != (end = strchr (pos, ':')))
227 sprintf (buf, "%s/%s", pos, "gnunetd");
228 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
230 pos = GNUNET_strdup (pos);
237 sprintf (buf, "%s/%s", pos, "gnunetd");
238 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
240 pos = GNUNET_strdup (pos);
251 get_path_from_GNUNET_PREFIX ()
255 p = getenv ("GNUNET_PREFIX");
257 return GNUNET_strdup (p);
262 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
265 * @return a pointer to the executable path, or NULL on error
268 os_get_gnunet_path ()
272 ret = get_path_from_GNUNET_PREFIX ();
276 ret = get_path_from_proc_maps ();
279 ret = get_path_from_proc_exe ();
284 ret = get_path_from_module_filename ();
289 ret = get_path_from_dyld_image ();
292 ret = get_path_from_NSGetExecutablePath ();
296 ret = get_path_from_PATH ();
299 /* other attempts here */
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 ("Could not determine installation path for GNUnet. Set `%s' environment variable.\n"),
308 * @brief get the path to current app's bin/
311 * @return a pointer to the executable path, or NULL on error
320 ret = get_path_from_proc_exe ();
325 ret = get_path_from_module_filename ();
330 ret = get_path_from_NSGetExecutablePath ();
334 /* other attempts here */
341 * @brief get the path to a specific GNUnet installation directory or,
342 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
344 * @return a pointer to the dir path (to be freed by the caller)
347 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
351 char *execpath = NULL;
355 /* if wanted, try to get the current app's bin/ */
356 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
357 execpath = os_get_exec_path ();
359 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
360 * guess for the current app */
361 if (execpath == NULL)
362 execpath = os_get_gnunet_path ();
364 if (execpath == NULL)
367 n = strlen (execpath);
370 /* should never happen, but better safe than sorry */
371 GNUNET_free (execpath);
374 /* remove filename itself */
375 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
376 execpath[--n] = '\0';
380 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
381 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
383 if (dirkind != GNUNET_OS_IPK_LIBDIR)
385 /* strip '/lib32' or '/lib64' */
386 execpath[n - 5] = '\0';
393 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
394 (0 == strcasecmp (&execpath[n - 3], "lib"))))
396 /* strip '/bin' or '/lib' */
397 execpath[n - 3] = '\0';
400 /* in case this was a directory named foo-bin, remove "foo-" */
401 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
402 execpath[--n] = '\0';
405 case GNUNET_OS_IPK_PREFIX:
406 case GNUNET_OS_IPK_SELF_PREFIX:
407 dirname = DIR_SEPARATOR_STR;
409 case GNUNET_OS_IPK_BINDIR:
410 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
412 case GNUNET_OS_IPK_LIBDIR:
415 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
418 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
420 case GNUNET_OS_IPK_DATADIR:
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
425 case GNUNET_OS_IPK_LOCALEDIR:
427 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
431 GNUNET_free (execpath);
434 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
435 sprintf (tmp, "%s%s", execpath, dirname);
436 GNUNET_free (execpath);
440 #if 0 /* keep Emacsens' auto-indent happy */
446 /* end of os_installation.c */