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>
43 * Try to determine path by reading /proc/PID/exe
46 get_path_from_proc_maps ()
61 while (NULL != fgets (line, sizeof(line), f))
63 if ((1 == sscanf (line,
64 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
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 ()
88 sizeof(fn), "/proc/%u/exe", getpid ());
89 size = readlink (fn, lnk, sizeof (lnk)-1);
92 GNUNET_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 ()
119 GetModuleFileName (NULL, path, sizeof(path)-1);
120 idx = path + strlen (path);
121 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
124 return GNUNET_strdup (path);
129 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
132 get_path_from_NSGetExecutablePath ()
134 static char zero = '\0';
137 MyNSGetExecutablePathProto func;
142 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
147 /* get the path len, including the trailing \0 */
151 path = GNUNET_malloc (len);
152 ret = func (path, &len);
159 while ((path[len] != '/') && (len > 0))
166 get_path_from_dyld_image ()
174 c = _dyld_image_count ();
175 for (i = 0; i < c; i++)
177 if (_dyld_get_image_header (i) == &_mh_dylib_header)
179 path = _dyld_get_image_name (i);
180 if (path != NULL && strlen (path) > 0)
184 while ((s > p) && (*s != '/'))
197 * Return the actual path to a file found in the current
198 * PATH environment variable.
200 * @param binary the name of the file to find
201 * @return path to binary, NULL if not found
204 get_path_from_PATH (const char *binary)
215 path = GNUNET_strdup (p); /* because we write on it */
216 buf = GNUNET_malloc (strlen (path) + 20);
219 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
222 sprintf (buf, "%s/%s", pos, binary);
223 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
225 pos = GNUNET_strdup (pos);
232 sprintf (buf, "%s/%s", pos, binary);
233 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
235 pos = GNUNET_strdup (pos);
246 get_path_from_GNUNET_PREFIX ()
250 p = getenv ("GNUNET_PREFIX");
252 return GNUNET_strdup (p);
257 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
260 * @return a pointer to the executable path, or NULL on error
263 os_get_gnunet_path ()
267 ret = get_path_from_GNUNET_PREFIX ();
271 ret = get_path_from_proc_maps ();
274 ret = get_path_from_proc_exe ();
279 ret = get_path_from_module_filename ();
284 ret = get_path_from_dyld_image ();
287 ret = get_path_from_NSGetExecutablePath ();
291 ret = get_path_from_PATH ("gnunet-arm");
294 /* other attempts here */
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
304 * @brief get the path to current app's bin/
307 * @return a pointer to the executable path, or NULL on error
316 ret = get_path_from_proc_exe ();
321 ret = get_path_from_module_filename ();
326 ret = get_path_from_NSGetExecutablePath ();
330 /* other attempts here */
337 * @brief get the path to a specific GNUnet installation directory or,
338 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
340 * @return a pointer to the dir path (to be freed by the caller)
343 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
347 char *execpath = NULL;
351 /* if wanted, try to get the current app's bin/ */
352 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
353 execpath = os_get_exec_path ();
355 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
356 * guess for the current app */
357 if (execpath == NULL)
358 execpath = os_get_gnunet_path ();
360 if (execpath == NULL)
363 n = strlen (execpath);
366 /* should never happen, but better safe than sorry */
367 GNUNET_free (execpath);
370 /* remove filename itself */
371 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
372 execpath[--n] = '\0';
376 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
377 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
379 if (dirkind != GNUNET_OS_IPK_LIBDIR)
381 /* strip '/lib32' or '/lib64' */
382 execpath[n - 5] = '\0';
389 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
390 (0 == strcasecmp (&execpath[n - 3], "lib"))))
392 /* strip '/bin' or '/lib' */
393 execpath[n - 3] = '\0';
396 /* in case this was a directory named foo-bin, remove "foo-" */
397 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
398 execpath[--n] = '\0';
401 case GNUNET_OS_IPK_PREFIX:
402 case GNUNET_OS_IPK_SELF_PREFIX:
403 dirname = DIR_SEPARATOR_STR;
405 case GNUNET_OS_IPK_BINDIR:
406 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
408 case GNUNET_OS_IPK_LIBDIR:
411 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
414 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
416 case GNUNET_OS_IPK_DATADIR:
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
421 case GNUNET_OS_IPK_LOCALEDIR:
423 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
426 case GNUNET_OS_IPK_ICONDIR:
428 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" 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 the suid bit is set on a 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,
449 * GNUNET_SYSERR on error
452 GNUNET_OS_check_helper_binary (const char *binary)
460 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
461 p = get_path_from_PATH (binaryexe);
464 p = get_path_from_PATH (binary);
468 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
470 _("Could not find binary `%s' in PATH!\n"),
474 if (0 != STAT (p, &statbuf))
476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
477 _("stat (%s) failed: %s\n"),
481 return GNUNET_SYSERR;
485 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
486 (statbuf.st_uid == 0) )
490 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
491 if (INVALID_SOCKET == rawsock)
493 DWORD err = GetLastError ();
494 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
496 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
497 return GNUNET_NO; /* not running as administrator */
499 closesocket (rawsock);
505 /* end of os_installation.c */