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 ()
118 GetModuleFileName (NULL, path, sizeof (path) - 1);
119 idx = path + strlen (path);
120 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
123 return GNUNET_strdup (path);
128 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
131 get_path_from_NSGetExecutablePath ()
133 static char zero = '\0';
136 MyNSGetExecutablePathProto func;
141 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
146 /* get the path len, including the trailing \0 */
150 path = GNUNET_malloc (len);
151 ret = func (path, &len);
158 while ((path[len] != '/') && (len > 0))
165 get_path_from_dyld_image ()
173 c = _dyld_image_count ();
174 for (i = 0; i < c; i++)
176 if (_dyld_get_image_header (i) == &_mh_dylib_header)
178 path = _dyld_get_image_name (i);
179 if (path != NULL && strlen (path) > 0)
181 p = GNUNET_strdup (path);
183 while ((s > p) && (*s != '/'))
196 * Return the actual path to a file found in the current
197 * PATH environment variable.
199 * @param binary the name of the file to find
200 * @return path to binary, NULL if not found
203 get_path_from_PATH (const char *binary)
214 path = GNUNET_strdup (p); /* because we write on it */
215 buf = GNUNET_malloc (strlen (path) + 20);
217 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
220 sprintf (buf, "%s/%s", pos, binary);
221 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
223 pos = GNUNET_strdup (pos);
230 sprintf (buf, "%s/%s", pos, binary);
231 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
233 pos = GNUNET_strdup (pos);
244 get_path_from_GNUNET_PREFIX ()
248 p = getenv ("GNUNET_PREFIX");
250 return GNUNET_strdup (p);
255 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
258 * @return a pointer to the executable path, or NULL on error
261 os_get_gnunet_path ()
265 ret = get_path_from_GNUNET_PREFIX ();
269 ret = get_path_from_proc_maps ();
272 ret = get_path_from_proc_exe ();
277 ret = get_path_from_module_filename ();
282 ret = get_path_from_dyld_image ();
285 ret = get_path_from_NSGetExecutablePath ();
289 ret = get_path_from_PATH ("gnunet-arm");
292 /* other attempts here */
293 LOG (GNUNET_ERROR_TYPE_ERROR,
295 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
296 "GNUnet", "GNUNET_PREFIX");
301 * @brief get the path to current app's bin/
304 * @return a pointer to the executable path, or NULL on error
313 ret = get_path_from_proc_exe ();
318 ret = get_path_from_module_filename ();
323 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 (execpath == NULL)
355 execpath = os_get_gnunet_path ();
357 if (execpath == NULL)
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) && (execpath[n - 1] == DIR_SEPARATOR))
369 execpath[--n] = '\0';
373 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
374 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
376 if (dirkind != GNUNET_OS_IPK_LIBDIR)
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;
425 GNUNET_free (execpath);
428 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
429 sprintf (tmp, "%s%s", execpath, dirname);
430 GNUNET_free (execpath);
436 * Check whether an executable exists and possibly
437 * if the suid bit is set on the file.
438 * Attempts to find the file using the current
439 * PATH environment variable as a search path.
441 * @param binary the name of the file to check
442 * @return GNUNET_YES if the file is SUID,
443 * GNUNET_NO if not SUID (but binary exists)
444 * GNUNET_SYSERR on error (no such binary or not executable)
447 GNUNET_OS_check_helper_binary (const char *binary)
457 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
458 p = get_path_from_PATH (binaryexe);
461 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
467 p = get_path_from_PATH (binary);
470 GNUNET_asprintf (&pf, "%s/%s", p, binary);
477 LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"),
479 return GNUNET_SYSERR;
481 if (0 != STAT (p, &statbuf))
483 LOG (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
486 return GNUNET_SYSERR;
489 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
494 if (0 == ACCESS (p, X_OK))
500 return GNUNET_SYSERR;
503 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
504 if (INVALID_SOCKET == rawsock)
506 DWORD err = GetLastError ();
508 LOG (GNUNET_ERROR_TYPE_INFO,
509 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
510 return GNUNET_NO; /* not running as administrator */
512 closesocket (rawsock);
518 /* end of os_installation.c */