XSDL: another approach to detecting when we unable to launch due to port already in use

This commit is contained in:
Sergii Pylypenko
2015-06-25 23:08:07 +03:00
parent 97c256b03d
commit 14704717dc
3 changed files with 34 additions and 59 deletions

View File

@@ -7,10 +7,10 @@ AppName="XServer XSDL"
AppFullName=x.org.server
# Application version code (integer)
AppVersionCode=11130
AppVersionCode=11131
# Application user-visible version name (string)
AppVersionName="1.11.30"
AppVersionName="1.11.31"
# Specify path to download application data in zip archive in the form 'Description|URL|MirrorURL^Description2|URL2|MirrorURL2^...'
# If you'll start Description with '!' symbol it will be enabled by default, other downloads should be selected by user from startup config menu

View File

@@ -10,21 +10,23 @@
#include <errno.h>
#include <SDL/SDL.h>
#include <SDL/SDL_screenkeyboard.h>
#include <SDL/SDL_android.h>
#include <android/log.h>
#include "gfx.h"
extern int android_main( int argc, char *argv[], char *envp[] );
static void setupEnv(void);
static void retryLaunchWithDifferentPort(void);
static void showError(void);
static void setupEnv(void);
static char port[16] = ":0";
int main( int argc, char* argv[] )
{
int i;
char screenres[128] = "640x480x24";
char clientcmd[PATH_MAX] = "";
char port[16] = ":1111";
char * cmd = "";
char fontpath[PATH_MAX] = "";
char* args[64] = {
@@ -32,12 +34,15 @@ int main( int argc, char* argv[] )
port,
"-nolock",
"-noreset",
"-nopn",
"-nolisten",
"unix",
"-fp",
fontpath,
"-screen",
screenres,
};
int argnum = 8;
int argnum = 11;
char * envp[] = { NULL };
int printHelp = 1;
int screenResOverride = 0;
@@ -56,52 +61,6 @@ int main( int argc, char* argv[] )
XSDL_initSDL();
for(i = 0; i < 1024; i++)
{
int s = socket(AF_INET, SOCK_STREAM, 0);
if( s >= 0 )
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(6000 + i);
if( bind (s, (struct sockaddr *) &addr, sizeof(addr) ) != 0 )
{
__android_log_print(ANDROID_LOG_INFO, "XSDL", "TCP port %d already used, trying next one: %s", 6000 + i, strerror(errno));
close(s);
continue;
}
close(s);
}
FILE * ff = fopen("/proc/net/unix", "rb");
if( ff )
{
char buf[512], name[512];
int found = 0;
sprintf(name, "/tmp/.X11-unix/X%d", i);
while( fgets(buf, sizeof(buf), ff) )
{
if( strstr(buf, name) != NULL )
{
__android_log_print(ANDROID_LOG_INFO, "XSDL", "UNIX path %s already used, trying next one", name);
found = 1;
break;
}
}
fclose(ff);
if( found )
continue;
}
sprintf( port, ":%d", i );
break;
}
while( argc > 1 )
{
if( argv[1][0] == ':')
@@ -138,6 +97,9 @@ int main( int argc, char* argv[] )
argv++;
}
if (getenv("SDL_RESTART_PARAMS") && getenv("SDL_RESTART_PARAMS")[0])
strcpy(port, getenv("SDL_RESTART_PARAMS"));
sprintf(fontpath, "%s/img/usr/share/fonts/X11/misc,"
"%s/img/usr/share/fonts/X11/Type1,"
"%s/img/usr/share/fonts/X11/100dpi,"
@@ -188,11 +150,15 @@ int main( int argc, char* argv[] )
for( i = 0; i < argnum; i++ )
__android_log_print(ANDROID_LOG_INFO, "XSDL", "> %s", args[i]);
// We should never quit. If that happens, then the server did not start - show error.
atexit( &showError );
// We should never quit. If that happens, then the server did not start - try with different port number.
atexit( &retryLaunchWithDifferentPort );
__android_log_print(ANDROID_LOG_INFO, "XSDL", "XSDL chdir to: %s", getenv("SECURE_STORAGE_DIR"));
chdir( getenv("SECURE_STORAGE_DIR") ); // Megahack: change /proc/self/cwd to the X.org data dir, and use /proc/self/cwd path in libX11
return android_main( argnum, args, envp );
android_main( argnum, args, envp ); // Should never exit on success, if we want to terminate we kill ourselves
return 0;
}
void setupEnv(void)
@@ -220,9 +186,18 @@ void setupEnv(void)
sprintf( buf, "%s/usr/share/X11/locale", getenv("SECURE_STORAGE_DIR") );
}
void showError(void)
void retryLaunchWithDifferentPort(void)
{
XSDL_initSDL();
XSDL_showServerLaunchErrorMessage();
XSDL_deinitSDL();
int portNum = atoi(port + 1);
if (portNum > 10)
{
// Server was ultimately unable to start - show error and exit
XSDL_initSDL();
XSDL_showServerLaunchErrorMessage();
XSDL_deinitSDL();
return;
}
sprintf(port, ":%d", portNum + 1);
__android_log_print(ANDROID_LOG_INFO, "XSDL", "XSDL launch failed, retrying with new display number %s", port);
SDL_ANDROID_RestartMyself(port);
}