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%*[ ]%s", 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 if ((size < 4) || (lnk[size - 4] != '/'))
106 /* not installed in "/bin/" -- binary path probably useless */
110 return GNUNET_strdup (lnk);
116 static HINSTANCE dll_instance;
119 /* GNUNET_util_cl_init() in common_logging.c is preferred.
120 * This function is only for thread-local storage (not used in GNUnet)
121 * and hInstance saving.
124 DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
128 case DLL_PROCESS_ATTACH:
129 dll_instance = hinstDLL;
131 case DLL_THREAD_ATTACH:
133 case DLL_THREAD_DETACH:
135 case DLL_PROCESS_DETACH:
142 * Try to determine path with win32-specific function
144 * @return NULL on error
147 get_path_from_module_filename ()
149 size_t pathlen = 512;
152 wchar_t *modulepath = NULL;
155 size_t u8_string_length;
157 /* This braindead function won't tell us how much space it needs, so
158 * we start at 1024 and double the space up if it doesn't fit, until
159 * it fits, or we exceed the threshold.
163 pathlen = pathlen * 2;
164 modulepath = GNUNET_realloc (modulepath, pathlen * sizeof (wchar_t));
166 real_pathlen = GetModuleFileNameW (dll_instance, modulepath, pathlen * sizeof (wchar_t));
167 } while (real_pathlen >= pathlen && pathlen < 16*1024);
168 if (real_pathlen >= pathlen)
171 modulepath[real_pathlen] = '\0';
173 idx = modulepath + real_pathlen;
174 while ((idx > modulepath) && (*idx != L'\\') && (*idx != L'/'))
178 /* Now modulepath holds full path to the directory where libgnunetutil is.
179 * This directory should look like <GNUNET_PREFIX>/bin or <GNUNET_PREFIX>.
181 if (wcschr (modulepath, L'/') || wcschr (modulepath, L'\\'))
183 /* At least one directory component (i.e. we're not in a root directory) */
184 wchar_t *dirname = idx;
185 while ((dirname > modulepath) && (*dirname != L'\\') && (*dirname != L'/'))
188 if (dirname > modulepath)
191 /* Now modulepath holds full path to the parent directory of the directory
192 * where libgnunetutil is.
193 * dirname holds the name of the directory where libgnunetutil is.
195 if (wcsicmp (dirname, L"bin") == 0)
201 /* Roll back our changes to modulepath */
208 /* modulepath is GNUNET_PREFIX */
209 u8_string = u16_to_u8 (modulepath, wcslen (modulepath), NULL, &u8_string_length);
210 if (NULL == u8_string)
213 upath = GNUNET_malloc (u8_string_length + 1);
214 memcpy (upath, u8_string, u8_string_length);
215 upath[u8_string_length] = '\0';
218 GNUNET_free (modulepath);
226 * Signature of the '_NSGetExecutablePath" function.
228 * @param buf where to write the path
229 * @param number of bytes available in 'buf'
230 * @return 0 on success, otherwise desired number of bytes is stored in 'bufsize'
232 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
236 * Try to obtain the path of our executable using '_NSGetExecutablePath'.
238 * @return NULL on error
241 get_path_from_NSGetExecutablePath ()
243 static char zero = '\0';
246 MyNSGetExecutablePathProto func;
250 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath")))
254 /* get the path len, including the trailing \0 */
255 (void) func (path, &len);
258 path = GNUNET_malloc (len);
259 if (0 != func (path, &len))
265 while ((path[len] != '/') && (len > 0))
273 * Try to obtain the path of our executable using '_dyld_image' API.
275 * @return NULL on error
278 get_path_from_dyld_image ()
286 c = _dyld_image_count ();
287 for (i = 0; i < c; i++)
289 if (_dyld_get_image_header (i) != &_mh_dylib_header)
291 path = _dyld_get_image_name (i);
292 if ( (NULL == path) || (0 == strlen (path)) )
294 p = GNUNET_strdup (path);
296 while ((s > p) && ('/' != *s))
308 * Return the actual path to a file found in the current
309 * PATH environment variable.
311 * @param binary the name of the file to find
312 * @return path to binary, NULL if not found
315 get_path_from_PATH (const char *binary)
323 if (NULL == (p = getenv ("PATH")))
326 /* On W32 look in CWD first. */
327 GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p);
329 path = GNUNET_strdup (p); /* because we write on it */
331 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
333 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
336 sprintf (buf, "%s/%s", pos, binary);
337 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
339 pos = GNUNET_strdup (pos);
346 sprintf (buf, "%s/%s", pos, binary);
347 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
349 pos = GNUNET_strdup (pos);
361 * Try to obtain the installation path using the "GNUNET_PREFIX" environment
364 * @return NULL on error (environment variable not set)
367 get_path_from_GNUNET_PREFIX ()
371 if (NULL != (p = getenv ("GNUNET_PREFIX")))
372 return GNUNET_strdup (p);
378 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
381 * @return a pointer to the executable path, or NULL on error
384 os_get_gnunet_path ()
388 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
391 if (NULL != (ret = get_path_from_proc_maps ()))
393 if (NULL != (ret = get_path_from_proc_exe ()))
397 if (NULL != (ret = get_path_from_module_filename ()))
401 if (NULL != (ret = get_path_from_dyld_image ()))
403 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
406 if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
408 /* other attempts here */
409 LOG (GNUNET_ERROR_TYPE_ERROR,
411 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
412 "GNUnet", "GNUNET_PREFIX");
418 * @brief get the path to current app's bin/
421 * @return a pointer to the executable path, or NULL on error
429 if (NULL != (ret = get_path_from_proc_exe ()))
433 if (NULL != (ret = get_path_from_module_filename ()))
437 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
440 /* other attempts here */
446 * @brief get the path to a specific GNUnet installation directory or,
447 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
449 * @return a pointer to the dir path (to be freed by the caller)
452 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
456 char *execpath = NULL;
460 /* if wanted, try to get the current app's bin/ */
461 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
462 execpath = os_get_exec_path ();
464 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
465 * guess for the current app */
466 if (NULL == execpath)
467 execpath = os_get_gnunet_path ();
469 if (NULL == execpath)
472 n = strlen (execpath);
475 /* should never happen, but better safe than sorry */
476 GNUNET_free (execpath);
479 /* remove filename itself */
480 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
481 execpath[--n] = '\0';
485 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
486 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
488 if (GNUNET_OS_IPK_LIBDIR != dirkind)
490 /* strip '/lib32' or '/lib64' */
491 execpath[n - 5] = '\0';
498 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
499 (0 == strcasecmp (&execpath[n - 3], "lib"))))
501 /* strip '/bin' or '/lib' */
502 execpath[n - 3] = '\0';
505 /* in case this was a directory named foo-bin, remove "foo-" */
506 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
507 execpath[--n] = '\0';
510 case GNUNET_OS_IPK_PREFIX:
511 case GNUNET_OS_IPK_SELF_PREFIX:
512 dirname = DIR_SEPARATOR_STR;
514 case GNUNET_OS_IPK_BINDIR:
515 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
517 case GNUNET_OS_IPK_LIBDIR:
520 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
522 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
524 case GNUNET_OS_IPK_DATADIR:
526 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
528 case GNUNET_OS_IPK_LOCALEDIR:
530 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
532 case GNUNET_OS_IPK_ICONDIR:
534 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
536 case GNUNET_OS_IPK_DOCDIR:
538 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
539 "gnunet" DIR_SEPARATOR_STR;
541 case GNUNET_OS_IPK_LIBEXECDIR:
543 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
544 "libexec" DIR_SEPARATOR_STR;
547 GNUNET_free (execpath);
550 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
551 sprintf (tmp, "%s%s", execpath, dirname);
552 GNUNET_free (execpath);
558 * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon
559 * binary, try to prefix it with the libexec/-directory to get the
562 * @param progname name of the binary
563 * @return full path to the binary, if possible, otherwise copy of 'progname'
566 GNUNET_OS_get_libexec_binary_path (const char *progname)
571 libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
572 if (NULL == libexecdir)
573 return GNUNET_strdup (progname);
574 GNUNET_asprintf (&binary,
578 GNUNET_free (libexecdir);
584 * Check whether an executable exists and possibly
585 * if the suid bit is set on the file.
586 * Attempts to find the file using the current
587 * PATH environment variable as a search path.
589 * @param binary the name of the file to check.
590 * W32: must not have an .exe suffix.
591 * @return GNUNET_YES if the file is SUID,
592 * GNUNET_NO if not SUID (but binary exists)
593 * GNUNET_SYSERR on error (no such binary or not executable)
596 GNUNET_OS_check_helper_binary (const char *binary)
605 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
606 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binaryexe, GNUNET_NO,
608 (0 == strncmp (binary, "./", 2)) )
609 p = GNUNET_strdup (binaryexe);
612 p = get_path_from_PATH (binaryexe);
615 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
620 GNUNET_free (binaryexe);
622 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO,
624 (0 == strncmp (binary, "./", 2)) )
625 p = GNUNET_strdup (binary);
628 p = get_path_from_PATH (binary);
631 GNUNET_asprintf (&pf, "%s/%s", p, binary);
639 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
641 return GNUNET_SYSERR;
643 if (0 != ACCESS (p, X_OK))
645 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", p);
647 return GNUNET_SYSERR;
652 /* as we run as root, we don't insist on SUID */
657 if (0 != STAT (p, &statbuf))
659 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", p);
661 return GNUNET_SYSERR;
664 if ((0 != (statbuf.st_mode & S_ISUID)) && (0 == statbuf.st_uid))
669 /* binary exists, but not SUID */
675 static int once; /* remember result from previous runs... */
679 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
680 if (INVALID_SOCKET == rawsock)
682 DWORD err = GetLastError ();
684 LOG (GNUNET_ERROR_TYPE_DEBUG,
685 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
687 return GNUNET_NO; /* not running as administrator */
690 closesocket (rawsock);
700 /* end of os_installation.c */