/** * @param host The host to connect too * * SSH into the selected host, if successful update history. */ static void exec_ssh ( const char *host ) { if ( !host || !host[0] ) { return; } if ( !execshssh ( host ) ) { return; } // This happens in non-critical time (After launching app) // It is allowed to be a bit slower. char *path = g_build_filename ( cache_dir, SSH_CACHE_FILE, NULL ); history_set ( path, host ); g_free ( path ); }
// execute sub-process static pid_t exec_ssh ( const char *cmd ) { if ( !cmd || !cmd[0] ) { return -1; } execshssh ( cmd ); /** * This happens in non-critical time (After launching app) * It is allowed to be a bit slower. */ char *path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE ); history_set ( path, cmd ); g_free ( path ); return 0; }
// execute sub-process static pid_t exec_ssh( const char *cmd ) { if ( !cmd || !cmd[0] ) return -1; signal( SIGCHLD, catch_exit ); pid_t pid = fork(); if ( !pid ) { setsid(); execshssh( cmd ); exit( EXIT_FAILURE ); } int curr = -1; unsigned int index = 0; char **retv = NULL; /** * This happens in non-critical time (After launching app) * It is allowed to be a bit slower. */ char *path = allocate( strlen( cache_dir ) + strlen( SSH_CACHE_FILE )+3 ); sprintf( path, "%s/%s", cache_dir, SSH_CACHE_FILE ); FILE *fd = fopen ( path, "r" ); if ( fd != NULL ) { char buffer[1024]; while ( fgets( buffer,1024,fd ) != NULL ) { retv = reallocate( retv, ( index+2 )*sizeof( char* ) ); buffer[strlen( buffer )-1] = '\0'; retv[index] = strdup( buffer ); retv[index+1] = NULL; if ( strcasecmp( retv[index], cmd ) == 0 ) { curr = index; } index++; } fclose( fd ); } /** * Write out the last 25 results again. */ fd = fopen ( path, "w" ); if ( fd ) { // Last one goes on top! fputs( cmd, fd ); fputc( '\n', fd ); for ( int i = 0; i < ( int )index && i < 20; i++ ) { if ( i != curr ) { fputs( retv[i], fd ); fputc( '\n', fd ); } } fclose( fd ); } for ( int i=0; retv != NULL && retv[i] != NULL; i++ ) { free( retv[i] ); } free( retv ); free( path ); return pid; }