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
22 #include "wine/port.h"
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 */
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
;
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)));
56 #if defined(__linux__) || defined(__FreeBSD_kernel__ )
57 static const char exe_link
[] = "/proc/self/exe";
58 #elif defined (__FreeBSD__) || defined(__DragonFly__)
59 static const char exe_link
[] = "/proc/curproc/file";
61 static const char exe_link
[] = "";
64 /* die on a fatal error */
65 static void fatal_error( const char *err
, ... )
69 va_start( args
, err
);
70 fprintf( stderr
, "wine: " );
71 vfprintf( stderr
, err
, args
);
76 /* die on a fatal error */
77 static void fatal_perror( const char *err
, ... )
81 va_start( args
, err
);
82 fprintf( stderr
, "wine: " );
83 vfprintf( stderr
, err
, args
);
90 static void *xmalloc( size_t size
)
95 if (!(res
= malloc( size
))) fatal_error( "virtual memory exhausted\n");
100 static char *xstrdup( const char *str
)
102 size_t len
= strlen(str
) + 1;
103 char *res
= xmalloc( len
);
104 memcpy( res
, str
, len
);
108 /* check if a string ends in a given substring */
109 static inline int strendswith( const char* str
, const char* end
)
111 size_t len
= strlen( str
);
112 size_t tail
= strlen( end
);
113 return len
>= tail
&& !strcmp( str
+ len
- tail
, end
);
116 /* remove all trailing slashes from a path name */
117 static inline void remove_trailing_slashes( char *path
)
119 int len
= strlen( path
);
120 while (len
> 1 && path
[len
-1] == '/') path
[--len
] = 0;
123 /* build a path from the specified dir and name */
124 static char *build_path( const char *dir
, const char *name
)
126 size_t len
= strlen(dir
);
127 char *ret
= xmalloc( len
+ strlen(name
) + 2 );
129 memcpy( ret
, dir
, len
);
130 if (len
&& ret
[len
-1] != '/') ret
[len
++] = '/';
131 strcpy( ret
+ len
, name
);
135 /* return the directory that contains the library at run-time */
136 static char *get_runtime_libdir(void)
142 if (dladdr( get_runtime_libdir
, &info
) && info
.dli_fname
[0] == '/')
144 const char *p
= strrchr( info
.dli_fname
, '/' );
145 unsigned int len
= p
- info
.dli_fname
;
146 if (!len
) len
++; /* include initial slash */
147 libdir
= xmalloc( len
+ 1 );
148 memcpy( libdir
, info
.dli_fname
, len
);
152 #endif /* HAVE_DLADDR */
156 /* read a symlink and return its directory */
157 static char *symlink_dirname( const char *name
)
162 for (size
= 256; ; size
*= 2)
164 if (!(buffer
= malloc( size
))) return NULL
;
165 if ((ret
= readlink( name
, buffer
, size
)) == -1) break;
169 if (!(p
= strrchr( buffer
, '/' ))) break;
170 if (p
== buffer
) p
++;
180 /* return the directory that contains the main exe at run-time */
181 static char *get_runtime_exedir(void)
183 if (exe_link
[0]) return symlink_dirname( exe_link
);
187 /* return the base directory from argv0 */
188 static char *get_runtime_argvdir( const char *argv0
)
190 char *p
, *bindir
, *cwd
;
193 if (!(p
= strrchr( argv0
, '/' ))) return NULL
;
196 if (!len
) len
++; /* include leading slash */
198 if (argv0
[0] == '/') /* absolute path */
200 bindir
= xmalloc( len
+ 1 );
201 memcpy( bindir
, argv0
, len
);
206 /* relative path, make it absolute */
207 for (size
= 256 + len
; ; size
*= 2)
209 if (!(cwd
= malloc( size
))) return NULL
;
210 if (getcwd( cwd
, size
- len
))
215 memcpy( cwd
, argv0
, len
);
220 if (errno
!= ERANGE
) return NULL
;
226 /* initialize the server directory value */
227 static void init_server_dir( dev_t dev
, ino_t ino
)
231 #ifdef __ANDROID__ /* there's no /tmp dir on Android */
232 root
= build_path( config_dir
, ".wineserver" );
233 #elif defined(HAVE_GETUID)
234 root
= xmalloc( sizeof(server_root_prefix
) + 12 );
235 sprintf( root
, "%s-%u", server_root_prefix
, getuid() );
237 root
= xstrdup( server_root_prefix
);
240 server_dir
= xmalloc( strlen(root
) + sizeof(server_dir_prefix
) + 2*sizeof(dev
) + 2*sizeof(ino
) + 2 );
241 strcpy( server_dir
, root
);
242 strcat( server_dir
, server_dir_prefix
);
243 p
= server_dir
+ strlen(server_dir
);
245 if (dev
!= (unsigned long)dev
)
246 p
+= sprintf( p
, "%lx%08lx-", (unsigned long)((unsigned long long)dev
>> 32), (unsigned long)dev
);
248 p
+= sprintf( p
, "%lx-", (unsigned long)dev
);
250 if (ino
!= (unsigned long)ino
)
251 sprintf( p
, "%lx%08lx", (unsigned long)((unsigned long long)ino
>> 32), (unsigned long)ino
);
253 sprintf( p
, "%lx", (unsigned long)ino
);
257 /* retrieve the default dll dir */
258 const char *get_dlldir( const char **default_dlldir
)
260 *default_dlldir
= DLLDIR
;
264 /* initialize all the paths values */
265 static void init_paths(void)
269 const char *home
= getenv( "HOME" );
270 const char *user
= NULL
;
271 const char *prefix
= getenv( "WINEPREFIX" );
275 struct passwd
*pwd
= getpwuid( getuid() );
280 if (!home
) home
= pwd
->pw_dir
;
284 sprintf( uid_str
, "%lu", (unsigned long)getuid() );
287 #else /* HAVE_GETPWUID */
288 if (!(user
= getenv( "USER" )))
289 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
290 #endif /* HAVE_GETPWUID */
291 user_name
= xstrdup( user
);
293 /* build config_dir */
297 config_dir
= xstrdup( prefix
);
298 remove_trailing_slashes( config_dir
);
299 if (config_dir
[0] != '/')
300 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix
);
301 if (stat( config_dir
, &st
) == -1)
303 if (errno
== ENOENT
) return; /* will be created later on */
304 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir
);
309 if (!home
) fatal_error( "could not determine your home directory\n" );
310 if (home
[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home
);
311 config_dir
= xmalloc( strlen(home
) + sizeof(server_config_dir
) );
312 strcpy( config_dir
, home
);
313 remove_trailing_slashes( config_dir
);
314 strcat( config_dir
, server_config_dir
);
315 if (stat( config_dir
, &st
) == -1)
317 if (errno
== ENOENT
) return; /* will be created later on */
318 fatal_perror( "cannot open %s", config_dir
);
321 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", config_dir
);
323 if (st
.st_uid
!= getuid()) fatal_error( "%s is not owned by you\n", config_dir
);
326 init_server_dir( st
.st_dev
, st
.st_ino
);
329 /* check if bindir is valid by checking for wineserver */
330 static int is_valid_bindir( const char *bindir
)
333 char *path
= build_path( bindir
, "wineserver" );
334 int ret
= (stat( path
, &st
) != -1);
339 /* check if dlldir is valid by checking for ntdll */
340 static int is_valid_dlldir( const char *dlldir
)
343 char *path
= build_path( dlldir
, "ntdll.dll.so" );
344 int ret
= (stat( path
, &st
) != -1);
349 /* check if basedir is a valid build dir by checking for wineserver and ntdll */
350 /* helper for running_from_build_dir */
351 static inline int is_valid_build_dir( char *basedir
, int baselen
)
355 strcpy( basedir
+ baselen
, "/server/wineserver" );
356 if (stat( basedir
, &st
) == -1) return 0; /* no wineserver found */
357 /* check for ntdll too to make sure */
358 strcpy( basedir
+ baselen
, "/dlls/ntdll/ntdll.dll.so" );
359 if (stat( basedir
, &st
) == -1) return 0; /* no ntdll found */
361 basedir
[baselen
] = 0;
365 /* check if we are running from the build directory */
366 static char *running_from_build_dir( const char *basedir
)
371 /* remove last component from basedir */
372 p
= basedir
+ strlen(basedir
) - 1;
373 while (p
> basedir
&& *p
== '/') p
--;
374 while (p
> basedir
&& *p
!= '/') p
--;
375 if (p
== basedir
) return NULL
;
376 path
= xmalloc( p
- basedir
+ sizeof("/dlls/ntdll/ntdll.dll.so") );
377 memcpy( path
, basedir
, p
- basedir
);
379 if (!is_valid_build_dir( path
, p
- basedir
))
381 /* remove another component */
382 while (p
> basedir
&& *p
== '/') p
--;
383 while (p
> basedir
&& *p
!= '/') p
--;
384 if (p
== basedir
|| !is_valid_build_dir( path
, p
- basedir
))
393 /* try to set the specified directory as bindir, or set build_dir if it's inside the build directory */
394 static int set_bindir( char *dir
)
397 if (is_valid_bindir( dir
))
400 dlldir
= build_path( bindir
, BIN_TO_DLLDIR
);
404 build_dir
= running_from_build_dir( dir
);
407 return bindir
|| build_dir
;
410 /* try to set the specified directory as dlldir, or set build_dir if it's inside the build directory */
411 static int set_dlldir( char *libdir
)
415 if (!libdir
) return 0;
417 path
= build_path( libdir
, LIB_TO_DLLDIR
);
418 if (is_valid_dlldir( path
))
421 bindir
= build_path( libdir
, LIB_TO_BINDIR
);
425 build_dir
= running_from_build_dir( libdir
);
429 return dlldir
|| build_dir
;
432 /* initialize the argv0 path */
433 void wine_init_argv0_path( const char *argv0
)
435 const char *basename
, *wineloader
;
437 if (!(basename
= strrchr( argv0
, '/' ))) basename
= argv0
;
440 if (set_bindir( get_runtime_exedir() )) goto done
;
441 if (set_dlldir( get_runtime_libdir() )) goto done
;
442 if (set_bindir( get_runtime_argvdir( argv0
))) goto done
;
443 if ((wineloader
= getenv( "WINELOADER" ))) set_bindir( get_runtime_argvdir( wineloader
));
448 argv0_name
= build_path( "loader/", basename
);
452 if (bindir
) datadir
= build_path( bindir
, BIN_TO_DATADIR
);
453 argv0_name
= xstrdup( basename
);
457 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
458 const char *wine_get_config_dir(void)
460 if (!config_dir
) init_paths();
464 /* retrieve the wine data dir */
465 const char *wine_get_data_dir(void)
470 /* retrieve the wine build dir (if we are running from there) */
471 const char *wine_get_build_dir(void)
476 /* return the full name of the server directory (the one containing the socket) */
477 const char *wine_get_server_dir(void)
481 if (!config_dir
) init_paths();
486 if (stat( config_dir
, &st
) == -1)
488 if (errno
!= ENOENT
) fatal_error( "cannot stat %s\n", config_dir
);
489 return NULL
; /* will have to try again once config_dir has been created */
491 init_server_dir( st
.st_dev
, st
.st_ino
);
497 /* return the current user name */
498 const char *wine_get_user_name(void)
500 if (!user_name
) init_paths();
504 /* return the standard version string */
505 const char *wine_get_version(void)
507 return PACKAGE_VERSION
;
510 /* return the build id string */
511 const char *wine_get_build_id(void)
513 extern const char wine_build
[];
517 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
518 static void preloader_exec( char **argv
, int use_preloader
)
522 static const char preloader
[] = "wine-preloader";
523 static const char preloader64
[] = "wine64-preloader";
525 char **last_arg
= argv
, **new_argv
;
527 if (!(p
= strrchr( argv
[0], '/' ))) p
= argv
[0];
530 full_name
= xmalloc( p
- argv
[0] + sizeof(preloader64
) );
531 memcpy( full_name
, argv
[0], p
- argv
[0] );
532 if (strendswith( p
, "64" ))
533 memcpy( full_name
+ (p
- argv
[0]), preloader64
, sizeof(preloader64
) );
535 memcpy( full_name
+ (p
- argv
[0]), preloader
, sizeof(preloader
) );
537 /* make a copy of argv */
538 while (*last_arg
) last_arg
++;
539 new_argv
= xmalloc( (last_arg
- argv
+ 2) * sizeof(*argv
) );
540 memcpy( new_argv
+ 1, argv
, (last_arg
- argv
+ 1) * sizeof(*argv
) );
541 new_argv
[0] = full_name
;
542 execv( full_name
, new_argv
);
546 execv( argv
[0], argv
);
549 /* exec a wine internal binary (either the wine loader or the wine server) */
550 void wine_exec_wine_binary( const char *name
, char **argv
, const char *env_var
)
552 const char *path
, *pos
, *ptr
;
555 if (!name
) name
= argv0_name
; /* no name means default loader */
558 use_preloader
= !strendswith( name
, "wineserver" );
563 if ((ptr
= strrchr( name
, '/' )))
565 /* if we are in build dir and name contains a path, try that */
568 argv
[0] = build_path( build_dir
, name
);
569 preloader_exec( argv
, use_preloader
);
572 name
= ptr
+ 1; /* get rid of path */
575 /* first, bin directory from the current libdir or argv0 */
578 argv
[0] = build_path( bindir
, name
);
579 preloader_exec( argv
, use_preloader
);
583 /* then specified environment variable */
586 argv
[0] = (char *)env_var
;
587 preloader_exec( argv
, use_preloader
);
590 /* now search in the Unix path */
591 if ((path
= getenv( "PATH" )))
593 argv
[0] = xmalloc( strlen(path
) + strlen(name
) + 2 );
597 while (*pos
== ':') pos
++;
599 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
600 memcpy( argv
[0], pos
, ptr
- pos
);
601 strcpy( argv
[0] + (ptr
- pos
), "/" );
602 strcat( argv
[0] + (ptr
- pos
), name
);
603 preloader_exec( argv
, use_preloader
);
609 /* and finally try BINDIR */
610 argv
[0] = build_path( BINDIR
, name
);
611 preloader_exec( argv
, use_preloader
);