da0449b91f7cb72ba79ce6611c79f31b3009e723
[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] <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 "       data:          operation string or file with several operations."
30   echo
31   echo "       For example:"
32   echo "          $0 help"
33   echo "          $0 \"test|ttps|50\""
34   echo "          $0 --file myOperationsList.txt"
35   echo "          $0 --timeout 10 --file ./bigList.txt"
36   _exit
37 }
38
39 parse_arguments() {
40   is_file=
41   timeout=$TIMEOUT__dflt
42   data=
43
44   while [ $# -gt 0 ]; do
45     case $1 in
46       -h|--help)
47         usage
48       ;;
49
50       -t|--timeout)
51         timeout=$2
52         [ -z "$timeout" ] && _exit "Missing timeout value"
53         shift
54       ;;
55
56       -f|--file)
57         is_file=yes
58         data=$2
59         [ -z "$data" ] && _exit "Missing file"
60         [ ! -f "$data" ] && _exit "Can't found provided file '$data'."
61         shift
62       ;;
63
64       *)
65         first=$(echo $1 | cut -c1)
66         [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options."
67         data=$1
68       ;;
69     esac
70     shift
71   done
72
73   [ -z "$data" ] && _exit "Missing data value"
74 }
75
76 # $1: pid to check
77 check_pid() {
78   kill -0 $1 2>/dev/null
79   [ $? -ne 0 ] && _exit "Operation error: missing process with pid $1"
80 }
81
82 #############
83 # EXECUTION #
84 #############
85 cd `dirname $0`
86 # Get the PID:
87 [ ! -f .pid ] && _exit "Can't found '`pwd`/.pid'.\nTry to pgrep your process name and dump pid to that file."
88 PID=`cat .pid`
89
90 # Arguments:
91 [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage
92 parse_arguments $@
93
94 # Send operation:
95 if [ -n "$is_file" ]
96 then
97   cp $data sigusr2.in
98 else
99   echo $data > sigusr2.in
100 fi
101 0>sigusr2.out
102 check_pid $PID
103 kill -s SIGUSR2 $PID
104
105 # Detect EOF and print all except that last line:
106 count=$((10*timeout))                                                      
107 expired=yes
108 while [ $count -gt 0 ]                                           
109 do                                                                               
110   sleep 0.1                                                                      
111   count=$((count-1))                                                             
112   if tail -1 sigusr2.out | grep "^EOF" >/dev/null; then
113     expired=
114     break;
115   fi
116 done                                                                             
117
118 if [ -z "$expired" ]
119 then
120   head --lines=-1 sigusr2.out
121 else
122   _exit "Operation error: timeout expired ($timeout seconds)"
123 fi
124
125 exception=$(grep "^Operation processed with exception: " sigusr2.out)
126 [ $? -eq 0 ] && _exit "(detected 'exception' within operation output: see 'launcher.trace')"
127 exit 0
128