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