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
51 get_path_from_proc_maps ()
59 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
60 if (NULL == (f = FOPEN (fn, "r")))
62 while (NULL != fgets (line, sizeof (line), f))
65 SSCANF (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
66 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
70 return GNUNET_strdup (dir);
79 * Try to determine path by reading /proc/PID/exe
82 get_path_from_proc_exe ()
88 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
89 size = readlink (fn, lnk, sizeof (lnk) - 1);
92 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
95 GNUNET_assert (size < sizeof (lnk));
97 while ((lnk[size] != '/') && (size > 0))
99 if ((size < 4) || (lnk[size - 4] != '/'))
101 /* not installed in "/bin/" -- binary path probably useless */
105 return GNUNET_strdup (lnk);
111 * Try to determine path with win32-specific function
114 get_path_from_module_filename ()
120 GetModuleFileNameW (NULL, path, sizeof (path) - 1);
121 idx = path + wcslen (path);
122 while ((idx > path) && (*idx != L'\\') && (*idx != L'/'))
126 WideCharToMultiByte (CP_UTF8, 0, path, -1, upath, 4097, NULL, NULL);
128 return GNUNET_strdup (upath);
133 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
137 get_path_from_NSGetExecutablePath ()
139 static char zero = '\0';
142 MyNSGetExecutablePathProto func;
146 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath")))
150 /* get the path len, including the trailing \0 */
154 path = GNUNET_malloc (len);
155 if (0 != func (path, &len))
161 while ((path[len] != '/') && (len > 0))
169 get_path_from_dyld_image ()
178 c = _dyld_image_count ();
179 for (i = 0; i < c; i++)
181 if (_dyld_get_image_header (i) == &_mh_dylib_header)
183 path = _dyld_get_image_name (i);
184 if ( (NULL != path) && (strlen (path) > 0) )
186 p = GNUNET_strdup (path);
188 while ((s > p) && ('/' != *s))
202 * Return the actual path to a file found in the current
203 * PATH environment variable.
205 * @param binary the name of the file to find
206 * @return path to binary, NULL if not found
209 get_path_from_PATH (const char *binary)
221 /* On W32 look in CWD first. */
222 GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p);
224 path = GNUNET_strdup (p); /* because we write on it */
226 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
228 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
231 sprintf (buf, "%s/%s", pos, binary);
232 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
234 pos = GNUNET_strdup (pos);
241 sprintf (buf, "%s/%s", pos, binary);
242 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
244 pos = GNUNET_strdup (pos);
256 get_path_from_GNUNET_PREFIX ()
260 if (NULL != (p = getenv ("GNUNET_PREFIX")))
261 return GNUNET_strdup (p);
267 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
270 * @return a pointer to the executable path, or NULL on error
273 os_get_gnunet_path ()
277 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
280 if (NULL != (ret = get_path_from_proc_maps ()))
282 if (NULL != (ret = get_path_from_proc_exe ()))
286 if (NULL != (ret = get_path_from_module_filename ()))
290 if (NULL != (ret = get_path_from_dyld_image ()))
292 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
295 if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
297 /* other attempts here */
298 LOG (GNUNET_ERROR_TYPE_ERROR,
300 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
301 "GNUnet", "GNUNET_PREFIX");
306 * @brief get the path to current app's bin/
309 * @return a pointer to the executable path, or NULL on error
317 if (NULL != (ret = get_path_from_proc_exe ()))
321 if (NULL != (ret = get_path_from_module_filename ()))
325 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
328 /* other attempts here */
335 * @brief get the path to a specific GNUnet installation directory or,
336 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
338 * @return a pointer to the dir path (to be freed by the caller)
341 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
345 char *execpath = NULL;
349 /* if wanted, try to get the current app's bin/ */
350 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
351 execpath = os_get_exec_path ();
353 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
354 * guess for the current app */
355 if (NULL == execpath)
356 execpath = os_get_gnunet_path ();
358 if (NULL == execpath)
361 n = strlen (execpath);
364 /* should never happen, but better safe than sorry */
365 GNUNET_free (execpath);
368 /* remove filename itself */
369 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
370 execpath[--n] = '\0';
374 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
375 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
377 if (GNUNET_OS_IPK_LIBDIR != dirkind)
379 /* strip '/lib32' or '/lib64' */
380 execpath[n - 5] = '\0';
387 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
388 (0 == strcasecmp (&execpath[n - 3], "lib"))))
390 /* strip '/bin' or '/lib' */
391 execpath[n - 3] = '\0';
394 /* in case this was a directory named foo-bin, remove "foo-" */
395 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
396 execpath[--n] = '\0';
399 case GNUNET_OS_IPK_PREFIX:
400 case GNUNET_OS_IPK_SELF_PREFIX:
401 dirname = DIR_SEPARATOR_STR;
403 case GNUNET_OS_IPK_BINDIR:
404 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
406 case GNUNET_OS_IPK_LIBDIR:
409 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
411 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
413 case GNUNET_OS_IPK_DATADIR:
415 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
417 case GNUNET_OS_IPK_LOCALEDIR:
419 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
421 case GNUNET_OS_IPK_ICONDIR:
423 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
425 case GNUNET_OS_IPK_DOCDIR:
427 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
428 "gnunet" DIR_SEPARATOR_STR;
431 GNUNET_free (execpath);
434 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
435 sprintf (tmp, "%s%s", execpath, dirname);
436 GNUNET_free (execpath);
442 * Check whether an executable exists and possibly
443 * if the suid bit is set on the file.
444 * Attempts to find the file using the current
445 * PATH environment variable as a search path.
447 * @param binary the name of the file to check.
448 * W32: must not have an .exe suffix.
449 * @return GNUNET_YES if the file is SUID,
450 * GNUNET_NO if not SUID (but binary exists)
451 * GNUNET_SYSERR on error (no such binary or not executable)
454 GNUNET_OS_check_helper_binary (const char *binary)
463 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
464 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binaryexe, GNUNET_NO,
466 (0 == strncmp (binary, "./", 2)) )
467 p = GNUNET_strdup (binaryexe);
470 p = get_path_from_PATH (binaryexe);
473 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
478 GNUNET_free (binaryexe);
480 if ( (GNUNET_YES == GNUNET_STRINGS_path_is_absolute (binary, GNUNET_NO,
482 (0 == strncmp (binary, "./", 2)) )
483 p = GNUNET_strdup (binary);
486 p = get_path_from_PATH (binary);
489 GNUNET_asprintf (&pf, "%s/%s", p, binary);
497 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
499 return GNUNET_SYSERR;
501 if (0 != ACCESS (p, X_OK))
503 LOG (GNUNET_ERROR_TYPE_WARNING, _("access (%s, X_OK) failed: %s\n"), p,
506 return GNUNET_SYSERR;
511 /* as we run as root, we don't insist on SUID */
516 if (0 != STAT (p, &statbuf))
518 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
521 return GNUNET_SYSERR;
524 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
529 /* binary exists, but not SUID */
535 static int once; /* remember result from previous runs... */
539 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
540 if (INVALID_SOCKET == rawsock)
542 DWORD err = GetLastError ();
544 LOG (GNUNET_ERROR_TYPE_INFO,
545 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
547 return GNUNET_NO; /* not running as administrator */
550 closesocket (rawsock);
560 /* end of os_installation.c */