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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 */
42 static char *config_dir
;
43 static char *server_dir
;
44 static char *user_name
;
45 static char *argv0_path
;
46 static char *argv0_name
;
49 static void fatal_error( const char *err
, ... ) __attribute__((noreturn
,format(printf
,1,2)));
50 static void fatal_perror( const char *err
, ... ) __attribute__((noreturn
,format(printf
,1,2)));
53 /* die on a fatal error */
54 static void fatal_error( const char *err
, ... )
58 va_start( args
, err
);
59 fprintf( stderr
, "wine: " );
60 vfprintf( stderr
, err
, args
);
65 /* die on a fatal error */
66 static void fatal_perror( const char *err
, ... )
70 va_start( args
, err
);
71 fprintf( stderr
, "wine: " );
72 vfprintf( stderr
, err
, args
);
79 static void *xmalloc( size_t size
)
84 if (!(res
= malloc( size
))) fatal_error( "virtual memory exhausted\n");
89 static char *xstrdup( const char *str
)
91 size_t len
= strlen(str
) + 1;
92 char *res
= xmalloc( len
);
93 memcpy( res
, str
, len
);
97 /* remove all trailing slashes from a path name */
98 inline static void remove_trailing_slashes( char *path
)
100 int len
= strlen( path
);
101 while (len
> 1 && path
[len
-1] == '/') path
[--len
] = 0;
104 /* initialize the server directory value */
105 static void init_server_dir( dev_t dev
, ino_t ino
)
109 const unsigned int uid
= getuid();
111 const unsigned int uid
= 0;
114 server_dir
= xmalloc( sizeof(server_root_prefix
) + 32 + sizeof(server_dir_prefix
) +
115 2*sizeof(dev
) + 2*sizeof(ino
) );
116 sprintf( server_dir
, "%s%u%s", server_root_prefix
, uid
, server_dir_prefix
);
117 p
= server_dir
+ strlen(server_dir
);
119 if (sizeof(dev
) > sizeof(unsigned long) && dev
> ~0UL)
120 p
+= sprintf( p
, "%lx%08lx-", (unsigned long)(dev
>> 32), (unsigned long)dev
);
122 p
+= sprintf( p
, "%lx-", (unsigned long)dev
);
124 if (sizeof(ino
) > sizeof(unsigned long) && ino
> ~0UL)
125 sprintf( p
, "%lx%08lx", (unsigned long)(ino
>> 32), (unsigned long)ino
);
127 sprintf( p
, "%lx", (unsigned long)ino
);
130 /* initialize all the paths values */
131 static void init_paths(void)
135 const char *home
= getenv( "HOME" );
136 const char *user
= NULL
;
137 const char *prefix
= getenv( "WINEPREFIX" );
141 struct passwd
*pwd
= getpwuid( getuid() );
146 if (!home
) home
= pwd
->pw_dir
;
150 sprintf( uid_str
, "%u", getuid() );
153 #else /* HAVE_GETPWUID */
154 if (!(user
= getenv( "USER" )))
155 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
156 #endif /* HAVE_GETPWUID */
157 user_name
= xstrdup( user
);
159 /* build config_dir */
163 if (!(config_dir
= strdup( prefix
))) fatal_error( "virtual memory exhausted\n");
164 remove_trailing_slashes( config_dir
);
165 if (config_dir
[0] != '/')
166 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix
);
167 if (stat( config_dir
, &st
) == -1)
170 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir
);
171 fatal_error( "the '%s' directory specified in WINEPREFIX doesn't exist.\n"
172 "You may want to create it by running 'wineprefixcreate'.\n", config_dir
);
177 if (!home
) fatal_error( "could not determine your home directory\n" );
178 if (home
[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home
);
179 config_dir
= xmalloc( strlen(home
) + sizeof(server_config_dir
) );
180 strcpy( config_dir
, home
);
181 remove_trailing_slashes( config_dir
);
182 strcat( config_dir
, server_config_dir
);
183 if (stat( config_dir
, &st
) == -1)
185 if (errno
== ENOENT
) return; /* will be created later on */
186 fatal_perror( "cannot open %s", config_dir
);
189 if (!S_ISDIR(st
.st_mode
)) fatal_error( "%s is not a directory\n", config_dir
);
191 init_server_dir( st
.st_dev
, st
.st_ino
);
194 /* initialize the argv0 path */
195 void wine_init_argv0_path( const char *argv0
)
201 if (!(p
= strrchr( argv0
, '/' )))
203 argv0_name
= xstrdup( argv0
);
204 return; /* if argv0 doesn't contain a path, don't store any path */
206 else argv0_name
= xstrdup( p
+ 1 );
209 if (argv0
[0] == '/') /* absolute path */
211 argv0_path
= xmalloc( len
+ 1 );
212 memcpy( argv0_path
, argv0
, len
);
217 /* relative path, make it absolute */
218 for (size
= 256 + len
; ; size
*= 2)
220 if (!(cwd
= malloc( size
))) break;
221 if (getcwd( cwd
, size
- len
))
226 memcpy( cwd
, argv0
, len
);
231 if (errno
!= ERANGE
) break;
235 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
236 const char *wine_get_config_dir(void)
238 if (!config_dir
) init_paths();
242 /* return the full name of the server directory (the one containing the socket) */
243 const char *wine_get_server_dir(void)
247 if (!config_dir
) init_paths();
252 if (stat( config_dir
, &st
) == -1)
254 if (errno
!= ENOENT
) fatal_error( "cannot stat %s\n", config_dir
);
255 return NULL
; /* will have to try again once config_dir has been created */
257 init_server_dir( st
.st_dev
, st
.st_ino
);
263 /* return the current user name */
264 const char *wine_get_user_name(void)
266 if (!user_name
) init_paths();
270 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
271 static void preloader_exec( char **argv
, char **envp
, int use_preloader
)
276 static const char preloader
[] = "wine-preloader";
278 char **last_arg
= argv
, **new_argv
;
280 if (!(p
= strrchr( argv
[0], '/' ))) p
= argv
[0];
283 full_name
= xmalloc( p
- argv
[0] + sizeof(preloader
) );
284 memcpy( full_name
, argv
[0], p
- argv
[0] );
285 memcpy( full_name
+ (p
- argv
[0]), preloader
, sizeof(preloader
) );
287 /* make a copy of argv */
288 while (*last_arg
) last_arg
++;
289 new_argv
= xmalloc( (last_arg
- argv
+ 2) * sizeof(*argv
) );
290 memcpy( new_argv
+ 1, argv
, (last_arg
- argv
+ 1) * sizeof(*argv
) );
291 new_argv
[0] = full_name
;
292 if (envp
) execve( full_name
, new_argv
, envp
);
293 else execv( full_name
, new_argv
);
299 if (envp
) execve( argv
[0], argv
, envp
);
300 else execv( argv
[0], argv
);
303 /* exec a wine internal binary (either the wine loader or the wine server) */
304 void wine_exec_wine_binary( const char *name
, char **argv
, char **envp
, int use_preloader
)
306 const char *path
, *pos
, *ptr
;
308 if (name
&& strchr( name
, '/' ))
310 argv
[0] = (char *)name
;
311 preloader_exec( argv
, envp
, use_preloader
);
314 else if (!name
) name
= argv0_name
;
316 /* first, try bin directory */
317 argv
[0] = xmalloc( sizeof(BINDIR
"/") + strlen(name
) );
318 strcpy( argv
[0], BINDIR
"/" );
319 strcat( argv
[0], name
);
320 preloader_exec( argv
, envp
, use_preloader
);
323 /* now try the path of argv0 of the current binary */
326 argv
[0] = xmalloc( strlen(argv0_path
) + strlen(name
) + 1 );
327 strcpy( argv
[0], argv0_path
);
328 strcat( argv
[0], name
);
329 preloader_exec( argv
, envp
, use_preloader
);
333 /* now search in the Unix path */
334 if ((path
= getenv( "PATH" )))
336 argv
[0] = xmalloc( strlen(path
) + strlen(name
) + 2 );
340 while (*pos
== ':') pos
++;
342 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
343 memcpy( argv
[0], pos
, ptr
- pos
);
344 strcpy( argv
[0] + (ptr
- pos
), "/" );
345 strcat( argv
[0] + (ptr
- pos
), name
);
346 preloader_exec( argv
, envp
, use_preloader
);