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);
218 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
221 sprintf (buf, "%s/%s", pos, binary);
222 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
224 pos = GNUNET_strdup (pos);
231 sprintf (buf, "%s/%s", pos, binary);
232 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
234 pos = GNUNET_strdup (pos);
245 get_path_from_GNUNET_PREFIX ()
249 p = getenv ("GNUNET_PREFIX");
251 return GNUNET_strdup (p);
256 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
259 * @return a pointer to the executable path, or NULL on error
262 os_get_gnunet_path ()
266 ret = get_path_from_GNUNET_PREFIX ();
270 ret = get_path_from_proc_maps ();
273 ret = get_path_from_proc_exe ();
278 ret = get_path_from_module_filename ();
283 ret = get_path_from_dyld_image ();
286 ret = get_path_from_NSGetExecutablePath ();
290 ret = get_path_from_PATH ("gnunet-arm");
293 /* other attempts here */
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
303 * @brief get the path to current app's bin/
306 * @return a pointer to the executable path, or NULL on error
315 ret = get_path_from_proc_exe ();
320 ret = get_path_from_module_filename ();
325 ret = get_path_from_NSGetExecutablePath ();
329 /* other attempts here */
336 * @brief get the path to a specific GNUnet installation directory or,
337 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
339 * @return a pointer to the dir path (to be freed by the caller)
342 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
346 char *execpath = NULL;
350 /* if wanted, try to get the current app's bin/ */
351 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
352 execpath = os_get_exec_path ();
354 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
355 * guess for the current app */
356 if (execpath == NULL)
357 execpath = os_get_gnunet_path ();
359 if (execpath == NULL)
362 n = strlen (execpath);
365 /* should never happen, but better safe than sorry */
366 GNUNET_free (execpath);
369 /* remove filename itself */
370 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
371 execpath[--n] = '\0';
375 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
376 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
378 if (dirkind != GNUNET_OS_IPK_LIBDIR)
380 /* strip '/lib32' or '/lib64' */
381 execpath[n - 5] = '\0';
388 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
389 (0 == strcasecmp (&execpath[n - 3], "lib"))))
391 /* strip '/bin' or '/lib' */
392 execpath[n - 3] = '\0';
395 /* in case this was a directory named foo-bin, remove "foo-" */
396 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
397 execpath[--n] = '\0';
400 case GNUNET_OS_IPK_PREFIX:
401 case GNUNET_OS_IPK_SELF_PREFIX:
402 dirname = DIR_SEPARATOR_STR;
404 case GNUNET_OS_IPK_BINDIR:
405 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
407 case GNUNET_OS_IPK_LIBDIR:
410 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
413 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
415 case GNUNET_OS_IPK_DATADIR:
417 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
420 case GNUNET_OS_IPK_LOCALEDIR:
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
425 case GNUNET_OS_IPK_ICONDIR:
427 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" 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 p = get_path_from_PATH (binaryexe);
465 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
471 p = get_path_from_PATH (binary);
474 GNUNET_asprintf (&pf, "%s/%s", p, binary);
481 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
482 _("Could not find binary `%s' in PATH!\n"),
484 return GNUNET_SYSERR;
486 if (0 != STAT (p, &statbuf))
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 _("stat (%s) failed: %s\n"),
493 return GNUNET_SYSERR;
496 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
497 (statbuf.st_uid == 0) )
502 if (0 == ACCESS (p, X_OK))
508 return GNUNET_SYSERR;
511 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
512 if (INVALID_SOCKET == rawsock)
514 DWORD err = GetLastError ();
515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
516 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
517 return GNUNET_NO; /* not running as administrator */
519 closesocket (rawsock);
525 /* end of os_installation.c */