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 ()
70 while (NULL != fgets (line, sizeof(line), f))
72 if ((1 == sscanf (line,
73 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
75 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
79 return GNUNET_strdup (dir);
87 * Try to determine path by reading /proc/PID/exe
90 get_path_from_proc_exe ()
97 sizeof(fn), "/proc/%u/exe", getpid ());
98 size = readlink (fn, lnk, sizeof (lnk)-1);
99 if ((size == 0) || (size >= sizeof(lnk)-1))
101 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
105 while ((lnk[size] != '/') && (size > 0))
107 if ((size < 4) || (lnk[size - 4] != '/'))
109 /* not installed in "/bin/" -- binary path probably useless */
113 return GNUNET_strdup (lnk);
119 * Try to determine path with win32-specific function
122 get_path_from_module_filename ()
127 GetModuleFileName (NULL, path, sizeof(path)-1);
128 idx = path + strlen (path);
129 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
132 return GNUNET_strdup (path);
137 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
140 get_path_from_NSGetExecutablePath ()
142 static char zero = '\0';
145 MyNSGetExecutablePathProto func;
150 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
155 /* get the path len, including the trailing \0 */
159 path = GNUNET_malloc (len);
160 ret = func (path, &len);
167 while ((path[len] != '/') && (len > 0))
174 get_path_from_dyld_image ()
182 c = _dyld_image_count ();
183 for (i = 0; i < c; i++)
185 if (_dyld_get_image_header (i) == &_mh_dylib_header)
187 path = _dyld_get_image_name (i);
188 if (path != NULL && strlen (path) > 0)
192 while ((s > p) && (*s != '/'))
205 get_path_from_PATH ()
216 path = GNUNET_strdup (p); /* because we write on it */
217 buf = GNUNET_malloc (strlen (path) + 20);
220 while (NULL != (end = strchr (pos, ':')))
223 sprintf (buf, "%s/%s", pos, "gnunetd");
224 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
226 pos = GNUNET_strdup (pos);
233 sprintf (buf, "%s/%s", pos, "gnunetd");
234 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
236 pos = GNUNET_strdup (pos);
247 get_path_from_GNUNET_PREFIX ()
251 p = getenv ("GNUNET_PREFIX");
253 return GNUNET_strdup (p);
258 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
261 * @return a pointer to the executable path, or NULL on error
264 os_get_gnunet_path ()
268 ret = get_path_from_GNUNET_PREFIX ();
272 ret = get_path_from_proc_maps ();
275 ret = get_path_from_proc_exe ();
280 ret = get_path_from_module_filename ();
285 ret = get_path_from_dyld_image ();
288 ret = get_path_from_NSGetExecutablePath ();
292 ret = get_path_from_PATH ();
295 /* other attempts here */
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298 ("Could not determine installation path for GNUnet. Set `%s' environment variable.\n"),
304 * @brief get the path to current app's bin/
307 * @return a pointer to the executable path, or NULL on error
316 ret = get_path_from_proc_exe ();
321 ret = get_path_from_module_filename ();
326 ret = get_path_from_NSGetExecutablePath ();
330 /* other attempts here */
337 * @brief get the path to a specific GNUnet installation directory or,
338 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
340 * @return a pointer to the dir path (to be freed by the caller)
343 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
347 char *execpath = NULL;
351 /* if wanted, try to get the current app's bin/ */
352 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
353 execpath = os_get_exec_path ();
355 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
356 * guess for the current app */
357 if (execpath == NULL)
358 execpath = os_get_gnunet_path ();
360 if (execpath == NULL)
363 n = strlen (execpath);
366 /* should never happen, but better safe than sorry */
367 GNUNET_free (execpath);
370 /* remove filename itself */
371 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
372 execpath[--n] = '\0';
376 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
377 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
379 if (dirkind != GNUNET_OS_IPK_LIBDIR)
381 /* strip '/lib32' or '/lib64' */
382 execpath[n - 5] = '\0';
389 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
390 (0 == strcasecmp (&execpath[n - 3], "lib"))))
392 /* strip '/bin' or '/lib' */
393 execpath[n - 3] = '\0';
396 /* in case this was a directory named foo-bin, remove "foo-" */
397 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
398 execpath[--n] = '\0';
401 case GNUNET_OS_IPK_PREFIX:
402 case GNUNET_OS_IPK_SELF_PREFIX:
403 dirname = DIR_SEPARATOR_STR;
405 case GNUNET_OS_IPK_BINDIR:
406 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
408 case GNUNET_OS_IPK_LIBDIR:
411 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
414 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
416 case GNUNET_OS_IPK_DATADIR:
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
421 case GNUNET_OS_IPK_LOCALEDIR:
423 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
427 GNUNET_free (execpath);
430 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
431 sprintf (tmp, "%s%s", execpath, dirname);
432 GNUNET_free (execpath);
436 #if 0 /* keep Emacsens' auto-indent happy */
442 /* end of os_installation.c */