Initial version of the Wine preloader, used to reserve memory
[wine] / libs / wine / config.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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"
37
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 */
41
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;
47
48 #ifdef __GNUC__
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)));
51 #endif
52
53 /* die on a fatal error */
54 static void fatal_error( const char *err, ... )
55 {
56     va_list args;
57
58     va_start( args, err );
59     fprintf( stderr, "wine: " );
60     vfprintf( stderr, err, args );
61     va_end( args );
62     exit(1);
63 }
64
65 /* die on a fatal error */
66 static void fatal_perror( const char *err, ... )
67 {
68     va_list args;
69
70     va_start( args, err );
71     fprintf( stderr, "wine: " );
72     vfprintf( stderr, err, args );
73     perror( " " );
74     va_end( args );
75     exit(1);
76 }
77
78 /* malloc wrapper */
79 static void *xmalloc( size_t size )
80 {
81     void *res;
82
83     if (!size) size = 1;
84     if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
85     return res;
86 }
87
88 /* strdup wrapper */
89 static char *xstrdup( const char *str )
90 {
91     size_t len = strlen(str) + 1;
92     char *res = xmalloc( len );
93     memcpy( res, str, len );
94     return res;
95 }
96
97 /* remove all trailing slashes from a path name */
98 inline static void remove_trailing_slashes( char *path )
99 {
100     int len = strlen( path );
101     while (len > 1 && path[len-1] == '/') path[--len] = 0;
102 }
103
104 /* initialize the server directory value */
105 static void init_server_dir( dev_t dev, ino_t ino )
106 {
107     const char *user = wine_get_user_name();
108     char *p;
109
110     server_dir = xmalloc( sizeof(server_root_prefix) + strlen(user) + sizeof(server_dir_prefix) +
111                           2*sizeof(dev) + 2*sizeof(ino) );
112     strcpy( server_dir, server_root_prefix );
113     p = server_dir + sizeof(server_root_prefix) - 1;
114     strcpy( p, user );
115     while (*p)
116     {
117         if (*p == '/') *p = '!';
118         p++;
119     }
120     strcpy( p, server_dir_prefix );
121     p += sizeof(server_dir_prefix) - 1;
122
123     if (sizeof(dev) > sizeof(unsigned long) && dev > ~0UL)
124         sprintf( p, "%lx%08lx-", (unsigned long)(dev >> 32), (unsigned long)dev );
125     else
126         sprintf( p, "%lx-", (unsigned long)dev );
127
128     if (sizeof(ino) > sizeof(unsigned long) && ino > ~0UL)
129         sprintf( p, "%lx%08lx", (unsigned long)(ino >> 32), (unsigned long)ino );
130     else
131         sprintf( p, "%lx", (unsigned long)ino );
132 }
133
134 /* initialize all the paths values */
135 static void init_paths(void)
136 {
137     struct stat st;
138
139     const char *home = getenv( "HOME" );
140     const char *user = NULL;
141     const char *prefix = getenv( "WINEPREFIX" );
142
143 #ifdef HAVE_GETPWUID
144     char uid_str[32];
145     struct passwd *pwd = getpwuid( getuid() );
146
147     if (pwd)
148     {
149         user = pwd->pw_name;
150         if (!home) home = pwd->pw_dir;
151     }
152     if (!user)
153     {
154         sprintf( uid_str, "%d", getuid() );
155         user = uid_str;
156     }
157 #else  /* HAVE_GETPWUID */
158     if (!(user = getenv( "USER" )))
159         fatal_error( "cannot determine your user name, set the USER environment variable\n" );
160 #endif  /* HAVE_GETPWUID */
161     user_name = xstrdup( user );
162
163     /* build config_dir */
164
165     if (prefix)
166     {
167         if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
168         remove_trailing_slashes( config_dir );
169         if (config_dir[0] != '/')
170             fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
171         if (stat( config_dir, &st ) == -1)
172         {
173             if (errno != ENOENT)
174                 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
175             fatal_error( "the '%s' directory specified in WINEPREFIX doesn't exist.\n"
176                          "You may want to create it by running 'wineprefixcreate'.\n", config_dir );
177         }
178     }
179     else
180     {
181         if (!home) fatal_error( "could not determine your home directory\n" );
182         if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
183         config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
184         strcpy( config_dir, home );
185         remove_trailing_slashes( config_dir );
186         strcat( config_dir, server_config_dir );
187         if (stat( config_dir, &st ) == -1)
188         {
189             if (errno == ENOENT) return;  /* will be created later on */
190             fatal_perror( "cannot open %s", config_dir );
191         }
192     }
193     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
194
195     init_server_dir( st.st_dev, st.st_ino );
196 }
197
198 /* initialize the argv0 path */
199 void wine_init_argv0_path( const char *argv0 )
200 {
201     size_t size, len;
202     const char *p;
203     char *cwd;
204
205     if (!(p = strrchr( argv0, '/' )))
206     {
207         argv0_name = xstrdup( argv0 );
208         return;  /* if argv0 doesn't contain a path, don't store any path */
209     }
210     else argv0_name = xstrdup( p + 1 );
211
212     len = p - argv0 + 1;
213     if (argv0[0] == '/')  /* absolute path */
214     {
215         argv0_path = xmalloc( len + 1 );
216         memcpy( argv0_path, argv0, len );
217         argv0_path[len] = 0;
218         return;
219     }
220
221     /* relative path, make it absolute */
222     for (size = 256 + len; ; size *= 2)
223     {
224         if (!(cwd = malloc( size ))) break;
225         if (getcwd( cwd, size - len ))
226         {
227             argv0_path = cwd;
228             cwd += strlen(cwd);
229             *cwd++ = '/';
230             memcpy( cwd, argv0, len );
231             cwd[len] = 0;
232             return;
233         }
234         free( cwd );
235         if (errno != ERANGE) break;
236     }
237 }
238
239 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
240 const char *wine_get_config_dir(void)
241 {
242     if (!config_dir) init_paths();
243     return config_dir;
244 }
245
246 /* return the full name of the server directory (the one containing the socket) */
247 const char *wine_get_server_dir(void)
248 {
249     if (!server_dir)
250     {
251         if (!config_dir) init_paths();
252         else
253         {
254             struct stat st;
255
256             if (stat( config_dir, &st ) == -1)
257             {
258                 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
259                 return NULL;  /* will have to try again once config_dir has been created */
260             }
261             init_server_dir( st.st_dev, st.st_ino );
262         }
263     }
264     return server_dir;
265 }
266
267 /* return the current user name */
268 const char *wine_get_user_name(void)
269 {
270     if (!user_name) init_paths();
271     return user_name;
272 }
273
274 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
275 static void preloader_exec( char **argv, char **envp, int use_preloader )
276 {
277 #ifdef linux
278     if (use_preloader)
279     {
280         static const char preloader[] = "wine-preloader";
281         char *p, *full_name;
282
283         if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
284         else p++;
285
286         full_name = xmalloc( p - argv[0] + sizeof(preloader) );
287         memcpy( full_name, argv[0], p - argv[0] );
288         memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
289         if (envp) execve( full_name, argv, envp );
290         else execv( full_name, argv );
291         free( full_name );
292         return;
293     }
294 #else
295     if (envp) execve( argv[0], argv, envp );
296     else execv( argv[0], argv );
297 #endif
298 }
299
300 /* exec a wine internal binary (either the wine loader or the wine server) */
301 void wine_exec_wine_binary( const char *name, char **argv, char **envp, int use_preloader )
302 {
303     const char *path, *pos, *ptr;
304
305     if (name && strchr( name, '/' ))
306     {
307         argv[0] = (char *)name;
308         preloader_exec( argv, envp, use_preloader );
309         return;
310     }
311     else if (!name) name = argv0_name;
312
313     /* first, try bin directory */
314     argv[0] = xmalloc( sizeof(BINDIR "/") + strlen(name) );
315     strcpy( argv[0], BINDIR "/" );
316     strcat( argv[0], name );
317     preloader_exec( argv, envp, use_preloader );
318     free( argv[0] );
319
320     /* now try the path of argv0 of the current binary */
321     if (argv0_path)
322     {
323         argv[0] = xmalloc( strlen(argv0_path) + strlen(name) + 1 );
324         strcpy( argv[0], argv0_path );
325         strcat( argv[0], name );
326         preloader_exec( argv, envp, use_preloader );
327         free( argv[0] );
328     }
329
330     /* now search in the Unix path */
331     if ((path = getenv( "PATH" )))
332     {
333         argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
334         pos = path;
335         for (;;)
336         {
337             while (*pos == ':') pos++;
338             if (!*pos) break;
339             if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
340             memcpy( argv[0], pos, ptr - pos );
341             strcpy( argv[0] + (ptr - pos), "/" );
342             strcat( argv[0] + (ptr - pos), name );
343             preloader_exec( argv, envp, use_preloader );
344             pos = ptr;
345         }
346         free( argv[0] );
347     }
348 }