summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQC <qball@gmpclient.org>2014-08-25 20:02:48 +0200
committerQC <qball@gmpclient.org>2014-08-25 20:02:48 +0200
commit24e7041604a7637bee9daf32413526c22f23bf19 (patch)
tree84d2c06f4166398cded63c66ad41b669f95aa4aa
parent30051c8e80c9c5fe1f40b6c5ce79e21f3ace7f4e (diff)
Use g_spawn instead of fork+exec*
* Avoids issue with atexit being called for fork. * less code.
-rw-r--r--source/rofi.c7
-rw-r--r--source/run-dialog.c31
-rw-r--r--source/script-dialog.c80
-rw-r--r--source/ssh-dialog.c21
4 files changed, 65 insertions, 74 deletions
diff --git a/source/rofi.c b/source/rofi.c
index 1a74571d..a4030bb8 100644
--- a/source/rofi.c
+++ b/source/rofi.c
@@ -1397,9 +1397,10 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
*selected_line = line_map[selected];
}
// No item selected, but user entered something
- else if ( strlen (text->text) > 0 ){
+ else if ( strlen ( text->text ) > 0 ) {
retv = MENU_CUSTOM_INPUT;
- }else{
+ }
+ else{
retv = MENU_CANCEL;
}
@@ -1734,7 +1735,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode )
// Otherwise check if requested mode is enabled.
if ( switchers[mode].cb != NULL ) {
do {
- SwitcherMode retv ;
+ SwitcherMode retv;
retv = switchers[mode].cb ( &input, switchers[mode].cb_data );
diff --git a/source/run-dialog.c b/source/run-dialog.c
index 6853aa7b..084e2c5f 100644
--- a/source/run-dialog.c
+++ b/source/run-dialog.c
@@ -49,29 +49,34 @@
static inline int execsh ( const char *cmd, int run_in_term )
{
-// use sh for args parsing
+ char **args = g_malloc_n ( 6, sizeof ( char* ) );
+ int i = 0;
if ( run_in_term ) {
- return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL );
+ args[i++] = g_strdup ( config.terminal_emulator );
+ args[i++] = g_strdup ( "-e" );
}
+ args[i++] = g_strdup ( "sh" );
+ args[i++] = g_strdup ( "-c" );
+ args[i++] = g_strdup ( cmd );
+ args[i++] = NULL;
- return execlp ( "/bin/sh", "sh", "-c", cmd, NULL );
+ g_spawn_async ( NULL, args, NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL, NULL, NULL, NULL );
+
+ // Free the args list.
+ g_strfreev ( args );
}
// execute sub-process
-static pid_t exec_cmd ( const char *cmd, int run_in_term )
+static void exec_cmd ( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] ) {
- return -1;
+ return;
}
- signal ( SIGCHLD, catch_exit );
- pid_t pid = fork ();
- if ( !pid ) {
- setsid ();
- execsh ( cmd, run_in_term );
- exit ( EXIT_FAILURE );
- }
+ execsh ( cmd, run_in_term );
/**
* This happens in non-critical time (After launching app)
@@ -82,8 +87,6 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
history_set ( path, cmd );
g_free ( path );
-
- return pid;
}
// execute sub-process
static void delete_entry ( const char *cmd )
diff --git a/source/script-dialog.c b/source/script-dialog.c
index 1be41af7..807ab289 100644
--- a/source/script-dialog.c
+++ b/source/script-dialog.c
@@ -41,39 +41,24 @@
pid_t execute_generator ( char * cmd )
{
- int filedes[2];
- pid_t pid;
-
-
- if ( -1 == pipe ( filedes ) ) {
- perror ( "pipe failed" );
- return 0;
- }
-
- switch ( pid = fork () )
- {
- case -1:
- perror ( "Failed to fork, executing generator failed" );
- pid = 0;
- break;
-
- case 0: /* child */
- close ( 1 );
- dup ( filedes[1] );
- close ( filedes[1] );
- execlp ( "/bin/sh", "sh", "-c", cmd, NULL );
- perror ( cmd );
- break;
-
- default: /* parent */
- close ( 0 );
- dup ( filedes[0] );
- close ( filedes[0] );
- close ( filedes[1] );
- break;
- }
-
- return pid;
+ char **args = g_malloc_n ( 4, sizeof ( char* ) );
+ args[0] = g_strdup ( "sh" );
+ args[1] = g_strdup ( "-c" );
+ args[2] = g_strdup ( cmd );
+ args[3] = NULL;
+
+ int fd;
+ g_spawn_async_with_pipes ( NULL,
+ args,
+ NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL,
+ NULL,
+ NULL,
+ NULL, &fd, NULL,
+ NULL );
+ g_strfreev ( args );
+ return fd;
}
@@ -83,20 +68,25 @@ static char **get_script_output ( char *command, unsigned int *length )
char **retv = NULL;
*length = 0;
- execute_generator ( command );
- while ( fgets ( buffer, 1024, stdin ) != NULL ) {
- retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
- retv[( *length )] = g_strdup ( buffer );
- retv[( *length ) + 1] = NULL;
-
- // Filter out line-end.
- if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
- retv[( *length )][strlen ( buffer ) - 1] = '\0';
+ int fd = execute_generator ( command );
+ if ( fd >= 0 ) {
+ FILE *inp = fdopen ( fd, "r" );
+ if ( inp ) {
+ while ( fgets ( buffer, 1024, inp ) != NULL ) {
+ retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
+ retv[( *length )] = g_strdup ( buffer );
+ retv[( *length ) + 1] = NULL;
+
+ // Filter out line-end.
+ if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
+ retv[( *length )][strlen ( buffer ) - 1] = '\0';
+ }
+
+ ( *length )++;
+ }
+ fclose ( inp );
}
-
- ( *length )++;
}
-
return retv;
}
diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c
index dfc809cf..33ef03af 100644
--- a/source/ssh-dialog.c
+++ b/source/ssh-dialog.c
@@ -55,7 +55,7 @@ static inline int execshssh ( const char *host )
*/
char **args = g_malloc_n ( 7, sizeof ( char* ) );
int i = 0;
- args[i++] = config.terminal_emulator;
+ args[i++] = g_strdup ( config.terminal_emulator );
if ( config.ssh_set_title ) {
args[i++] = g_strdup ( "-T" );
args[i++] = g_strdup_printf ( "ssh %s", host );
@@ -64,11 +64,15 @@ static inline int execshssh ( const char *host )
args[i++] = g_strdup ( "ssh" );
args[i++] = g_strdup ( host );
args[i++] = NULL;
- int retv = execvp ( config.terminal_emulator, (char * const *) args );
+
+ g_spawn_async ( NULL, args, NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL, NULL, NULL, NULL );
// Free the args list.
g_strfreev ( args );
- return retv;
+
+ return 0;
}
// execute sub-process
static pid_t exec_ssh ( const char *cmd )
@@ -77,14 +81,7 @@ static pid_t exec_ssh ( const char *cmd )
return -1;
}
- signal ( SIGCHLD, catch_exit );
- pid_t pid = fork ();
-
- if ( !pid ) {
- setsid ();
- execshssh ( cmd );
- exit ( EXIT_FAILURE );
- }
+ execshssh ( cmd );
/**
* This happens in non-critical time (After launching app)
@@ -94,7 +91,7 @@ static pid_t exec_ssh ( const char *cmd )
history_set ( path, cmd );
g_free ( path );
- return pid;
+ return 0;
}
static void delete_ssh ( const char *cmd )
{