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 ());
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);
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);
135 get_path_from_NSGetExecutablePath ()
137 static char zero = '\0';
140 MyNSGetExecutablePathProto func;
145 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
150 /* get the path len, including the trailing \0 */
154 path = GNUNET_malloc (len);
155 ret = func (path, &len);
162 while ((path[len] != '/') && (len > 0))
169 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 (path != NULL && strlen (path) > 0)
185 p = GNUNET_strdup (path);
187 while ((s > p) && (*s != '/'))
200 * Return the actual path to a file found in the current
201 * PATH environment variable.
203 * @param binary the name of the file to find
204 * @return path to binary, NULL if not found
207 get_path_from_PATH (const char *binary)
218 path = GNUNET_strdup (p); /* because we write on it */
219 buf = GNUNET_malloc (strlen (path) + 20);
221 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
224 sprintf (buf, "%s/%s", pos, binary);
225 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
227 pos = GNUNET_strdup (pos);
234 sprintf (buf, "%s/%s", pos, binary);
235 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
237 pos = GNUNET_strdup (pos);
248 get_path_from_GNUNET_PREFIX ()
252 p = getenv ("GNUNET_PREFIX");
254 return GNUNET_strdup (p);
259 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
262 * @return a pointer to the executable path, or NULL on error
265 os_get_gnunet_path ()
269 ret = get_path_from_GNUNET_PREFIX ();
273 ret = get_path_from_proc_maps ();
276 ret = get_path_from_proc_exe ();
281 ret = get_path_from_module_filename ();
286 ret = get_path_from_dyld_image ();
289 ret = get_path_from_NSGetExecutablePath ();
293 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
317 ret = get_path_from_proc_exe ();
322 ret = get_path_from_module_filename ();
327 ret = get_path_from_NSGetExecutablePath ();
331 /* other attempts here */
338 * @brief get the path to a specific GNUnet installation directory or,
339 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
341 * @return a pointer to the dir path (to be freed by the caller)
344 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
348 char *execpath = NULL;
352 /* if wanted, try to get the current app's bin/ */
353 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
354 execpath = os_get_exec_path ();
356 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
357 * guess for the current app */
358 if (execpath == NULL)
359 execpath = os_get_gnunet_path ();
361 if (execpath == NULL)
364 n = strlen (execpath);
367 /* should never happen, but better safe than sorry */
368 GNUNET_free (execpath);
371 /* remove filename itself */
372 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
373 execpath[--n] = '\0';
377 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
378 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
380 if (dirkind != GNUNET_OS_IPK_LIBDIR)
382 /* strip '/lib32' or '/lib64' */
383 execpath[n - 5] = '\0';
390 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
391 (0 == strcasecmp (&execpath[n - 3], "lib"))))
393 /* strip '/bin' or '/lib' */
394 execpath[n - 3] = '\0';
397 /* in case this was a directory named foo-bin, remove "foo-" */
398 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
399 execpath[--n] = '\0';
402 case GNUNET_OS_IPK_PREFIX:
403 case GNUNET_OS_IPK_SELF_PREFIX:
404 dirname = DIR_SEPARATOR_STR;
406 case GNUNET_OS_IPK_BINDIR:
407 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
409 case GNUNET_OS_IPK_LIBDIR:
412 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
414 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
416 case GNUNET_OS_IPK_DATADIR:
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
420 case GNUNET_OS_IPK_LOCALEDIR:
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
424 case GNUNET_OS_IPK_ICONDIR:
426 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
428 case GNUNET_OS_IPK_DOCDIR:
430 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR \
431 "gnunet" DIR_SEPARATOR_STR;
434 GNUNET_free (execpath);
437 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
438 sprintf (tmp, "%s%s", execpath, dirname);
439 GNUNET_free (execpath);
445 * Check whether an executable exists and possibly
446 * if the suid bit is set on the file.
447 * Attempts to find the file using the current
448 * PATH environment variable as a search path.
450 * @param binary the name of the file to check
451 * @return GNUNET_YES if the file is SUID,
452 * GNUNET_NO if not SUID (but binary exists)
453 * GNUNET_SYSERR on error (no such binary or not executable)
456 GNUNET_OS_check_helper_binary (const char *binary)
466 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
467 p = get_path_from_PATH (binaryexe);
470 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
476 p = get_path_from_PATH (binary);
479 GNUNET_asprintf (&pf, "%s/%s", p, binary);
486 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
488 return GNUNET_SYSERR;
490 if (0 != ACCESS (p, X_OK))
492 LOG (GNUNET_ERROR_TYPE_WARNING, _("access (%s, X_OK) failed: %s\n"), p,
495 return GNUNET_SYSERR;
499 /* as we run as root, we don't insist on SUID */
503 if (0 != STAT (p, &statbuf))
505 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
508 return GNUNET_SYSERR;
511 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
516 /* binary exists, but not SUID */
521 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
522 if (INVALID_SOCKET == rawsock)
524 DWORD err = GetLastError ();
526 LOG (GNUNET_ERROR_TYPE_INFO,
527 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
528 return GNUNET_NO; /* not running as administrator */
530 closesocket (rawsock);
536 /* end of os_installation.c */