319 lines
7.9 KiB
C
319 lines
7.9 KiB
C
/*
|
|
* Emulator initialisation code
|
|
*
|
|
* Copyright 2000 Alexandre Julliard
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "wine/port.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
# include <sys/mman.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
# include <sys/resource.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
# include <sys/syscall.h>
|
|
#endif
|
|
#ifdef HAVE_UNISTD_H
|
|
# include <unistd.h>
|
|
#endif
|
|
|
|
#include "wine/library.h"
|
|
#include "main.h"
|
|
|
|
extern char **environ;
|
|
|
|
extern const char *wine_get_build_id(void);
|
|
|
|
/* the preloader will set this variable */
|
|
const struct wine_preload_info *wine_main_preload_info = NULL;
|
|
|
|
/***********************************************************************
|
|
* check_command_line
|
|
*
|
|
* Check if command line is one that needs to be handled specially.
|
|
*/
|
|
static void check_command_line( int argc, char *argv[] )
|
|
{
|
|
static const char usage[] =
|
|
"Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n"
|
|
" wine --help Display this help and exit\n"
|
|
" wine --version Output version information and exit";
|
|
|
|
if (argc <= 1)
|
|
{
|
|
fprintf( stderr, "%s\n", usage );
|
|
exit(1);
|
|
}
|
|
if (!strcmp( argv[1], "--help" ))
|
|
{
|
|
printf( "%s\n", usage );
|
|
exit(0);
|
|
}
|
|
if (!strcmp( argv[1], "--version" ))
|
|
{
|
|
printf( "%s\n", wine_get_build_id() );
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
return 1; /* we have a preloader */
|
|
#else
|
|
return 0; /* no exec needed */
|
|
#endif
|
|
}
|
|
|
|
#elif defined(__linux__) && (defined(__i386__) || defined(__arm__))
|
|
|
|
static void check_vmsplit( void *stack )
|
|
{
|
|
if (stack < (void *)0x80000000)
|
|
{
|
|
/* if the stack is below 0x80000000, assume we can safely try a munmap there */
|
|
if (munmap( (void *)0x80000000, 1 ) == -1 && errno == EINVAL)
|
|
fprintf( stderr,
|
|
"Warning: memory above 0x80000000 doesn't seem to be accessible.\n"
|
|
"Wine requires a 3G/1G user/kernel memory split to work properly.\n" );
|
|
}
|
|
}
|
|
|
|
static void set_max_limit( int limit )
|
|
{
|
|
struct rlimit rlimit;
|
|
|
|
if (!getrlimit( limit, &rlimit ))
|
|
{
|
|
rlimit.rlim_cur = rlimit.rlim_max;
|
|
setrlimit( limit, &rlimit );
|
|
}
|
|
}
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
int temp;
|
|
|
|
check_vmsplit( &temp );
|
|
set_max_limit( RLIMIT_AS );
|
|
#ifdef __i386__
|
|
return 1; /* we have a preloader on x86 */
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
#elif defined(__linux__) && (defined(__x86_64__) || defined(__aarch64__))
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
return 1; /* we have a preloader on x86-64/arm64 */
|
|
}
|
|
|
|
#elif defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
return 1; /* we have a preloader */
|
|
}
|
|
|
|
#elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__))
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
struct rlimit rl;
|
|
|
|
rl.rlim_cur = 0x02000000;
|
|
rl.rlim_max = 0x02000000;
|
|
setrlimit( RLIMIT_DATA, &rl );
|
|
return 1;
|
|
}
|
|
|
|
#else
|
|
|
|
static int pre_exec(void)
|
|
{
|
|
return 0; /* no exec needed */
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
/* canonicalize path and return its directory name */
|
|
static char *realpath_dirname( const char *name )
|
|
{
|
|
char *p, *fullpath = realpath( name, NULL );
|
|
|
|
if (fullpath)
|
|
{
|
|
p = strrchr( fullpath, '/' );
|
|
if (p == fullpath) p++;
|
|
if (p) *p = 0;
|
|
}
|
|
return fullpath;
|
|
}
|
|
|
|
/* if string ends with tail, remove it */
|
|
static char *remove_tail( const char *str, const char *tail )
|
|
{
|
|
size_t len = strlen( str );
|
|
size_t tail_len = strlen( tail );
|
|
char *ret;
|
|
|
|
if (len < tail_len) return NULL;
|
|
if (strcmp( str + len - tail_len, tail )) return NULL;
|
|
ret = malloc( len - tail_len + 1 );
|
|
memcpy( ret, str, len - tail_len );
|
|
ret[len - tail_len] = 0;
|
|
return ret;
|
|
}
|
|
|
|
/* build a path from the specified dir and name */
|
|
static char *build_path( const char *dir, const char *name )
|
|
{
|
|
size_t len = strlen( dir );
|
|
char *ret = malloc( len + strlen( name ) + 2 );
|
|
|
|
memcpy( ret, dir, len );
|
|
if (len && ret[len - 1] != '/') ret[len++] = '/';
|
|
strcpy( ret + len, name );
|
|
return ret;
|
|
}
|
|
|
|
static const char *get_self_exe( char *argv0 )
|
|
{
|
|
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
|
|
return "/proc/self/exe";
|
|
#elif defined (__FreeBSD__) || defined(__DragonFly__)
|
|
return "/proc/curproc/file";
|
|
#else
|
|
if (!strchr( argv0, '/' )) /* search in PATH */
|
|
{
|
|
char *p, *path = getenv( "PATH" );
|
|
|
|
if (!path || !(path = strdup(path))) return NULL;
|
|
for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
|
|
{
|
|
char *name = build_path( p, argv0 );
|
|
if (!access( name, X_OK ))
|
|
{
|
|
free( path );
|
|
return name;
|
|
}
|
|
free( name );
|
|
}
|
|
free( path );
|
|
return NULL;
|
|
}
|
|
return argv0;
|
|
#endif
|
|
}
|
|
|
|
static void *try_dlopen( const char *dir, const char *name )
|
|
{
|
|
char *path = build_path( dir, name );
|
|
void *handle = dlopen( path, RTLD_NOW );
|
|
free( path );
|
|
return handle;
|
|
}
|
|
|
|
static void *load_ntdll( char *argv0 )
|
|
{
|
|
const char *self = get_self_exe( argv0 );
|
|
char *path, *p;
|
|
void *handle = NULL;
|
|
|
|
if (self && ((path = realpath_dirname( self ))))
|
|
{
|
|
if ((p = remove_tail( path, "/loader" )))
|
|
{
|
|
handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
|
|
free( p );
|
|
}
|
|
else handle = try_dlopen( path, BIN_TO_DLLDIR "/ntdll.so" );
|
|
free( path );
|
|
}
|
|
|
|
if (!handle && (path = getenv( "WINEDLLPATH" )))
|
|
{
|
|
path = strdup( path );
|
|
for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
|
|
{
|
|
handle = try_dlopen( p, "ntdll.so" );
|
|
if (handle) break;
|
|
}
|
|
free( path );
|
|
}
|
|
|
|
if (!handle && !self) handle = try_dlopen( DLLDIR, "ntdll.so" );
|
|
|
|
return handle;
|
|
}
|
|
|
|
|
|
/**********************************************************************
|
|
* main
|
|
*/
|
|
int main( int argc, char *argv[] )
|
|
{
|
|
char error[1024];
|
|
int i;
|
|
void *handle;
|
|
|
|
if ((handle = load_ntdll( argv[0] )))
|
|
{
|
|
void (*init_func)(int, char **, char **) = dlsym( handle, "__wine_main" );
|
|
if (init_func) init_func( argc, argv, environ );
|
|
fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
|
|
exit(1);
|
|
}
|
|
|
|
if (!getenv( "WINELOADERNOEXEC" )) /* first time around */
|
|
{
|
|
static char noexec[] = "WINELOADERNOEXEC=1";
|
|
|
|
putenv( noexec );
|
|
check_command_line( argc, argv );
|
|
if (pre_exec())
|
|
{
|
|
wine_init_argv0_path( argv[0] );
|
|
wine_exec_wine_binary( NULL, argv, getenv( "WINELOADER" ));
|
|
fprintf( stderr, "wine: could not exec the wine loader\n" );
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
if (wine_main_preload_info)
|
|
{
|
|
for (i = 0; wine_main_preload_info[i].size; i++)
|
|
wine_mmap_add_reserved_area( wine_main_preload_info[i].addr, wine_main_preload_info[i].size );
|
|
}
|
|
|
|
wine_init( argc, argv, error, sizeof(error) );
|
|
fprintf( stderr, "wine: failed to initialize: %s\n", error );
|
|
exit(1);
|
|
}
|