clearer names for actions, and infer actions better
[monitor.git] / histlog / histlog_snoopy_sh
1 #!/bin/bash
2
3 # NOTE: OMG - evidently 'read' can't be part of a pipeline if you want to export the variables
4 function child_of_sshd ()
5 {
6     unset pid2ppid
7     unset pid2cmd
8     while read xPID xPPID xO ; do 
9         
10         pid2ppid[$xPID]="$xPPID"
11         pid2cmd[$xPID]="$xO"
12         
13     done < <( ps ax -opid,ppid,cmd | grep -E "sshd|bash" )
14
15     pid=$$
16     while /bin/true ; do 
17         # is the working PID part of the pid2ppid mapping?
18         if echo ${!pid2ppid[@]} | grep -q $pid ; then
19             pid=${pid2ppid[$pid]}
20         else
21             break
22         fi
23         # is the working PID command sshd?
24         if echo "${pid2cmd[$pid]}" | grep -q 'sshd' ; then
25             # then success; we're a child or sshd
26             return 0
27         fi
28     done
29     
30     # we are not a child of sshd
31     return 1
32 }
33
34 if child_of_sshd ; then 
35     export LD_PRELOAD=/usr/lib/snoopy.so
36 fi
37
38 unset pid2ppid
39 unset pid2cmd