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 # Notes: Doesn't handle multiple sessions properly.
13 use POSIX ":sys_wait_h";
15 my $PROG = $FindBin::Script;
17 # Ensure tmux is our parent and find its PID.
18 our $tmux_pid = &get_tmux_pid;
20 print STDERR "$PROG: Not a child of tmux!\n";
24 # Ensure there isn't already a helper running for this tmux.
25 my $tmux_helper = &get_tmux_helper;
26 exit 0 if $tmux_helper;
30 getopts('I:L:R:', \%opts);
32 my $kinit = $opts{'I'} || "kinit";
33 my $klist = $opts{'L'} || "klist";
34 my $krenew = $opts{'R'} || "krenew";
39 $SIG{INT} = \&cleanup;
40 $SIG{QUIT} = \&cleanup;
41 $SIG{TERM} = \&cleanup;
42 $SIG{USR1} = \&want_credentials;
44 LOOP: while (&ping_tmux) {
46 die "$PROG: Can't fork: $!\n" unless defined $pid;
50 my $exited = waitpid $pid, WNOHANG;
51 goto LOOP if $exited == $pid || $exited < 0;
55 # tmux is dead so kill krenew.
61 exit 1 if &check_credentials;
62 exec $krenew, "-K", "60";
63 print "$PROG: Can't run krenew: $!\n";
70 my $cmd = `/bin/ps -o args= -p $pid`;
71 return $pid if $cmd =~ /\btmux\b/;
75 sub check_kinit_child {
76 foreach my $pid (`/bin/ps -o ppid= -C kinit`) {
77 return 1 if $pid =~ /^\s*$tmux_pid\s*$/;
84 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
98 return kill 0, $tmux_pid;
101 # Try to check existing Kerberos credentials.
102 sub check_credentials {
106 kill USR1, $tmux_helper;
110 # We were signalled by our child which noticed that our credentials expired.
111 sub want_credentials {
112 # Do we already know?
113 return sleep 1 if &check_kinit_child;
114 system "tmux", "new-window", "-n", "Renew Kerberos credentials", "exec $kinit";
121 waitpid $pid, WNOHANG;