libwine: Don't try to use the preloader on non-i386.
[wine] / libs / wine / config.c
1 /*
2  * Configuration parameters shared between Wine server and clients
3  *
4  * Copyright 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_PWD_H
34 #include <pwd.h>
35 #endif
36 #include "wine/library.h"
37
38 static const char server_config_dir[] = "/.wine";        /* config dir relative to $HOME */
39 static const char server_root_prefix[] = "/tmp/.wine-";  /* prefix for server root dir */
40 static const char server_dir_prefix[] = "/server-";      /* prefix for server dir */
41
42 static char *bindir;
43 static char *dlldir;
44 static char *datadir;
45 static char *config_dir;
46 static char *server_dir;
47 static char *build_dir;
48 static char *user_name;
49 static char *argv0_name;
50
51 #ifdef __GNUC__
52 static void fatal_error( const char *err, ... )  __attribute__((noreturn,format(printf,1,2)));
53 static void fatal_perror( const char *err, ... )  __attribute__((noreturn,format(printf,1,2)));
54 #endif
55
56 /* die on a fatal error */
57 static void fatal_error( const char *err, ... )
58 {
59     va_list args;
60
61     va_start( args, err );
62     fprintf( stderr, "wine: " );
63     vfprintf( stderr, err, args );
64     va_end( args );
65     exit(1);
66 }
67
68 /* die on a fatal error */
69 static void fatal_perror( const char *err, ... )
70 {
71     va_list args;
72
73     va_start( args, err );
74     fprintf( stderr, "wine: " );
75     vfprintf( stderr, err, args );
76     perror( " " );
77     va_end( args );
78     exit(1);
79 }
80
81 /* malloc wrapper */
82 static void *xmalloc( size_t size )
83 {
84     void *res;
85
86     if (!size) size = 1;
87     if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
88     return res;
89 }
90
91 /* strdup wrapper */
92 static char *xstrdup( const char *str )
93 {
94     size_t len = strlen(str) + 1;
95     char *res = xmalloc( len );
96     memcpy( res, str, len );
97     return res;
98 }
99
100 /* remove all trailing slashes from a path name */
101 static inline void remove_trailing_slashes( char *path )
102 {
103     int len = strlen( path );
104     while (len > 1 && path[len-1] == '/') path[--len] = 0;
105 }
106
107 /* build a path from the specified dir and name */
108 static char *build_path( const char *dir, const char *name )
109 {
110     size_t len = strlen(dir);
111     char *ret = xmalloc( len + strlen(name) + 2 );
112
113     memcpy( ret, dir, len );
114     if (len && ret[len-1] != '/') ret[len++] = '/';
115     strcpy( ret + len, name );
116     return ret;
117 }
118
119 /* return the directory that contains the library at run-time */
120 static char *get_runtime_libdir(void)
121 {
122 #ifdef HAVE_DLADDR
123     Dl_info info;
124     char *libdir;
125
126     if (dladdr( get_runtime_libdir, &info ) && info.dli_fname[0] == '/')
127     {
128         const char *p = strrchr( info.dli_fname, '/' );
129         unsigned int len = p - info.dli_fname;
130         if (!len) len++;  /* include initial slash */
131         libdir = xmalloc( len + 1 );
132         memcpy( libdir, info.dli_fname, len );
133         libdir[len] = 0;
134         return libdir;
135     }
136 #endif /* HAVE_DLADDR */
137     return NULL;
138 }
139
140 /* initialize the server directory value */
141 static void init_server_dir( dev_t dev, ino_t ino )
142 {
143     char *p;
144 #ifdef HAVE_GETUID
145     const unsigned int uid = getuid();
146 #else
147     const unsigned int uid = 0;
148 #endif
149
150     server_dir = xmalloc( sizeof(server_root_prefix) + 32 + sizeof(server_dir_prefix) +
151                           2*sizeof(dev) + 2*sizeof(ino) );
152     sprintf( server_dir, "%s%u%s", server_root_prefix, uid, server_dir_prefix );
153     p = server_dir + strlen(server_dir);
154
155     if (sizeof(dev) > sizeof(unsigned long) && dev > ~0UL)
156         p += sprintf( p, "%lx%08lx-", (unsigned long)((unsigned long long)dev >> 32), (unsigned long)dev );
157     else
158         p += sprintf( p, "%lx-", (unsigned long)dev );
159
160     if (sizeof(ino) > sizeof(unsigned long) && ino > ~0UL)
161         sprintf( p, "%lx%08lx", (unsigned long)((unsigned long long)ino >> 32), (unsigned long)ino );
162     else
163         sprintf( p, "%lx", (unsigned long)ino );
164 }
165
166 /* retrieve the default dll dir */
167 const char *get_dlldir( const char **default_dlldir )
168 {
169     *default_dlldir = DLLDIR;
170     return dlldir;
171 }
172
173 /* initialize all the paths values */
174 static void init_paths(void)
175 {
176     struct stat st;
177
178     const char *home = getenv( "HOME" );
179     const char *user = NULL;
180     const char *prefix = getenv( "WINEPREFIX" );
181
182 #ifdef HAVE_GETPWUID
183     char uid_str[32];
184     struct passwd *pwd = getpwuid( getuid() );
185
186     if (pwd)
187     {
188         user = pwd->pw_name;
189         if (!home) home = pwd->pw_dir;
190     }
191     if (!user)
192     {
193         sprintf( uid_str, "%lu", (unsigned long)getuid() );
194         user = uid_str;
195     }
196 #else  /* HAVE_GETPWUID */
197     if (!(user = getenv( "USER" )))
198         fatal_error( "cannot determine your user name, set the USER environment variable\n" );
199 #endif  /* HAVE_GETPWUID */
200     user_name = xstrdup( user );
201
202     /* build config_dir */
203
204     if (prefix)
205     {
206         config_dir = xstrdup( prefix );
207         remove_trailing_slashes( config_dir );
208         if (config_dir[0] != '/')
209             fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
210         if (stat( config_dir, &st ) == -1)
211         {
212             if (errno == ENOENT) return;  /* will be created later on */
213             fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
214         }
215     }
216     else
217     {
218         if (!home) fatal_error( "could not determine your home directory\n" );
219         if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
220         config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
221         strcpy( config_dir, home );
222         remove_trailing_slashes( config_dir );
223         strcat( config_dir, server_config_dir );
224         if (stat( config_dir, &st ) == -1)
225         {
226             if (errno == ENOENT) return;  /* will be created later on */
227             fatal_perror( "cannot open %s", config_dir );
228         }
229     }
230     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
231 #ifdef HAVE_GETUID
232     if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", config_dir );
233 #endif
234
235     init_server_dir( st.st_dev, st.st_ino );
236 }
237
238 /* check if basedir is a valid build dir by checking for wineserver and ntdll */
239 /* helper for running_from_build_dir */
240 static inline int is_valid_build_dir( char *basedir, int baselen )
241 {
242     struct stat st;
243
244     strcpy( basedir + baselen, "/server/wineserver" );
245     if (stat( basedir, &st ) == -1) return 0;  /* no wineserver found */
246     /* check for ntdll too to make sure */
247     strcpy( basedir + baselen, "/dlls/ntdll/ntdll.dll.so" );
248     if (stat( basedir, &st ) == -1) return 0;  /* no ntdll found */
249
250     basedir[baselen] = 0;
251     return 1;
252 }
253
254 /* check if we are running from the build directory */
255 static char *running_from_build_dir( const char *basedir, const char *bindir )
256 {
257     struct stat st;
258     const char *p;
259     char *path;
260     int res;
261
262     if (!(path = build_path( bindir, "wineserver" ))) return NULL;
263     res = stat( path, &st );
264     free( path );
265     if (res != -1) return NULL;  /* the real bindir is valid */
266
267     /* remove last component from basedir */
268     p = basedir + strlen(basedir) - 1;
269     while (p > basedir && *p == '/') p--;
270     while (p > basedir && *p != '/') p--;
271     if (p == basedir) return NULL;
272     path = xmalloc( p - basedir + sizeof("/dlls/ntdll/ntdll.dll.so") );
273     memcpy( path, basedir, p - basedir );
274
275     if (!is_valid_build_dir( path, p - basedir ))
276     {
277         /* remove another component */
278         while (p > basedir && *p == '/') p--;
279         while (p > basedir && *p != '/') p--;
280         if (p == basedir || !is_valid_build_dir( path, p - basedir ))
281         {
282             free( path );
283             return NULL;
284         }
285     }
286     return path;
287 }
288
289 /* initialize the argv0 path */
290 void wine_init_argv0_path( const char *argv0 )
291 {
292     size_t size, len;
293     const char *p, *basename;
294     char *cwd, *libdir;
295
296     if (!(p = strrchr( argv0, '/' )))
297         basename = argv0;
298     else
299         basename = p + 1;
300
301     argv0_name = xstrdup( basename );
302
303     if ((libdir = get_runtime_libdir()))
304     {
305         bindir = build_path( libdir, LIB_TO_BINDIR );
306         if ((build_dir = running_from_build_dir( libdir, bindir )))
307         {
308             free( libdir );
309             goto in_build_dir;
310         }
311         dlldir = build_path( libdir, LIB_TO_DLLDIR );
312         datadir = build_path( libdir, LIB_TO_DATADIR );
313         free( libdir );
314         return;
315     }
316
317     if (!p) return;  /* if argv0 doesn't contain a path, don't store anything */
318
319     len = p - argv0;
320     if (!len) len++;  /* include leading slash */
321
322     if (argv0[0] == '/')  /* absolute path */
323     {
324         bindir = xmalloc( len + 1 );
325         memcpy( bindir, argv0, len );
326         bindir[len] = 0;
327     }
328     else
329     {
330         /* relative path, make it absolute */
331         for (size = 256 + len; ; size *= 2)
332         {
333             if (!(cwd = malloc( size ))) return;
334             if (getcwd( cwd, size - len ))
335             {
336                 bindir = cwd;
337                 cwd += strlen(cwd);
338                 *cwd++ = '/';
339                 memcpy( cwd, argv0, len );
340                 cwd[len] = 0;
341                 break;
342             }
343             free( cwd );
344             if (errno != ERANGE) return;
345         }
346     }
347
348     if ((build_dir = running_from_build_dir( bindir, bindir ))) goto in_build_dir;
349
350     dlldir = build_path( bindir, BIN_TO_DLLDIR );
351     datadir = build_path( bindir, BIN_TO_DATADIR );
352     return;
353
354 in_build_dir:
355     free( bindir );
356     free( argv0_name );
357     bindir = NULL;
358     argv0_name = build_path( "loader/", basename );
359 }
360
361 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
362 const char *wine_get_config_dir(void)
363 {
364     if (!config_dir) init_paths();
365     return config_dir;
366 }
367
368 /* retrieve the wine data dir */
369 const char *wine_get_data_dir(void)
370 {
371     return datadir;
372 }
373
374 /* retrieve the wine build dir (if we are running from there) */
375 const char *wine_get_build_dir(void)
376 {
377     return build_dir;
378 }
379
380 /* return the full name of the server directory (the one containing the socket) */
381 const char *wine_get_server_dir(void)
382 {
383     if (!server_dir)
384     {
385         if (!config_dir) init_paths();
386         else
387         {
388             struct stat st;
389
390             if (stat( config_dir, &st ) == -1)
391             {
392                 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
393                 return NULL;  /* will have to try again once config_dir has been created */
394             }
395             init_server_dir( st.st_dev, st.st_ino );
396         }
397     }
398     return server_dir;
399 }
400
401 /* return the current user name */
402 const char *wine_get_user_name(void)
403 {
404     if (!user_name) init_paths();
405     return user_name;
406 }
407
408 /* return the standard version string */
409 const char *wine_get_version(void)
410 {
411     return PACKAGE_VERSION;
412 }
413
414 /* return the build id string */
415 const char *wine_get_build_id(void)
416 {
417     extern const char wine_build[];
418     return wine_build;
419 }
420
421 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
422 static void preloader_exec( char **argv, int use_preloader )
423 {
424 #if defined(linux) && defined(__i386__)
425     if (use_preloader)
426     {
427         static const char preloader[] = "wine-preloader";
428         char *p, *full_name;
429         char **last_arg = argv, **new_argv;
430
431         if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
432         else p++;
433
434         full_name = xmalloc( p - argv[0] + sizeof(preloader) );
435         memcpy( full_name, argv[0], p - argv[0] );
436         memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
437
438         /* make a copy of argv */
439         while (*last_arg) last_arg++;
440         new_argv = xmalloc( (last_arg - argv + 2) * sizeof(*argv) );
441         memcpy( new_argv + 1, argv, (last_arg - argv + 1) * sizeof(*argv) );
442         new_argv[0] = full_name;
443         execv( full_name, new_argv );
444         free( new_argv );
445         free( full_name );
446         return;
447     }
448 #endif
449     execv( argv[0], argv );
450 }
451
452 /* exec a wine internal binary (either the wine loader or the wine server) */
453 void wine_exec_wine_binary( const char *name, char **argv, const char *env_var )
454 {
455     const char *path, *pos, *ptr;
456     int use_preloader = 0;
457
458     if (!name)  /* no name means default loader */
459     {
460         name = argv0_name;
461         use_preloader = 1;
462     }
463
464     if ((ptr = strrchr( name, '/' )))
465     {
466         /* if we are in build dir and name contains a path, try that */
467         if (build_dir)
468         {
469             argv[0] = build_path( build_dir, name );
470             preloader_exec( argv, use_preloader );
471             free( argv[0] );
472         }
473         name = ptr + 1;  /* get rid of path */
474     }
475
476     /* first, bin directory from the current libdir or argv0 */
477     if (bindir)
478     {
479         argv[0] = build_path( bindir, name );
480         preloader_exec( argv, use_preloader );
481         free( argv[0] );
482     }
483
484     /* then specified environment variable */
485     if (env_var)
486     {
487         argv[0] = (char *)env_var;
488         preloader_exec( argv, use_preloader );
489     }
490
491     /* now search in the Unix path */
492     if ((path = getenv( "PATH" )))
493     {
494         argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
495         pos = path;
496         for (;;)
497         {
498             while (*pos == ':') pos++;
499             if (!*pos) break;
500             if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
501             memcpy( argv[0], pos, ptr - pos );
502             strcpy( argv[0] + (ptr - pos), "/" );
503             strcat( argv[0] + (ptr - pos), name );
504             preloader_exec( argv, use_preloader );
505             pos = ptr;
506         }
507         free( argv[0] );
508     }
509
510     /* and finally try BINDIR */
511     argv[0] = build_path( BINDIR, name );
512     preloader_exec( argv, use_preloader );
513     free( argv[0] );
514 }