VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/libWine/config.c@ 19678

Last change on this file since 19678 was 19678, checked in by vboxsync, 16 years ago

opengl: update wine to 1.1.21, add d3d9.dll to build list

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette