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 our $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;
27 $SIG{INT} = \&cleanup;
28 $SIG{QUIT} = \&cleanup;
29 $SIG{TERM} = \&cleanup;
30 $SIG{USR1} = \&want_credentials;
32 LOOP: while (&ping_tmux) {
34 die "$PROG: Can't fork: $!\n" unless defined $pid;
38 my $exited = waitpid $pid, WNOHANG;
39 goto LOOP if $exited == $pid || $exited < 0;
43 # tmux is dead so kill krenew.
49 exit 1 if &check_credentials;
50 exec "krenew", "-K", "60";
51 print "$PROG: Can't run krenew: $!\n";
58 my $cmd = `/bin/ps -o args= -p $pid`;
59 return $pid if $cmd =~ /\btmux\b/;
63 sub check_kinit_child {
64 foreach my $pid (`/bin/ps -o ppid= -C kinit`) {
65 return 1 if $pid =~ /^\s*$tmux_pid\s*$/;
72 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
86 return kill 0, $tmux_pid;
89 # Try to check existing Kerberos credentials.
90 sub check_credentials {
94 kill USR1, $tmux_helper;
98 # We were signalled by our child which noticed that our credentials expired.
99 sub want_credentials {
100 # Do we already know?
101 return sleep 1 if &check_kinit_child;
102 system "tmux", "new-window", "-n", "Renew Kerberos credentials", "exec kinit";
109 waitpid $pid, WNOHANG;