3 # ktmux_helper: Run krenew in the background for tmux.
4 # Notes: Doesn't handle multiple sessions properly.
8 use POSIX ":sys_wait_h";
10 my $PROG = $FindBin::Script;
12 # Ensure tmux is our parent and find its PID.
13 my $tmux_pid = &get_tmux_pid;
15 print STDERR "$PROG: Not a child of tmux!\n";
19 # Ensure there isn't already a helper running for this tmux.
20 my $tmux_helper = &get_tmux_helper;
21 exit 0 if $tmux_helper;
26 our $have_valid_credentials = undef;
28 $SIG{INT} = \&cleanup;
29 $SIG{QUIT} = \&cleanup;
30 $SIG{TERM} = \&cleanup;
31 $SIG{USR1} = \&want_credentials;
32 $SIG{USR2} = \&got_credentials;
34 LOOP: while (&ping_tmux) {
36 die "$PROG: Can't fork: $!\n" unless defined $pid;
40 my $exited = waitpid $pid, WNOHANG;
41 goto LOOP if $exited == $pid || $exited < 0;
45 # tmux is dead so kill krenew.
51 kill USR2, $tmux_helper unless &check_credentials;
52 exec "krenew", "-K", "60";
53 print "$PROG: Can't run krenew: $!\n";
60 my $cmd = `/bin/ps -o args= -p $pid`;
61 return $pid if $cmd =~ /\btmux\b/;
67 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
81 return kill 0, $tmux_pid;
84 # Try to check existing Kerberos credentials.
85 sub check_credentials {
89 kill USR1, $tmux_helper;
93 # We were signalled by our child which noticed that our credentials expired.
94 sub want_credentials {
96 return if defined $have_valid_credentials && $have_valid_credentials == 0;
98 $have_valid_credentials = 0;
99 system "tmux", "new-window", "exec kinit";
102 # We were signalled by our child which noticed that our credentials are valid.
103 sub got_credentials {
104 $have_valid_credentials = 1;
105 $SIG{USR1} = \&want_credentials;
112 waitpid $pid, WNOHANG;