Knowledge Base : How to add a service to svc

How to add a service to Solaris‘ SMF architecture (with example for rsyncd)

Write a manifesto

[root@sol2 FILER /root]# cat /var/svc/manifest/network/rsync.xml
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type="manifest" name="rsync">
<service name="network/rsync" type="service" version="1">
<create_default_instance enabled="false"/>
<single_instance/>
<dependency name="net-loopback" grouping="require_all" restart_on="none" type="service">
<service_fmri value="svc:/network/loopback"/>
</dependency>
<dependency name="net-physical" grouping="require_all" restart_on="none" type="service">
<service_fmri value="svc:/network/physical"/>
</dependency>
<dependency name="config_data" grouping="require_all" restart_on="restart" type="path">
<service_fmri value="file://localhost/etc/rsyncd.conf"/>
<service_fmri value="file://localhost/etc/default/rsync"/>
</dependency>
<exec_method type="method" name="start" exec="/lib/svc/method/rsync %m" timeout_seconds="30">
</exec_method>
<exec_method type="method" name="restart" exec="/lib/svc/method/rsync %m" timeout_seconds="30">
</exec_method>
<exec_method type="method" name="stop" exec="/lib/svc/method/rsync %m" timeout_seconds="30">
</exec_method>
<template>
<common_name>
<loctext xml:lang="C">
Rsync Service
</loctext>
</common_name>
</template>
</service>
</service_bundle>

Write a method

[root@sol2 FILER /root]# cat /lib/svc/method/rsync
#!/usr/bin/bash
set -e

# /lib/svc/method/rsync: start and stop the RSYNC daemon

DAEMON=/usr/local/bin/rsync
RSYNC_ENABLE=false
RSYNC_OPTS=
RSYNC_DEFAULTS_FILE=/etc/default/rsync
RSYNC_CONFIG_FILE=/etc/rsyncd.conf
PIDFILE=/var/run/rsyncd.pid

test -x $DAEMON || exit 0

if [ -s $RSYNC_DEFAULTS_FILE ]; then
. $RSYNC_DEFAULTS_FILE
case "x$RSYNC_ENABLE" in
xtrue|xfalse) ;;
xinetd) exit 0
;;
*) echo "Value of RSYNC_ENABLE in $RSYNC_DEFAULTS_FILE must be either 'true' or 'false';"
echo "not starting rsync daemon."
exit 1
;;
esac
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
start)
if "$RSYNC_ENABLE"; then
echo -n "Starting rsync daemon: rsync"
if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
echo " apparently already running."
exit 0
fi
if [ ! -s "$RSYNC_CONFIG_FILE" ]; then
echo " missing or empty config file $RSYNC_CONFIG_FILE"
exit 1
fi
$DAEMON --daemon --config="$RSYNC_CONFIG_FILE" $RSYNC_OPTS
echo "."
else
if [ -s "$RSYNC_CONFIG_FILE" ]; then
echo "rsync daemon not enabled in /etc/default/rsync"
fi
fi
;;
stop)
if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
echo -n "Stopping rsync daemon: rsync"
/usr/bin/kill -TERM `/usr/bin/cat $PIDFILE`
rm -f $PIDFILE 
echo "."
fi
;;

reload|force-reload)
echo "Reloading rsync daemon: not needed, as the daemon"
echo "re-reads the config file whenever a client connects."
;;

restart)
# set +e
if $RSYNC_ENABLE; then
echo -n "Restarting rsync daemon: "
if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
/usr/bin/kill -TERM `/usr/bin/cat $PIDFILE`
rm -f $PIDFILE
echo -n "."
fi
if [ ! -s "$RSYNC_CONFIG_FILE" ]; then
echo " missing or empty config file $RSYNC_CONFIG_FILE"
exit 1
fi
sleep 5
$DAEMON --daemon --config="$RSYNC_CONFIG_FILE" $RSYNC_OPTS
if [ $? -ne 0 ]; then
echo "start failed? $?"
rm -f $PIDFILE 
else 
echo "."
fi
else
echo "rsync daemon not enabled in /etc/default/rsync"
fi
;;

*)
echo "Usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart}"
exit 1
esac

exit 0

Validate and import

svccfg validate /var/svc/manifest/network/rsync.xml 
svccfg import /var/svc/manifest/network/rsync.xml

Configure

svcadm enable svc:/network/rsync
svcs -xv

This article was based on the now-dead blog post: http://blogs.sun.com/ritu/entry/how_to_configure_mysql_to