1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-03 04:23:42 -05:00

Use g_spawn instead of fork+exec*

* Avoids issue with atexit being called for fork.
        * less code.
This commit is contained in:
QC 2014-08-25 20:02:48 +02:00
parent 30051c8e80
commit 24e7041604
4 changed files with 63 additions and 72 deletions

View file

@ -1399,7 +1399,8 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
// No item selected, but user entered something
else if ( strlen ( text->text ) > 0 ) {
retv = MENU_CUSTOM_INPUT;
}else{
}
else{
retv = MENU_CANCEL;
}

View file

@ -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 );
}
/**
* 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 )

View file

@ -41,39 +41,24 @@
pid_t execute_generator ( char * cmd )
{
int filedes[2];
pid_t 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;
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;
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,8 +68,11 @@ static char **get_script_output ( char *command, unsigned int *length )
char **retv = NULL;
*length = 0;
execute_generator ( command );
while ( fgets ( buffer, 1024, stdin ) != NULL ) {
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;
@ -96,7 +84,9 @@ static char **get_script_output ( char *command, unsigned int *length )
( *length )++;
}
fclose ( inp );
}
}
return retv;
}

View file

@ -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 );
}
/**
* 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 )
{