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