04684bd8ce0482031b8a1330ea7116451b341c42
[anna.git] / example / diameter / launcher / resources / scripts / operation_signal.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 TIMEOUT__dflt=3
7 SCR_BN=`basename $0`
8
9 #############
10 # FUNCTIONS #
11 #############
12 _exit() {
13   echo -e "\n$1\n"
14   exit 1
15 }
16
17 usage() {
18   echo
19   echo "Usage: $0 [-h|--help] [-t|--timeout <value>] [-f|--file] [-p|--ping] <data>"
20   echo
21   echo "       -h|--help:     this usage help."
22   echo "       -t|--timeout:  timeout for operation in seconds."
23   echo "                      Defaults to $TIMEOUT__dflt seconds if not provided."
24   echo
25   echo "       -f|--file:     the parameter 'data' will be interpreted as a file"
26   echo "                      with one operation per line. If missing, it will be"
27   echo "                      a single operation string."
28   echo
29   echo "       -p|--ping:     Check the target process id."
30   echo "                      Returns 1 (dead) or 0 (alive)."
31   echo
32   echo "       data:          operation string or file with several operations."
33   echo
34   echo "       For example:"
35   echo "          $0 help"
36   echo "          $0 \"test|ttps|50\""
37   echo "          $0 --file myOperationsList.txt"
38   echo "          $0 --timeout 10 --file ./bigList.txt"
39   _exit
40 }
41
42 parse_arguments() {
43   is_file=
44   timeout=$TIMEOUT__dflt
45   data=
46   ping=
47
48   while [ $# -gt 0 ]; do
49     case $1 in
50       -h|--help)
51         usage
52       ;;
53
54       -t|--timeout)
55         timeout=$2
56         [ -z "$timeout" ] && _exit "Missing timeout value"
57         shift
58       ;;
59
60       -f|--file)
61         is_file=yes
62         data=$2
63         [ -z "$data" ] && _exit "Missing file"
64         [ ! -f "$data" ] && _exit "Can't found provided file '$data'."
65         shift
66       ;;
67
68       -p|--ping)
69         ping=yes
70       ;;
71
72       *)
73         first=$(echo $1 | cut -c1)
74         [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options."
75         data=$1
76       ;;
77     esac
78     shift
79   done
80
81   [ -z "$ping" -a -z "$data" ] && _exit "Missing data value"
82 }
83
84 # $1: pid to check
85 check_pid() {
86   kill -0 $1 2>/dev/null
87   return $?
88 }
89
90 #############
91 # EXECUTION #
92 #############
93 cd `dirname $0`
94 # Get the PID:
95 [ ! -f .pid ] && _exit "Can't found '`pwd`/.pid'.\nTry to pgrep your process name and dump pid to that file."
96 PID=`cat .pid`
97
98 # Arguments:
99 [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage
100 parse_arguments $@
101
102 # Check pid:
103 check_pid $PID
104 res=$?
105 [ -n "$ping" ] && exit $res
106 [ $res -ne 0 ] && _exit "Operation error: missing process with pid $PID"
107
108 # Send operation:
109 if [ -n "$is_file" ]
110 then
111   cp $data sigusr2.in
112 else
113   echo $data > sigusr2.in
114 fi
115 0>sigusr2.out
116 check_pid $PID
117 kill -s SIGUSR2 $PID
118
119 # Detect EOF and print all except that last line:
120 count=$((10*timeout))                                                      
121 expired=yes
122 while [ $count -gt 0 ]                                           
123 do                                                                               
124   sleep 0.1                                                                      
125   count=$((count-1))                                                             
126   if tail -1 sigusr2.out | grep "^EOF" >/dev/null; then
127     expired=
128     break;
129   fi
130 done                                                                             
131
132 if [ -z "$expired" ]
133 then
134   head --lines=-1 sigusr2.out
135 else
136   _exit "Operation error: timeout expired ($timeout seconds)"
137 fi
138
139 exception=$(grep "^Operation processed with exception: " sigusr2.out)
140 [ $? -eq 0 ] && _exit "(detected 'exception' within operation output: see 'launcher.trace')"
141 exit 0
142