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
32 #include "gnunet_common.h"
33 #include "gnunet_configuration_lib.h"
34 #include "gnunet_disk_lib.h"
35 #include "gnunet_os_lib.h"
36 #include "gnunet_strings_lib.h"
38 #include <mach-o/ldsyms.h>
39 #include <mach-o/dyld.h>
42 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
44 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
48 * Try to determine path by reading /proc/PID/exe
50 * @return NULL on error
53 get_path_from_proc_maps ()
61 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
62 if (NULL == (f = FOPEN (fn, "r")))
64 while (NULL != fgets (line, sizeof (line), f))
67 SSCANF (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
68 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
72 return GNUNET_strdup (dir);
81 * Try to determine path by reading /proc/PID/exe
83 * @return NULL on error
86 get_path_from_proc_exe ()
92 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
93 size = readlink (fn, lnk, sizeof (lnk) - 1);
96 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
99 GNUNET_assert (size < sizeof (lnk));
101 while ((lnk[size] != '/') && (size > 0))
103 if ((size < 4) || (lnk[size - 4] != '/'))
105 /* not installed in "/bin/" -- binary path probably useless */
109 return GNUNET_strdup (lnk);
115 * Try to determine path with win32-specific function
117 * @return NULL on error
120 get_path_from_module_filename ()
126 GetModuleFileNameW (NULL, path, sizeof (path) - 1);
127 idx = path + wcslen (path);
128 while ((idx > path) && (*idx != L'\\') && (*idx != L'/'))
132 WideCharToMultiByte (CP_UTF8, 0, path, -1, upath, 4097, NULL, NULL);
134 return GNUNET_strdup (upath);
140 * Signature of the '_NSGetExecutablePath" function.
142 * @param buf where to write the path
143 * @param number of bytes available in 'buf'
144 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
146 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
150 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
152 * @return NULL on error
155 get_path_from_NSGetExecutablePath ()
157 static char zero = '\0';
160 MyNSGetExecutablePathProto func;
164 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath")))
168 /* get the path len, including the trailing \0 */
169 (void) func (path, &len);
172 path = GNUNET_malloc (len);
173 if (0 != func (path, &len))
179 while ((path[len] != '/') && (len > 0))
187 * Try to obtain the path of our executable using '_dyld_image' API.
189 * @return NULL on error
192 get_path_from_dyld_image ()
200 c = _dyld_image_count ();
201 for (i = 0; i < c; i++)
203 if (_dyld_get_image_header (i) != &_mh_dylib_header)
205 path = _dyld_get_image_name (i);
206 if ( (NULL == path) || (0 == strlen (path)) )
208 p = GNUNET_strdup (path);
210 while ((s > p) && ('/' != *s))
222 * Return the actual path to a file found in the current
223 * PATH environment variable.
225 * @param binary the name of the file to find
226 * @return path to binary, NULL if not found
229 get_path_from_PATH (const char *binary)
237 if (NULL == (p = getenv ("PATH")))
240 /* On W32 look in CWD first. */
241 GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p);
243 path = GNUNET_strdup (p); /* because we write on it */
245 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
247 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
250 sprintf (buf, "%s/%s", pos, binary);
251 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
253 pos = GNUNET_strdup (pos);
260 sprintf (buf, "%s/%s", pos, binary);
261 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
263 pos = GNUNET_strdup (pos);
275 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
278 * @return NULL on error (environment variable not set)
281 get_path_from_GNUNET_PREFIX ()
285 if (NULL != (p = getenv ("GNUNET_PREFIX")))
286 return GNUNET_strdup (p);
292 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
295 * @return a pointer to the executable path, or NULL on error
298 os_get_gnunet_path ()
302 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
305 if (NULL != (ret = get_path_from_proc_maps ()))
307 if (NULL != (ret = get_path_from_proc_exe ()))
311 if (NULL != (ret = get_path_from_module_filename ()))
315 if (NULL != (ret = get_path_from_dyld_image ()))
317 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
320 if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
322 /* other attempts here */
323 LOG (GNUNET_ERROR_TYPE_ERROR,
325 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
326 "GNUnet", "GNUNET_PREFIX");
332 * @brief get the path to current app's bin/
335 * @return a pointer to the executable path, or NULL on error
343 if (NULL != (ret = get_path_from_proc_exe ()))
347 if (NULL != (ret = get_path_from_module_filename ()))
351 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
354 /* other attempts here */
360 * @brief get the path to a specific GNUnet installation directory or,
361 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
363 * @return a pointer to the dir path (to be freed by the caller)
366 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
370 char *execpath = NULL;
374 /* if wanted, try to get the current app's bin/ */
375 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
376 execpath = os_get_exec_path ();
378 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
379 * guess for the current app */
380 if (NULL == execpath)
381 execpath = os_get_gnunet_path ();
383 if (NULL == execpath)
386 n = strlen (execpath);
389 /* should never happen, but better safe than sorry */
390 GNUNET_free (execpath);
393 /* remove filename itself */
394 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
395 execpath[--n] = '\0';
399 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
400 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
402 if (GNUNET_OS_IPK_LIBDIR != dirkind)
404 /* strip '/lib32' or '/lib64' */
405 execpath[n - 5] = '\0';
412 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
413 (0 == strcasecmp (&execpath[n - 3], "lib"))))
415 /* strip '/bin' or '/lib' */
416 execpath[n - 3] = '\0';
419 /* in case this was a directory named foo-bin, remove "foo-" */
420 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
421 execpath[--n] = '\0';
424 case GNUNET_OS_IPK_PREFIX:
425 case GNUNET_OS_IPK_SELF_PREFIX:
426 dirname = DIR_SEPARATOR_STR;
428 case GNUNET_OS_IPK_BINDIR:
429 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
431 case GNUNET_OS_IPK_LIBDIR:
434 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
436 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
438 case GNUNET_OS_IPK_DATADIR:
440 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
442 case GNUNET_OS_IPK_LOCALEDIR:
444 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
446 case GNUNET_OS_IPK_ICONDIR:
448 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
450 case GNUNET_OS_IPK_DOCDIR:
452 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
453 "gnunet" DIR_SEPARATOR_STR;
456 GNUNET_free (execpath);
459 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
460 sprintf (tmp, "%s%s", execpath, dirname);
461 GNUNET_free (execpath);
467 * Check whether an executable exists and possibly
468 * if the suid bit is set on the file.
469 * Attempts to find the file using the current
470 * PATH environment variable as a search path.
472 * @param binary the name of the file to check.
473 * W32: must not have an .exe suffix.
474 * @return GNUNET_YES if the file is SUID,
475 * GNUNET_NO if not SUID (but binary exists)
476 * GNUNET_SYSERR on error (no such binary or not executable)
479 GNUNET_OS_check_helper_binary (const char *binary)
488 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
489 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binaryexe, GNUNET_NO,
491 (0 == strncmp (binary, "./", 2)) )
492 p = GNUNET_strdup (binaryexe);
495 p = get_path_from_PATH (binaryexe);
498 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
503 GNUNET_free (binaryexe);
505 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO,
507 (0 == strncmp (binary, "./", 2)) )
508 p = GNUNET_strdup (binary);
511 p = get_path_from_PATH (binary);
514 GNUNET_asprintf (&pf, "%s/%s", p, binary);
522 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
524 return GNUNET_SYSERR;
526 if (0 != ACCESS (p, X_OK))
528 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
530 return GNUNET_SYSERR;
535 /* as we run as root, we don't insist on SUID */
540 if (0 != STAT (p, &statbuf))
542 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
544 return GNUNET_SYSERR;
547 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
552 /* binary exists, but not SUID */
558 static int once; /* remember result from previous runs... */
562 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
563 if (INVALID_SOCKET == rawsock)
565 DWORD err = GetLastError ();
567 LOG (GNUNET_ERROR_TYPE_DEBUG,
568 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
570 return GNUNET_NO; /* not running as administrator */
573 closesocket (rawsock);
583 /* end of os_installation.c */