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"
37 #include <mach-o/ldsyms.h>
38 #include <mach-o/dyld.h>
41 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
43 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
47 * Try to determine path by reading /proc/PID/exe
50 get_path_from_proc_maps ()
58 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
59 if (NULL == (f = FOPEN (fn, "r")))
61 while (NULL != fgets (line, sizeof (line), f))
64 SSCANF (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
65 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
69 return GNUNET_strdup (dir);
78 * Try to determine path by reading /proc/PID/exe
81 get_path_from_proc_exe ()
87 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
88 size = readlink (fn, lnk, sizeof (lnk) - 1);
91 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
94 GNUNET_assert (size < sizeof (lnk));
96 while ((lnk[size] != '/') && (size > 0))
98 if ((size < 4) || (lnk[size - 4] != '/'))
100 /* not installed in "/bin/" -- binary path probably useless */
104 return GNUNET_strdup (lnk);
110 * Try to determine path with win32-specific function
113 get_path_from_module_filename ()
119 GetModuleFileNameW (NULL, path, sizeof (path) - 1);
120 idx = path + wcslen (path);
121 while ((idx > path) && (*idx != L'\\') && (*idx != L'/'))
125 WideCharToMultiByte (CP_UTF8, 0, path, -1, upath, 4097, NULL, NULL);
127 return GNUNET_strdup (upath);
132 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
136 get_path_from_NSGetExecutablePath ()
138 static char zero = '\0';
141 MyNSGetExecutablePathProto func;
145 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath")))
149 /* get the path len, including the trailing \0 */
153 path = GNUNET_malloc (len);
154 if (0 != func (path, &len))
160 while ((path[len] != '/') && (len > 0))
168 get_path_from_dyld_image ()
177 c = _dyld_image_count ();
178 for (i = 0; i < c; i++)
180 if (_dyld_get_image_header (i) == &_mh_dylib_header)
182 path = _dyld_get_image_name (i);
183 if ( (NULL != path) && (strlen (path) > 0) )
185 p = GNUNET_strdup (path);
187 while ((s > p) && ('/' != *s))
201 * Return the actual path to a file found in the current
202 * PATH environment variable.
204 * @param binary the name of the file to find
205 * @return path to binary, NULL if not found
208 get_path_from_PATH (const char *binary)
220 /* On W32 look in CWD first. */
221 GNUNET_asprintf (&path, ".%c%s", PATH_SEPARATOR, p);
223 path = GNUNET_strdup (p); /* because we write on it */
225 buf = GNUNET_malloc (strlen (path) + strlen (binary) + 1 + 1);
227 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
230 sprintf (buf, "%s/%s", pos, binary);
231 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
233 pos = GNUNET_strdup (pos);
240 sprintf (buf, "%s/%s", pos, binary);
241 if (GNUNET_YES == GNUNET_DISK_file_test (buf))
243 pos = GNUNET_strdup (pos);
255 get_path_from_GNUNET_PREFIX ()
259 if (NULL != (p = getenv ("GNUNET_PREFIX")))
260 return GNUNET_strdup (p);
266 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
269 * @return a pointer to the executable path, or NULL on error
272 os_get_gnunet_path ()
276 if (NULL != (ret = get_path_from_GNUNET_PREFIX ()))
279 if (NULL != (ret = get_path_from_proc_maps ()))
281 if (NULL != (ret = get_path_from_proc_exe ()))
285 if (NULL != (ret = get_path_from_module_filename ()))
289 if (NULL != (ret = get_path_from_dyld_image ()))
291 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
294 if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
296 /* other attempts here */
297 LOG (GNUNET_ERROR_TYPE_ERROR,
299 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
300 "GNUnet", "GNUNET_PREFIX");
305 * @brief get the path to current app's bin/
308 * @return a pointer to the executable path, or NULL on error
316 if (NULL != (ret = get_path_from_proc_exe ()))
320 if (NULL != (ret = get_path_from_module_filename ()))
324 if (NULL != (ret = get_path_from_NSGetExecutablePath ()))
327 /* other attempts here */
334 * @brief get the path to a specific GNUnet installation directory or,
335 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
337 * @return a pointer to the dir path (to be freed by the caller)
340 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
344 char *execpath = NULL;
348 /* if wanted, try to get the current app's bin/ */
349 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
350 execpath = os_get_exec_path ();
352 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
353 * guess for the current app */
354 if (NULL == execpath)
355 execpath = os_get_gnunet_path ();
357 if (NULL == execpath)
360 n = strlen (execpath);
363 /* should never happen, but better safe than sorry */
364 GNUNET_free (execpath);
367 /* remove filename itself */
368 while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
369 execpath[--n] = '\0';
373 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
374 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
376 if (GNUNET_OS_IPK_LIBDIR != dirkind)
378 /* strip '/lib32' or '/lib64' */
379 execpath[n - 5] = '\0';
386 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
387 (0 == strcasecmp (&execpath[n - 3], "lib"))))
389 /* strip '/bin' or '/lib' */
390 execpath[n - 3] = '\0';
393 /* in case this was a directory named foo-bin, remove "foo-" */
394 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
395 execpath[--n] = '\0';
398 case GNUNET_OS_IPK_PREFIX:
399 case GNUNET_OS_IPK_SELF_PREFIX:
400 dirname = DIR_SEPARATOR_STR;
402 case GNUNET_OS_IPK_BINDIR:
403 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
405 case GNUNET_OS_IPK_LIBDIR:
408 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
410 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
412 case GNUNET_OS_IPK_DATADIR:
414 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
416 case GNUNET_OS_IPK_LOCALEDIR:
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
420 case GNUNET_OS_IPK_ICONDIR:
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
424 case GNUNET_OS_IPK_DOCDIR:
426 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
427 "gnunet" DIR_SEPARATOR_STR;
430 GNUNET_free (execpath);
433 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
434 sprintf (tmp, "%s%s", execpath, dirname);
435 GNUNET_free (execpath);
441 * Check whether an executable exists and possibly
442 * if the suid bit is set on the file.
443 * Attempts to find the file using the current
444 * PATH environment variable as a search path.
446 * @param binary the name of the file to check
447 * @return GNUNET_YES if the file is SUID,
448 * GNUNET_NO if not SUID (but binary exists)
449 * GNUNET_SYSERR on error (no such binary or not executable)
452 GNUNET_OS_check_helper_binary (const char *binary)
461 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
462 if (DIR_SEPARATOR == binary[0])
463 p = GNUNET_strdup (binary);
466 p = get_path_from_PATH (binaryexe);
469 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
474 GNUNET_free (binaryexe);
476 if (DIR_SEPARATOR == binary[0])
477 p = GNUNET_strdup (binary);
480 p = get_path_from_PATH (binary);
483 GNUNET_asprintf (&pf, "%s/%s", p, binary);
491 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
493 return GNUNET_SYSERR;
495 if (0 != ACCESS (p, X_OK))
497 LOG (GNUNET_ERROR_TYPE_WARNING, _("access (%s, X_OK) failed: %s\n"), p,
500 return GNUNET_SYSERR;
505 /* as we run as root, we don't insist on SUID */
510 if (0 != STAT (p, &statbuf))
512 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
515 return GNUNET_SYSERR;
518 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
523 /* binary exists, but not SUID */
529 static int once; /* remember result from previous runs... */
533 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
534 if (INVALID_SOCKET == rawsock)
536 DWORD err = GetLastError ();
538 LOG (GNUNET_ERROR_TYPE_INFO,
539 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
541 return GNUNET_NO; /* not running as administrator */
544 closesocket (rawsock);
554 /* end of os_installation.c */