aboutsummaryrefslogtreecommitdiff
path: root/src/util/os_installation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/os_installation.c')
-rw-r--r--src/util/os_installation.c294
1 files changed, 142 insertions, 152 deletions
diff --git a/src/util/os_installation.c b/src/util/os_installation.c
index 36627a8d8..188401abb 100644
--- a/src/util/os_installation.c
+++ b/src/util/os_installation.c
@@ -51,25 +51,21 @@ get_path_from_proc_maps ()
51 FILE *f; 51 FILE *f;
52 char *lgu; 52 char *lgu;
53 53
54 GNUNET_snprintf (fn, 54 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
55 sizeof(fn),
56 "/proc/%u/maps",
57 getpid ());
58 f = fopen (fn, "r"); 55 f = fopen (fn, "r");
59 if (f == NULL) 56 if (f == NULL)
60 return NULL; 57 return NULL;
61 while (NULL != fgets (line, sizeof(line), f)) 58 while (NULL != fgets (line, sizeof (line), f))
59 {
60 if ((1 == sscanf (line,
61 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
62 dir)) && (NULL != (lgu = strstr (dir, "libgnunetutil"))))
62 { 63 {
63 if ((1 == sscanf (line, 64 lgu[0] = '\0';
64 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", 65 fclose (f);
65 dir)) && 66 return GNUNET_strdup (dir);
66 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
67 {
68 lgu[0] = '\0';
69 fclose (f);
70 return GNUNET_strdup (dir);
71 }
72 } 67 }
68 }
73 fclose (f); 69 fclose (f);
74 return NULL; 70 return NULL;
75} 71}
@@ -84,23 +80,22 @@ get_path_from_proc_exe ()
84 char lnk[1024]; 80 char lnk[1024];
85 ssize_t size; 81 ssize_t size;
86 82
87 GNUNET_snprintf (fn, 83 GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ());
88 sizeof(fn), "/proc/%u/exe", getpid ()); 84 size = readlink (fn, lnk, sizeof (lnk) - 1);
89 size = readlink (fn, lnk, sizeof (lnk)-1);
90 if (size <= 0) 85 if (size <= 0)
91 { 86 {
92 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); 87 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
93 return NULL; 88 return NULL;
94 } 89 }
95 GNUNET_assert (size < sizeof (lnk)); 90 GNUNET_assert (size < sizeof (lnk));
96 lnk[size] = '\0'; 91 lnk[size] = '\0';
97 while ((lnk[size] != '/') && (size > 0)) 92 while ((lnk[size] != '/') && (size > 0))
98 size--; 93 size--;
99 if ((size < 4) || (lnk[size - 4] != '/')) 94 if ((size < 4) || (lnk[size - 4] != '/'))
100 { 95 {
101 /* not installed in "/bin/" -- binary path probably useless */ 96 /* not installed in "/bin/" -- binary path probably useless */
102 return NULL; 97 return NULL;
103 } 98 }
104 lnk[size] = '\0'; 99 lnk[size] = '\0';
105 return GNUNET_strdup (lnk); 100 return GNUNET_strdup (lnk);
106} 101}
@@ -116,7 +111,7 @@ get_path_from_module_filename ()
116 char path[4097]; 111 char path[4097];
117 char *idx; 112 char *idx;
118 113
119 GetModuleFileName (NULL, path, sizeof(path)-1); 114 GetModuleFileName (NULL, path, sizeof (path) - 1);
120 idx = path + strlen (path); 115 idx = path + strlen (path);
121 while ((idx > path) && (*idx != '\\') && (*idx != '/')) 116 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
122 idx--; 117 idx--;
@@ -139,7 +134,7 @@ get_path_from_NSGetExecutablePath ()
139 134
140 path = NULL; 135 path = NULL;
141 func = 136 func =
142 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); 137 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
143 if (!func) 138 if (!func)
144 return NULL; 139 return NULL;
145 path = &zero; 140 path = &zero;
@@ -151,10 +146,10 @@ get_path_from_NSGetExecutablePath ()
151 path = GNUNET_malloc (len); 146 path = GNUNET_malloc (len);
152 ret = func (path, &len); 147 ret = func (path, &len);
153 if (ret != 0) 148 if (ret != 0)
154 { 149 {
155 GNUNET_free (path); 150 GNUNET_free (path);
156 return NULL; 151 return NULL;
157 } 152 }
158 len = strlen (path); 153 len = strlen (path);
159 while ((path[len] != '/') && (len > 0)) 154 while ((path[len] != '/') && (len > 0))
160 len--; 155 len--;
@@ -173,22 +168,22 @@ get_path_from_dyld_image ()
173 p = NULL; 168 p = NULL;
174 c = _dyld_image_count (); 169 c = _dyld_image_count ();
175 for (i = 0; i < c; i++) 170 for (i = 0; i < c; i++)
171 {
172 if (_dyld_get_image_header (i) == &_mh_dylib_header)
176 { 173 {
177 if (_dyld_get_image_header (i) == &_mh_dylib_header) 174 path = _dyld_get_image_name (i);
178 { 175 if (path != NULL && strlen (path) > 0)
179 path = _dyld_get_image_name (i); 176 {
180 if (path != NULL && strlen (path) > 0) 177 p = strdup (path);
181 { 178 s = p + strlen (p);
182 p = strdup (path); 179 while ((s > p) && (*s != '/'))
183 s = p + strlen (p); 180 s--;
184 while ((s > p) && (*s != '/')) 181 s++;
185 s--; 182 *s = '\0';
186 s++; 183 }
187 *s = '\0'; 184 break;
188 }
189 break;
190 }
191 } 185 }
186 }
192 return p; 187 return p;
193} 188}
194#endif 189#endif
@@ -214,28 +209,28 @@ get_path_from_PATH (const char *binary)
214 return NULL; 209 return NULL;
215 path = GNUNET_strdup (p); /* because we write on it */ 210 path = GNUNET_strdup (p); /* because we write on it */
216 buf = GNUNET_malloc (strlen (path) + 20); 211 buf = GNUNET_malloc (strlen (path) + 20);
217 pos = path; 212 pos = path;
218 while (NULL != (end = strchr (pos, PATH_SEPARATOR))) 213 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
219 { 214 {
220 *end = '\0'; 215 *end = '\0';
221 sprintf (buf, "%s/%s", pos, binary); 216 sprintf (buf, "%s/%s", pos, binary);
222 if (GNUNET_DISK_file_test (buf) == GNUNET_YES) 217 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
223 {
224 pos = GNUNET_strdup (pos);
225 GNUNET_free (buf);
226 GNUNET_free (path);
227 return pos;
228 }
229 pos = end + 1;
230 }
231 sprintf (buf, "%s/%s", pos, binary);
232 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
233 { 218 {
234 pos = GNUNET_strdup (pos); 219 pos = GNUNET_strdup (pos);
235 GNUNET_free (buf); 220 GNUNET_free (buf);
236 GNUNET_free (path); 221 GNUNET_free (path);
237 return pos; 222 return pos;
238 } 223 }
224 pos = end + 1;
225 }
226 sprintf (buf, "%s/%s", pos, binary);
227 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
228 {
229 pos = GNUNET_strdup (pos);
230 GNUNET_free (buf);
231 GNUNET_free (path);
232 return pos;
233 }
239 GNUNET_free (buf); 234 GNUNET_free (buf);
240 GNUNET_free (path); 235 GNUNET_free (path);
241 return NULL; 236 return NULL;
@@ -294,8 +289,7 @@ os_get_gnunet_path ()
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _ 290 _
296 ("Could not determine installation path for %s. Set `%s' environment variable.\n"), 291 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
297 "GNUnet", 292 "GNUnet", "GNUNET_PREFIX");
298 "GNUNET_PREFIX");
299 return NULL; 293 return NULL;
300} 294}
301 295
@@ -361,11 +355,11 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
361 355
362 n = strlen (execpath); 356 n = strlen (execpath);
363 if (n == 0) 357 if (n == 0)
364 { 358 {
365 /* should never happen, but better safe than sorry */ 359 /* should never happen, but better safe than sorry */
366 GNUNET_free (execpath); 360 GNUNET_free (execpath);
367 return NULL; 361 return NULL;
368 } 362 }
369 /* remove filename itself */ 363 /* remove filename itself */
370 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 364 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
371 execpath[--n] = '\0'; 365 execpath[--n] = '\0';
@@ -374,62 +368,59 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
374 if ((n > 5) && 368 if ((n > 5) &&
375 ((0 == strcasecmp (&execpath[n - 5], "lib32")) || 369 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
376 (0 == strcasecmp (&execpath[n - 5], "lib64")))) 370 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
371 {
372 if (dirkind != GNUNET_OS_IPK_LIBDIR)
377 { 373 {
378 if (dirkind != GNUNET_OS_IPK_LIBDIR) 374 /* strip '/lib32' or '/lib64' */
379 { 375 execpath[n - 5] = '\0';
380 /* strip '/lib32' or '/lib64' */ 376 n -= 5;
381 execpath[n - 5] = '\0';
382 n -= 5;
383 }
384 else
385 isbasedir = 0;
386 } 377 }
378 else
379 isbasedir = 0;
380 }
387 else if ((n > 3) && 381 else if ((n > 3) &&
388 ((0 == strcasecmp (&execpath[n - 3], "bin")) || 382 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
389 (0 == strcasecmp (&execpath[n - 3], "lib")))) 383 (0 == strcasecmp (&execpath[n - 3], "lib"))))
390 { 384 {
391 /* strip '/bin' or '/lib' */ 385 /* strip '/bin' or '/lib' */
392 execpath[n - 3] = '\0'; 386 execpath[n - 3] = '\0';
393 n -= 3; 387 n -= 3;
394 } 388 }
395 /* in case this was a directory named foo-bin, remove "foo-" */ 389 /* in case this was a directory named foo-bin, remove "foo-" */
396 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) 390 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
397 execpath[--n] = '\0'; 391 execpath[--n] = '\0';
398 switch (dirkind) 392 switch (dirkind)
399 { 393 {
400 case GNUNET_OS_IPK_PREFIX: 394 case GNUNET_OS_IPK_PREFIX:
401 case GNUNET_OS_IPK_SELF_PREFIX: 395 case GNUNET_OS_IPK_SELF_PREFIX:
402 dirname = DIR_SEPARATOR_STR; 396 dirname = DIR_SEPARATOR_STR;
403 break; 397 break;
404 case GNUNET_OS_IPK_BINDIR: 398 case GNUNET_OS_IPK_BINDIR:
405 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; 399 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
406 break; 400 break;
407 case GNUNET_OS_IPK_LIBDIR: 401 case GNUNET_OS_IPK_LIBDIR:
408 if (isbasedir) 402 if (isbasedir)
409 dirname =
410 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
411 DIR_SEPARATOR_STR;
412 else
413 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
414 break;
415 case GNUNET_OS_IPK_DATADIR:
416 dirname =
417 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
418 DIR_SEPARATOR_STR;
419 break;
420 case GNUNET_OS_IPK_LOCALEDIR:
421 dirname =
422 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
423 DIR_SEPARATOR_STR;
424 break;
425 case GNUNET_OS_IPK_ICONDIR:
426 dirname = 403 dirname =
404 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
405 else
406 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
407 break;
408 case GNUNET_OS_IPK_DATADIR:
409 dirname =
410 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
411 break;
412 case GNUNET_OS_IPK_LOCALEDIR:
413 dirname =
414 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
415 break;
416 case GNUNET_OS_IPK_ICONDIR:
417 dirname =
427 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; 418 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
428 break; 419 break;
429 default: 420 default:
430 GNUNET_free (execpath); 421 GNUNET_free (execpath);
431 return NULL; 422 return NULL;
432 } 423 }
433 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); 424 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
434 sprintf (tmp, "%s%s", execpath, dirname); 425 sprintf (tmp, "%s%s", execpath, dirname);
435 GNUNET_free (execpath); 426 GNUNET_free (execpath);
@@ -454,6 +445,7 @@ GNUNET_OS_check_helper_binary (const char *binary)
454 struct stat statbuf; 445 struct stat statbuf;
455 char *p; 446 char *p;
456 char *pf; 447 char *pf;
448
457#ifdef MINGW 449#ifdef MINGW
458 SOCKET rawsock; 450 SOCKET rawsock;
459 char *binaryexe; 451 char *binaryexe;
@@ -461,61 +453,59 @@ GNUNET_OS_check_helper_binary (const char *binary)
461 GNUNET_asprintf (&binaryexe, "%s.exe", binary); 453 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
462 p = get_path_from_PATH (binaryexe); 454 p = get_path_from_PATH (binaryexe);
463 if (p != NULL) 455 if (p != NULL)
464 { 456 {
465 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); 457 GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
466 GNUNET_free (p); 458 GNUNET_free (p);
467 p = pf; 459 p = pf;
468 } 460 }
469 free (binaryexe); 461 free (binaryexe);
470#else 462#else
471 p = get_path_from_PATH (binary); 463 p = get_path_from_PATH (binary);
472 if (p != NULL) 464 if (p != NULL)
473 { 465 {
474 GNUNET_asprintf (&pf, "%s/%s", p, binary); 466 GNUNET_asprintf (&pf, "%s/%s", p, binary);
475 GNUNET_free (p); 467 GNUNET_free (p);
476 p = pf; 468 p = pf;
477 } 469 }
478#endif 470#endif
479 if (p == NULL) 471 if (p == NULL)
480 { 472 {
481 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 473 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
482 _("Could not find binary `%s' in PATH!\n"), 474 _("Could not find binary `%s' in PATH!\n"), binary);
483 binary); 475 return GNUNET_SYSERR;
484 return GNUNET_SYSERR; 476 }
485 }
486 if (0 != STAT (p, &statbuf)) 477 if (0 != STAT (p, &statbuf))
487 { 478 {
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 _("stat (%s) failed: %s\n"), 480 _("stat (%s) failed: %s\n"), p, STRERROR (errno));
490 p, 481 GNUNET_free (p);
491 STRERROR (errno)); 482 return GNUNET_SYSERR;
492 GNUNET_free (p); 483 }
493 return GNUNET_SYSERR;
494 }
495#ifndef MINGW 484#ifndef MINGW
496 if ( (0 != (statbuf.st_mode & S_ISUID)) && 485 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
497 (statbuf.st_uid == 0) ) 486 {
498 { 487 GNUNET_free (p);
499 GNUNET_free (p); 488 return GNUNET_YES;
500 return GNUNET_YES; 489 }
501 }
502 if (0 == ACCESS (p, X_OK)) 490 if (0 == ACCESS (p, X_OK))
503 { 491 {
504 GNUNET_free (p); 492 GNUNET_free (p);
505 return GNUNET_NO; 493 return GNUNET_NO;
506 } 494 }
507 GNUNET_free (p); 495 GNUNET_free (p);
508 return GNUNET_SYSERR; 496 return GNUNET_SYSERR;
509#else 497#else
510 GNUNET_free (p); 498 GNUNET_free (p);
511 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 499 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
512 if (INVALID_SOCKET == rawsock) 500 if (INVALID_SOCKET == rawsock)
513 { 501 {
514 DWORD err = GetLastError (); 502 DWORD err = GetLastError ();
515 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 503
516 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err); 504 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
517 return GNUNET_NO; /* not running as administrator */ 505 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n",
518 } 506 err);
507 return GNUNET_NO; /* not running as administrator */
508 }
519 closesocket (rawsock); 509 closesocket (rawsock);
520 return GNUNET_YES; 510 return GNUNET_YES;
521#endif 511#endif