+sub get_tmux_session {
+ # Set from command line?
+ my $session = shift;
+
+ # Set from environment?
+ unless ($session) {
+ # The session identifier will be -1 if we were launched from run-shell
+ # because a run-shell command doesn't count as being part of a session.
+ if ($ENV{TMUX} =~ /,(-?\d+)$/) {
+ $session = $1;
+ }
+ }
+
+ return $session;
+}
+
+# Check that a given process was launched from our session.
+sub check_tmux_session {
+ my $pid = shift;
+
+ return undef unless defined $tmux_session;
+
+ my $cmd;
+ if ($^O eq "linux") {
+ $cmd = "xargs -0 -n 1 < /proc/$pid/environ";
+ }
+ elsif ($^O eq "solaris") {
+ $cmd = "pargs -Fe $pid";
+ }
+ else {
+ # Don't know how to check on this OS.
+ return undef;
+ }
+
+ return undef unless (open IN, "$cmd | ");
+
+ while (<IN>) {
+ chomp;
+ next unless /TMUX=.+,(-?\d+)/;
+ # Abort if the running helper doesn't know what session it's for.
+ if ($1 eq $tmux_session || $1 eq "-1") {
+ close IN;
+ return 1;
+ }
+ }
+
+ close IN;
+ return 0;
+}
+
+sub check_kinit_child {
+ foreach my $pid (`/bin/ps -o ppid= -C kinit`) {
+ next unless $pid =~ /^\s*$tmux_pid\s*$/;
+ next if &check_tmux_session($pid);
+ return 1;
+ }
+ return 0;
+}
+