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 #include <unistr.h> /* for u16_to_u8 */
33 #include "gnunet_common.h"
34 #include "gnunet_configuration_lib.h"
35 #include "gnunet_disk_lib.h"
36 #include "gnunet_os_lib.h"
37 #include "gnunet_strings_lib.h"
39 #include <mach-o/ldsyms.h>
40 #include <mach-o/dyld.h>
43 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
45 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
49 * Try to determine path by reading /proc/PID/exe
51 * @return NULL on error
54 get_path_from_proc_maps ()
62 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
63 if (NULL == (f = FOPEN (fn, "r")))
65 while (NULL != fgets (line, sizeof (line), f))
68 SSCANF (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%1023s", dir)) &&
69 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
73 return GNUNET_strdup (dir);
82 * Try to determine path by reading /proc/PID/exe
84 * @return NULL on error
87 get_path_from_proc_exe ()
93 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
94 size = readlink (fn, lnk, sizeof (lnk) - 1);
97 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
100 GNUNET_assert (size < sizeof (lnk));
102 while ((lnk[size] != '/') && (size > 0))
104 /* test for being in lib/gnunet/libexec/ */
105 if ( (size > strlen ("/gnunet/libexec/")) &&
106 (0 == strcmp ("/gnunet/libexec/",
107 &lnk[size - strlen ("/gnunet/libexec/")])) )
108 size -= strlen ("gnunet/libexec/");
109 if ((size < 4) || (lnk[size - 4] != '/'))
111 /* not installed in "/bin/" -- binary path probably useless */
115 return GNUNET_strdup (lnk);
121 static HINSTANCE dll_instance;
124 /* GNUNET_util_cl_init() in common_logging.c is preferred.
125 * This function is only for thread-local storage (not used in GNUnet)
126 * and hInstance saving.
129 DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
133 case DLL_PROCESS_ATTACH:
134 dll_instance = hinstDLL;
136 case DLL_THREAD_ATTACH:
138 case DLL_THREAD_DETACH:
140 case DLL_PROCESS_DETACH:
148 * Try to determine path with win32-specific function
150 * @return NULL on error
153 get_path_from_module_filename ()
155 size_t pathlen = 512;
158 wchar_t *modulepath = NULL;
161 size_t u8_string_length;
163 /* This braindead function won't tell us how much space it needs, so
164 * we start at 1024 and double the space up if it doesn't fit, until
165 * it fits, or we exceed the threshold.
169 pathlen = pathlen * 2;
170 modulepath = GNUNET_realloc (modulepath, pathlen * sizeof (wchar_t));
172 real_pathlen = GetModuleFileNameW (dll_instance, modulepath, pathlen * sizeof (wchar_t));
173 } while (real_pathlen >= pathlen && pathlen < 16*1024);
174 if (real_pathlen >= pathlen)
177 modulepath[real_pathlen] = '\0';
179 idx = modulepath + real_pathlen;
180 while ((idx > modulepath) && (*idx != L'\\') && (*idx != L'/'))
184 /* Now modulepath holds full path to the directory where libgnunetutil is.
185 * This directory should look like <GNUNET_PREFIX>/bin or <GNUNET_PREFIX>.
187 if (wcschr (modulepath, L'/') || wcschr (modulepath, L'\\'))
189 /* At least one directory component (i.e. we're not in a root directory) */
190 wchar_t *dirname = idx;
191 while ((dirname > modulepath) && (*dirname != L'\\') && (*dirname != L'/'))
194 if (dirname > modulepath)
197 /* Now modulepath holds full path to the parent directory of the directory
198 * where libgnunetutil is.
199 * dirname holds the name of the directory where libgnunetutil is.
201 if (wcsicmp (dirname, L"bin") == 0)
207 /* Roll back our changes to modulepath */
214 /* modulepath is GNUNET_PREFIX */
215 u8_string = u16_to_u8 (modulepath, wcslen (modulepath), NULL, &u8_string_length);
216 if (NULL == u8_string)
219 upath = GNUNET_malloc (u8_string_length + 1);
220 memcpy (upath, u8_string, u8_string_length);
221 upath[u8_string_length] = '\0';
224 GNUNET_free (modulepath);
232 * Signature of the '_NSGetExecutablePath" function.
234 * @param buf where to write the path
235 * @param number of bytes available in 'buf'
236 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
238 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
242 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
244 * @return NULL on error
247 get_path_from_NSGetExecutablePath ()
249 static char zero = '\0';
252 MyNSGetExecutablePathProto func;
256 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath")))
260 /* get the path len, including the trailing \0 */
261 (void) func (path, &len);
264 path = GNUNET_malloc (len);
265 if (0 != func (path, &len))
271 while ((path[len] != '/') && (len > 0))
279 * Try to obtain the path of our executable using '_dyld_image' API.
281 * @return NULL on error
284 get_path_from_dyld_image ()
292 c = _dyld_image_count ();
293 for (i = 0; i < c; i++)
295 if (_dyld_get_image_header (i) != &_mh_dylib_header)
297 path = _dyld_get_image_name (i);
298 if ( (NULL == path) || (0 == strlen (path)) )
300 p = GNUNET_strdup (path);
302 while ((s > p) && ('/' != *s))
314 * Return the actual path to a file found in the current
315 * PATH environment variable.
317 * @param binary the name of the file to find
318 * @return path to binary, NULL if not found
321 get_path_from_PATH (const char *binary)
329 if (NULL == (p = getenv ("PATH")))
332 /* On W32 look in CWD first. */
333 GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p);
335 path = GNUNET_strdup (p); /* because we write on it */
337 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
339 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
342 sprintf (buf, "%s/%s", pos, binary);
343 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
345 pos = GNUNET_strdup (pos);
352 sprintf (buf, "%s/%s", pos, binary);
353 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
355 pos = GNUNET_strdup (pos);
367 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
370 * @return NULL on error (environment variable not set)
373 get_path_from_GNUNET_PREFIX ()
377 if (NULL != (p = getenv ("GNUNET_PREFIX")))
378 return GNUNET_strdup (p);
384 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
387 * @return a pointer to the executable path, or NULL on error
390 os_get_gnunet_path ()
394 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
397 if (NULL != (ret = get_path_from_proc_maps ()))
399 if (NULL != (ret = get_path_from_proc_exe ()))
403 if (NULL != (ret = get_path_from_module_filename ()))
407 if (NULL != (ret = get_path_from_dyld_image ()))
409 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
412 if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
414 /* other attempts here */
415 LOG (GNUNET_ERROR_TYPE_ERROR,
417 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
418 "GNUnet", "GNUNET_PREFIX");
424 * @brief get the path to current app's bin/
427 * @return a pointer to the executable path, or NULL on error
435 if (NULL != (ret = get_path_from_proc_exe ()))
439 if (NULL != (ret = get_path_from_module_filename ()))
443 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
446 /* other attempts here */
452 * @brief get the path to a specific GNUnet installation directory or,
453 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
455 * @return a pointer to the dir path (to be freed by the caller)
458 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
462 char *execpath = NULL;
466 /* if wanted, try to get the current app's bin/ */
467 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
468 execpath = os_get_exec_path ();
470 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
471 * guess for the current app */
472 if (NULL == execpath)
473 execpath = os_get_gnunet_path ();
475 if (NULL == execpath)
478 n = strlen (execpath);
481 /* should never happen, but better safe than sorry */
482 GNUNET_free (execpath);
485 /* remove filename itself */
486 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
487 execpath[--n] = '\0';
491 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
492 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
494 if ( (GNUNET_OS_IPK_LIBDIR != dirkind) &&
495 (GNUNET_OS_IPK_LIBEXECDIR != dirkind) )
497 /* strip '/lib32' or '/lib64' */
498 execpath[n - 5] = '\0';
505 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
506 (0 == strcasecmp (&execpath[n - 3], "lib"))))
508 /* strip '/bin' or '/lib' */
509 execpath[n - 3] = '\0';
512 /* in case this was a directory named foo-bin, remove "foo-" */
513 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
514 execpath[--n] = '\0';
517 case GNUNET_OS_IPK_PREFIX:
518 case GNUNET_OS_IPK_SELF_PREFIX:
519 dirname = DIR_SEPARATOR_STR;
521 case GNUNET_OS_IPK_BINDIR:
522 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
524 case GNUNET_OS_IPK_LIBDIR:
528 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
529 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
530 sprintf (tmp, "%s%s", execpath, dirname);
532 GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
533 (4 == sizeof (void *)) )
537 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
538 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
539 sprintf (tmp, "%s%s", execpath, dirname);
542 GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
543 (8 == sizeof (void *)) )
546 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
551 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
553 case GNUNET_OS_IPK_DATADIR:
555 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
557 case GNUNET_OS_IPK_LOCALEDIR:
559 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
561 case GNUNET_OS_IPK_ICONDIR:
563 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
565 case GNUNET_OS_IPK_DOCDIR:
567 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
568 "gnunet" DIR_SEPARATOR_STR;
570 case GNUNET_OS_IPK_LIBEXECDIR:
574 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
575 "libexec" DIR_SEPARATOR_STR;
576 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
577 sprintf (tmp, "%s%s", execpath, dirname);
579 GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
580 (4 == sizeof (void *)) )
584 DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
585 "libexec" DIR_SEPARATOR_STR;
586 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
587 sprintf (tmp, "%s%s", execpath, dirname);
590 GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
591 (8 == sizeof (void *)) )
594 DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
595 "libexec" DIR_SEPARATOR_STR;
601 DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
602 "libexec" DIR_SEPARATOR_STR;
605 GNUNET_free (execpath);
608 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
609 sprintf (tmp, "%s%s", execpath, dirname);
610 GNUNET_free (execpath);
616 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
617 * binary, try to prefix it with the libexec/-directory to get the
620 * @param progname name of the binary
621 * @return full path to the binary, if possible, otherwise copy of 'progname'
624 GNUNET_OS_get_libexec_binary_path (const char *progname)
629 if (DIR_SEPARATOR == progname[0])
630 return GNUNET_strdup (progname);
631 libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
632 if (NULL == libexecdir)
633 return GNUNET_strdup (progname);
634 GNUNET_asprintf (&binary,
638 GNUNET_free (libexecdir);
644 * Check whether an executable exists and possibly
645 * if the suid bit is set on the file.
646 * Attempts to find the file using the current
647 * PATH environment variable as a search path.
649 * @param binary the name of the file to check.
650 * W32: must not have an .exe suffix.
651 * @return GNUNET_YES if the file is SUID,
652 * GNUNET_NO if not SUID (but binary exists)
653 * GNUNET_SYSERR on error (no such binary or not executable)
656 GNUNET_OS_check_helper_binary (const char *binary)
665 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
666 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binaryexe, GNUNET_NO,
668 (0 == strncmp (binary, "./", 2)) )
669 p = GNUNET_strdup (binaryexe);
672 p = get_path_from_PATH (binaryexe);
675 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
680 GNUNET_free (binaryexe);
682 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO,
684 (0 == strncmp (binary, "./", 2)) )
685 p = GNUNET_strdup (binary);
688 p = get_path_from_PATH (binary);
691 GNUNET_asprintf (&pf, "%s/%s", p, binary);
699 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
701 return GNUNET_SYSERR;
703 if (0 != ACCESS (p, X_OK))
705 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
707 return GNUNET_SYSERR;
712 /* as we run as root, we don't insist on SUID */
717 if (0 != STAT (p, &statbuf))
719 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
721 return GNUNET_SYSERR;
724 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
729 /* binary exists, but not SUID */
735 static int once; /* remember result from previous runs... */
739 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
740 if (INVALID_SOCKET == rawsock)
742 DWORD err = GetLastError ();
744 LOG (GNUNET_ERROR_TYPE_DEBUG,
745 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
747 return GNUNET_NO; /* not running as administrator */
750 closesocket (rawsock);
760 /* end of os_installation.c */