Unlock the master socket if the boot thread terminates early.
[wine/multimedia.git] / library / config.c
blob07e17854062ee93203de0789286a99aa33b90270
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <errno.h>
25 #include <pwd.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
33 static const char * const server_config_dir = "/.wine"; /* config dir relative to $HOME */
34 static const char * const server_root_prefix = "/tmp/.wine-"; /* prefix for server root dir */
35 static const char * const server_dir_prefix = "/server-"; /* prefix for server dir */
37 static char *config_dir;
38 static char *server_dir;
40 #ifdef __GNUC__
41 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
42 static void fatal_perror( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
43 #endif
45 /* die on a fatal error */
46 static void fatal_error( const char *err, ... )
48 va_list args;
50 va_start( args, err );
51 fprintf( stderr, "wine: " );
52 vfprintf( stderr, err, args );
53 va_end( args );
54 exit(1);
57 /* die on a fatal error */
58 static void fatal_perror( const char *err, ... )
60 va_list args;
62 va_start( args, err );
63 fprintf( stderr, "wine: " );
64 vfprintf( stderr, err, args );
65 perror( " " );
66 va_end( args );
67 exit(1);
70 /* malloc wrapper */
71 static void *xmalloc( size_t size )
73 void *res;
75 if (!size) size = 1;
76 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
77 return res;
80 /* remove all trailing slashes from a path name */
81 inline static void remove_trailing_slashes( char *path )
83 int len = strlen( path );
84 while (len > 1 && path[len-1] == '/') path[--len] = 0;
87 /* initialize all the paths values */
88 static void init_paths(void)
90 struct stat st;
91 char uid_str[32], *p;
93 const char *home = getenv( "HOME" );
94 const char *user = NULL;
95 const char *prefix = getenv( "WINEPREFIX" );
96 struct passwd *pwd = getpwuid( getuid() );
98 if (pwd)
100 user = pwd->pw_name;
101 if (!home) home = pwd->pw_dir;
103 if (!user)
105 sprintf( uid_str, "%d", getuid() );
106 user = uid_str;
109 /* build config_dir */
111 if (prefix)
113 if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
114 remove_trailing_slashes( config_dir );
115 if (config_dir[0] != '/')
116 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
117 if (stat( config_dir, &st ) == -1)
118 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
120 else
122 if (!home) fatal_error( "could not determine your home directory\n" );
123 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
124 config_dir = xmalloc( strlen(home) + strlen(server_config_dir) + 1 );
125 strcpy( config_dir, home );
126 remove_trailing_slashes( config_dir );
127 strcat( config_dir, server_config_dir );
128 if (stat( config_dir, &st ) == -1)
129 fatal_perror( "cannot open %s", config_dir );
131 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
133 /* build server_dir */
135 server_dir = xmalloc( strlen(server_root_prefix) + strlen(user) + strlen( server_dir_prefix ) +
136 2*sizeof(st.st_dev) + 2*sizeof(st.st_ino) + 2 );
137 strcpy( server_dir, server_root_prefix );
138 p = server_dir + strlen(server_dir);
139 strcpy( p, user );
140 while (*p)
142 if (*p == '/') *p = '!';
143 p++;
145 strcpy( p, server_dir_prefix );
147 if (sizeof(st.st_dev) > sizeof(unsigned long))
148 sprintf( server_dir + strlen(server_dir), "%llx-", (unsigned long long)st.st_dev );
149 else
150 sprintf( server_dir + strlen(server_dir), "%lx-", (unsigned long)st.st_dev );
152 if (sizeof(st.st_ino) > sizeof(unsigned long))
153 sprintf( server_dir + strlen(server_dir), "%llx", (unsigned long long)st.st_ino );
154 else
155 sprintf( server_dir + strlen(server_dir), "%lx", (unsigned long)st.st_ino );
158 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
159 const char *wine_get_config_dir(void)
161 if (!config_dir) init_paths();
162 return config_dir;
165 /* return the full name of the server directory (the one containing the socket) */
166 const char *wine_get_server_dir(void)
168 if (!server_dir) init_paths();
169 return server_dir;