]> sourceware.org Git - systemtap.git/blame - stap-server
Consolidate task_finder/vma tracker initialization.
[systemtap.git] / stap-server
CommitLineData
ab19dac7 1#!/bin/bash
60045090
DB
2#
3# stap-server script for managing the systemtap compile server
ab19dac7 4#
64aa100f 5# Copyright (C) 2008, 2009 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
60045090 15. /etc/rc.d/init.d/functions
0e47827d 16
60045090 17# Systemtap function library
961588fd 18. ${PKGLIBEXECDIR}stap-env
98f552c2 19
60045090
DB
20prog=stap-server
21
22# Commands
23STAP_START_SERVER=${stap_pkglibexecdir}stap-start-server
24STAP_STOP_SERVER=${stap_pkglibexecdir}stap-stop-server
25UNAME=/bin/uname
26
27# Default Global Configuration
28CONFIG_FILE=$stap_sysconfdir/sysconfig/stap-server
29CONFIG_PATH=$stap_sysconfdir/stap-server/conf.d
30STAT_PATH=$stap_localstatedir/run/stap-server
2648a9b4 31LOG_FILE=$stap_localstatedir/log/stap-server/log
60045090
DB
32
33# Default option settings
34# Optional global config file
35OPT_CONFIG_FILE=
36
37echo_usage () {
38 echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|status} [options]"
39 echo $"Options:"
40 echo $" -c configfile : specify additional global configuration file."
41 echo $" -a arch : specify the target architecture."
42 echo $" -r release : specify the kernel release."
43 echo $" -I path : augment the search path for tapsets."
44 echo $" -R path : specify the location of the systemtap runtime."
45 echo $" -B options : specify 'make' options for building systemtap modules."
46 echo $" -u username : specify the user who will run the server(s)."
47 echo $" -i : specify a server for each installed kernel release."
48 echo $" -n nickname : specify a server configuration by nickname."
49 echo $" -p pid : specify a server or server configuration by process id."
50 echo $""
51 echo $"All options, may be specified more than once."
52 echo $""
53 echo $"If -a is not specified, the default architecture is that of the host"
54 echo $"platform."
55 echo $""
56 echo $"If -r is not specified, the default kernel release is that currently"
57 echo $"running on the host platform."
58 echo $""
59 echo $"If -u is not specified, the default user is 'stap-server'"
60 echo $""
61 echo $"Each -I and -B option specifies an additional path or option"
62 echo $"respectively. For other options, each new instance overrides the"
63 echo $"previous setting."
64 echo $""
65 echo $"The -i option is a shortcut which specifies one server for each kernel"
66 echo $"release installed in /lib/modules/. Previous -I, -R, -B and -u"
67 echo $"options will be applied to each server, however previous -a options"
68 echo $"are ignored and the default architecture is used."
69 echo $""
70 echo $"The -n option allows the specification of a server configuration by"
71 echo $"nickname. When -n is specified, a currently running server with the"
72 echo $"given nickname will be searched for. If no currently running server"
73 echo $"with the given nickname is found, a server configuration with the"
74 echo $"given nickname will be searched for in $stap_sysconfdir/stap-server/conf.d/*.conf."
75 echo $"If a server configuration for the given nickname is found, the -a, -r,"
76 echo $"-I, -R, -B and -u options for that server will be used as if they were"
77 echo $"specified on the command line. If no configuration with the given"
78 echo $"nickname is found, and the action is 'start' (or an action behaving"
79 echo $"like 'start' (see below), the server will be started with the given"
80 echo $"nickname. If no configuration with the given nickname is found, and"
81 echo $"the action is not 'start' (or an action behaving" "like 'start',"
82 echo $"it is an error. If a nickname is not specified for a server, its"
83 echo $"nickname will be its process id."
84 echo $""
85 echo $"The -p option allows the specification of a server configuration by"
86 echo $"process id. When -p is specified, a currently running server with the"
87 echo $"given process id will be searched for. If no such server is found,"
88 echo $"it is an error. If a server with the given pid is found, the -a, -r,"
89 echo $"-I, -R, -B and -u options for that server will be used as if they were"
90 echo $"specified on the command line."
91 echo $""
92 echo $"The specified action is performed for the server(s) specified on the"
93 echo $"command line. If no servers are specified on the command line, the"
94 echo $"behavior is as follows:"
95 echo $""
96 echo $" start: Start the servers configured in $stap_sysconfdir/stap-server/conf.d/*.conf."
97 echo $" If none are configured, start a server for the kernel release"
98 echo $" and architecture of the host platform."
99 echo $""
100 echo $" stop: Stop all currently running servers."
101 echo $""
102 echo $" restart: Restart all currently running servers. If no servers are running,"
103 echo $" behave as if 'start' was specified."
104 echo $""
105 echo $" condrestart: Restart all currently running servers. If no servers are running,"
106 echo $" do nothing."
107 echo $""
108 echo $" try-restart: Same as condrestart."
109 echo $""
110 echo $" force-reload: Stop all currently running servers and behave as if 'start'"
111 echo $" was specified."
112 echo $""
113 echo $" status: Report the status of all current running servers."
114 echo $""
115}
ab19dac7 116
60045090
DB
117#-----------------------------------------------------------------
118# Helper functions
119#-----------------------------------------------------------------
120log () { # message
121 echo `LC_ALL=en date +"%b %e %T"`": $1" >> "$LOG_FILE"
122}
123clog () { # message [-n]
124 echo $2 "$1"
125 log "$1"
126}
127slog () { # message
128 logger "$1" # if syslogd is running, this message will be sent to syslog.
129 log "$1"
130}
131logex () { # command
132 eval log \"Exec: $@\"
133 "$@" >> "$LOG_FILE" 2>&1
134 return $?
135}
136do_failure () { # message
137 slog "Error: $1"
138 failure "$1"
139}
140do_success () { # message
141 log "Pass: $1"
142 success "$1"
143}
66e1a139 144
60045090
DB
145#------------------------------------------------------------------
146# Parameter parsing and setup options
147#------------------------------------------------------------------
148parse_args () { # arguments
149 local rc=0
150 while [ -n "$1" ]; do
151 case "$1" in
152 -a)
153 OPT_SERVER_CMDS="$OPT_SERVER_CMDS ARCH='$2'"
154 shift 1
155 ;;
156 -B)
157 OPT_SERVER_CMDS="$OPT_SERVER_CMDS BUILD='$2'"
158 shift 1
159 ;;
160 -c)
161 OPT_CONFIG_FILE=$2
162 shift 1
163 ;;
164 -i)
165 process_i
166 ;;
167 -I)
168 OPT_SERVER_CMDS="$OPT_SERVER_CMDS INCLUDE='$2'"
169 shift 1
170 ;;
171 -n)
172 process_n $2
173 shift 1
174 ;;
175 -p)
176 process_p $2
177 test $? = 0 || rc=1
178 shift 1
179 ;;
180 -r)
181 process_r $2
182 test $? = 0 || rc=1
183 shift 1
184 ;;
185 -R)
186 OPT_SERVER_CMDS="$OPT_SERVER_CMDS RUNTIME='$2'"
187 shift 1
188 ;;
189 -u)
190 OPT_SERVER_CMDS="$OPT_SERVER_CMDS USER='$2'"
191 shift 1
192 ;;
193 --)
194 ;;
195 *)
196 rc=1
197 ;;
198 esac
199 shift 1
200 done
201
202 # Add an EXEC command to the end if any server options were specified
203 test -n "$OPT_SERVER_CMDS" && OPT_SERVER_CMDS="$OPT_SERVER_CMDS EXEC"
204
205 test $rc != 0 && echo_usage
206 return $rc
207}
66e1a139 208
60045090
DB
209# Process the -i flag.
210process_i () {
211 cd /lib/modules
212 local release
213 for release in `ls`; do
214 test -n "$OPT_SERVER_CMDS" && OPT_SERVER_CMDS="$OPT_SERVER_CMDS EXEC"
215 process_r $release
216 done
66e1a139 217
60045090 218 return 0
ab19dac7
DB
219}
220
60045090
DB
221# Process the -n flag.
222process_n () {
223 local target_NICKNAME="$1"
224
225 # Is there a running server with this nickname?
226 local pid=`get_server_pid_by_nickname "$target_NICKNAME"`
227 if [ -n "$pid" ]; then
228 # Read the configuration and add it to the configuration commands.
229 . $STAT_PATH/$pid
230 OPT_SERVER_CMDS="$OPT_SERVER_CMDS `echo_server_commands`"
231 return
232 fi
233
234 # Is there a server configuration with this nickname?
235 for f in "$CONFIG_PATH"/*.conf; do
236 if [ -f "$f" ]; then
237 . "$f"
238 test "X$NICKNAME" = "X$target_NICKNAME" || continue
239 OPT_SERVER_CMDS="$OPT_SERVER_CMDS `echo_server_commands`"
240 return
241 fi
242 done
ab19dac7 243
60045090
DB
244 # No server configuration could be found for this nickname. Add a
245 # NICKNAME_NOT_FOUND=... command to the configuration commands.
246 OPT_SERVER_CMDS="$OPT_SERVER_CMDS NICKNAME_NOT_FOUND='$target_NICKNAME'"
247}
248
249# Process the -p flag.
250process_p () {
251 local pid="$1"
9e494cbb 252
60045090
DB
253 # Are we managing a server with the given pid?
254 test ! -f $STAT_PATH/$pid && echo "No stap-server running as pid $pid" && \
255 exit 1
9e494cbb 256
60045090
DB
257 # Add the configuration of the server running as $pid to OPT_SERVER_CMDS
258 . $STAT_PATH/$pid
259 OPT_SERVER_CMDS="$CONFIG_SERVER_CMDS `echo_server_commands`"
ab19dac7 260
60045090 261 return 0
66e1a139 262}
ab19dac7 263
60045090
DB
264# Process the -r flag.
265process_r () {
266 local first_char=`expr "$1" : '\(.\).*'`
267
268 if test "$first_char" = "/"; then # fully specified path
269 local kernel_build_tree=$1
270 local version_file_name="$kernel_build_tree/include/config/kernel.release"
271 # The file include/config/kernel.release within the kernel
272 # build tree is used to pull out the version information
273 local kernel_release=`cat $version_file_name 2>/dev/null`
274 if test "X$kernel_release" = "X"; then
275 echo "Missing $version_file_name"
276 return 1
277 fi
278 OPT_SERVER_CMDS="$OPT_SERVER_CMDS RELEASE='$kernel_release'"
279 return 0
66e1a139 280 fi
ab19dac7 281
60045090
DB
282 # kernel release specified directly
283 OPT_SERVER_CMDS="$OPT_SERVER_CMDS RELEASE='$1'"
284 return 0
ab19dac7
DB
285}
286
60045090
DB
287load_config () {
288 # Include configs
289 if [ -f "$CONFIG_FILE" ]; then
290 . "$CONFIG_FILE"
291 fi
292 if [ -f "$OPT_CONFIG_FILE" ]; then
293 . "$OPT_CONFIG_FILE"
294 fi
295}
d5658775 296
60045090
DB
297# Default to the currently running kernel release
298get_release () {
299 $UNAME -r
300}
d5658775 301
60045090
DB
302# Default to the currently running kernel release
303get_arch () {
304 stap_get_arch
ab19dac7
DB
305}
306
60045090
DB
307echo_server_commands () {
308 # Echo the configuration command string.
309 echo -n "ARCH='$ARCH'"
310 echo -n " RELEASE='$RELEASE'"
311 echo -n " RUNTIME='$RUNTIME'"
312 for i in $INCLUDE; do
313 echo -n " INCLUDE='$i'"
314 done
315 for b in $BUILD; do
316 echo -n " BUILD='$b'"
317 done
318 echo -n " USER='$USER'"
319 echo -n " NICKNAME='$NICKNAME'"
320 echo
321}
8afee8bb 322
60045090
DB
323echo_server_options () {
324 # Echo the configuration options.
325 echo -n "-a '$ARCH'"
326 echo -n " -r '$RELEASE'"
327 test -n "$RUNTIME" && echo -n " -R '$RUNTIME'"
328 for i in $INCLUDE; do
329 echo -n " -I '$i'"
330 done
331 for b in $BUILD; do
332 echo -n " -B '$b'"
ab19dac7 333 done
60045090
DB
334 test -n "$USER" && echo -n " -u '$USER'"
335 echo -n " -n '$NICKNAME'"
336 echo
ab19dac7
DB
337}
338
60045090
DB
339load_server_config () {
340 CONFIG_SERVER_CMDS=
341 for f in "$CONFIG_PATH"/*.conf; do
342 if [ -f "$f" ]; then
343 # Obtain a configuration from each config file.
344 # Ensure that we get the correct defaults for items not specified.
345 local ARCH=
346 local BUILD=
347 local INCLUDE=
348 local RUNTIME=
349 local USER=
350 local RELEASE=
351 . "$f"
352 # Other options default to empty. These ones don't.
353 [ -z "$ARCH" ] && ARCH=`get_arch`
354 [ -z "$RELEASE" ] && RELEASE=`get_release`
355 [ -z "$USER" ] && USER=$STAP_USER
356 CONFIG_SERVER_CMDS="$CONFIG_SERVER_CMDS `echo_server_commands` EXEC"
ab19dac7 357 fi
60045090 358 done
ab19dac7
DB
359}
360
60045090
DB
361prepare_stat_dir () {
362 if [ ! -d "$STAT_PATH" ]; then
363 logex mkdir -p "$STAT_PATH"
364 [ $? -ne 0 ] && return 1
365 fi
366 return 0
ab19dac7
DB
367}
368
60045090
DB
369prepare_log_dir () {
370 local log_path=`dirname "$LOG_FILE"`
371 if [ ! -d "$log_path" ]; then
372 mkdir -p "$log_path"
373 [ $? -ne 0 ] && return 1
374 fi
375 return 0
ab19dac7
DB
376}
377
60045090
DB
378stat_file () { # server-spec
379 echo $STAT_PATH/$1
380}
0e47827d 381
60045090
DB
382default_server_cmds () {
383 echo "EXEC"
384}
d5658775 385
60045090
DB
386init_server_opts () {
387 ARCH=`get_arch`
388 RELEASE=`get_release`
389 BUILD=
390 INCLUDE=
391 NICKNAME=
392 NICKNAME_NOT_FOUND=
393 RUNTIME=
394 USER=$STAP_USER
ab19dac7
DB
395}
396
60045090
DB
397server_still_running () { # PID
398 (ps -e | grep stap-serverd | grep -q $1) && return 0 # Still running
399
400 rm -f $STAT_PATH/$1
401 return 1 # Not running
402}
403
404get_server_pid_by_config () {
405 # Need to save the config, since the process of checking the running
406 # servers alters it.
407 local target_ARCH="$ARCH"
408 local target_RELEASE="$RELEASE"
409 local target_INCLUDE="$INCLUDE"
410 local target_RUNTIME="$RUNTIME"
411 local target_BUILD="$BUILD"
412 local target_USER="$USER"
413 local target_NICKNAME="$NICKNAME"
414
415 # Check the status file for each running server to see if it matches
416 # the one currently configured. We're checking for a given configuration,
417 # so don't compare the nickname.
418 for f in $STAT_PATH/*; do
419 test ! -e $f && continue
420 . $f
421 test "X$ARCH" = "X$target_ARCH" || continue
422 test "X$RELEASE" = "X$target_RELEASE" || continue
423 test "X$INCLUDE" = "X$target_INCLUDE" || continue
424 test "X$RUNTIME" = "X$target_RUNTIME" || continue
425 test "X$BUILD" = "X$target_BUILD" || continue
426 test "X$USER" = "X$target_USER" || continue
427 echo `basename $f` # Server has a pid
428 return
429 done
430
431 ARCH="$target_ARCH"
432 RELEASE="$target_RELEASE"
433 INCLUDE="$target_INCLUDE"
434 RUNTIME="$target_RUNTIME"
435 BUILD="$target_BUILD"
436 USER="$target_USER"
437 NICKNAME="$target_NICKNAME"
438}
439
440get_server_pid_by_nickname () {
441 # No need to save the current configuration. This function is not called
442 # in a context requiring it.
443 local target_NICKNAME="$1"
444
445 # Check the status file for each running server to see if the nickname
446 # matches the one we want.
447 for f in $STAT_PATH/*; do
448 test ! -e $f && continue
449 . $f
450 test "X$NICKNAME" = "X$target_NICKNAME" || continue
451 echo `basename $f` # Server with nickname was found
452 return
453 done
454}
455
456managed_servers () {
457 if [ ! -d $STAT_PATH ]; then
458 echo ""
459 return 1
460 fi
461 cd $STAT_PATH
462 local list=`ls`
463 if [ -z "$list" ]; then
464 echo ""
465 return 1
466 fi
467
468 echo "$list"
469}
470
471eval_server_command () {
472 local cmd="$1"
473
474 # Accumulate the results of BUILD and INCLUDE commands.
475 if echo $cmd | grep -q ^BUILD; then
476 local prevBUILD="$BUILD"
477 eval $cmd
478 BUILD="$prevBUILD $BUILD"
479 BUILD=`echo $BUILD | sed 's/^ //'`
480 elif echo $cmd | grep -q ^INCLUDE; then
481 local prevINCLUDE="$INCLUDE"
482 eval $cmd
483 INCLUDE="$prevINCLUDE $INCLUDE"
484 INCLUDE=`echo $INCLUDE | sed 's/^ //'`
485 else
486 eval $cmd
487 fi
488}
0e47827d 489
60045090
DB
490start_server () {
491 clog $"Starting $prog for $RELEASE $ARCH: " -n
b9c1510e 492
60045090
DB
493 # Is there already a server running for the requested kernel release
494 # and arch?
495 local server_pid=`get_server_pid_by_config`
496 if test -n "$server_pid"; then
497 if server_still_running $server_pid; then
498 do_success $"$prog start `echo_server_options`"
499 return 0 # Success
b9c1510e 500 fi
ab19dac7
DB
501 fi
502
60045090
DB
503 # Construct the server start command.
504 local server_cmd="$STAP_START_SERVER -r '$RELEASE' -a '$ARCH'"
505 for b in $BUILD; do
506 server_cmd="$server_cmd -B '$b'"
507 done
508 for i in $INCLUDE; do
509 server_cmd="$server_cmd -I '$i'"
510 done
511 test -n "$RUNTIME" && server_cmd="$server_cmd -R '$RUNTIME'"
512 server_cmd="$server_cmd --log=$LOG_FILE"
513
514 # Start the server here.
515 local pid
516 if [ -n "$USER" ]; then
517 pid=`runuser -s /bin/bash - $USER -c "$server_cmd"`
518 else
519 pid=`eval $server_cmd`
520 fi
521 if [ $? != 0 -o -z "$pid" ]; then
522 if [ -n "$pid" ]; then
523 rm -f $STAT_PATH/$pid
524 fi
525 do_failure $"$prog start `echo_server_options`"
526 return 1 # Failure
ab19dac7 527 fi
b7afc05d 528
60045090
DB
529 # Nickname defaults to the pid.
530 test -z "$NICKNAME" && NICKNAME="$pid"
ab19dac7 531
60045090
DB
532 # Write the configuration to the status file.
533 local server_status_file=$STAT_PATH/$pid
534 echo "ARCH='$ARCH'" > $server_status_file
535 echo "USER='$USER'" >> $server_status_file
536 echo "BUILD='$BUILD'" >> $server_status_file
537 echo "INCLUDE='$INCLUDE'" >> $server_status_file
538 echo "NICKNAME='$NICKNAME'" >> $server_status_file
539 echo "RUNTIME='$RUNTIME'" >> $server_status_file
540 echo "RELEASE='$RELEASE'" >> $server_status_file
b5935516 541
60045090 542 do_success $"$prog start `echo_server_options`"
ab19dac7
DB
543}
544
60045090
DB
545start () { # server-cmds
546 prepare_stat_dir
547 if [ $? -ne 0 ]; then
548 do_failure $"Failed to make stat directory ($STAT_PATH)"
549 return 1
550 fi
551
552 # Start the specified servers
553 server_cmds="$1"
554 # If none specified, start the configured servers
555 if [ -z "$server_cmds" ]; then
556 load_server_config
557 server_cmds="$CONFIG_SERVER_CMDS"
558
559 # If none configured, start the default servers
560 [ -z "$server_cmds" ] && server_cmds=`default_server_cmds`
561 fi
562
563 # Start each requested server in turn
564 local rc=0
565 local first=1
566 init_server_opts
567 local cmd
568 local prevCmd
569 for cmd in $server_cmds; do
570 prevCmd=$cmd
571 # Evaluate commands until the EXEC command is found.
572 if test "$cmd" != "EXEC"; then
573 eval_server_command $cmd
574 # A specified nickname only sticks if it is the final command.
575 # Otherwise, we have a configuration based on a nicknamed
576 # configuration.
577 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
578 continue
579 fi
580 # If a nickname was specified, but the corresponding config was not found,
581 # then it is the nickname for this new configuration.
582 if test -n "$NICKNAME_NOT_FOUND"; then
583 NICKNAME="$NICKNAME_NOT_FOUND"
584 NICKNAME_NOT_FOUND=
585 fi
586
587 # Start the configured server
588 test $first = 0 && echo
589 first=0
590 start_server || rc=1
591
592 # Don't use the same nickname for the next server.
593 NICKNAME=
594 done
595
596 return $rc
ab19dac7
DB
597}
598
60045090
DB
599stop () { # server-cmds
600 local first=1
601 local server_list=
602 server_cmds="$1"
603 if [ -n "$server_cmds" ]; then
604 # Get the pids of all the requested servers.
605 init_server_opts
606 local cmd
607 local prevCmd
608 for cmd in $server_cmds; do
609 prevCmd=$cmd
610 # Evaluate commands until the EXEC command is found.
611 if test "$cmd" != "EXEC"; then
612 eval_server_command $cmd
613 # A specified nickname only sticks if it is the final command.
614 # Otherwise, we have a configuration based on a nicknamed
615 # configuration.
616 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
617 continue
618 fi
619 # If a nickname was specified, but the corresponding config was not
620 # found, it is an error.
621 if test -n "$NICKNAME_NOT_FOUND"; then
622 clog "No configuration found for the nickname '$NICKNAME_NOT_FOUND'" -n
623 NICKNAME="$NICKNAME_NOT_FOUND"
624 do_failure $"$prog stop `echo_server_options`"
625 NICKNAME_NOT_FOUND=
626 rc=1
627 continue
628 fi
629
630 # Get the pid for this server, if it's running
631 local server_pid=`get_server_pid_by_config`
632 if test -n "$server_pid"; then
633 server_list="$server_list $server_pid"
634 continue
635 fi
636
637 # This server is not running, but give a success stop status anyway.
638 test $first = 0 && echo
639 first=0
640 clog $"Stopping $prog for $RELEASE $ARCH: " -n
641 do_success $"$prog stop `echo_server_options`"
642 done
643 else
644 server_list=`managed_servers`
645 if [ -z "$server_list" ]; then
646 clog $"Stopping $prog: " -n
647 do_success $"$prog: No managed servers to stop"
648 return 0
649 fi
650 fi
651
652 # Stop each server in turn
653 local rc=0
654 local pid
655 for pid in $server_list; do
656 . $STAT_PATH/$pid
657
658 test $first = 0 && echo
659 first=0
660 clog $"Stopping $prog for $RELEASE $ARCH: " -n
661
662 local this_rc=0
663 if server_still_running $pid; then
664 if [ -n "$USER" ]; then
665 runuser -s /bin/bash - $USER -c "$STAP_STOP_SERVER $pid"
666 else
667 eval $STAP_STOP_SERVER $pid
668 fi
669 if [ $? != 0 ]; then
670 do_failure $"$prog stop `echo_server_options`"
671 this_rc=1
672 rc=1
673 fi
674 fi
675 if [ $this_rc = 0 ]; then
676 rm -f $STAT_PATH/$pid
677 do_success $"$prog stop `echo_server_options`"
678 fi
679 done
680
681 return $rc
ab19dac7
DB
682}
683
60045090
DB
684status () { # server-list
685 local rc=0
686
687 # Report status for the specified servers or all running servers, if none
688 # specified.
689 local server_list=
690 server_cmds="$1"
691 if [ -n "$server_cmds" ]; then
692 # Get the pids of all the requested servers.
693 init_server_opts
694 local cmd
695 local prevCmd
696 for cmd in $server_cmds; do
697 prevCmd=$cmd
698 # Evaluate commands until the EXEC command is found.
699 if test "$cmd" != "EXEC"; then
700 eval_server_command $cmd
701 # A specified nickname only sticks if it is the final command.
702 # Otherwise, we have a configuration based on a nicknamed
703 # configuration.
704 echo "$cmd" | grep -q "^NICKNAME=" || NICKNAME=""
705 continue
706 fi
707 # If a nickname was specified, but the corresponding config was not
708 # found, say so.
709 if test -n "$NICKNAME_NOT_FOUND"; then
710 echo "No configuration found for the nickname '$NICKNAME_NOT_FOUND'"
711 NICKNAME_NOT_FOUND=
712 rc=3
713 continue
714 fi
715
716 # Get the pid for this server, if it's running
717 local server_pid=`get_server_pid_by_config`
718 if test -n "$server_pid"; then
719 server_list="$server_list $server_pid"
720 continue
721 fi
722 # This server is not running
723 echo "stap-server `echo_server_options`"
724 rc=3
725 done
726 else
727 server_list=`managed_servers`
728 if [ -z "$server_list" ]; then
729 echo "No managed stap-server is running"
730 return 3
731 fi
732 fi
733
734 # Get status of each server in turn
735 local pid
736 for pid in $server_list; do
737 . $STAT_PATH/$pid
738 if ! server_still_running $pid; then
739 echo "stap-server `echo_server_options` started as PID $pid is no longer running"
740 rc=1
741 continue
742 fi
743 echo "stap-server `echo_server_options` running as PID $pid"
744 done
745
746 return $rc
ab19dac7
DB
747}
748
60045090
DB
749# Restart or start if not running
750function restart () { # server-cmds
751 # Restart the specified servers or all servers, if none specified.
752 local server_cmds="$1"
753 if [ -z "$server_cmds" ]; then
754 local server_list=`managed_servers`
755 local pid
756 for pid in $server_list; do
757 . $STAT_PATH/$pid
758 server_cmds="$server_cmds `echo_server_commands` EXEC"
759 done
760 fi
761
762 # Stop the specified servers, or all if none specified
763 stop "$server_cmds"
764 local rc=$?
765 echo
766
767 # Restart the same servers. If none were specified then
768 # start the configured or default server(s)).
769 start "$server_cmds"
770 local this_rc=$?
771 [ $this_rc != 0 ] && rc=$this_rc
772
773 return $rc
0e47827d
DB
774}
775
60045090
DB
776# Restart only if running
777function condrestart () { # server-list
778 # Restart the specified servers or all servers, if none specified,
779 # but only if they are already running.
780 local server_cmds="$1"
781 if [ -z "$server_cmds" ]; then
782 local server_list=`managed_servers`
783 local pid
784 for pid in $server_list; do
785 . $STAT_PATH/$pid
786 server_cmds="$server_cmds `echo_server_commands` EXEC"
787 done
788 # No server specified or running?
789 if [ -z "$server_cmds" ]; then
790 clog "No managed stap-server is running" -n
791 do_success "No managed stap-server is running"
792 return 0
793 fi
794 fi
795
796 # For each server in the list, stop it if it is running
797 local start_cmds=
798 local first=1
799 local server_cmd=
800 local cmd
801 for cmd in $server_cmds; do
802 # Execute and collect commands until the EXEC command is found.
803 server_cmd="$server_cmd $cmd"
804 if test "$cmd" != "EXEC"; then
805 eval_server_command $cmd
806 continue
807 fi
808
809 test $first = 0 && echo
810 first=0
811
812 # Now see if this server is running
813 if ! status "$server_cmd" >/dev/null 2>&1; then
814 clog $"$prog for $RELEASE $ARCH is not running" -n
815 do_success "$prog `echo_server_options` is not running"
816 server_cmd=
817 continue
818 fi
819
820 start_cmds="$start_cmds $server_cmd"
821
822 stop "$server_cmd"
823 this_rc=$?
824 [ $this_rc != 0 ] && rc=$this_rc
825
826 server_cmd=
827 done
828
829 # Now restart the servers that were running
830 if [ "X$start_cmds" != "X" ]; then
831 echo
832 start "$start_cmds"
833 local this_rc=$?
834 [ $this_rc != 0 ] && rc=$this_rc
835 fi
836
837 return $rc
838}
ab19dac7 839
60045090
DB
840#------------------------------------------------------------------
841# Mainline script
842#------------------------------------------------------------------
843CMD=$1
844shift 1
845
846prepare_log_dir
847if [ $? -ne 0 ]; then
848 echo $"Failed to make log directory (`dirname $LOG_FILE`)" >&2
849 exit 1
850fi
851
852OPTS=`getopt -s bash -u -o 'a:B:c:iI:n:p:r:R:u:' -- $@`
853if [ $? -ne 0 ]; then
854 echo "Error: Argument parse error: $@" >&2
855 echo_usage
856 exit 2
857fi
858
859# Initialize server specs
860OPT_SERVER_CMDS=
861parse_args $OPTS || exit 2
862load_config
863
864RETVAL=0
865
866case $CMD in
867 start)
868 # Start specified servers. If none specified, start configured servers
869 start "$OPT_SERVER_CMDS"
870 RETVAL=$?
871 ;;
872 stop)
873 # Stop specified servers
874 stop "$OPT_SERVER_CMDS"
875 RETVAL=$?
876 ;;
877 # Restart specified servers
878 restart)
879 restart "$OPT_SERVER_CMDS"
880 RETVAL=$?
881 ;;
882 # Restart specified servers if they are running
883 condrestart|try-restart)
884 condrestart "$OPT_SERVER_CMDS"
885 RETVAL=$?
886 ;;
887 # Give status on specified servers
888 status)
889 status "$OPT_SERVER_CMDS"
890 exit $?
891 ;;
892 # Reloading config without stop/restart is not supported
893 reload)
894 RETVAL=3
895 ;;
896 # Reload config with stop/start
897 force-reload)
898 # stop all running servers
899 stop
900 echo
901 # Restart specified servers
902 # If none specified, restart configured servers
903 start "$OPT_SERVER_CMDS"
904 RETVAL=$?
905 ;;
906 usage|*)
907 echo_usage
908 RETVAL=0
909 ;;
910esac
911
912echo
913exit $RETVAL
This page took 0.135319 seconds and 5 git commands to generate.