Header

Creating the simple service

The service script

We are going to use tcpserver to create a service that tells you hello, and then gives some information about how it was invoked. The tcpserver program that it is based on sets (some of) the variables

  • TCPREMOTEHOST
  • TCPREMOTEIP
  • TCPREMOTEPORT
  • TCPLOCALHOST
  • TCPLOCALIP
  • TCPLOCALPORT
and when run with the provided set simple.rules of rules also sets the variable
  • HOST
so the server (simple.sh) shows these variables.

Note the use of a "here-is" script in the server shell.

The tcpserver service

This script is itself invoked when a connection is made to the start-simple.sh script by tcpserver.

The tcpserver service manifest

The manifest tcpserver_manifest.xml is imported to create the service by the command


#svccfg import tcpserver_manifest.xml
and the service is started or stopped by

#svcadm enable simple-server
#svcadm disable simple-server
where the name simple-server is the name of the service provided in the manifest.

The tcpserver service setup

A number of tasks to create needed directories and so on must be done before the service can begin, and that is all done by the setup program.

zipped source code

Code links

simple.sh
start-simple.sh
simple.rules
simple.xml
tcpserver_manifest.xml
setup

Listings

simple.sh
#!/bin/bash
echo "Hello there"
echo "HOST" is $HOST
cat <<EOD
TCPREMOTEHOST is: $TCPREMOTEHOST
TCPREMOTEIP   is: $TCPREMOTEIP
TCPREMOTEPORT is: $TCPREMOTEPORT
TCPLOCALHOST is: $TCPLOCALHOST
TCPLOCALIP   is: $TCPLOCALIP
TCPLOCALPORT is: $TCPLOCALPORT
EOD
echo $TCPREMOTEHOST:$TCPREMOTEIP:$TCPREMOTEPORT >>simple.log

start-simple.sh
tcpserver -x /var/tcpserver/rules/simple.cdb 192.168.1.6 25 simple.sh &

simple.rules
192.168.1.3:allow,HOST="FRIEND"
192.168.1.6:allow,HOST="BAD"
192.168.1.:allow,HOST="LOCAL"
134.48.:allow,HOST="MU"
:deny

simple.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<article
xmlns:xi="http://www.w3.org/2001/XInclude"
>
  <title>A simple service</title>
<section>
</section>
<section>
</section>
</article>

tcpserver_manifest.xml
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle
  type='manifest'
  name='application:simple-server'>
  <service
    name='application/simple-server'
    type='service' version='1'
  >
    <single_instance />
    <instance
      name='default'
      enabled='true'
    >
      <dependency
        name='milestone'
        grouping='require_all'
        restart_on='error'
        type='service'
      >
        <service_fmri
          value='svc:/milestone/multi-user-server:default'
        />
      </dependency>
    
      <dependency name='filesystem'
        grouping='require_all'
        restart_on='error'
        type='service'
      >
        <service_fmri
          value='svc:/system/filesystem/local:default'
        />
      </dependency>
    
      <method_context
        working_directory='/var/tcpserver'
        project=':default'
      >
        <method_credential
          user='doug'
          group='doug'
          privileges='basic,net_privaddr'
        />
        <method_environment>
          <envvar name='PATH'
              value='/var/tcpserver/bin:/usr/local/bin:/usr/bin'
          />
        </method_environment>
      </method_context>
      <exec_method
        type='method'
        name='start'
        exec='start-simple.sh'
        timeout_seconds='60'
      />
      <exec_method
        type='method'
        name='stop'
        exec=':kill'
        timeout_seconds='60'
      />
    </instance>
    <stability
      value='Stable'
    />
    <template>
      <common_name>
        <loctext xml:lang='C'>
          Application - tcpserver based
        </loctext>
      </common_name>
      <documentation>
        <doc_link
          name='tcpserver'
  	  uri='http://cr.yp.to/ucspi-tcp.html'
        />
      </documentation>
    </template>
  </service>
</service_bundle>

setup
mkdir -p /var/tcpserver
chown simple /var/tcpserver
cd /var/tcpserver
mkdir bin rules
cat > bin/start-simple.sh<EODAGAIN
tcpserver -x /var/tcpserver/rules/simple.cdb 192.168.1.6 25 simple.sh &
EODAGAIN
cat > bin/simple.sh<EODAGAIN
#!/bin/bash
echo "Hello there"
echo "HOST" is $HOST
cat <<EOD
TCPREMOTEHOST is: $TCPREMOTEHOST
TCPREMOTEIP   is: $TCPREMOTEIP
TCPREMOTEPORT is: $TCPREMOTEPORT
TCPLOCALHOST is: $TCPLOCALHOST
TCPLOCALIP   is: $TCPLOCALIP
TCPLOCALPORT is: $TCPLOCALPORT
EOD
EODAGAIN

cat >rule/simple.rules<EODAGAIN
192.168.1.3:allow,HOST="FRIEND"
192.168.1.6:allow,HOST="BAD"
192.168.1.:allow,HOST="LOCAL"
134.48.:allow,HOST="MU"
:deny
EODAGAIN