Changes: Refactoring: Too long invoke() split into invoke_fallback() and invoke_remote().

invoked_pid renamed to g_invoked_pid as it's a global variable.
pull/1/head
Jussi Lind 15 years ago
parent 21aade02a0
commit 0fbedf04b8

@ -60,16 +60,16 @@ enum APP_TYPE { M_APP, QT_APP, WRT_APP, UNKNOWN_APP };
extern char ** environ;
// pid of the invoked process
static pid_t invoked_pid = -1;
static pid_t g_invoked_pid = -1;
static void sigs_restore(void);
// Forwards Unix signals from invoker to the invoked process
static void sig_forwarder(int sig)
{
if (invoked_pid >= 0)
if (g_invoked_pid >= 0)
{
if (kill(invoked_pid, sig) != 0)
if (kill(g_invoked_pid, sig) != 0)
{
report(report_error, "Can't send signal to application: %s \n", strerror(errno));
}
@ -445,32 +445,16 @@ static unsigned int get_delay(char *delay_arg)
return delay;
}
// Invokes the given application
static int invoke(int prog_argc, char **prog_argv, char *prog_name,
enum APP_TYPE app_type, int magic_options, bool wait_term)
{
int status = 0;
if (prog_name && prog_argv)
{
// Get process priority
errno = 0;
int prog_prio = getpriority(PRIO_PROCESS, 0);
if (errno && prog_prio < 0)
{
prog_prio = 0;
}
// This is a fallback if connection with the launcher
// process is broken
int fd = invoker_init(app_type);
if (fd == -1)
// Fallback for invoke if connection to the launcher is broken.
// Forks a new process if wait term is not used.
void invoke_fallback(char **prog_argv, char *prog_name, bool wait_term)
{
// Connection with launcher is broken,
// try to launch application via execve
warning("Connection with launcher process is broken. \n");
warning("Trying to start application as a binary executable without launcher...\n");
// Fork if wait_term not set
if(!wait_term)
{
// Fork a new process
@ -481,18 +465,32 @@ static int invoke(int prog_argc, char **prog_argv, char *prog_name,
report(report_error, "Invoker failed to fork");
exit(EXIT_FAILURE);
}
if (newPid != 0) /* parent process */
else if (newPid != 0) /* parent process */
{
return 0;
return;
}
}
// Exec the process image
execve(prog_name, prog_argv, environ);
perror("execve"); /* execve() only returns on error */
exit(EXIT_FAILURE);
}
else
// "normal" invoke through a socket connection
int invoke_remote(int fd, int prog_argc, char **prog_argv, char *prog_name,
int magic_options, bool wait_term)
{
int status = 0;
// Get process priority
errno = 0;
int prog_prio = getpriority(PRIO_PROCESS, 0);
if (errno && prog_prio < 0)
{
prog_prio = 0;
}
// Connection with launcher process is established,
// send the data.
invoker_send_magic(fd, magic_options);
@ -513,8 +511,8 @@ static int invoke(int prog_argc, char **prog_argv, char *prog_name,
// Wait for launched process to exit
if (wait_term)
{
invoked_pid = invoker_recv_pid(fd);
debug("Booster's pid is %d \n ", invoked_pid);
g_invoked_pid = invoker_recv_pid(fd);
debug("Booster's pid is %d \n ", g_invoked_pid);
// Forward signals to the invoked process
sigs_init();
@ -526,6 +524,29 @@ static int invoke(int prog_argc, char **prog_argv, char *prog_name,
sigs_restore();
}
return status;
}
// Invokes the given application
static int invoke(int prog_argc, char **prog_argv, char *prog_name,
enum APP_TYPE app_type, int magic_options, bool wait_term)
{
int status = 0;
if (prog_name && prog_argv)
{
// This is a fallback if connection with the launcher
// process is broken
int fd = invoker_init(app_type);
if (fd == -1)
{
invoke_fallback(prog_argv, prog_name, wait_term);
}
// "normal" invoke through a socket connetion
else
{
status = invoke_remote(fd, prog_argc, prog_argv, prog_name,
magic_options, wait_term);
close(fd);
}
}
@ -565,6 +586,7 @@ int main(int argc, char *argv[])
};
// Parse options
// TODO: Move to a function
int opt;
while ((opt = getopt_long(argc, argv, "hcwd:t:", longopts, NULL)) != -1)
{

Loading…
Cancel
Save