3 # ktmux_helper: Run krenew in the background for tmux.
4 # Usage: ktmux_helper [options]
5 # Options: -I <path> Specify path to kinit.
6 # -L <path> Specify path to klist.
7 # -R <path> Specify path to krenew.
8 # -T <path> Specify path to tmux.
9 # Notes: Doesn't handle multiple sessions properly.
14 use POSIX ":sys_wait_h";
16 my $PROG = $FindBin::Script;
18 # Ensure tmux is our parent and find its PID.
19 our $tmux_pid = &get_tmux_pid;
21 print STDERR "$PROG: Not a child of tmux!\n";
25 # Ensure there isn't already a helper running for this tmux.
26 my $tmux_helper = &get_tmux_helper;
27 exit 0 if $tmux_helper;
31 getopts('I:L:R:T:', \%opts);
33 my $kinit = $opts{'I'} || "kinit";
34 my $klist = $opts{'L'} || "klist";
35 my $krenew = $opts{'R'} || "krenew";
36 my $tmux = $opts{'T'} || "tmux";
41 $SIG{INT} = \&cleanup;
42 $SIG{QUIT} = \&cleanup;
43 $SIG{TERM} = \&cleanup;
44 $SIG{USR1} = \&want_credentials;
46 LOOP: while (&ping_tmux) {
48 die "$PROG: Can't fork: $!\n" unless defined $pid;
52 my $exited = waitpid $pid, WNOHANG;
53 goto LOOP if $exited == $pid || $exited < 0;
57 # tmux is dead so kill krenew.
63 exit 1 if &check_credentials;
64 exec $krenew, "-K", "60";
65 print "$PROG: Can't run krenew: $!\n";
72 my $cmd = `/bin/ps -o args= -p $pid`;
73 return $pid if $cmd =~ /\btmux\b/;
77 sub check_kinit_child {
78 foreach my $pid (`/bin/ps -o ppid= -C kinit`) {
79 return 1 if $pid =~ /^\s*$tmux_pid\s*$/;
86 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
100 return kill 0, $tmux_pid;
103 # Try to check existing Kerberos credentials.
104 sub check_credentials {
108 kill USR1, $tmux_helper;
112 # We were signalled by our child which noticed that our credentials expired.
113 sub want_credentials {
114 # Do we already know?
115 return sleep 1 if &check_kinit_child;
116 system $tmux, "new-window", "-n", "Renew Kerberos credentials", "exec $kinit";
123 waitpid $pid, WNOHANG;