2011-10-28 16:26:37 -04:00
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2011-10-31 20:24:38 -04:00
*
* Kill a child process when the current directory is deleted
2011-10-28 16:26:37 -04:00
*
* Copyright ( C ) 2011 Colin Walters < walters @ verbum . org >
*
2011-11-10 13:17:04 -05:00
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2 of the License , or ( at your option ) any later version .
2011-10-28 16:26:37 -04:00
*
2011-11-10 13:17:04 -05:00
* This library is distributed in the hope that it will be useful ,
2011-10-28 16:26:37 -04:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2011-11-10 13:17:04 -05:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
2011-10-28 16:26:37 -04:00
*
2011-11-10 13:17:04 -05:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library ; if not , write to the
* Free Software Foundation , Inc . , 59 Temple Place - Suite 330 ,
* Boston , MA 02111 - 1307 , USA .
2011-10-28 16:26:37 -04:00
*
* Author : Colin Walters < walters @ verbum . org >
*/
2011-10-31 20:24:38 -04:00
# include <gio/gio.h>
2011-10-28 16:26:37 -04:00
# include <unistd.h>
2011-10-28 16:56:50 -04:00
# include <stdlib.h>
2011-10-31 20:24:38 -04:00
# include <string.h>
2011-10-28 16:26:37 -04:00
2011-10-31 20:24:38 -04:00
struct TmpdirLifecyleData {
GMainLoop * loop ;
GPid pid ;
gboolean exited ;
} ;
2011-10-28 16:26:37 -04:00
2011-10-28 16:56:50 -04:00
static void
on_dir_changed ( GFileMonitor * mon ,
GFile * file ,
GFile * other ,
GFileMonitorEvent event ,
gpointer user_data )
{
2011-10-31 20:24:38 -04:00
struct TmpdirLifecyleData * data = user_data ;
2011-10-28 16:56:50 -04:00
if ( event = = G_FILE_MONITOR_EVENT_DELETED )
2011-10-31 20:24:38 -04:00
g_main_loop_quit ( data - > loop ) ;
}
static void
on_child_exited ( GPid pid ,
int status ,
gpointer user_data )
{
struct TmpdirLifecyleData * data = user_data ;
data - > exited = TRUE ;
g_main_loop_quit ( data - > loop ) ;
2011-10-28 16:56:50 -04:00
}
2011-10-28 16:26:37 -04:00
int
main ( int argc ,
char * * argv )
{
2011-10-28 16:56:50 -04:00
GFileMonitor * monitor ;
GFile * curdir ;
GError * error = NULL ;
2011-10-31 20:24:38 -04:00
GPtrArray * new_argv ;
int i ;
struct TmpdirLifecyleData data ;
2011-10-28 16:26:37 -04:00
g_type_init ( ) ;
2011-10-31 20:24:38 -04:00
memset ( & data , 0 , sizeof ( data ) ) ;
data . loop = g_main_loop_new ( NULL , TRUE ) ;
2011-10-28 16:56:50 -04:00
curdir = g_file_new_for_path ( " . " ) ;
monitor = g_file_monitor_directory ( curdir , 0 , NULL , & error ) ;
if ( ! monitor )
exit ( 1 ) ;
g_signal_connect ( monitor , " changed " ,
2011-10-31 20:24:38 -04:00
G_CALLBACK ( on_dir_changed ) , & data ) ;
2011-10-28 16:26:37 -04:00
2011-10-31 20:24:38 -04:00
new_argv = g_ptr_array_new ( ) ;
for ( i = 1 ; i < argc ; i + + )
g_ptr_array_add ( new_argv , argv [ i ] ) ;
g_ptr_array_add ( new_argv , NULL ) ;
2011-10-28 16:26:37 -04:00
2011-10-31 20:24:38 -04:00
if ( ! g_spawn_async ( NULL , ( char * * ) new_argv - > pdata , NULL , G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD ,
2011-11-01 12:35:50 -04:00
NULL , NULL , & data . pid , & error ) )
2011-10-31 20:24:38 -04:00
{
g_printerr ( " %s \n " , error - > message ) ;
return 1 ;
}
2011-11-01 12:35:50 -04:00
g_child_watch_add ( data . pid , on_child_exited , & data ) ;
2011-10-28 16:26:37 -04:00
2011-10-31 20:24:38 -04:00
g_main_loop_run ( data . loop ) ;
2011-10-28 16:26:37 -04:00
2011-10-31 20:24:38 -04:00
if ( ! data . exited )
kill ( data . pid , SIGTERM ) ;
2011-10-28 16:26:37 -04:00
return 0 ;
}