rpcrt4/tests: Use standard wine_dbgstr_longlong.
[wine.git] / libs / wine / config.c
blob13f016af762275ff53fcbc5ddab27bf9513612d2
1 /*
2 * Configuration parameters shared between Wine server and clients
4 * Copyright 2002 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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"
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 */
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;
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
56 #if defined(__linux__) || defined(__FreeBSD_kernel__ )
57 #define EXE_LINK "/proc/self/exe"
58 #elif defined (__FreeBSD__) || defined(__DragonFly__)
59 #define EXE_LINK "/proc/curproc/file"
60 #endif
62 /* die on a fatal error */
63 static void fatal_error( const char *err, ... )
65 va_list args;
67 va_start( args, err );
68 fprintf( stderr, "wine: " );
69 vfprintf( stderr, err, args );
70 va_end( args );
71 exit(1);
74 /* die on a fatal error */
75 static void fatal_perror( const char *err, ... )
77 va_list args;
79 va_start( args, err );
80 fprintf( stderr, "wine: " );
81 vfprintf( stderr, err, args );
82 perror( " " );
83 va_end( args );
84 exit(1);
87 /* malloc wrapper */
88 static void *xmalloc( size_t size )
90 void *res;
92 if (!size) size = 1;
93 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
94 return res;
97 /* strdup wrapper */
98 static char *xstrdup( const char *str )
100 size_t len = strlen(str) + 1;
101 char *res = xmalloc( len );
102 memcpy( res, str, len );
103 return res;
106 /* check if a string ends in a given substring */
107 static inline int strendswith( const char* str, const char* end )
109 size_t len = strlen( str );
110 size_t tail = strlen( end );
111 return len >= tail && !strcmp( str + len - tail, end );
114 /* remove all trailing slashes from a path name */
115 static inline void remove_trailing_slashes( char *path )
117 int len = strlen( path );
118 while (len > 1 && path[len-1] == '/') path[--len] = 0;
121 /* build a path from the specified dir and name */
122 static char *build_path( const char *dir, const char *name )
124 size_t len = strlen(dir);
125 char *ret = xmalloc( len + strlen(name) + 2 );
127 memcpy( ret, dir, len );
128 if (len && ret[len-1] != '/') ret[len++] = '/';
129 strcpy( ret + len, name );
130 return ret;
133 /* return the directory that contains the library at run-time */
134 static char *get_runtime_libdir(void)
136 #ifdef HAVE_DLADDR
137 Dl_info info;
138 char *libdir;
140 if (dladdr( get_runtime_libdir, &info ) && info.dli_fname[0] == '/')
142 const char *p = strrchr( info.dli_fname, '/' );
143 unsigned int len = p - info.dli_fname;
144 if (!len) len++; /* include initial slash */
145 libdir = xmalloc( len + 1 );
146 memcpy( libdir, info.dli_fname, len );
147 libdir[len] = 0;
148 return libdir;
150 #endif /* HAVE_DLADDR */
151 return NULL;
154 /* return the directory that contains the main exe at run-time */
155 static char *get_runtime_exedir(void)
157 #ifdef EXE_LINK
158 char *p, *bindir;
159 int size;
161 for (size = 256; ; size *= 2)
163 int ret;
164 if (!(bindir = malloc( size ))) return NULL;
165 if ((ret = readlink( EXE_LINK, bindir, size )) == -1) break;
166 if (ret != size)
168 bindir[ret] = 0;
169 if (!(p = strrchr( bindir, '/' ))) break;
170 if (p == bindir) p++;
171 *p = 0;
172 return bindir;
174 free( bindir );
176 free( bindir );
177 #endif
178 return NULL;
181 /* return the base directory from argv0 */
182 static char *get_runtime_argvdir( const char *argv0 )
184 char *p, *bindir, *cwd;
185 int len, size;
187 if (!(p = strrchr( argv0, '/' ))) return NULL;
189 len = p - argv0;
190 if (!len) len++; /* include leading slash */
192 if (argv0[0] == '/') /* absolute path */
194 bindir = xmalloc( len + 1 );
195 memcpy( bindir, argv0, len );
196 bindir[len] = 0;
198 else
200 /* relative path, make it absolute */
201 for (size = 256 + len; ; size *= 2)
203 if (!(cwd = malloc( size ))) return NULL;
204 if (getcwd( cwd, size - len ))
206 bindir = cwd;
207 cwd += strlen(cwd);
208 *cwd++ = '/';
209 memcpy( cwd, argv0, len );
210 cwd[len] = 0;
211 break;
213 free( cwd );
214 if (errno != ERANGE) return NULL;
217 return bindir;
220 /* initialize the server directory value */
221 static void init_server_dir( dev_t dev, ino_t ino )
223 char *p, *root;
225 #ifdef __ANDROID__ /* there's no /tmp dir on Android */
226 root = build_path( config_dir, ".wineserver" );
227 #elif defined(HAVE_GETUID)
228 root = xmalloc( sizeof(server_root_prefix) + 12 );
229 sprintf( root, "%s-%u", server_root_prefix, getuid() );
230 #else
231 root = xstrdup( server_root_prefix );
232 #endif
234 server_dir = xmalloc( strlen(root) + sizeof(server_dir_prefix) + 2*sizeof(dev) + 2*sizeof(ino) + 2 );
235 strcpy( server_dir, root );
236 strcat( server_dir, server_dir_prefix );
237 p = server_dir + strlen(server_dir);
239 if (dev != (unsigned long)dev)
240 p += sprintf( p, "%lx%08lx-", (unsigned long)((unsigned long long)dev >> 32), (unsigned long)dev );
241 else
242 p += sprintf( p, "%lx-", (unsigned long)dev );
244 if (ino != (unsigned long)ino)
245 sprintf( p, "%lx%08lx", (unsigned long)((unsigned long long)ino >> 32), (unsigned long)ino );
246 else
247 sprintf( p, "%lx", (unsigned long)ino );
248 free( root );
251 /* retrieve the default dll dir */
252 const char *get_dlldir( const char **default_dlldir )
254 *default_dlldir = DLLDIR;
255 return dlldir;
258 /* initialize all the paths values */
259 static void init_paths(void)
261 struct stat st;
263 const char *home = getenv( "HOME" );
264 const char *user = NULL;
265 const char *prefix = getenv( "WINEPREFIX" );
267 #ifdef HAVE_GETPWUID
268 char uid_str[32];
269 struct passwd *pwd = getpwuid( getuid() );
271 if (pwd)
273 user = pwd->pw_name;
274 if (!home) home = pwd->pw_dir;
276 if (!user)
278 sprintf( uid_str, "%lu", (unsigned long)getuid() );
279 user = uid_str;
281 #else /* HAVE_GETPWUID */
282 if (!(user = getenv( "USER" )))
283 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
284 #endif /* HAVE_GETPWUID */
285 user_name = xstrdup( user );
287 /* build config_dir */
289 if (prefix)
291 config_dir = xstrdup( prefix );
292 remove_trailing_slashes( config_dir );
293 if (config_dir[0] != '/')
294 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
295 if (stat( config_dir, &st ) == -1)
297 if (errno == ENOENT) return; /* will be created later on */
298 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
301 else
303 if (!home) fatal_error( "could not determine your home directory\n" );
304 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
305 config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
306 strcpy( config_dir, home );
307 remove_trailing_slashes( config_dir );
308 strcat( config_dir, server_config_dir );
309 if (stat( config_dir, &st ) == -1)
311 if (errno == ENOENT) return; /* will be created later on */
312 fatal_perror( "cannot open %s", config_dir );
315 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
316 #ifdef HAVE_GETUID
317 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", config_dir );
318 #endif
320 init_server_dir( st.st_dev, st.st_ino );
323 /* check if bindir is valid by checking for wineserver */
324 static int is_valid_bindir( const char *bindir )
326 struct stat st;
327 char *path = build_path( bindir, "wineserver" );
328 int ret = (stat( path, &st ) != -1);
329 free( path );
330 return ret;
333 /* check if dlldir is valid by checking for ntdll */
334 static int is_valid_dlldir( const char *dlldir )
336 struct stat st;
337 char *path = build_path( dlldir, "ntdll.dll.so" );
338 int ret = (stat( path, &st ) != -1);
339 free( path );
340 return ret;
343 /* check if basedir is a valid build dir by checking for wineserver and ntdll */
344 /* helper for running_from_build_dir */
345 static inline int is_valid_build_dir( char *basedir, int baselen )
347 struct stat st;
349 strcpy( basedir + baselen, "/server/wineserver" );
350 if (stat( basedir, &st ) == -1) return 0; /* no wineserver found */
351 /* check for ntdll too to make sure */
352 strcpy( basedir + baselen, "/dlls/ntdll/ntdll.dll.so" );
353 if (stat( basedir, &st ) == -1) return 0; /* no ntdll found */
355 basedir[baselen] = 0;
356 return 1;
359 /* check if we are running from the build directory */
360 static char *running_from_build_dir( const char *basedir )
362 const char *p;
363 char *path;
365 /* remove last component from basedir */
366 p = basedir + strlen(basedir) - 1;
367 while (p > basedir && *p == '/') p--;
368 while (p > basedir && *p != '/') p--;
369 if (p == basedir) return NULL;
370 path = xmalloc( p - basedir + sizeof("/dlls/ntdll/ntdll.dll.so") );
371 memcpy( path, basedir, p - basedir );
373 if (!is_valid_build_dir( path, p - basedir ))
375 /* remove another component */
376 while (p > basedir && *p == '/') p--;
377 while (p > basedir && *p != '/') p--;
378 if (p == basedir || !is_valid_build_dir( path, p - basedir ))
380 free( path );
381 return NULL;
384 return path;
387 /* try to set the specified directory as bindir, or set build_dir if it's inside the build directory */
388 static int set_bindir( char *dir )
390 if (!dir) return 0;
391 if (is_valid_bindir( dir ))
393 bindir = dir;
394 dlldir = build_path( bindir, BIN_TO_DLLDIR );
396 else
398 build_dir = running_from_build_dir( dir );
399 free( dir );
401 return bindir || build_dir;
404 /* try to set the specified directory as dlldir, or set build_dir if it's inside the build directory */
405 static int set_dlldir( char *libdir )
407 char *path;
409 if (!libdir) return 0;
411 path = build_path( libdir, LIB_TO_DLLDIR );
412 if (is_valid_dlldir( path ))
414 dlldir = path;
415 bindir = build_path( libdir, LIB_TO_BINDIR );
417 else
419 build_dir = running_from_build_dir( libdir );
420 free( path );
422 free( libdir );
423 return dlldir || build_dir;
426 /* initialize the argv0 path */
427 void wine_init_argv0_path( const char *argv0 )
429 const char *basename, *wineloader;
431 if (!(basename = strrchr( argv0, '/' ))) basename = argv0;
432 else basename++;
434 if (set_bindir( get_runtime_exedir() )) goto done;
435 if (set_dlldir( get_runtime_libdir() )) goto done;
436 if (set_bindir( get_runtime_argvdir( argv0 ))) goto done;
437 if ((wineloader = getenv( "WINELOADER" ))) set_bindir( get_runtime_argvdir( wineloader ));
439 done:
440 if (build_dir)
442 argv0_name = build_path( "loader/", basename );
444 else
446 if (bindir) datadir = build_path( bindir, BIN_TO_DATADIR );
447 argv0_name = xstrdup( basename );
451 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
452 const char *wine_get_config_dir(void)
454 if (!config_dir) init_paths();
455 return config_dir;
458 /* retrieve the wine data dir */
459 const char *wine_get_data_dir(void)
461 return datadir;
464 /* retrieve the wine build dir (if we are running from there) */
465 const char *wine_get_build_dir(void)
467 return build_dir;
470 /* return the full name of the server directory (the one containing the socket) */
471 const char *wine_get_server_dir(void)
473 if (!server_dir)
475 if (!config_dir) init_paths();
476 else
478 struct stat st;
480 if (stat( config_dir, &st ) == -1)
482 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
483 return NULL; /* will have to try again once config_dir has been created */
485 init_server_dir( st.st_dev, st.st_ino );
488 return server_dir;
491 /* return the current user name */
492 const char *wine_get_user_name(void)
494 if (!user_name) init_paths();
495 return user_name;
498 /* return the standard version string */
499 const char *wine_get_version(void)
501 return PACKAGE_VERSION;
504 /* return the build id string */
505 const char *wine_get_build_id(void)
507 extern const char wine_build[];
508 return wine_build;
511 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
512 static void preloader_exec( char **argv, int use_preloader )
514 if (use_preloader)
516 static const char preloader[] = "wine-preloader";
517 static const char preloader64[] = "wine64-preloader";
518 char *p, *full_name;
519 char **last_arg = argv, **new_argv;
521 if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
522 else p++;
524 full_name = xmalloc( p - argv[0] + sizeof(preloader64) );
525 memcpy( full_name, argv[0], p - argv[0] );
526 if (strendswith( p, "64" ))
527 memcpy( full_name + (p - argv[0]), preloader64, sizeof(preloader64) );
528 else
529 memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
531 /* make a copy of argv */
532 while (*last_arg) last_arg++;
533 new_argv = xmalloc( (last_arg - argv + 2) * sizeof(*argv) );
534 memcpy( new_argv + 1, argv, (last_arg - argv + 1) * sizeof(*argv) );
535 new_argv[0] = full_name;
536 execv( full_name, new_argv );
537 free( new_argv );
538 free( full_name );
540 execv( argv[0], argv );
543 /* exec a wine internal binary (either the wine loader or the wine server) */
544 void wine_exec_wine_binary( const char *name, char **argv, const char *env_var )
546 const char *path, *pos, *ptr;
547 int use_preloader;
549 if (!name) name = argv0_name; /* no name means default loader */
551 #ifdef linux
552 use_preloader = !strendswith( name, "wineserver" );
553 #else
554 use_preloader = 0;
555 #endif
557 if ((ptr = strrchr( name, '/' )))
559 /* if we are in build dir and name contains a path, try that */
560 if (build_dir)
562 argv[0] = build_path( build_dir, name );
563 preloader_exec( argv, use_preloader );
564 free( argv[0] );
566 name = ptr + 1; /* get rid of path */
569 /* first, bin directory from the current libdir or argv0 */
570 if (bindir)
572 argv[0] = build_path( bindir, name );
573 preloader_exec( argv, use_preloader );
574 free( argv[0] );
577 /* then specified environment variable */
578 if (env_var)
580 argv[0] = (char *)env_var;
581 preloader_exec( argv, use_preloader );
584 /* now search in the Unix path */
585 if ((path = getenv( "PATH" )))
587 argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
588 pos = path;
589 for (;;)
591 while (*pos == ':') pos++;
592 if (!*pos) break;
593 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
594 memcpy( argv[0], pos, ptr - pos );
595 strcpy( argv[0] + (ptr - pos), "/" );
596 strcat( argv[0] + (ptr - pos), name );
597 preloader_exec( argv, use_preloader );
598 pos = ptr;
600 free( argv[0] );
603 /* and finally try BINDIR */
604 argv[0] = build_path( BINDIR, name );
605 preloader_exec( argv, use_preloader );
606 free( argv[0] );