]> sourceware.org Git - systemtap.git/blob - stap-serverd
Merge branch 'master' of ssh://sources.redhat.com/git/systemtap
[systemtap.git] / stap-serverd
1 #!/bin/bash
2
3 # Compile server manager for systemtap
4 #
5 # Copyright (C) 2008 Red Hat Inc.
6 #
7 # This file is part of systemtap, and is free software. You can
8 # redistribute it and/or modify it under the terms of the GNU General
9 # Public License (GPL); either version 2, or (at your option) any
10 # later version.
11
12 # This script publishes its presence on the network and then listens for
13 # incoming connections. When a connection is detected, the stap-server script
14 # is run to handle the request.
15
16 # Catch ctrl-c and other termination signals
17 trap 'terminate' SIGTERM SIGINT
18
19 #-----------------------------------------------------------------------------
20 # Helper functions.
21 #-----------------------------------------------------------------------------
22 # function: initialization PORT
23 function initialization {
24 # Default settings.
25 avahi_type=_stap._tcp
26 port=$1
27 test "X$port" = "X" && port=65000
28 export port2=$(($port + 1))
29 if netstat -atn | awk '{print $4}' | cut -f2 -d: | egrep -q "^($port|$port2)\$"; then
30 # Whoops, the port is busy; try another one.
31 initialization $((1024+($port + $RANDOM)%64000))
32 fi
33 }
34
35 # function: advertise_presence
36 #
37 # Advertise the availability of the server on the network.
38 function advertise_presence {
39 # Build up a string representing our server's properties.
40 # TODO: this needs fleshing out.
41 local sysinfo=`uname -rvm`
42 local txt="sysinfo=$sysinfo"
43
44 # Call avahi-publish-service to advertise our presence.
45 avahi-publish-service "Systemtap Compile Server on `uname -n`" \
46 $avahi_type $port "$txt" > /dev/null 2>&1 &
47
48 echo "Systemtap Compile Server on `uname -n` listening on port $port"
49 }
50
51 # function: listen
52 #
53 # Listen for and handle requests to the server.
54 function listen {
55 # Loop forever accepting requests
56 while true
57 do
58 for ((attempt=0; $attempt < 5; ++attempt))
59 do
60 nc -ld $port 2>/dev/null | process_request &
61 wait '%nc -l'
62 rc=$?
63 if test $rc = 0 -o $rc = 127; then
64 break; # port was read ok
65 fi
66 done
67 if test $attempt = 5; then
68 fatal "ERROR: cannot listen on port $port. rc==$rc"
69 fi
70 done
71 }
72
73 # function: process_request
74 #
75 # Process an incoming request on stdin
76 function process_request {
77 read
78 case $REPLY in
79 request:)
80 stap-server $port2 >/dev/null 2>&1 &
81 wait '%stap-server'
82 rc=$?
83 test $rc = 127 && rc=0
84 ;;
85 *)
86 rc=1
87 esac
88
89 exit $rc
90 }
91
92 # function: fatal [ MESSAGE ]
93 #
94 # Fatal error
95 # Prints its arguments to stderr and exits
96 function fatal {
97 echo "$@" >&2
98 terminate
99 exit 1
100 }
101
102 # function: terminate
103 #
104 # Terminate gracefully.
105 function terminate {
106 echo "$0: Exiting"
107
108 # Kill the running 'avahi-publish-service' job
109 kill -s SIGTERM %avahi-publish-service 2> /dev/null
110 wait '%avahi-publish-service' >/dev/null 2>&1
111
112 # Kill any running 'stap-server' job.
113 kill -s SIGTERM "%stap-server" 2> /dev/null
114 wait "%stap-server" >/dev/null 2>&1
115
116 # Kill any running 'nc -l' job.
117 kill -s SIGTERM "%?nc -l" 2> /dev/null
118 wait "%?nc - l" >/dev/null 2>&1
119
120 exit
121 }
122
123 #-----------------------------------------------------------------------------
124 # Beginning of main line execution.
125 #-----------------------------------------------------------------------------
126 initialization "$@"
127 advertise_presence
128 listen
This page took 0.045594 seconds and 6 git commands to generate.