winegstreamer: Use gst_audio_info_to_caps for media type translation.
[wine.git] / libs / wine / config.c
blob6d4ecb95ae4e78db1705cfa9315fbb414181463c
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 #ifdef __APPLE__
37 #include <crt_externs.h>
38 #include <spawn.h>
39 #ifndef _POSIX_SPAWN_DISABLE_ASLR
40 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
41 #endif
42 #endif
43 #include "wine/asm.h"
45 static char *bindir;
46 static char *dlldir;
47 static char *datadir;
48 const char *build_dir;
49 static char *argv0_name;
50 static char *wineserver64;
52 #ifdef __GNUC__
53 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
54 #endif
56 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
57 static const char exe_link[] = "/proc/self/exe";
58 #elif defined (__FreeBSD__) || defined(__DragonFly__)
59 static const char exe_link[] = "/proc/curproc/file";
60 #else
61 static const char exe_link[] = "";
62 #endif
64 /* die on a fatal error */
65 static void fatal_error( const char *err, ... )
67 va_list args;
69 va_start( args, err );
70 fprintf( stderr, "wine: " );
71 vfprintf( stderr, err, args );
72 va_end( args );
73 exit(1);
76 /* malloc wrapper */
77 static void *xmalloc( size_t size )
79 void *res;
81 if (!size) size = 1;
82 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
83 return res;
86 /* strdup wrapper */
87 static char *xstrdup( const char *str )
89 size_t len = strlen(str) + 1;
90 char *res = xmalloc( len );
91 memcpy( res, str, len );
92 return res;
95 /* build a path from the specified dir and name */
96 static char *build_path( const char *dir, const char *name )
98 size_t len = strlen(dir);
99 char *ret = xmalloc( len + strlen(name) + 2 );
101 memcpy( ret, dir, len );
102 if (len && ret[len-1] != '/') ret[len++] = '/';
103 strcpy( ret + len, name );
104 return ret;
107 /* return the directory that contains the library at run-time */
108 static char *get_runtime_libdir(void)
110 #ifdef HAVE_DLADDR
111 Dl_info info;
112 char *libdir;
114 if (dladdr( get_runtime_libdir, &info ) && info.dli_fname[0] == '/')
116 const char *p = strrchr( info.dli_fname, '/' );
117 unsigned int len = p - info.dli_fname;
118 if (!len) len++; /* include initial slash */
119 libdir = xmalloc( len + 1 );
120 memcpy( libdir, info.dli_fname, len );
121 libdir[len] = 0;
122 return libdir;
124 #endif /* HAVE_DLADDR */
125 return NULL;
128 /* read a symlink and return its directory */
129 static char *symlink_dirname( const char *name )
131 char *p, *fullpath = realpath( name, NULL );
133 if (fullpath)
135 p = strrchr( fullpath, '/' );
136 if (p == fullpath) p++;
137 if (p) *p = 0;
139 return fullpath;
142 /* return the directory that contains the main exe at run-time */
143 static char *get_runtime_exedir(void)
145 if (exe_link[0]) return symlink_dirname( exe_link );
146 return NULL;
149 /* return the base directory from argv0 */
150 static char *get_runtime_argvdir( const char *argv0 )
152 char *p, *bindir, *cwd;
153 int len, size;
155 if (!(p = strrchr( argv0, '/' ))) return NULL;
157 len = p - argv0;
158 if (!len) len++; /* include leading slash */
160 if (argv0[0] == '/') /* absolute path */
162 bindir = xmalloc( len + 1 );
163 memcpy( bindir, argv0, len );
164 bindir[len] = 0;
166 else
168 /* relative path, make it absolute */
169 for (size = 256 + len; ; size *= 2)
171 if (!(cwd = malloc( size ))) return NULL;
172 if (getcwd( cwd, size - len ))
174 bindir = cwd;
175 cwd += strlen(cwd);
176 *cwd++ = '/';
177 memcpy( cwd, argv0, len );
178 cwd[len] = 0;
179 break;
181 free( cwd );
182 if (errno != ERANGE) return NULL;
185 return bindir;
188 /* retrieve the default dll dir */
189 const char *get_dlldir( const char **default_dlldir )
191 *default_dlldir = DLLDIR;
192 return dlldir;
195 /* check if bindir is valid by checking for wineserver */
196 static int is_valid_bindir( const char *bindir )
198 struct stat st;
199 char *path = build_path( bindir, "wineserver" );
200 int ret = (stat( path, &st ) != -1);
201 free( path );
202 return ret;
205 /* check if dlldir is valid by checking for ntdll */
206 static int is_valid_dlldir( const char *dlldir )
208 struct stat st;
209 char *path = build_path( dlldir, "ntdll.dll.so" );
210 int ret = (stat( path, &st ) != -1);
211 free( path );
212 return ret;
215 /* check if basedir is a valid build dir by checking for wineserver and ntdll */
216 /* helper for running_from_build_dir */
217 static inline int is_valid_build_dir( char *basedir, int baselen )
219 struct stat st;
221 strcpy( basedir + baselen, "/server/wineserver" );
222 if (stat( basedir, &st ) == -1) return 0; /* no wineserver found */
223 /* check for ntdll too to make sure */
224 strcpy( basedir + baselen, "/dlls/ntdll/ntdll.dll.so" );
225 if (stat( basedir, &st ) == -1) return 0; /* no ntdll found */
227 basedir[baselen] = 0;
228 return 1;
231 /* check if we are running from the build directory */
232 static char *running_from_build_dir( const char *basedir )
234 const char *p;
235 char *path;
237 /* remove last component from basedir */
238 p = basedir + strlen(basedir) - 1;
239 while (p > basedir && *p == '/') p--;
240 while (p > basedir && *p != '/') p--;
241 if (p == basedir) return NULL;
242 path = xmalloc( p - basedir + sizeof("/dlls/ntdll/ntdll.dll.so") );
243 memcpy( path, basedir, p - basedir );
245 if (!is_valid_build_dir( path, p - basedir ))
247 /* remove another component */
248 while (p > basedir && *p == '/') p--;
249 while (p > basedir && *p != '/') p--;
250 if (p == basedir || !is_valid_build_dir( path, p - basedir ))
252 free( path );
253 return NULL;
256 return path;
259 /* try to set the specified directory as bindir, or set build_dir if it's inside the build directory */
260 static int set_bindir( char *dir )
262 if (!dir) return 0;
263 if (is_valid_bindir( dir ))
265 bindir = dir;
266 dlldir = build_path( bindir, BIN_TO_DLLDIR );
268 else
270 build_dir = running_from_build_dir( dir );
271 free( dir );
273 return bindir || build_dir;
276 /* try to set the specified directory as dlldir, or set build_dir if it's inside the build directory */
277 static int set_dlldir( char *libdir )
279 char *path;
281 if (!libdir) return 0;
283 path = build_path( libdir, LIB_TO_DLLDIR );
284 if (is_valid_dlldir( path ))
286 dlldir = path;
287 bindir = build_path( libdir, LIB_TO_BINDIR );
289 else
291 build_dir = running_from_build_dir( libdir );
292 free( path );
294 free( libdir );
295 return dlldir || build_dir;
298 /* initialize the argv0 path */
299 void wine_init_argv0_path( const char *argv0 )
301 const char *basename, *wineloader;
303 if (!(basename = strrchr( argv0, '/' ))) basename = argv0;
304 else basename++;
306 if (set_bindir( get_runtime_exedir() )) goto done;
307 if (set_dlldir( get_runtime_libdir() )) goto done;
308 if (set_bindir( get_runtime_argvdir( argv0 ))) goto done;
309 if ((wineloader = getenv( "WINELOADER" ))) set_bindir( get_runtime_argvdir( wineloader ));
311 done:
312 if (build_dir)
314 argv0_name = build_path( "loader/", basename );
315 if (sizeof(int) == sizeof(void *))
317 char *loader, *linkname = build_path( build_dir, "loader/wine64" );
318 if ((loader = symlink_dirname( linkname )))
320 wineserver64 = build_path( loader, "../server/wineserver" );
321 free( loader );
323 free( linkname );
326 else
328 if (bindir) datadir = build_path( bindir, BIN_TO_DATADIR );
329 argv0_name = xstrdup( basename );
333 #ifdef __ASM_OBSOLETE
335 static const char server_config_dir[] = "/.wine"; /* config dir relative to $HOME */
336 static const char server_root_prefix[] = "/tmp/.wine"; /* prefix for server root dir */
337 static const char server_dir_prefix[] = "/server-"; /* prefix for server dir */
339 static char *config_dir;
340 static char *server_dir;
341 static char *user_name;
343 /* check if a string ends in a given substring */
344 static inline int strendswith( const char* str, const char* end )
346 size_t len = strlen( str );
347 size_t tail = strlen( end );
348 return len >= tail && !strcmp( str + len - tail, end );
351 /* remove all trailing slashes from a path name */
352 static inline void remove_trailing_slashes( char *path )
354 int len = strlen( path );
355 while (len > 1 && path[len-1] == '/') path[--len] = 0;
358 /* die on a fatal error */
359 static void fatal_perror( const char *err, ... )
361 va_list args;
363 va_start( args, err );
364 fprintf( stderr, "wine: " );
365 vfprintf( stderr, err, args );
366 perror( " " );
367 va_end( args );
368 exit(1);
371 /* initialize the server directory value */
372 static void init_server_dir( dev_t dev, ino_t ino )
374 char *p, *root;
376 #ifdef __ANDROID__ /* there's no /tmp dir on Android */
377 root = build_path( config_dir, ".wineserver" );
378 #else
379 root = xmalloc( sizeof(server_root_prefix) + 12 );
380 sprintf( root, "%s-%u", server_root_prefix, getuid() );
381 #endif
383 server_dir = xmalloc( strlen(root) + sizeof(server_dir_prefix) + 2*sizeof(dev) + 2*sizeof(ino) + 2 );
384 strcpy( server_dir, root );
385 strcat( server_dir, server_dir_prefix );
386 p = server_dir + strlen(server_dir);
388 if (dev != (unsigned long)dev)
389 p += sprintf( p, "%lx%08lx-", (unsigned long)((unsigned long long)dev >> 32), (unsigned long)dev );
390 else
391 p += sprintf( p, "%lx-", (unsigned long)dev );
393 if (ino != (unsigned long)ino)
394 sprintf( p, "%lx%08lx", (unsigned long)((unsigned long long)ino >> 32), (unsigned long)ino );
395 else
396 sprintf( p, "%lx", (unsigned long)ino );
397 free( root );
400 /* initialize all the paths values */
401 static void init_paths(void)
403 struct stat st;
405 const char *home = getenv( "HOME" );
406 const char *user = NULL;
407 const char *prefix = getenv( "WINEPREFIX" );
408 char uid_str[32];
409 struct passwd *pwd = getpwuid( getuid() );
411 if (pwd)
413 user = pwd->pw_name;
414 if (!home) home = pwd->pw_dir;
416 if (!user)
418 sprintf( uid_str, "%lu", (unsigned long)getuid() );
419 user = uid_str;
421 user_name = xstrdup( user );
423 /* build config_dir */
425 if (prefix)
427 config_dir = xstrdup( prefix );
428 remove_trailing_slashes( config_dir );
429 if (config_dir[0] != '/')
430 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
431 if (stat( config_dir, &st ) == -1)
433 if (errno == ENOENT) return; /* will be created later on */
434 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
437 else
439 if (!home) fatal_error( "could not determine your home directory\n" );
440 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
441 config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
442 strcpy( config_dir, home );
443 remove_trailing_slashes( config_dir );
444 strcat( config_dir, server_config_dir );
445 if (stat( config_dir, &st ) == -1)
447 if (errno == ENOENT) return; /* will be created later on */
448 fatal_perror( "cannot open %s", config_dir );
451 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
452 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", config_dir );
453 init_server_dir( st.st_dev, st.st_ino );
456 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
457 const char *wine_get_config_dir_obsolete(void)
459 if (!config_dir) init_paths();
460 return config_dir;
463 /* retrieve the wine data dir */
464 const char *wine_get_data_dir_obsolete(void)
466 return datadir;
469 /* retrieve the wine build dir (if we are running from there) */
470 const char *wine_get_build_dir_obsolete(void)
472 return build_dir;
475 /* return the full name of the server directory (the one containing the socket) */
476 const char *wine_get_server_dir_obsolete(void)
478 if (!server_dir)
480 if (!config_dir) init_paths();
481 else
483 struct stat st;
485 if (stat( config_dir, &st ) == -1)
487 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
488 return NULL; /* will have to try again once config_dir has been created */
490 init_server_dir( st.st_dev, st.st_ino );
493 return server_dir;
496 /* return the current user name */
497 const char *wine_get_user_name_obsolete(void)
499 if (!user_name) init_paths();
500 return user_name;
503 /* return the standard version string */
504 const char *wine_get_version_obsolete(void)
506 return PACKAGE_VERSION;
509 /* return the build id string */
510 const char *wine_get_build_id_obsolete(void)
512 return PACKAGE_VERSION;
515 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
516 static void preloader_exec( char **argv, int use_preloader )
518 if (use_preloader)
520 static const char preloader[] = "wine-preloader";
521 static const char preloader64[] = "wine64-preloader";
522 char *p, *full_name;
523 char **last_arg = argv, **new_argv;
525 if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
526 else p++;
528 full_name = xmalloc( p - argv[0] + sizeof(preloader64) );
529 memcpy( full_name, argv[0], p - argv[0] );
530 if (strendswith( p, "64" ))
531 memcpy( full_name + (p - argv[0]), preloader64, sizeof(preloader64) );
532 else
533 memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
535 /* make a copy of argv */
536 while (*last_arg) last_arg++;
537 new_argv = xmalloc( (last_arg - argv + 2) * sizeof(*argv) );
538 memcpy( new_argv + 1, argv, (last_arg - argv + 1) * sizeof(*argv) );
539 new_argv[0] = full_name;
540 #ifdef __APPLE__
542 posix_spawnattr_t attr;
543 posix_spawnattr_init( &attr );
544 posix_spawnattr_setflags( &attr, POSIX_SPAWN_SETEXEC | _POSIX_SPAWN_DISABLE_ASLR );
545 posix_spawn( NULL, full_name, NULL, &attr, new_argv, *_NSGetEnviron() );
546 posix_spawnattr_destroy( &attr );
548 #endif
549 execv( full_name, new_argv );
550 free( new_argv );
551 free( full_name );
553 execv( argv[0], argv );
556 /* exec a wine internal binary (either the wine loader or the wine server) */
557 void wine_exec_wine_binary_obsolete( const char *name, char **argv, const char *env_var )
559 const char *path, *pos, *ptr;
560 int use_preloader;
562 if (!name) name = argv0_name; /* no name means default loader */
564 #if defined(linux) || defined(__APPLE__)
565 use_preloader = !strendswith( name, "wineserver" );
566 #else
567 use_preloader = 0;
568 #endif
570 if ((ptr = strrchr( name, '/' )))
572 /* if we are in build dir and name contains a path, try that */
573 if (build_dir)
575 if (wineserver64 && !strcmp( name, "server/wineserver" ))
576 argv[0] = xstrdup( wineserver64 );
577 else
578 argv[0] = build_path( build_dir, name );
579 preloader_exec( argv, use_preloader );
580 free( argv[0] );
582 name = ptr + 1; /* get rid of path */
585 /* first, bin directory from the current libdir or argv0 */
586 if (bindir)
588 argv[0] = build_path( bindir, name );
589 preloader_exec( argv, use_preloader );
590 free( argv[0] );
593 /* then specified environment variable */
594 if (env_var)
596 argv[0] = (char *)env_var;
597 preloader_exec( argv, use_preloader );
600 /* now search in the Unix path */
601 if ((path = getenv( "PATH" )))
603 argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
604 pos = path;
605 for (;;)
607 while (*pos == ':') pos++;
608 if (!*pos) break;
609 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
610 memcpy( argv[0], pos, ptr - pos );
611 strcpy( argv[0] + (ptr - pos), "/" );
612 strcat( argv[0] + (ptr - pos), name );
613 preloader_exec( argv, use_preloader );
614 pos = ptr;
616 free( argv[0] );
619 /* and finally try BINDIR */
620 argv[0] = build_path( BINDIR, name );
621 preloader_exec( argv, use_preloader );
622 free( argv[0] );
625 __ASM_OBSOLETE(wine_get_build_dir);
626 __ASM_OBSOLETE(wine_get_build_id);
627 __ASM_OBSOLETE(wine_get_config_dir);
628 __ASM_OBSOLETE(wine_get_data_dir);
629 __ASM_OBSOLETE(wine_get_server_dir);
630 __ASM_OBSOLETE(wine_get_user_name);
631 __ASM_OBSOLETE(wine_get_version);
632 __ASM_OBSOLETE(wine_exec_wine_binary);
634 #endif /* __ASM_OBSOLETE */