1#!/bin/sh
2#
3# PROVIDE: xencommons
4# REQUIRE: DAEMON
5
6XENSTORED=@XENSTORED@
7
8. /etc/rc.subr
9
10. @XEN_SCRIPT_DIR@/hotplugpath.sh
11
12LD_LIBRARY_PATH="${libdir}"
13export LD_LIBRARY_PATH
14
15name="xencommons"
16rcvar="xencommons_enable"
17start_precmd="xen_precmd"
18start_cmd="xen_startcmd"
19stop_cmd="xen_stop"
20status_cmd="xen_status"
21extra_commands="status"
22required_files="/dev/xen/xenstored"
23
24XENSTORED_PIDFILE="@XEN_RUN_DIR@/xenstored.pid"
25XENCONSOLED_PIDFILE="@XEN_RUN_DIR@/xenconsoled.pid"
26#XENCONSOLED_TRACE="@XEN_LOG_DIR@/xenconsole-trace.log"
27#XENSTORED_TRACE="@XEN_LOG_DIR@/xen/xenstore-trace.log"
28
29load_rc_config $name
30: ${xencommons_enable:=no}
31
32xen_precmd()
33{
34	mkdir -p @XEN_LIB_STORED@ || exit 1
35}
36
37xen_startcmd()
38{
39	local time=0
40	local timeout=30
41
42	xenstored_pid=$(check_pidfile ${XENSTORED_PIDFILE} ${XENSTORED})
43	if test -z "$xenstored_pid"; then
44		printf "Cleaning xenstore database.\n"
45		if [ -z "${XENSTORED_ROOTDIR}" ]; then
46			XENSTORED_ROOTDIR="@XEN_LIB_STORED@"
47		fi
48		rm -f ${XENSTORED_ROOTDIR}/tdb* >/dev/null 2>&1
49		printf "Starting xenservices: xenstored, xenconsoled."
50		XENSTORED_ARGS=" --pid-file ${XENSTORED_PIDFILE}"
51		if [ -n "${XENSTORED_TRACE}" ]; then
52			XENSTORED_ARGS="${XENSTORED_ARGS} -T @XEN_LOG_DIR@/xenstored-trace.log"
53		fi
54		${XENSTORED} ${XENSTORED_ARGS}
55		while [ $time -lt $timeout ] && ! `${bindir}/xenstore-read -s / >/dev/null 2>&1` ; do
56			printf "."
57			time=$(($time+1))
58			sleep 1
59		done
60	else
61		printf "Starting xenservices: xenconsoled."
62	fi
63
64	XENCONSOLED_ARGS=""
65	if [ -n "${XENCONSOLED_TRACE}" ]; then
66		XENCONSOLED_ARGS="${XENCONSOLED_ARGS} --log=${XENCONSOLED_TRACE}"
67	fi
68
69	${sbindir}/xenconsoled ${XENCONSOLED_ARGS}
70
71	printf "\n"
72
73	printf "Setting domain 0 name, domid and JSON config...\n"
74	${LIBEXEC_BIN}/xen-init-dom0
75}
76
77xen_stop()
78{
79	pids=""
80	printf "Stopping xencommons.\n"
81	printf "WARNING: Not stopping xenstored, as it cannot be restarted.\n"
82
83	rc_pid=$(check_pidfile ${XENCONSOLED_PIDFILE} ${sbindir}/xenconsoled)
84	pids="$pids $rc_pid"
85
86	kill -${sig_stop:-TERM} $pids
87	wait_for_pids $pids
88}
89
90xen_status()
91{
92	xenstored_pid=$(check_pidfile ${XENSTORED_PIDFILE} ${XENSTORED})
93	if test -n ${xenstored_pid}; then
94		pids="$pids $xenstored_pid"
95	fi
96
97	xenconsoled_pid=$(check_pidfile ${XENCONSOLED_PIDFILE} ${sbindir}/xenconsoled)
98	if test -n ${xenconsoled_pid}; then
99		pids="$pids $xenconsoled_pid"
100	fi
101
102	if test -n "$xenconsoled_pid" -a -n "$xenstored_pid";
103	then
104		echo "xencommons are running as pids $pids."
105		return 0
106	fi
107	if test -z "$xenconsoled_pid" -a -z "$xenstored_pid";
108	then
109		echo "xencommons are not running."
110		return 0
111	fi
112
113	if test -n $xenstored_pid; then
114		echo "xenstored is running as pid $xenstored_pid."
115	else
116		echo "xenstored is not running."
117	fi
118	if test -n $xenconsoled_pid; then
119		echo "xenconsoled is running as pid $xenconsoled_pid."
120	else
121		echo "xenconsoled is not running."
122	fi
123}
124
125run_rc_command "$1"
126