]> sourceware.org Git - systemtap.git/blame - stap-server
Give information about online servers and which servers were specified when no suitab...
[systemtap.git] / stap-server
CommitLineData
ab19dac7 1#!/bin/bash
60045090
DB
2#
3# stap-server script for managing the systemtap compile server
ab19dac7 4#
5ac7fee9 5# Copyright (C) 2008-2011 Red Hat Inc.
ab19dac7
DB
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.
60045090
DB
11#
12# This script provides management of systemtap compile servers as a service.
13# See stap-server(8) for more information.
ab19dac7 14
5ac7fee9
DB
15if [ -f /etc/rc.d/init.d/functions ]; then
16 # Red Hat init functions
17 . /etc/rc.d/init.d/functions
18else
19 # Default init functions
20 success () {
21 echo -n "OK"
22 }
23 failure () {
24 echo -n "FAILED"
25 }
26fi
0e47827d 27
60045090 28# Systemtap function library
961588fd 29. ${PKGLIBEXECDIR}stap-env
98f552c2 30
60045090
DB
31prog=stap-server
32
33# Commands
34STAP_START_SERVER=${stap_pkglibexecdir}stap-start-server
35STAP_STOP_SERVER=${stap_pkglibexecdir}stap-stop-server
36UNAME=/bin/uname
37
38# Default Global Configuration
39CONFIG_FILE=$stap_sysconfdir/sysconfig/stap-server
40CONFIG_PATH=$stap_sysconfdir/stap-server/conf.d
41STAT_PATH=$stap_localstatedir/run/stap-server
2648a9b4 42LOG_FILE=$stap_localstatedir/log/stap-server/log
60045090
DB
43
44# Default option settings
45# Optional global config file
46OPT_CONFIG_FILE=
71a522b5 47OPT_OTHER=
60045090 48
e7148436
DB
49OPT_PORT_IX=0
50OPT_LOG_IX=0
51OPT_SSL_IX=0
52
60045090
DB
53echo_usage () {
54 echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|status} [options]"
55 echo $"Options:"
56 echo $" -c configfile : specify additional global configuration file."
57 echo $" -a arch : specify the target architecture."
58 echo $" -r release : specify the kernel release."
59 echo $" -I path : augment the search path for tapsets."
60 echo $" -R path : specify the location of the systemtap runtime."
61 echo $" -B options : specify 'make' options for building systemtap modules."
071de8a6 62 echo $" -D name[=value] : add a macro definition for building systemtap modules."
60045090
DB
63 echo $" -u username : specify the user who will run the server(s)."
64 echo $" -i : specify a server for each installed kernel release."
65 echo $" -n nickname : specify a server configuration by nickname."
66 echo $" -p pid : specify a server or server configuration by process id."
e7148436 67 echo $" -P : use a password for the server's NSS certificate database."
71a522b5 68 echo $" -k : keep server temporary files."
e7148436
DB
69 echo $" --port port : specify the network port to be used by the server."
70 echo $" --log path : specify the location of the server's log file."
71 echo $" --ssl path : specify the location of the server's certificate database."
60045090 72 echo $""
e7148436 73 echo $"All options may be specified more than once."
60045090
DB
74 echo $""
75 echo $"If -a is not specified, the default architecture is that of the host"
76 echo $"platform."
77 echo $""
78 echo $"If -r is not specified, the default kernel release is that currently"
79 echo $"running on the host platform."
80 echo $""
81 echo $"If -u is not specified, the default user is 'stap-server'"
82 echo $""
e7148436
DB
83 echo $"If --port is not specified, the default is to use a randomly selected port."
84 echo $""
85 echo $"If --log is not specified, the default is '$stap_localstatedir/log/stap-server/log'."
86 echo $""
87 echo $"If --ssl is not specified, the default is '$stap_sysconfdir/ssl/server'."
88 echo $""
071de8a6
DB
89 echo $"Each -D, -I and -B option specifies an additional macro, path or option respectively"
90 echo $"to be applied to subsequent servers specified."
e7148436
DB
91 echo $""
92 echo $"Each --port, --log and --ssl option is added to an option-specific list which will"
93 echo $"be applied, in turn, to each server specified. If more servers are specified than"
94 echo $"options in a given list, the default for that option will be used for subsequent"
95 echo $"servers."
96 echo $""
97 echo $"For other options, each new instance overrides the previous setting."
60045090
DB
98 echo $""
99 echo $"The -i option is a shortcut which specifies one server for each kernel"
071de8a6 100 echo $"release installed in /lib/modules/. Previous -D, -I, -R, -B and -u"
60045090
DB
101 echo $"options will be applied to each server, however previous -a options"
102 echo $"are ignored and the default architecture is used."
103 echo $""
104 echo $"The -n option allows the specification of a server configuration by"
105 echo $"nickname. When -n is specified, a currently running server with the"
106 echo $"given nickname will be searched for. If no currently running server"
107 echo $"with the given nickname is found, a server configuration with the"
71a522b5
DB
108 echo $"given nickname will be searched for in"
109 echo $"$stap_sysconfdir/stap-server/conf.d/*.conf."
60045090 110 echo $"If a server configuration for the given nickname is found, the -a, -r,"
071de8a6 111 echo $"-D, -I, -R, -B and -u options for that server will be used as if they were"
60045090
DB
112 echo $"specified on the command line. If no configuration with the given"
113 echo $"nickname is found, and the action is 'start' (or an action behaving"
e7148436 114 echo $"like 'start' (see below)), the server will be started with the given"
60045090
DB
115 echo $"nickname. If no configuration with the given nickname is found, and"
116 echo $"the action is not 'start' (or an action behaving" "like 'start',"
117 echo $"it is an error. If a nickname is not specified for a server, its"
118 echo $"nickname will be its process id."
119 echo $""
120 echo $"The -p option allows the specification of a server configuration by"
121 echo $"process id. When -p is specified, a currently running server with the"
122 echo $"given process id will be searched for. If no such server is found,"
123 echo $"it is an error. If a server with the given pid is found, the -a, -r,"
071de8a6 124 echo $"-D, -I, -R, -B and -u options for that server will be used as if they were"
60045090
DB
125 echo $"specified on the command line."
126 echo $""
71a522b5
DB
127 echo $"The -k option tells the server to keep the temporary directories it creates"
128 echo $"during each transaction with a client."
129 echo $""
60045090
DB
130 echo $"The specified action is performed for the server(s) specified on the"
131 echo $"command line. If no servers are specified on the command line, the"
132 echo $"behavior is as follows:"
133 echo $""
134 echo $" start: Start the servers configured in $stap_sysconfdir/stap-server/conf.d/*.conf."
135 echo $" If none are configured, start a server for the kernel release"
136 echo $" and architecture of the host platform."
137 echo $""
138 echo $" stop: Stop all currently running servers."
139 echo $""
140 echo $" restart: Restart all currently running servers. If no servers are running,"
141 echo $" behave as if 'start' was specified."
142 echo $""
143 echo $" condrestart: Restart all currently running servers. If no servers are running,"
144 echo $" do nothing."
145 echo $""
146 echo $" try-restart: Same as condrestart."
147 echo $""
148 echo $" force-reload: Stop all currently running servers and behave as if 'start'"
149 echo $" was specified."
150 echo $""
151 echo $" status: Report the status of all current running servers."
152 echo $""
153}
ab19dac7 154
60045090
DB
155#-----------------------------------------------------------------
156# Helper functions
157#-----------------------------------------------------------------
158log () { # message
159 echo `LC_ALL=en date +"%b %e %T"`": $1" >> "$LOG_FILE"
160}
161clog () { # message [-n]
162 echo $2 "$1"
163 log "$1"
164}
165slog () { # message
166 logger "$1" # if syslogd is running, this message will be sent to syslog.
167 log "$1"
168}
169logex () { # command
c807ba1b 170 eval log \"Exec: "$@"\"
60045090
DB
171 "$@" >> "$LOG_FILE" 2>&1
172 return $?
173}
174do_failure () { # message
175 slog "Error: $1"
176 failure "$1"
177}
178do_success () { # message
179 log "Pass: $1"
180 success "$1"
181}
66e1a139 182
60045090
DB
183#------------------------------------------------------------------
184# Parameter parsing and setup options
185#------------------------------------------------------------------
186parse_args () { # arguments
187 local rc=0
e7148436 188 i_specified=0
60045090
DB
189 while [ -n "$1" ]; do
190 case "$1" in
71a522b5
DB
191 -k | -P)
192 OPT_OTHER="$OPT_OTHER $1"
193 shift 1
194 ;;
60045090 195 -a)
e7148436 196 process_i
c807ba1b 197 SERVER_CMDS+=("ARCH=\"`quote_for_cmd "$2"`\"")
60045090
DB
198 shift 1
199 ;;
200 -B)
e7148436 201 process_i
c807ba1b 202 SERVER_CMDS+=("BUILD+=(\"`quote_for_cmd "$2"`\")")
60045090
DB
203 shift 1
204 ;;
205 -c)
c807ba1b 206 OPT_CONFIG_FILE="$2"
60045090
DB
207 shift 1
208 ;;
209 -i)
e7148436
DB
210 # Delay processing of -i in case a distributed option like --log is specified which could
211 # apply to each server referenced by -i. We will process it just before handling other
212 # options which describe a new server (e.g. -r).
213 i_specified=$(($i_specified + 1))
60045090 214 ;;
071de8a6
DB
215 -D)
216 process_i
217 SERVER_CMDS+=("DEFINE+=(\"`quote_for_cmd "$2"`\")")
218 shift 1
219 ;;
60045090 220 -I)
e7148436 221 process_i
c807ba1b 222 SERVER_CMDS+=("INCLUDE+=(\"`quote_for_cmd "$2"`\")")
60045090
DB
223 shift 1
224 ;;
225 -n)
e7148436 226 process_i
c807ba1b 227 process_n "$2"
60045090
DB
228 shift 1
229 ;;
230 -p)
e7148436 231 process_i
c807ba1b 232 process_p "$2"
60045090
DB
233 test $? = 0 || rc=1
234 shift 1
235 ;;
236 -r)
e7148436 237 process_i
c807ba1b 238 process_r "$2"
60045090
DB
239 test $? = 0 || rc=1
240 shift 1
241 ;;
242 -R)
e7148436 243 process_i
c807ba1b 244 SERVER_CMDS+=("RUNTIME=\"`quote_for_cmd "$2"`\"")
60045090
DB
245 shift 1
246 ;;
247 -u)
e7148436 248 process_i
c807ba1b 249 SERVER_CMDS+=("USER=\"`quote_for_cmd "$2"`\"")
60045090
DB
250 shift 1
251 ;;
e7148436
DB
252 --log)
253 OPT_LOG+=("$2")
254 shift 1
255 ;;
256 --port)
257 OPT_PORT+=("$2")
258 shift 1
259 ;;
260 --ssl)
261 OPT_SSL+=("$2")
262 shift 1
263 ;;
60045090
DB
264 --)
265 ;;
266 *)
267 rc=1
268 ;;
269 esac
270 shift 1
271 done
272
e7148436
DB
273 # Process any lingering -i
274 process_i
275
276 # Add options from any lists and an EXEC command to the end if any server options were specified
277 if test -n "$SERVER_CMDS"; then
278 add_distributed_options
279 SERVER_CMDS+=("EXEC")
280 fi
60045090
DB
281
282 test $rc != 0 && echo_usage
283 return $rc
284}
66e1a139 285
e7148436
DB
286# Some options accumulate into lists when specified more than once. The items in the each list
287# are the applied to each specified server in turn. If a list runs out, then the default for that
288# option is applied.
289add_distributed_options () {
290 # The --log option
291 if test -n "${OPT_LOG[$OPT_LOG_IX]}"; then
292 SERVER_CMDS+=("LOG=\"`quote_for_cmd "${OPT_LOG[$OPT_LOG_IX]}"`\"")
293 OPT_LOG_IX=$(($OPT_LOG_IX + 1))
294 else
295 SERVER_CMDS+=("LOG=\"`quote_for_cmd "$LOG_FILE"`\"")
296 fi
297 # The --port option
298 if test -n "${OPT_PORT[$OPT_PORT_IX]}"; then
299 SERVER_CMDS+=("PORT=\"`quote_for_cmd "${OPT_PORT[$OPT_PORT_IX]}"`\"")
300 OPT_PORT_IX=$(($OPT_PORT_IX + 1))
301 else
302 SERVER_CMDS+=("PORT=\"\"")
303 fi
304 # The --ssl option
305 if test -n "${OPT_SSL[$OPT_SSL_IX]}"; then
306 SERVER_CMDS+=("SSL=\"`quote_for_cmd "${OPT_SSL[$OPT_SSL_IX]}"`\"")
307 OPT_SSL_IX=$(($OPT_SSL_IX + 1))
308 else
309 SERVER_CMDS+=("SSL=\"\"")
310 fi
311}
312
60045090
DB
313# Process the -i flag.
314process_i () {
315 cd /lib/modules
316 local release
e7148436
DB
317 while test $i_specified -gt 0; do
318 for release in `ls`; do
319 if test -n "$SERVER_CMDS"; then
320 add_distributed_options
321 SERVER_CMDS+=("EXEC")
322 fi
323 process_r $release
324 done
325 i_specified=$(($i_specified - 1))
60045090 326 done
66e1a139 327
60045090 328 return 0
ab19dac7
DB
329}
330
60045090
DB
331# Process the -n flag.
332process_n () {
333 local target_NICKNAME="$1"
334
335 # Is there a running server with this nickname?
336 local pid=`get_server_pid_by_nickname "$target_NICKNAME"`
337 if [ -n "$pid" ]; then
338 # Read the configuration and add it to the configuration commands.
c807ba1b
DB
339 interpret_server_status "$STAT_PATH/$pid.stat" || continue
340 add_server_commands
60045090
DB
341 return
342 fi
343
344 # Is there a server configuration with this nickname?
345 for f in "$CONFIG_PATH"/*.conf; do
346 if [ -f "$f" ]; then
c807ba1b 347 interpret_server_config "$f" || continue
60045090 348 test "X$NICKNAME" = "X$target_NICKNAME" || continue
c807ba1b 349 add_server_commands
60045090
DB
350 return
351 fi
352 done
ab19dac7 353
60045090
DB
354 # No server configuration could be found for this nickname. Add a
355 # NICKNAME_NOT_FOUND=... command to the configuration commands.
c807ba1b 356 SERVER_CMDS+=("NICKNAME_NOT_FOUND=\"`quote_for_cmd "$target_NICKNAME"`\"")
60045090
DB
357}
358
359# Process the -p flag.
360process_p () {
361 local pid="$1"
9e494cbb 362
60045090 363 # Are we managing a server with the given pid?
c807ba1b 364 test ! -f "$STAT_PATH/$pid.stat" && echo "No stap-server running as pid $pid" && \
60045090 365 exit 1
9e494cbb 366
c807ba1b
DB
367 # Add the configuration of the server running as $pid to SERVER_CMDS
368 interpret_server_status "$STAT_PATH/$pid.stat" || exit 1
369 add_server_commands
60045090 370 return 0
66e1a139 371}
ab19dac7 372
60045090
DB
373# Process the -r flag.
374process_r () {
c807ba1b 375 local first_char="${1:1:1}"
60045090
DB
376
377 if test "$first_char" = "/"; then # fully specified path
c807ba1b 378 local kernel_build_tree="$1"
60045090
DB
379 local version_file_name="$kernel_build_tree/include/config/kernel.release"
380 # The file include/config/kernel.release within the kernel
381 # build tree is used to pull out the version information
382 local kernel_release=`cat $version_file_name 2>/dev/null`
383 if test "X$kernel_release" = "X"; then
384 echo "Missing $version_file_name"
385 return 1
386 fi
4f747a64 387 SERVER_CMDS+=("RELEASE=\"`quote_for_cmd "$1"`\"") # pass the path as-is
60045090 388 return 0
66e1a139 389 fi
ab19dac7 390
60045090 391 # kernel release specified directly
c807ba1b 392 SERVER_CMDS+=("RELEASE=\"`quote_for_cmd "$1"`\"")
60045090 393 return 0
ab19dac7
DB
394}
395
60045090
DB
396load_config () {
397 # Include configs
398 if [ -f "$CONFIG_FILE" ]; then
c807ba1b 399 interpret_config_file "$CONFIG_FILE"
60045090
DB
400 fi
401 if [ -f "$OPT_CONFIG_FILE" ]; then
c807ba1b 402 interpret_config_file "$OPT_CONFIG_FILE"
60045090
DB
403 fi
404}
d5658775 405
60045090
DB
406# Default to the currently running kernel release
407get_release () {
408 $UNAME -r
409}
d5658775 410
60045090
DB
411# Default to the currently running kernel release
412get_arch () {
413 stap_get_arch
ab19dac7
DB
414}
415
c807ba1b
DB
416add_server_commands () {
417 # Add commands based on the current state to SERVER_CMDS
418 SERVER_CMDS+=("ARCH=\"`quote_for_cmd "$ARCH"`\"")
419 SERVER_CMDS+=("RELEASE=\"`quote_for_cmd "$RELEASE"`\"")
420 SERVER_CMDS+=("RUNTIME=\"`quote_for_cmd "$RUNTIME"`\"")
071de8a6
DB
421 SERVER_CMDS+=("DEFINE=()")
422 for d in "${DEFINE[@]}"; do
423 SERVER_CMDS+=("DEFINE+=(\"`quote_for_cmd "$d"`\")")
424 done
c807ba1b
DB
425 SERVER_CMDS+=("INCLUDE=()")
426 for i in "${INCLUDE[@]}"; do
427 SERVER_CMDS+=("INCLUDE+=(\"`quote_for_cmd "$i"`\")")
60045090 428 done
c807ba1b
DB
429 SERVER_CMDS+=("BUILD=()")
430 for b in "${BUILD[@]}"; do
431 SERVER_CMDS+=("BUILD+=(\"`quote_for_cmd "$b"`\")")
60045090 432 done
c807ba1b
DB
433 SERVER_CMDS+=("USER=\"`quote_for_cmd "$USER"`\"")
434 SERVER_CMDS+=("NICKNAME=\"`quote_for_cmd "$NICKNAME"`\"")
e7148436
DB
435 SERVER_CMDS+=("LOG=\"`quote_for_cmd "$LOG"`\"")
436 test -n "$PORT" && SERVER_CMDS+=("PORT=\"`quote_for_cmd "$PORT"`\"")
437 test -n "$SSL" && SERVER_CMDS+=("SSL=\"`quote_for_cmd "$SSL"`\"")
60045090 438}
8afee8bb 439
60045090 440echo_server_options () {
c807ba1b
DB
441 # Echo the current state.
442 echo -n "-a \"`quote_for_cmd "$ARCH"`\""
443 echo -n " -r \"`quote_for_cmd "$RELEASE"`\""
444 test -n "$RUNTIME" && echo -n " -R \"`quote_for_cmd "$RUNTIME"`\""
071de8a6
DB
445 for d in "${DEFINE[@]}"; do
446 echo -n " -D \"`quote_for_cmd "$d"`\""
447 done
c807ba1b
DB
448 for i in "${INCLUDE[@]}"; do
449 echo -n " -I \"`quote_for_cmd "$i"`\""
60045090 450 done
c807ba1b
DB
451 for b in "${BUILD[@]}"; do
452 echo -n " -B \"`quote_for_cmd "$b"`\""
ab19dac7 453 done
56b3f5cf 454 echo -n " -u \"`quote_for_cmd "$USER"`\""
d0d71073 455 test -n "$NICKNAME" && echo -n " -n \"`quote_for_cmd "$NICKNAME"`\""
e7148436
DB
456 echo -n " --log \"`quote_for_cmd "$LOG"`\""
457 test -n "$PORT" && echo -n " --port \"`quote_for_cmd "$PORT"`\""
458 test -n "$SSL" && echo -n " --ssl \"`quote_for_cmd "$SSL"`\""
60045090 459 echo
ab19dac7
DB
460}
461
60045090
DB
462prepare_stat_dir () {
463 if [ ! -d "$STAT_PATH" ]; then
464 logex mkdir -p "$STAT_PATH"
465 [ $? -ne 0 ] && return 1
466 fi
467 return 0
ab19dac7
DB
468}
469
60045090 470prepare_log_dir () {
e7148436 471 local log_path=`dirname "$1"`
60045090
DB
472 if [ ! -d "$log_path" ]; then
473 mkdir -p "$log_path"
474 [ $? -ne 0 ] && return 1
475 fi
476 return 0
ab19dac7
DB
477}
478
60045090
DB
479init_server_opts () {
480 ARCH=`get_arch`
481 RELEASE=`get_release`
071de8a6 482 unset DEFINE
c807ba1b
DB
483 unset BUILD
484 unset INCLUDE
60045090
DB
485 NICKNAME=
486 NICKNAME_NOT_FOUND=
487 RUNTIME=
488 USER=$STAP_USER
56b3f5cf 489 test -z "$USER" && USER=`id -un`
e7148436
DB
490 LOG=$LOG_FILE
491 PORT=
492 SSL=
ab19dac7
DB
493}
494
c807ba1b
DB
495# Double quotes, backslashes within generated command
496# arguments must be quoted.
497quote_for_cmd () {
498 echo "$1" | \
499 sed -e 's/\\/\\\\/g' \
500 -e 's/"/\\"/g'
501}
502
503# Interpret the contents of a global config file.
504interpret_config_file () {
505 local config_file="$1"
506
507 # Save the results locally first, in case there is an error.
508 local local_CONFIG_PATH=
509 local local_STAT_PATH=
510 local local_LOG_FILE=
511 local local_STAP_USER=
512
513 local input
514 while read -r -u3 input
515 do
516 case "$input" in
517 CONFIG_PATH=*)
518 local_CONFIG_PATH="${input:12}"
519 ;;
520 STAT_PATH=*)
521 local_STAT_PATH="${input:10}"
522 ;;
523 LOG_FILE=*)
524 local_LOG_FILE="${input:9}"
525 ;;
526 STAP_USER=*)
527 local_STAP_USER="${input:10}"
528 ;;
529 \#*)
530 ;; # Comment, do nothing
531 "")
532 ;; # Empty line, do nothing
533 *)
534 echo $"Invalid input, \"$input\", in $config_file" >&2
535 exit 1
536 ;;
537 esac
538 done 3< "$config_file"
539
540 # Now set the results globally
541 test -n "$local_CONFIG_PATH" && CONFIG_PATH="$local_CONFIG_PATH"
542 test -n "$local_STAT_PATH" && STAT_PATH="$local_STAT_PATH"
543 test -n "$local_LOG_FILE" && LOG_FILE="$local_LOG_FILE"
544 test -n "$local_STAP_USER" && STAP_USER="$local_STAP_USER"
545}
546
547# Interpret the contents of a server config file.
548interpret_server_config () {
549 local config_file="$1"
550
551 # Save the results locally first, in case there is an error.
552 local local_ARCH=
553 local local_RELEASE=
071de8a6 554 local local_DEFINE=()
c807ba1b
DB
555 local local_BUILD=()
556 local local_INCLUDE=()
557 local local_RUNTIME=
558 local local_USER=
559 local local_NICKNAME=
e7148436
DB
560 local local_LOG=
561 local local_PORT=
562 local local_SSL=
c807ba1b
DB
563
564 local input
565 while read -r -u3 input
566 do
567 case "$input" in
568 ARCH=*)
569 local_ARCH="${input:5}"
570 ;;
571 RELEASE=*)
572 local_RELEASE="${input:8}"
573 ;;
071de8a6
DB
574 DEFINE=*)
575 if [ -z "${input:7}" -o "${input:7}" = "()" ]; then
576 local_DEFINE=()
577 else
578 local_DEFINE=("${input:7}")
579 fi
580 ;;
581 DEFINE+=*)
582 test -z "${input:8}" -o "${input:8}" = "()" || \
583 local_DEFINE+=("${input:8}")
584 ;;
c807ba1b
DB
585 BUILD=*)
586 if [ -z "${input:6}" -o "${input:6}" = "()" ]; then
587 local_BUILD=()
588 else
589 local_BUILD=("${input:6}")
590 fi
591 ;;
592 BUILD+=*)
593 test -z "${input:7}" -o "${input:7}" = "()" || \
594 local_BUILD+=("${input:7}")
595 ;;
596 INCLUDE=*)
597 if [ -z "${input:8}" -o "${input:8}" = "()" ]; then
598 local_INCLUDE=()
599 else
600 local_INCLUDE=("${input:8}")
601 fi
602 ;;
603 INCLUDE+=*)
604 test -z "${input:8}" -o "${input:8}" = "()" || \
605 local_INCLUDE+=("${input:9}")
606 ;;
607 RUNTIME=*)
608 local_RUNTIME="${input:8}"
609 ;;
610 USER=*)
611 local_USER="${input:5}"
612 ;;
613 NICKNAME=*)
614 local_NICKNAME="${input:9}"
615 ;;
e7148436
DB
616 LOG=*)
617 local_LOG="${input:4}"
618 ;;
619 PORT=*)
620 local_PORT="${input:5}"
621 ;;
622 SSL=*)
623 local_SSL="${input:4}"
624 ;;
c807ba1b
DB
625 \#*)
626 ;; # Comment, do nothing
627 "")
628 ;; # Empty line, do nothing
629 *)
630 echo $"Invalid input, \"$input\", in $config_file" >&2
631 exit 1
632 ;;
633 esac
634 done 3< "$config_file"
635
636 # Now set the results globally. All variables are expected to be set, even
637 # if to nothing.
638 ARCH="$local_ARCH"
639 RELEASE="$local_RELEASE"
071de8a6 640 DEFINE=("${local_DEFINE[@]}")
c807ba1b
DB
641 BUILD=("${local_BUILD[@]}")
642 INCLUDE=("${local_INCLUDE[@]}")
643 RUNTIME="$local_RUNTIME"
644 USER="$local_USER"
645 NICKNAME="$local_NICKNAME"
e7148436
DB
646 LOG="$local_LOG"
647 PORT="$local_PORT"
648 SSL="$local_SSL"
c807ba1b
DB
649}
650
651# Interpret the contents of a server status file.
652interpret_server_status () {
653 # The contents of the server status files are currently the same as
654 # that of server config files.
655 interpret_server_config "$1"
656}
657
658# Load the default server config and add the results to SERVER_CMDS.
659load_server_config () {
660 for f in "$CONFIG_PATH"/*.conf; do
661 if [ -f "$f" ]; then
662 # Obtain a configuration from each config file.
663 # Ensure that we get the correct defaults for items not specified.
664 local ARCH=
071de8a6 665 local DEFINE=()
c807ba1b
DB
666 local BUILD=()
667 local INCLUDE=()
668 local RUNTIME=
669 local USER=
670 local RELEASE=
e7148436
DB
671 local LOG=
672 local PORT=
673 local SSL=
c807ba1b
DB
674 interpret_server_config "$f" || continue
675 # Other options default to empty. These ones don't.
676 [ -z "$ARCH" ] && ARCH=`get_arch`
677 [ -z "$RELEASE" ] && RELEASE=`get_release`
678 [ -z "$USER" ] && USER=$STAP_USER
56b3f5cf 679 [ -z "$USER" ] && USER=`id -un`
e7148436 680 [ -z "$LOG" ] && LOG="$LOG_FILE"
c807ba1b
DB
681 add_server_commands
682 SERVER_CMDS+=("EXEC")
683 fi
684 done
685}
686
60045090
DB
687server_still_running () { # PID
688 (ps -e | grep stap-serverd | grep -q $1) && return 0 # Still running
689
c807ba1b 690 rm -f "$STAT_PATH/$1.stat"
60045090
DB
691 return 1 # Not running
692}
693
694get_server_pid_by_config () {
695 # Need to save the config, since the process of checking the running
696 # servers alters it.
697 local target_ARCH="$ARCH"
698 local target_RELEASE="$RELEASE"
60045090 699 local target_RUNTIME="$RUNTIME"
c807ba1b
DB
700 local target_INCLUDE=("${INCLUDE[@]}")
701 local target_BUILD=("${BUILD[@]}")
071de8a6 702 local target_DEFINE=("${DEFINE[@]}")
60045090
DB
703 local target_USER="$USER"
704 local target_NICKNAME="$NICKNAME"
e7148436
DB
705 local target_LOG="$LOG"
706 local target_PORT="$PORT"
707 local target_SSL="$SSL"
60045090
DB
708
709 # Check the status file for each running server to see if it matches
710 # the one currently configured. We're checking for a given configuration,
711 # so don't compare the nickname.
c807ba1b
DB
712 for f in "$STAT_PATH"/*.stat; do
713 test ! -e "$f" && continue
714 interpret_server_status "$f" || continue
60045090
DB
715 test "X$ARCH" = "X$target_ARCH" || continue
716 test "X$RELEASE" = "X$target_RELEASE" || continue
717 test "X$INCLUDE" = "X$target_INCLUDE" || continue
718 test "X$RUNTIME" = "X$target_RUNTIME" || continue
719 test "X$BUILD" = "X$target_BUILD" || continue
071de8a6 720 test "X$DEFINE" = "X$target_DEFINE" || continue
60045090 721 test "X$USER" = "X$target_USER" || continue
e7148436
DB
722 test "X$LOG" = "X$target_LOG" || continue
723 test "X$PORT" = "X$target_PORT" || continue
724 test "X$SSL" = "X$target_SSL" || continue
c807ba1b 725 echo `basename "$f" | sed 's/.stat//'` # Server has a pid
60045090
DB
726 return
727 done
728
729 ARCH="$target_ARCH"
730 RELEASE="$target_RELEASE"
60045090 731 RUNTIME="$target_RUNTIME"
c807ba1b
DB
732 INCLUDE=("${target_INCLUDE[@]}")
733 BUILD=("${target_BUILD[@]}")
071de8a6 734 DEFINE=("${target_DEFINE[@]}")
60045090
DB
735 USER="$target_USER"
736 NICKNAME="$target_NICKNAME"
e7148436
DB
737 LOG="$target_LOG"
738 PORT="$target_PORT"
739 SSL="$target_SSL"
60045090
DB
740}
741
742get_server_pid_by_nickname () {
743 # No need to save the current configuration. This function is not called
744 # in a context requiring it.
745 local target_NICKNAME="$1"
746
747 # Check the status file for each running server to see if the nickname
748 # matches the one we want.
c807ba1b
DB
749 for f in "$STAT_PATH"/*.stat; do
750 test ! -e "$f" && continue
751 interpret_server_status "$f" || continue
60045090 752 test "X$NICKNAME" = "X$target_NICKNAME" || continue
c807ba1b 753 echo `basename "$f" | sed 's/\.stat//'` # Server with nickname was found
60045090
DB
754 return
755 done
756}
757
758managed_servers () {
c807ba1b 759 if [ ! -d "$STAT_PATH" ]; then
60045090
DB
760 echo ""
761 return 1
762 fi
c807ba1b
DB
763 cd "$STAT_PATH"
764 local list=`ls | sed 's/\.stat//'`
60045090
DB
765 if [ -z "$list" ]; then
766 echo ""
767 return 1
768 fi
769
770 echo "$list"
771}
772
773eval_server_command () {
774 local cmd="$1"
c807ba1b 775 eval $cmd
60045090 776}
0e47827d 777
60045090 778start_server () {
d0d71073 779 clog $"Starting $prog `echo_server_options`"
b9c1510e 780
60045090
DB
781 # Is there already a server running for the requested kernel release
782 # and arch?
783 local server_pid=`get_server_pid_by_config`
784 if test -n "$server_pid"; then
785 if server_still_running $server_pid; then
786 do_success $"$prog start `echo_server_options`"
787 return 0 # Success
b9c1510e 788 fi
ab19dac7
DB
789 fi
790
e7148436
DB
791 # Create the log directory for this server
792 prepare_log_dir "$LOG"
793 if [ $? -ne 0 ]; then
794 echo $"Failed to make log directory (`dirname "$LOG"`)" >&2
795 exit 1
796 fi
797
60045090 798 # Construct the server start command.
71a522b5 799 local server_cmd="$STAP_START_SERVER -r \"`quote_for_cmd "$RELEASE"`\" -a \"`quote_for_cmd "$ARCH"`\" $OPT_OTHER"
c807ba1b
DB
800 for b in "${BUILD[@]}"; do
801 server_cmd="$server_cmd -B \"`quote_for_cmd "$b"`\""
60045090 802 done
c807ba1b
DB
803 for i in "${INCLUDE[@]}"; do
804 server_cmd="$server_cmd -I \"`quote_for_cmd "$i"`\""
60045090 805 done
071de8a6
DB
806 for d in "${DEFINE[@]}"; do
807 server_cmd="$server_cmd -D \"`quote_for_cmd "$d"`\""
808 done
c807ba1b 809 test -n "$RUNTIME" && server_cmd="$server_cmd -R \"`quote_for_cmd "$RUNTIME"`\""
e7148436
DB
810 server_cmd="$server_cmd --log=\"`quote_for_cmd "$LOG"`\""
811 test -n "$PORT" && server_cmd="$server_cmd --port \"`quote_for_cmd "$PORT"`\""
812 test -n "$SSL" && server_cmd="$server_cmd --ssl \"`quote_for_cmd "$SSL"`\""
60045090
DB
813
814 # Start the server here.
815 local pid
56b3f5cf 816 if [ "$USER" != "`id -un`" ]; then
60045090
DB
817 pid=`runuser -s /bin/bash - $USER -c "$server_cmd"`
818 else
819 pid=`eval $server_cmd`
820 fi
821 if [ $? != 0 -o -z "$pid" ]; then
822 if [ -n "$pid" ]; then
c807ba1b 823 rm -f "$STAT_PATH/$pid.stat"
60045090
DB
824 fi
825 do_failure $"$prog start `echo_server_options`"
826 return 1 # Failure
ab19dac7 827 fi
b7afc05d 828
60045090
DB
829 # Nickname defaults to the pid.
830 test -z "$NICKNAME" && NICKNAME="$pid"
ab19dac7 831
60045090 832 # Write the configuration to the status file.
c807ba1b
DB
833 local server_status_file="$STAT_PATH/$pid.stat"
834 echo "ARCH=$ARCH" > "$server_status_file"
835 echo "USER=$USER" >> "$server_status_file"
836 for b in "${BUILD[@]}"; do
837 echo "BUILD+=$b" >> "$server_status_file"
838 done
839 for i in "${INCLUDE[@]}"; do
840 echo "INCLUDE+=$i" >> "$server_status_file"
841 done
071de8a6
DB
842 for d in "${DEFINE[@]}"; do
843 echo "DEFINE+=$d" >> "$server_status_file"
844 done
c807ba1b
DB
845 echo "NICKNAME=$NICKNAME" >> "$server_status_file"
846 echo "RUNTIME=$RUNTIME" >> "$server_status_file"
847 echo "RELEASE=$RELEASE" >> "$server_status_file"
e7148436
DB
848 echo "LOG=$LOG" >> "$server_status_file"
849 echo "PORT=$PORT" >> "$server_status_file"
850 echo "SSL=$SSL" >> "$server_status_file"
b5935516 851
60045090 852 do_success $"$prog start `echo_server_options`"
ab19dac7
DB
853}
854
60045090
DB
855start () { # server-cmds
856 prepare_stat_dir
857 if [ $? -ne 0 ]; then
858 do_failure $"Failed to make stat directory ($STAT_PATH)"
859 return 1
860 fi
861
862 # Start the specified servers
c807ba1b
DB
863 local server_cmds=("$@")
864
60045090
DB
865 # If none specified, start the configured servers
866 if [ -z "$server_cmds" ]; then
c807ba1b 867 unset SERVER_CMDS
60045090 868 load_server_config
c807ba1b 869 server_cmds=("${SERVER_CMDS[@]}")
60045090 870 # If none configured, start the default servers
e7148436
DB
871 if test -z "$server_cmds"; then
872 add_distributed_options
873 server_cmds=("${SERVER_CMDS[@]}")
874 server_cmds+=("EXEC")
875 fi
60045090
DB
876 fi
877
878 # Start each requested server in turn
879 local rc=0
880 local first=1
881 init_server_opts
882 local cmd
883 local prevCmd
c807ba1b
DB
884 for cmd in "${server_cmds[@]}"; do
885 prevCmd="$cmd"
60045090
DB
886 # Evaluate commands until the EXEC command is found.
887 if test "$cmd" != "EXEC"; then
c807ba1b 888 eval_server_command "$cmd"
60045090
DB
889 # A specified nickname only sticks if it is the final command.
890 # Otherwise, we have a configuration based on a nicknamed
891 # configuration.
892 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
893 continue
894 fi
895 # If a nickname was specified, but the corresponding config was not found,
896 # then it is the nickname for this new configuration.
897 if test -n "$NICKNAME_NOT_FOUND"; then
898 NICKNAME="$NICKNAME_NOT_FOUND"
899 NICKNAME_NOT_FOUND=
900 fi
901
902 # Start the configured server
903 test $first = 0 && echo
904 first=0
905 start_server || rc=1
906
907 # Don't use the same nickname for the next server.
908 NICKNAME=
909 done
910
911 return $rc
ab19dac7
DB
912}
913
60045090
DB
914stop () { # server-cmds
915 local first=1
916 local server_list=
c807ba1b 917 local server_cmds=("$@")
60045090
DB
918 if [ -n "$server_cmds" ]; then
919 # Get the pids of all the requested servers.
920 init_server_opts
921 local cmd
922 local prevCmd
c807ba1b
DB
923 for cmd in "${server_cmds[@]}"; do
924 prevCmd="$cmd"
60045090
DB
925 # Evaluate commands until the EXEC command is found.
926 if test "$cmd" != "EXEC"; then
c807ba1b 927 eval_server_command "$cmd"
60045090
DB
928 # A specified nickname only sticks if it is the final command.
929 # Otherwise, we have a configuration based on a nicknamed
930 # configuration.
931 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
932 continue
933 fi
934 # If a nickname was specified, but the corresponding config was not
935 # found, it is an error.
936 if test -n "$NICKNAME_NOT_FOUND"; then
d0d71073 937 clog "No configuration found for the nickname '$NICKNAME_NOT_FOUND'"
60045090
DB
938 NICKNAME="$NICKNAME_NOT_FOUND"
939 do_failure $"$prog stop `echo_server_options`"
940 NICKNAME_NOT_FOUND=
941 rc=1
942 continue
943 fi
944
945 # Get the pid for this server, if it's running
946 local server_pid=`get_server_pid_by_config`
947 if test -n "$server_pid"; then
948 server_list="$server_list $server_pid"
949 continue
950 fi
951
952 # This server is not running, but give a success stop status anyway.
953 test $first = 0 && echo
954 first=0
d0d71073 955 clog $"Stopping $prog `echo_server_options`"
60045090
DB
956 do_success $"$prog stop `echo_server_options`"
957 done
958 else
959 server_list=`managed_servers`
960 if [ -z "$server_list" ]; then
961 clog $"Stopping $prog: " -n
962 do_success $"$prog: No managed servers to stop"
963 return 0
964 fi
965 fi
966
967 # Stop each server in turn
968 local rc=0
969 local pid
970 for pid in $server_list; do
c807ba1b
DB
971 if ! interpret_server_status "$STAT_PATH/$pid.stat"; then
972 rc=1
973 continue
974 fi
60045090
DB
975
976 test $first = 0 && echo
977 first=0
d0d71073 978 clog $"Stopping $prog `echo_server_options`"
60045090
DB
979
980 local this_rc=0
981 if server_still_running $pid; then
56b3f5cf 982 if [ "$USER" != "`id -un`" ]; then
60045090
DB
983 runuser -s /bin/bash - $USER -c "$STAP_STOP_SERVER $pid"
984 else
985 eval $STAP_STOP_SERVER $pid
986 fi
987 if [ $? != 0 ]; then
988 do_failure $"$prog stop `echo_server_options`"
989 this_rc=1
990 rc=1
991 fi
992 fi
993 if [ $this_rc = 0 ]; then
c807ba1b 994 rm -f "$STAT_PATH/$pid.stat"
60045090
DB
995 do_success $"$prog stop `echo_server_options`"
996 fi
997 done
998
999 return $rc
ab19dac7
DB
1000}
1001
60045090
DB
1002status () { # server-list
1003 local rc=0
1004
1005 # Report status for the specified servers or all running servers, if none
1006 # specified.
1007 local server_list=
c807ba1b 1008 local server_cmds=("$@")
60045090
DB
1009 if [ -n "$server_cmds" ]; then
1010 # Get the pids of all the requested servers.
1011 init_server_opts
1012 local cmd
1013 local prevCmd
c807ba1b
DB
1014 for cmd in "${server_cmds[@]}"; do
1015 prevCmd="$cmd"
60045090
DB
1016 # Evaluate commands until the EXEC command is found.
1017 if test "$cmd" != "EXEC"; then
c807ba1b 1018 eval_server_command "$cmd"
60045090
DB
1019 # A specified nickname only sticks if it is the final command.
1020 # Otherwise, we have a configuration based on a nicknamed
1021 # configuration.
1022 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
1023 continue
1024 fi
1025 # If a nickname was specified, but the corresponding config was not
1026 # found, say so.
1027 if test -n "$NICKNAME_NOT_FOUND"; then
1028 echo "No configuration found for the nickname '$NICKNAME_NOT_FOUND'"
1029 NICKNAME_NOT_FOUND=
1030 rc=3
1031 continue
1032 fi
1033
1034 # Get the pid for this server, if it's running
1035 local server_pid=`get_server_pid_by_config`
1036 if test -n "$server_pid"; then
1037 server_list="$server_list $server_pid"
1038 continue
1039 fi
1040 # This server is not running
c807ba1b 1041 echo "stap-server `echo_server_options` is not running"
60045090
DB
1042 rc=3
1043 done
1044 else
1045 server_list=`managed_servers`
1046 if [ -z "$server_list" ]; then
1047 echo "No managed stap-server is running"
1048 return 3
1049 fi
1050 fi
1051
1052 # Get status of each server in turn
1053 local pid
1054 for pid in $server_list; do
c807ba1b
DB
1055 if ! interpret_server_status "$STAT_PATH/$pid.stat"; then
1056 rc=1
1057 continue
1058 fi
60045090
DB
1059 if ! server_still_running $pid; then
1060 echo "stap-server `echo_server_options` started as PID $pid is no longer running"
1061 rc=1
1062 continue
1063 fi
1064 echo "stap-server `echo_server_options` running as PID $pid"
1065 done
1066
1067 return $rc
ab19dac7
DB
1068}
1069
60045090
DB
1070# Restart or start if not running
1071function restart () { # server-cmds
1072 # Restart the specified servers or all servers, if none specified.
c807ba1b
DB
1073 local rc=0
1074 local server_cmds=("$@")
60045090
DB
1075 if [ -z "$server_cmds" ]; then
1076 local server_list=`managed_servers`
1077 local pid
1078 for pid in $server_list; do
c807ba1b
DB
1079 if ! interpret_server_status "$STAT_PATH/$pid.stat"; then
1080 rc=1
1081 continue
1082 fi
1083 unset SERVER_CMDS
1084 add_server_commands
1085 server_cmds+=("${SERVER_CMDS[@]}")
1086 server_cmds+=("EXEC")
60045090
DB
1087 done
1088 fi
1089
1090 # Stop the specified servers, or all if none specified
c807ba1b 1091 stop "${server_cmds[@]}" || rc=1
60045090
DB
1092 echo
1093
1094 # Restart the same servers. If none were specified then
1095 # start the configured or default server(s)).
c807ba1b 1096 start "${server_cmds[@]}"
60045090
DB
1097 local this_rc=$?
1098 [ $this_rc != 0 ] && rc=$this_rc
1099
1100 return $rc
0e47827d
DB
1101}
1102
60045090
DB
1103# Restart only if running
1104function condrestart () { # server-list
1105 # Restart the specified servers or all servers, if none specified,
1106 # but only if they are already running.
c807ba1b
DB
1107 local rc=0
1108 local server_cmds=("$@")
60045090
DB
1109 if [ -z "$server_cmds" ]; then
1110 local server_list=`managed_servers`
1111 local pid
1112 for pid in $server_list; do
c807ba1b
DB
1113 if ! interpret_server_status "$STAT_PATH/$pid.stat"; then
1114 rc=1
1115 continue
1116 fi
1117 unset SERVER_CMDS
1118 add_server_commands
1119 server_cmds+=("${SERVER_CMDS[@]}")
1120 server_cmds+=("EXEC")
60045090
DB
1121 done
1122 # No server specified or running?
1123 if [ -z "$server_cmds" ]; then
1124 clog "No managed stap-server is running" -n
1125 do_success "No managed stap-server is running"
1126 return 0
1127 fi
1128 fi
1129
1130 # For each server in the list, stop it if it is running
60045090 1131 local first=1
c807ba1b 1132 local this_server_cmds
60045090 1133 local cmd
c807ba1b 1134 for cmd in "${server_cmds[@]}"; do
60045090 1135 # Execute and collect commands until the EXEC command is found.
c807ba1b 1136 this_server_cmds+=("$cmd")
60045090 1137 if test "$cmd" != "EXEC"; then
c807ba1b 1138 eval_server_command "$cmd"
60045090
DB
1139 continue
1140 fi
1141
1142 test $first = 0 && echo
1143 first=0
1144
1145 # Now see if this server is running
c807ba1b 1146 if ! status "${this_server_cmds[@]}" >/dev/null 2>&1; then
d0d71073 1147 clog $"$prog `echo_server_options` is not running"
60045090 1148 do_success "$prog `echo_server_options` is not running"
c807ba1b 1149 unset this_server_cmds
60045090
DB
1150 continue
1151 fi
1152
c807ba1b 1153 start_cmds+=("${this_server_cmds[@]}")
60045090 1154
c807ba1b 1155 stop "${this_server_cmds[@]}"
60045090
DB
1156 this_rc=$?
1157 [ $this_rc != 0 ] && rc=$this_rc
1158
c807ba1b 1159 unset this_server_cmds
60045090
DB
1160 done
1161
1162 # Now restart the servers that were running
c807ba1b 1163 if [ -n "$start_cmds" ]; then
60045090 1164 echo
c807ba1b 1165 start "${start_cmds[@]}"
60045090
DB
1166 local this_rc=$?
1167 [ $this_rc != 0 ] && rc=$this_rc
1168 fi
1169
1170 return $rc
1171}
ab19dac7 1172
60045090
DB
1173#------------------------------------------------------------------
1174# Mainline script
1175#------------------------------------------------------------------
1176CMD=$1
1177shift 1
1178
e7148436 1179prepare_log_dir "$LOG_FILE"
60045090
DB
1180if [ $? -ne 0 ]; then
1181 echo $"Failed to make log directory (`dirname $LOG_FILE`)" >&2
1182 exit 1
1183fi
1184
071de8a6 1185OPTS=`getopt -s bash -u --options 'a:B:c:D:iI:n:p:kPr:R:u:' \
e7148436
DB
1186 --longoptions 'log:' \
1187 --longoptions 'port:' \
1188 --longoptions 'ssl:' \
1189 -- "$@"`
60045090
DB
1190if [ $? -ne 0 ]; then
1191 echo "Error: Argument parse error: $@" >&2
1192 echo_usage
1193 exit 2
1194fi
1195
1196# Initialize server specs
c807ba1b 1197parse_args "$@" || exit 2
60045090
DB
1198load_config
1199
1200RETVAL=0
1201
1202case $CMD in
c807ba1b
DB
1203 start) # Start specified servers. If none specified, start configured servers
1204 start "${SERVER_CMDS[@]}"
1205 RETVAL=$?
1206 ;;
1207 stop) # Stop specified servers
1208 stop "${SERVER_CMDS[@]}"
1209 RETVAL=$?
1210 ;;
1211 restart) # Restart specified servers
1212 restart "${SERVER_CMDS[@]}"
1213 RETVAL=$?
1214 ;;
1215 condrestart|try-restart) # Restart specified servers if they are running
1216 condrestart "${SERVER_CMDS[@]}"
1217 RETVAL=$?
1218 ;;
1219 status) # Give status on specified servers
1220 status "${SERVER_CMDS[@]}"
1221 exit $?
1222 ;;
1223 reload) # Reloading config without stop/restart is not supported
1224 RETVAL=3
1225 ;;
1226 force-reload) # Reload config with stop/start
1227 # stop all running servers
1228 stop
1229 echo
1230 # Restart specified servers
1231 # If none specified, restart configured servers
1232 start "${SERVER_CMDS[@]}"
1233 RETVAL=$?
1234 ;;
60045090 1235 usage|*)
c807ba1b
DB
1236 echo_usage
1237 RETVAL=0
1238 ;;
60045090
DB
1239esac
1240
1241echo
1242exit $RETVAL
This page took 0.215649 seconds and 5 git commands to generate.