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