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";
43 $SIG{INT} = \&cleanup;
44 $SIG{QUIT} = \&cleanup;
45 $SIG{TERM} = \&cleanup;
46 $SIG{USR1} = \&want_credentials;
48 LOOP: while (&ping_tmux) {
50 die "$PROG: Can't fork: $!\n" unless defined $pid;
54 my $exited = waitpid $pid, WNOHANG;
55 goto LOOP if $exited == $pid || $exited < 0;
59 # tmux is dead so kill krenew.
65 exit 1 if &check_credentials;
66 exec $krenew, "-K", "60";
67 print "$PROG: Can't run krenew: $!\n";
74 my $cmd = `/bin/ps -o args= -p $pid`;
75 return $pid if $cmd =~ /\btmux\b/;
79 sub check_kinit_child {
80 foreach my $pid (`/bin/ps -o ppid= -C kinit`) {
81 return 1 if $pid =~ /^\s*$tmux_pid\s*$/;
88 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
102 return kill 0, $tmux_pid;
105 # Try to check existing Kerberos credentials.
106 sub check_credentials {
110 kill USR1, $tmux_helper;
114 # We were signalled by our child which noticed that our credentials expired.
115 sub want_credentials {
116 return sleep 1 if $avoid_race;
118 # Do we already know?
119 system $tmux, "new-window", "-n", "Renew Kerberos credentials", "exec $kinit" unless &check_kinit_child;
128 waitpid $pid, WNOHANG;