X-Git-Url: http://git.iain.cx/?p=profile.git;a=blobdiff_plain;f=opt%2Fbin%2Fktmux_helper;h=fe942d4869d5c2537f9a9f1b7a08a42f3f4549d6;hp=1ed6e4673218a2cf8fe31776ddfb096df5a037aa;hb=5238da68cdb1f51a416564a0faeb909f25d9b168;hpb=d148b07acb5fb8998ec1e76da8a2f01ee0e74328 diff --git a/opt/bin/ktmux_helper b/opt/bin/ktmux_helper index 1ed6e46..fe942d4 100755 --- a/opt/bin/ktmux_helper +++ b/opt/bin/ktmux_helper @@ -1,16 +1,21 @@ #!/usr/bin/perl # # ktmux_helper: Run krenew in the background for tmux. +# Usage: ktmux_helper [options] +# Options: -I Specify path to kinit. +# -L Specify path to klist. +# -R Specify path to krenew. # Notes: Doesn't handle multiple sessions properly. # use FindBin; +use Getopt::Std; use POSIX ":sys_wait_h"; my $PROG = $FindBin::Script; # Ensure tmux is our parent and find its PID. -my $tmux_pid = &get_tmux_pid; +our $tmux_pid = &get_tmux_pid; unless ($tmux_pid) { print STDERR "$PROG: Not a child of tmux!\n"; exit 100; @@ -21,15 +26,20 @@ my $tmux_helper = &get_tmux_helper; exit 0 if $tmux_helper; $tmux_helper = $$; +my %opts; +getopts('I:L:R:', \%opts); + +my $kinit = $opts{'I'} || "kinit"; +my $klist = $opts{'L'} || "klist"; +my $krenew = $opts{'R'} || "krenew"; + my $exitasap = 0; my $pid = 0; -our $have_valid_credentials = undef; $SIG{INT} = \&cleanup; $SIG{QUIT} = \&cleanup; $SIG{TERM} = \&cleanup; $SIG{USR1} = \&want_credentials; -$SIG{USR2} = \&got_credentials; LOOP: while (&ping_tmux) { $pid = fork; @@ -48,8 +58,8 @@ LOOP: while (&ping_tmux) { exit 0; } else { - kill USR2, $tmux_helper unless &check_credentials; - exec "krenew", "-K", "60"; + exit 1 if &check_credentials; + exec $krenew, "-K", "60"; print "$PROG: Can't run krenew: $!\n"; exit 111; } @@ -62,6 +72,13 @@ sub get_tmux_pid { return undef; } +sub check_kinit_child { + foreach my $pid (`/bin/ps -o ppid= -C kinit`) { + return 1 if $pid =~ /^\s*$tmux_pid\s*$/; + } + return 0; +} + sub get_tmux_helper { my $pid = undef; if (open IN, "pgrep -x -P $tmux_pid $PROG | ") { @@ -83,7 +100,7 @@ sub ping_tmux { # Try to check existing Kerberos credentials. sub check_credentials { - system "klist", "-s"; + system $klist, "-s"; return 1 if $? < 0; return 0 unless $?; kill USR1, $tmux_helper; @@ -93,16 +110,8 @@ sub check_credentials { # We were signalled by our child which noticed that our credentials expired. sub want_credentials { # Do we already know? - return if defined $have_valid_credentials && $have_valid_credentials == 0; - $SIG{USR1} = IGNORE; - $have_valid_credentials = 0; - system "tmux", "new-window", "exec kinit"; -} - -# We were signalled by our child which noticed that our credentials are valid. -sub got_credentials { - $have_valid_credentials = 1; - $SIG{USR1} = \&want_credentials; + return sleep 1 if &check_kinit_child; + system "tmux", "new-window", "-n", "Renew Kerberos credentials", "exec $kinit"; } sub cleanup {