-
Notifications
You must be signed in to change notification settings - Fork 3
/
sessionmanager.pl
80 lines (60 loc) · 1.91 KB
/
sessionmanager.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Data::Dumper;
my $pid_map = {};
my $LOGFILE = ">>/tmp/session_manager.log";
my $LOG;
open $LOG , $LOGFILE
|| die( "Failed to open session manager log:\n" . $! );
use Getopt::Long;
my ( $display , $port , $command, $username );
GetOptions(
'display=i' => \$display
, 'port=i' => \$port
, 'username=s' => \$username
, 'command=s' => \$command
);
print $LOG "Got:\n"
. " display: [$display]\n"
. " port: [$port]\n"
. " command: [$command]\n\n";
sub fork_it {
my ( $type , $args , $display ) = @_;
my $pid = fork;
if ( $pid ) {
$pid_map->{$pid} = $type;
} elsif ( defined ( $pid ) ) {
my $cmd_string = join(' ', @{$args});
if ( $display ) {
$cmd_string = "BROADWAY_USER=$username GDK_BACKEND=broadway BROADWAY_DISPLAY=:$display $cmd_string";
}
print $LOG "Executing: [$cmd_string] username $username\n";
# redirect exec process output to $LOGFILE
open STDOUT, $LOGFILE or die $!;
open STDERR, $LOGFILE or die $!;
exec( $cmd_string )
or print $LOG "Couldn't exec $cmd_string ($!)\n";
} else {
die( 'Failed to fork: ' . $! );
}
}
my $args = [ 'broadwayd' , '-p' , $port , ':' . $display ];
fork_it( 'broadwayd', $args );
sleep( 2 );
$args = [ $command ];
fork_it( $command , $args , $display );
my $wait_pid = wait || die('Failed to wait: ' . $!);
if ( $wait_pid ) {
print $LOG "Caught PID [$wait_pid] exiting\n";
print $LOG " ... " . $pid_map->{$wait_pid} . "\n";
print $LOG "Whole map:\n" . Dumper( $pid_map ) . "\n";
# kill other process
foreach my $key ( keys %{ $pid_map } ) {
if ( $wait_pid ne $key ) {
kill "HUP" , $key;
}
}
}
close $LOG
|| die( "Failed to close session manager log:\n" . $! );