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 ()
54 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
58 while (NULL != fgets (line, sizeof (line), f))
61 sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) &&
62 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
66 return GNUNET_strdup (dir);
74 * Try to determine path by reading /proc/PID/exe
77 get_path_from_proc_exe ()
83 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
84 size = readlink (fn, lnk, sizeof (lnk) - 1);
87 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
90 GNUNET_assert (size < sizeof (lnk));
92 while ((lnk[size] != '/') && (size > 0))
94 if ((size < 4) || (lnk[size - 4] != '/'))
96 /* not installed in "/bin/" -- binary path probably useless */
100 return GNUNET_strdup (lnk);
106 * Try to determine path with win32-specific function
109 get_path_from_module_filename ()
114 GetModuleFileName (NULL, path, sizeof (path) - 1);
115 idx = path + strlen (path);
116 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
119 return GNUNET_strdup (path);
124 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
127 get_path_from_NSGetExecutablePath ()
129 static char zero = '\0';
132 MyNSGetExecutablePathProto func;
137 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
142 /* get the path len, including the trailing \0 */
146 path = GNUNET_malloc (len);
147 ret = func (path, &len);
154 while ((path[len] != '/') && (len > 0))
161 get_path_from_dyld_image ()
169 c = _dyld_image_count ();
170 for (i = 0; i < c; i++)
172 if (_dyld_get_image_header (i) == &_mh_dylib_header)
174 path = _dyld_get_image_name (i);
175 if (path != NULL && strlen (path) > 0)
179 while ((s > p) && (*s != '/'))
192 * Return the actual path to a file found in the current
193 * PATH environment variable.
195 * @param binary the name of the file to find
196 * @return path to binary, NULL if not found
199 get_path_from_PATH (const char *binary)
210 path = GNUNET_strdup (p); /* because we write on it */
211 buf = GNUNET_malloc (strlen (path) + 20);
213 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
216 sprintf (buf, "%s/%s", pos, binary);
217 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
219 pos = GNUNET_strdup (pos);
226 sprintf (buf, "%s/%s", pos, binary);
227 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
229 pos = GNUNET_strdup (pos);
240 get_path_from_GNUNET_PREFIX ()
244 p = getenv ("GNUNET_PREFIX");
246 return GNUNET_strdup (p);
251 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
254 * @return a pointer to the executable path, or NULL on error
257 os_get_gnunet_path ()
261 ret = get_path_from_GNUNET_PREFIX ();
265 ret = get_path_from_proc_maps ();
268 ret = get_path_from_proc_exe ();
273 ret = get_path_from_module_filename ();
278 ret = get_path_from_dyld_image ();
281 ret = get_path_from_NSGetExecutablePath ();
285 ret = get_path_from_PATH ("gnunet-arm");
288 /* other attempts here */
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
292 "GNUnet", "GNUNET_PREFIX");
297 * @brief get the path to current app's bin/
300 * @return a pointer to the executable path, or NULL on error
309 ret = get_path_from_proc_exe ();
314 ret = get_path_from_module_filename ();
319 ret = get_path_from_NSGetExecutablePath ();
323 /* other attempts here */
330 * @brief get the path to a specific GNUnet installation directory or,
331 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
333 * @return a pointer to the dir path (to be freed by the caller)
336 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
340 char *execpath = NULL;
344 /* if wanted, try to get the current app's bin/ */
345 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
346 execpath = os_get_exec_path ();
348 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
349 * guess for the current app */
350 if (execpath == NULL)
351 execpath = os_get_gnunet_path ();
353 if (execpath == NULL)
356 n = strlen (execpath);
359 /* should never happen, but better safe than sorry */
360 GNUNET_free (execpath);
363 /* remove filename itself */
364 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
365 execpath[--n] = '\0';
369 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
370 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
372 if (dirkind != GNUNET_OS_IPK_LIBDIR)
374 /* strip '/lib32' or '/lib64' */
375 execpath[n - 5] = '\0';
382 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
383 (0 == strcasecmp (&execpath[n - 3], "lib"))))
385 /* strip '/bin' or '/lib' */
386 execpath[n - 3] = '\0';
389 /* in case this was a directory named foo-bin, remove "foo-" */
390 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
391 execpath[--n] = '\0';
394 case GNUNET_OS_IPK_PREFIX:
395 case GNUNET_OS_IPK_SELF_PREFIX:
396 dirname = DIR_SEPARATOR_STR;
398 case GNUNET_OS_IPK_BINDIR:
399 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
401 case GNUNET_OS_IPK_LIBDIR:
404 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
406 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
408 case GNUNET_OS_IPK_DATADIR:
410 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
412 case GNUNET_OS_IPK_LOCALEDIR:
414 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
416 case GNUNET_OS_IPK_ICONDIR:
418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
421 GNUNET_free (execpath);
424 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
425 sprintf (tmp, "%s%s", execpath, dirname);
426 GNUNET_free (execpath);
432 * Check whether an executable exists and possibly
433 * if the suid bit is set on the file.
434 * Attempts to find the file using the current
435 * PATH environment variable as a search path.
437 * @param binary the name of the file to check
438 * @return GNUNET_YES if the file is SUID,
439 * GNUNET_NO if not SUID (but binary exists)
440 * GNUNET_SYSERR on error (no such binary or not executable)
443 GNUNET_OS_check_helper_binary (const char *binary)
453 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
454 p = get_path_from_PATH (binaryexe);
457 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
463 p = get_path_from_PATH (binary);
466 GNUNET_asprintf (&pf, "%s/%s", p, binary);
473 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
474 _("Could not find binary `%s' in PATH!\n"), binary);
475 return GNUNET_SYSERR;
477 if (0 != STAT (p, &statbuf))
479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
482 return GNUNET_SYSERR;
485 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
490 if (0 == ACCESS (p, X_OK))
496 return GNUNET_SYSERR;
499 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
500 if (INVALID_SOCKET == rawsock)
502 DWORD err = GetLastError ();
504 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
505 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n",
507 return GNUNET_NO; /* not running as administrator */
509 closesocket (rawsock);
515 /* end of os_installation.c */