You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
547 lines
14 KiB
547 lines
14 KiB
15 years ago
|
/*
|
||
|
This file is part of the KDE libraries
|
||
|
Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
|
||
|
(c) 1999 Mario Weilguni <mweilguni@sime.com>
|
||
|
(c) 2001 Lubos Lunak <l.lunak@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2 as published by the Free Software Foundation.
|
||
|
|
||
|
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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include <config.h>
|
||
|
|
||
12 years ago
|
#include "tdelauncher_cmds.h"
|
||
15 years ago
|
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/param.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/un.h>
|
||
|
|
||
|
#include <errno.h>
|
||
|
#include <string.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <pwd.h>
|
||
|
#include <signal.h>
|
||
|
|
||
|
extern char **environ;
|
||
|
|
||
|
static char *getDisplay()
|
||
|
{
|
||
|
const char *display;
|
||
|
char *result;
|
||
|
char *screen;
|
||
|
char *colon;
|
||
|
char *i;
|
||
|
/*
|
||
15 years ago
|
don't test for a value from tqglobal.h but instead distinguish
|
||
15 years ago
|
Qt/X11 from Qt/Embedded by the fact that Qt/E apps have -DQWS
|
||
15 years ago
|
on the commandline (which in tqglobal.h however triggers Q_WS_QWS,
|
||
15 years ago
|
but we don't want to include that here) (Simon)
|
||
|
#ifdef Q_WS_X11
|
||
|
*/
|
||
|
#if !defined(QWS)
|
||
|
display = getenv("DISPLAY");
|
||
|
#else
|
||
|
display = getenv("QWS_DISPLAY");
|
||
|
#endif
|
||
|
if (!display || !*display)
|
||
|
{
|
||
|
display = ":0";
|
||
|
}
|
||
|
result = malloc(strlen(display)+1);
|
||
|
if (result == NULL)
|
||
|
return NULL;
|
||
|
|
||
|
strcpy(result, display);
|
||
|
screen = strrchr(result, '.');
|
||
|
colon = strrchr(result, ':');
|
||
|
if (screen && (screen > colon))
|
||
|
*screen = '\0';
|
||
|
while((i = strchr(result, ':')))
|
||
|
*i = '_';
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Write 'len' bytes from 'buffer' into 'sock'.
|
||
|
* returns 0 on success, -1 on failure.
|
||
|
*/
|
||
|
static int write_socket(int sock, char *buffer, int len)
|
||
|
{
|
||
|
ssize_t result;
|
||
|
int bytes_left = len;
|
||
|
while ( bytes_left > 0)
|
||
|
{
|
||
|
result = write(sock, buffer, bytes_left);
|
||
|
if (result > 0)
|
||
|
{
|
||
|
buffer += result;
|
||
|
bytes_left -= result;
|
||
|
}
|
||
|
else if (result == 0)
|
||
|
return -1;
|
||
|
else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Read 'len' bytes from 'sock' into 'buffer'.
|
||
|
* returns 0 on success, -1 on failure.
|
||
|
*/
|
||
|
static int read_socket(int sock, char *buffer, int len)
|
||
|
{
|
||
|
ssize_t result;
|
||
|
int bytes_left = len;
|
||
|
while ( bytes_left > 0)
|
||
|
{
|
||
|
result = read(sock, buffer, bytes_left);
|
||
|
if (result > 0)
|
||
|
{
|
||
|
buffer += result;
|
||
|
bytes_left -= result;
|
||
|
}
|
||
|
else if (result == 0)
|
||
|
return -1;
|
||
|
else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int openSocket()
|
||
|
{
|
||
|
kde_socklen_t socklen;
|
||
|
int s;
|
||
|
struct sockaddr_un server;
|
||
|
#define MAX_SOCK_FILE 255
|
||
|
char sock_file[MAX_SOCK_FILE + 1];
|
||
|
const char *home_dir = getenv("HOME");
|
||
13 years ago
|
const char *kde_home = getenv("TDEHOME");
|
||
15 years ago
|
char *display;
|
||
|
|
||
|
sock_file[0] = sock_file[MAX_SOCK_FILE] = 0;
|
||
|
|
||
|
if (!kde_home || !kde_home[0])
|
||
|
{
|
||
14 years ago
|
kde_home = "~/.trinity/";
|
||
15 years ago
|
}
|
||
|
|
||
|
if (kde_home[0] == '~')
|
||
|
{
|
||
|
if (!home_dir || !home_dir[0])
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Warning: $HOME not set!\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
if (strlen(home_dir) > (MAX_SOCK_FILE-100))
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Warning: Home directory path too long!\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
kde_home++;
|
||
|
strncpy(sock_file, home_dir, MAX_SOCK_FILE);
|
||
|
}
|
||
|
strncat(sock_file, kde_home, MAX_SOCK_FILE - strlen(sock_file));
|
||
|
|
||
|
/** Strip trailing '/' **/
|
||
|
if ( sock_file[strlen(sock_file)-1] == '/')
|
||
|
sock_file[strlen(sock_file)-1] = 0;
|
||
|
|
||
|
strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
|
||
14 years ago
|
if( getenv("XAUTHLOCALHOSTNAME"))
|
||
|
strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
|
||
|
else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
|
||
15 years ago
|
{
|
||
12 years ago
|
perror("[tdeinit wrapper] Warning: Could not determine hostname: ");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
sock_file[sizeof(sock_file)-1] = '\0';
|
||
|
|
||
|
/* append $DISPLAY */
|
||
|
display = getDisplay();
|
||
|
if (display == NULL)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Error: Could not determine display.\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
13 years ago
|
if (strlen(sock_file)+strlen(display)+strlen("/tdeinit_")+2 > MAX_SOCK_FILE)
|
||
15 years ago
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Warning: Socket name will be too long.\n");
|
||
15 years ago
|
free (display);
|
||
|
return -1;
|
||
|
}
|
||
13 years ago
|
strcat(sock_file, "/tdeinit_");
|
||
15 years ago
|
strcat(sock_file, display);
|
||
|
free(display);
|
||
|
|
||
|
if (strlen(sock_file) >= sizeof(server.sun_path))
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Warning: Path of socket file exceeds UNIX_PATH_MAX.\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* create the socket stream
|
||
|
*/
|
||
|
s = socket(PF_UNIX, SOCK_STREAM, 0);
|
||
|
if (s < 0)
|
||
|
{
|
||
12 years ago
|
perror("[tdeinit wrapper] Warning: socket creation failed: ");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
server.sun_family = AF_UNIX;
|
||
|
strcpy(server.sun_path, sock_file);
|
||
|
socklen = sizeof(server);
|
||
|
if(connect(s, (struct sockaddr *)&server, socklen) == -1)
|
||
|
{
|
||
12 years ago
|
perror("[tdeinit wrapper] Warning: socket connection failed: ");
|
||
15 years ago
|
close(s);
|
||
|
return -1;
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
static pid_t kwrapper_pid;
|
||
|
static volatile pid_t got_sig; /* = 0 */
|
||
|
static pid_t last_sig; /* = 0 */
|
||
|
|
||
|
static void sig_pass_handler( int signo );
|
||
|
static void setup_signals( void );
|
||
|
|
||
|
static void setup_signal_handler( int signo, int clean )
|
||
|
{
|
||
|
struct sigaction sa;
|
||
|
if( clean )
|
||
|
sa.sa_handler = SIG_DFL;
|
||
|
else
|
||
|
sa.sa_handler = sig_pass_handler;
|
||
|
sigemptyset( &sa.sa_mask );
|
||
|
sigaddset( &sa.sa_mask, signo );
|
||
|
sa.sa_flags = 0; /* don't use SA_RESTART */
|
||
|
sigaction( signo, &sa, 0 );
|
||
|
}
|
||
|
|
||
|
static void sig_pass_handler( int signo )
|
||
|
{
|
||
|
int save_errno = errno;
|
||
|
if( signo == SIGTSTP )
|
||
|
kill( kwrapper_pid, SIGSTOP ); /* pass the signal to the real process */
|
||
|
else /* SIGTSTP wouldn't work ... I don't think is much */
|
||
|
kill( kwrapper_pid, signo ); /* of a problem */
|
||
|
got_sig = signo;
|
||
|
if( got_sig == SIGCONT )
|
||
|
setup_signals(); /* restore signals */
|
||
|
errno = save_errno;
|
||
|
}
|
||
|
|
||
|
static void setup_signals()
|
||
|
{
|
||
|
setup_signal_handler( SIGHUP, 0 );
|
||
|
setup_signal_handler( SIGINT, 0 );
|
||
|
setup_signal_handler( SIGQUIT, 0 );
|
||
|
setup_signal_handler( SIGILL, 0 ); /* e.g. this one is probably doesn't make sense to pass */
|
||
|
setup_signal_handler( SIGABRT, 0 ); /* but anyway ... */
|
||
|
setup_signal_handler( SIGFPE, 0 );
|
||
|
/* SIGKILL can't be handled :( */
|
||
|
setup_signal_handler( SIGSEGV, 0 );
|
||
|
setup_signal_handler( SIGPIPE, 0 );
|
||
|
setup_signal_handler( SIGALRM, 0 );
|
||
|
setup_signal_handler( SIGTERM, 0 );
|
||
|
setup_signal_handler( SIGUSR1, 0 );
|
||
|
setup_signal_handler( SIGUSR2, 0 );
|
||
|
setup_signal_handler( SIGCHLD, 0 ); /* is this a good idea ??? */
|
||
|
setup_signal_handler( SIGCONT, 0 ); /* SIGSTOP can't be handled, but SIGTSTP and SIGCONT can */
|
||
|
/* SIGSTOP */ /* which should be enough */
|
||
|
setup_signal_handler( SIGTSTP, 0 );
|
||
|
setup_signal_handler( SIGTTIN, 0 ); /* is this a good idea ??? */
|
||
|
setup_signal_handler( SIGTTOU, 0 ); /* is this a good idea ??? */
|
||
|
/* some more ? */
|
||
|
}
|
||
|
|
||
|
static void kwrapper_run( pid_t pid )
|
||
|
{
|
||
|
kwrapper_pid = pid;
|
||
|
setup_signals();
|
||
|
for(;;)
|
||
|
{
|
||
|
sleep( 1 ); /* poll and see if the real process is still alive */
|
||
|
if( got_sig != last_sig ) /* did we get a signal ? */
|
||
|
{
|
||
|
last_sig = got_sig;
|
||
|
if( got_sig == SIGCHLD )
|
||
|
; /* nothing, ignore */
|
||
|
else if( got_sig == SIGCONT )
|
||
|
; /* done in signal handler */
|
||
|
else /* do the default action ( most of them quit the app ) */
|
||
|
{
|
||
|
setup_signal_handler( got_sig, 1 );
|
||
|
raise( got_sig ); /* handle the signal again */
|
||
|
}
|
||
|
}
|
||
|
if( kill( pid, 0 ) < 0 )
|
||
|
break;
|
||
|
}
|
||
|
/* I'm afraid it won't be that easy to get the return value ... */
|
||
|
}
|
||
|
|
||
|
int main(int argc, char **argv)
|
||
|
{
|
||
|
int i;
|
||
|
int wrapper = 0;
|
||
|
int ext_wrapper = 0;
|
||
|
int kwrapper = 0;
|
||
|
long arg_count;
|
||
|
long env_count;
|
||
12 years ago
|
tdelauncher_header header;
|
||
15 years ago
|
char *start, *p, *buffer;
|
||
|
char cwd[8192];
|
||
|
const char *tty = NULL;
|
||
|
long avoid_loops = 0;
|
||
|
const char* startup_id = NULL;
|
||
|
int sock;
|
||
|
|
||
|
long size = 0;
|
||
|
|
||
|
start = argv[0];
|
||
|
p = start + strlen(argv[0]);
|
||
|
while (--p > start)
|
||
|
{
|
||
|
if (*p == '/') break;
|
||
|
}
|
||
|
if ( p > start)
|
||
|
p++;
|
||
|
start = p;
|
||
|
|
||
13 years ago
|
if (strcmp(start, "tdeinit_wrapper") == 0)
|
||
15 years ago
|
wrapper = 1;
|
||
|
else if (strcmp(start, "kshell") == 0)
|
||
|
ext_wrapper = 1;
|
||
|
else if (strcmp(start, "kwrapper") == 0)
|
||
|
kwrapper = 1;
|
||
13 years ago
|
else if (strcmp(start, "tdeinit_shutdown") == 0)
|
||
15 years ago
|
{
|
||
|
if( argc > 1)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Usage: %s\n\n", start);
|
||
|
fprintf(stderr, "[tdeinit wrapper] Shuts down tdeinit master process and terminates all processes spawned from it.\n");
|
||
15 years ago
|
exit( 255 );
|
||
|
}
|
||
|
sock = openSocket();
|
||
|
if( sock < 0 )
|
||
|
{
|
||
12 years ago
|
fprintf( stderr, "[tdeinit wrapper] Error: Can't contact tdeinit!\n" );
|
||
15 years ago
|
exit( 255 );
|
||
|
}
|
||
|
header.cmd = LAUNCHER_TERMINATE_KDE;
|
||
|
header.arg_length = 0;
|
||
|
write_socket(sock, (char *) &header, sizeof(header));
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (wrapper || ext_wrapper || kwrapper)
|
||
|
{
|
||
|
argv++;
|
||
|
argc--;
|
||
|
if (argc < 1)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Usage: %s <application> [<args>]\n", start);
|
||
15 years ago
|
exit(255); /* usage should be documented somewhere ... */
|
||
|
}
|
||
|
start = argv[0];
|
||
|
}
|
||
|
|
||
|
sock = openSocket();
|
||
13 years ago
|
if( sock < 0 ) /* couldn't contact tdeinit, start argv[ 0 ] directly */
|
||
15 years ago
|
{
|
||
|
execvp( argv[ 0 ], argv );
|
||
12 years ago
|
fprintf( stderr, "[tdeinit wrapper] Error: Can't run %s !\n", argv[ 0 ] );
|
||
15 years ago
|
exit( 255 );
|
||
|
}
|
||
|
|
||
|
if( !wrapper && !ext_wrapper && !kwrapper )
|
||
|
{ /* was called as a symlink */
|
||
|
avoid_loops = 1;
|
||
|
#if defined(WE_ARE_KWRAPPER)
|
||
|
kwrapper = 1;
|
||
|
#elif defined(WE_ARE_KSHELL)
|
||
|
ext_wrapper = 1;
|
||
|
#else
|
||
|
wrapper = 1;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
arg_count = argc;
|
||
|
|
||
|
size += sizeof(long); /* Number of arguments*/
|
||
|
|
||
|
size += strlen(start)+1; /* Size of first argument. */
|
||
|
|
||
|
for(i = 1; i < argc; i++)
|
||
|
{
|
||
|
size += strlen(argv[i])+1;
|
||
|
}
|
||
|
if( wrapper )
|
||
|
{
|
||
|
size += sizeof(long); /* empty envs */
|
||
|
}
|
||
|
if (ext_wrapper || kwrapper)
|
||
|
{
|
||
|
if (!getcwd(cwd, 8192))
|
||
|
cwd[0] = '\0';
|
||
|
size += strlen(cwd)+1;
|
||
|
|
||
|
env_count = 0;
|
||
|
size += sizeof(long); /* Number of env.vars. */
|
||
|
|
||
|
for(; environ[env_count] ; env_count++)
|
||
|
{
|
||
|
int l = strlen(environ[env_count])+1;
|
||
|
size += l;
|
||
|
}
|
||
|
|
||
|
if( kwrapper )
|
||
|
{
|
||
|
tty = ttyname(1);
|
||
|
if (!tty || !isatty(2))
|
||
|
tty = "";
|
||
|
size += strlen(tty)+1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
size += sizeof( avoid_loops );
|
||
|
|
||
|
if( !wrapper )
|
||
|
{
|
||
|
startup_id = getenv( "DESKTOP_STARTUP_ID" );
|
||
|
if( startup_id == NULL )
|
||
|
startup_id = "";
|
||
|
size += strlen( startup_id ) + 1;
|
||
|
}
|
||
|
|
||
|
if (wrapper)
|
||
|
header.cmd = LAUNCHER_EXEC_NEW;
|
||
|
else if (kwrapper)
|
||
|
header.cmd = LAUNCHER_KWRAPPER;
|
||
|
else
|
||
|
header.cmd = LAUNCHER_SHELL;
|
||
|
header.arg_length = size;
|
||
|
write_socket(sock, (char *) &header, sizeof(header));
|
||
|
|
||
|
buffer = (char *) malloc(size);
|
||
|
if (buffer == NULL)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Error: malloc() failed.");
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
p = buffer;
|
||
|
|
||
|
memcpy(p, &arg_count, sizeof(arg_count));
|
||
|
p += sizeof(arg_count);
|
||
|
|
||
|
memcpy(p, start, strlen(start)+1);
|
||
|
p += strlen(start)+1;
|
||
|
|
||
|
for(i = 1; i < argc; i++)
|
||
|
{
|
||
|
memcpy(p, argv[i], strlen(argv[i])+1);
|
||
|
p += strlen(argv[i])+1;
|
||
|
}
|
||
|
|
||
|
if( wrapper )
|
||
|
{
|
||
|
long dummy = 0;
|
||
|
memcpy(p, &dummy, sizeof(dummy)); /* empty envc */
|
||
|
p+= sizeof( dummy );
|
||
|
}
|
||
|
if (ext_wrapper || kwrapper)
|
||
|
{
|
||
|
memcpy(p, cwd, strlen(cwd)+1);
|
||
|
p+= strlen(cwd)+1;
|
||
|
|
||
|
memcpy(p, &env_count, sizeof(env_count));
|
||
|
p+= sizeof(env_count);
|
||
|
|
||
|
for(i = 0; i < env_count; i++)
|
||
|
{
|
||
|
int l = strlen(environ[i])+1;
|
||
|
memcpy(p, environ[i], l);
|
||
|
p += l;
|
||
|
}
|
||
|
|
||
|
if( kwrapper )
|
||
|
{
|
||
|
memcpy(p, tty, strlen(tty)+1);
|
||
|
p+=strlen(tty)+1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
memcpy( p, &avoid_loops, sizeof( avoid_loops ));
|
||
|
p += sizeof( avoid_loops );
|
||
|
|
||
|
if( !wrapper )
|
||
|
{
|
||
|
memcpy(p, startup_id, strlen(startup_id)+1);
|
||
|
p+= strlen(startup_id)+1;
|
||
|
}
|
||
|
|
||
|
if( p - buffer != size ) /* should fail only if you change this source and do */
|
||
|
/* a stupid mistake, it should be assert() actually */
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Oops. Invalid format.\n");
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
|
||
|
write_socket(sock, buffer, size);
|
||
|
free( buffer );
|
||
|
|
||
|
if (read_socket(sock, (char *) &header, sizeof(header))==-1)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Communication error.\n");
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
|
||
|
if (header.cmd == LAUNCHER_OK)
|
||
|
{
|
||
|
long pid;
|
||
|
buffer = (char *) malloc(header.arg_length);
|
||
|
if (buffer == NULL)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Error: malloc() failed\n");
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
read_socket(sock, buffer, header.arg_length);
|
||
|
pid = *((long *) buffer);
|
||
14 years ago
|
if ( !(!kwrapper) )
|
||
15 years ago
|
kwrapper_run( pid );
|
||
|
}
|
||
|
else if (header.cmd == LAUNCHER_ERROR)
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Could not launch '%s'.\n", start);
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
12 years ago
|
fprintf(stderr, "[tdeinit wrapper] Unexpected response (response = %ld).\n", header.cmd);
|
||
15 years ago
|
exit(255);
|
||
|
}
|
||
|
exit(0);
|
||
|
}
|