MinGW
[oweals/gnunet.git] / src / util / os_installation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file src/util/os_installation.c
23  * @brief get paths used by the program
24  * @author Milan
25  */
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #if 0                           /* keep Emacsens' auto-indent happy */
31 }
32 #endif
33 #endif
34
35 #include <sys/stat.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "platform.h"
41 #include "gnunet_common.h"
42 #include "gnunet_configuration_lib.h"
43 #include "gnunet_disk_lib.h"
44 #include "gnunet_os_lib.h"
45 #if DARWIN
46 #include <mach-o/ldsyms.h>
47 #include <mach-o/dyld.h>
48 #endif
49
50 #if LINUX
51 /**
52  * Try to determine path by reading /proc/PID/exe
53  */
54 static char *
55 get_path_from_proc_maps ()
56 {
57   char fn[64];
58   char *line;
59   char *dir;
60   FILE *f;
61
62   GNUNET_snprintf (fn, 64, "/proc/%u/maps", getpid ());
63   line = GNUNET_malloc (1024);
64   dir = GNUNET_malloc (1024);
65   f = fopen (fn, "r");
66   if (f != NULL)
67     {
68       while (NULL != fgets (line, 1024, f))
69         {
70           if ((1 == sscanf (line,
71                             "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
72                             dir)) && (NULL != strstr (dir, "libgnunetutil")))
73             {
74               strstr (dir, "libgnunetutil")[0] = '\0';
75               fclose (f);
76               GNUNET_free (line);
77               return dir;
78             }
79         }
80       fclose (f);
81     }
82   GNUNET_free (dir);
83   GNUNET_free (line);
84   return NULL;
85 }
86
87 /**
88  * Try to determine path by reading /proc/PID/exe
89  */
90 static char *
91 get_path_from_proc_exe ()
92 {
93   char fn[64];
94   char *lnk;
95   size_t size;
96
97   GNUNET_snprintf (fn, 64, "/proc/%u/exe", getpid ());
98   lnk = GNUNET_malloc (1024);
99   size = readlink (fn, lnk, 1023);
100   if ((size == 0) || (size >= 1024))
101     {
102       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
103       GNUNET_free (lnk);
104       return NULL;
105     }
106   lnk[size] = '\0';
107   while ((lnk[size] != '/') && (size > 0))
108     size--;
109   if ((size < 4) || (lnk[size - 4] != '/'))
110     {
111       /* not installed in "/bin/" -- binary path probably useless */
112       GNUNET_free (lnk);
113       return NULL;
114     }
115   lnk[size] = '\0';
116   return lnk;
117 }
118 #endif
119
120 #if WINDOWS
121 /**
122  * Try to determine path with win32-specific function
123  */
124 static char *
125 get_path_from_module_filename ()
126 {
127   char *path;
128   char *idx;
129
130   path = GNUNET_malloc (4097);
131   GetModuleFileName (NULL, path, 4096);
132   idx = path + strlen (path);
133   while ((idx > path) && (*idx != '\\') && (*idx != '/'))
134     idx--;
135   *idx = '\0';
136   return path;
137 }
138 #endif
139
140 #if DARWIN
141 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
142
143 static char *
144 get_path_from_NSGetExecutablePath ()
145 {
146   static char zero = '\0';
147   char *path;
148   size_t len;
149   MyNSGetExecutablePathProto func;
150   int ret;
151
152   path = NULL;
153   func =
154     (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
155   if (!func)
156     return NULL;
157   path = &zero;
158   len = 0;
159   /* get the path len, including the trailing \0 */
160   func (path, &len);
161   if (len == 0)
162     return NULL;
163   path = GNUNET_malloc (len);
164   ret = func (path, &len);
165   if (ret != 0)
166     {
167       GNUNET_free (path);
168       return NULL;
169     }
170   len = strlen (path);
171   while ((path[len] != '/') && (len > 0))
172     len--;
173   path[len] = '\0';
174   return path;
175 }
176
177 static char *
178 get_path_from_dyld_image ()
179 {
180   const char *path;
181   char *p, *s;
182   int i;
183   int c;
184
185   p = NULL;
186   c = _dyld_image_count ();
187   for (i = 0; i < c; i++)
188     {
189       if (_dyld_get_image_header (i) == &_mh_dylib_header)
190         {
191           path = _dyld_get_image_name (i);
192           if (path != NULL && strlen (path) > 0)
193             {
194               p = strdup (path);
195               s = p + strlen (p);
196               while ((s > p) && (*s != '/'))
197                 s--;
198               s++;
199               *s = '\0';
200             }
201           break;
202         }
203     }
204   return p;
205 }
206 #endif
207
208 static char *
209 get_path_from_PATH ()
210 {
211   char *path;
212   char *pos;
213   char *end;
214   char *buf;
215   const char *p;
216
217   p = getenv ("PATH");
218   if (p == NULL)
219     return NULL;
220   path = GNUNET_strdup (p);     /* because we write on it */
221   buf = GNUNET_malloc (strlen (path) + 20);
222   pos = path;
223
224   while (NULL != (end = strchr (pos, ':')))
225     {
226       *end = '\0';
227       sprintf (buf, "%s/%s", pos, "gnunetd");
228       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
229         {
230           pos = GNUNET_strdup (pos);
231           GNUNET_free (buf);
232           GNUNET_free (path);
233           return pos;
234         }
235       pos = end + 1;
236     }
237   sprintf (buf, "%s/%s", pos, "gnunetd");
238   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
239     {
240       pos = GNUNET_strdup (pos);
241       GNUNET_free (buf);
242       GNUNET_free (path);
243       return pos;
244     }
245   GNUNET_free (buf);
246   GNUNET_free (path);
247   return NULL;
248 }
249
250 static char *
251 get_path_from_GNUNET_PREFIX ()
252 {
253   const char *p;
254
255   p = getenv ("GNUNET_PREFIX");
256   if (p != NULL)
257     return GNUNET_strdup (p);
258   return NULL;
259 }
260
261 /*
262  * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
263  * @author Milan
264  *
265  * @return a pointer to the executable path, or NULL on error
266  */
267 static char *
268 os_get_gnunet_path ()
269 {
270   char *ret;
271
272   ret = get_path_from_GNUNET_PREFIX ();
273   if (ret != NULL)
274     return ret;
275 #if LINUX
276   ret = get_path_from_proc_maps ();
277   if (ret != NULL)
278     return ret;
279   ret = get_path_from_proc_exe ();
280   if (ret != NULL)
281     return ret;
282 #endif
283 #if WINDOWS
284   ret = get_path_from_module_filename ();
285   if (ret != NULL)
286     return ret;
287 #endif
288 #if DARWIN
289   ret = get_path_from_dyld_image ();
290   if (ret != NULL)
291     return ret;
292   ret = get_path_from_NSGetExecutablePath ();
293   if (ret != NULL)
294     return ret;
295 #endif
296   ret = get_path_from_PATH ();
297   if (ret != NULL)
298     return ret;
299   /* other attempts here */
300   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301               _
302               ("Could not determine installation path for GNUnet.  Set `%s' environment variable.\n"),
303               "GNUNET_PREFIX");
304   return NULL;
305 }
306
307 /*
308  * @brief get the path to current app's bin/
309  * @author Milan
310  *
311  * @return a pointer to the executable path, or NULL on error
312  */
313 static char *
314 os_get_exec_path ()
315 {
316   char *ret;
317
318   ret = NULL;
319 #if LINUX
320   ret = get_path_from_proc_exe ();
321   if (ret != NULL)
322     return ret;
323 #endif
324 #if WINDOWS
325   ret = get_path_from_module_filename ();
326   if (ret != NULL)
327     return ret;
328 #endif
329 #if DARWIN
330   ret = get_path_from_NSGetExecutablePath ();
331   if (ret != NULL)
332     return ret;
333 #endif
334   /* other attempts here */
335   return ret;
336 }
337
338
339
340 /**
341  * @brief get the path to a specific GNUnet installation directory or,
342  * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
343  * @author Milan
344  * @return a pointer to the dir path (to be freed by the caller)
345  */
346 char *
347 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
348 {
349   size_t n;
350   const char *dirname;
351   char *execpath = NULL;
352   char *tmp;
353   int isbasedir;
354
355   /* if wanted, try to get the current app's bin/ */
356   if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
357     execpath = os_get_exec_path ();
358
359   /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
360    * guess for the current app */
361   if (execpath == NULL)
362     execpath = os_get_gnunet_path ();
363
364   if (execpath == NULL)
365     return NULL;
366
367   n = strlen (execpath);
368   if (n == 0)
369     {
370       /* should never happen, but better safe than sorry */
371       GNUNET_free (execpath);
372       return NULL;
373     }
374   /* remove filename itself */
375   while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
376     execpath[--n] = '\0';
377
378   isbasedir = 1;
379   if ((n > 5) &&
380       ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
381        (0 == strcasecmp (&execpath[n - 5], "lib64"))))
382     {
383       if (dirkind != GNUNET_OS_IPK_LIBDIR)
384         {
385           /* strip '/lib32' or '/lib64' */
386           execpath[n - 5] = '\0';
387           n -= 5;
388         }
389       else
390         isbasedir = 0;
391     }
392   else if ((n > 3) &&
393            ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
394             (0 == strcasecmp (&execpath[n - 3], "lib"))))
395     {
396       /* strip '/bin' or '/lib' */
397       execpath[n - 3] = '\0';
398       n -= 3;
399     }
400   /* in case this was a directory named foo-bin, remove "foo-" */
401   while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
402     execpath[--n] = '\0';
403   switch (dirkind)
404     {
405     case GNUNET_OS_IPK_PREFIX:
406     case GNUNET_OS_IPK_SELF_PREFIX:
407       dirname = DIR_SEPARATOR_STR;
408       break;
409     case GNUNET_OS_IPK_BINDIR:
410       dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
411       break;
412     case GNUNET_OS_IPK_LIBDIR:
413       if (isbasedir)
414         dirname =
415           DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
416           DIR_SEPARATOR_STR;
417       else
418         dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
419       break;
420     case GNUNET_OS_IPK_DATADIR:
421       dirname =
422         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
423         DIR_SEPARATOR_STR;
424       break;
425     case GNUNET_OS_IPK_LOCALEDIR:
426       dirname =
427         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
428         DIR_SEPARATOR_STR;
429       break;
430     default:
431       GNUNET_free (execpath);
432       return NULL;
433     }
434   tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
435   sprintf (tmp, "%s%s", execpath, dirname);
436   GNUNET_free (execpath);
437   return tmp;
438 }
439
440 #if 0                           /* keep Emacsens' auto-indent happy */
441 {
442 #endif
443 #ifdef __cplusplus
444 }
445 #endif
446 /* end of os_installation.c */