summaryrefslogtreecommitdiff
path: root/system/cdemu-daemon/rc.cdemud
blob: c793981c825e5fcdbfe683797ec808ea23aaaaf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh
#
# cdemu-daemon: CDEmu daemon
#

### BEGIN INIT INFO
# Provides:          cdemu-daemon
# Short-Description: Start the CDEmu daemon
# Description:       System mode startup script for
#                    the CDEmu daemon.
### END INIT INFO

# This script is based on the ubuntu and fedora init script.
# Modified by Nille Åkerström
# Modifies by Niels Horn <niels.horn@gmail.com>

NAME="cdemud"
DESC="cdemu-daemon"
LOCK="/var/lock/${DESC}.lock"
PID=$(/sbin/pidof -o %PPID ${NAME})

# Get the configuration information from /etc/rc.d/rc.cdemud.conf:
. /etc/rc.d/rc.cdemud.conf

start() 
{
    # Check if already started
    if [ -n "${PID}" ] ;then
	echo "${DESC} is already running at pid: ${PID}"
	return ${RETVAL}
	exit
    fi

    # Load module
    if [ -n "${MODULE}" ]; then
        echo -n "Inserting ${MODULE} kernel module"
        modprobe "${MODULE}" >/dev/null 2>&1
        RETVAL=$?
	if [ ${RETVAL} -eq 0 ]; then
            echo -e "\t\t\t\t\t[  OK  ]\r"
        else
            echo -e "\t\t\t\t\t[FAILED]\r"
        fi
    fi
    
    # Wait until control device is created...
    if [ -n "${CTL_DEVICE}" ]; then
        echo -n "Waiting for ${CTL_DEVICE} to be created"

        CTL_WAIT=30 # Wait at most 30 seconds
        RETVAL=1
        until [ ${CTL_WAIT} -eq 0 ]; do
            if [ -c "${CTL_DEVICE}" ]; then
                CTL_WAIT=0
                RETVAL=0
            else
                CTL_WAIT=$((CTL_WAIT-1))
                sleep 1
            fi
        done

	if [ ${RETVAL} -eq 0 ]; then
            echo -e "\t\t\t\t[  OK  ]\r"
        else
            echo -e "\t\t\t\t[FAILED]\r"
        fi
    fi


    # Daemon arguments
    DAEMON_ARGS="--daemonize"
    
    if [ -n "${DEVICES}" ]; then
        DAEMON_ARGS="${DAEMON_ARGS} --num-devices=${DEVICES}"
    fi
    if [ -n "${CTL_DEVICE}" ]; then
        DAEMON_ARGS="${DAEMON_ARGS} --ctl-device=${CTL_DEVICE}"
    fi
    if [ -n "${AUDIO_DRIVER}" ]; then
        DAEMON_ARGS="${DAEMON_ARGS} --audio-driver=${AUDIO_DRIVER}"
    fi
    if [ -n "${BUS}" ]; then
        DAEMON_ARGS="${DAEMON_ARGS} --bus=${BUS}"
    fi

    # Start daemon
    echo -n "Starting ${DESC}"
    $NAME ${DAEMON_ARGS} >/dev/null 2>&1
    RETVAL=$?
    if [ ${RETVAL} -eq 0 ] && touch ${LOCK} ; then
        echo -e "\t\t\t\t\t\t[  OK  ]\r"
        else
        echo -e "\t\t\t\t\t\t[FAILED]\r"
    fi
    return ${RETVAL}
}

stop() 
{
        # Stopping daemon
        echo -n "Stopping ${DESC}"
	if [ -n "${PID}" ] ;then
            $NAME -k >/dev/null 2>&1
            RETVAL=$?
            if [ ${RETVAL} -eq 0 ]; then
                echo -e "\t\t\t\t\t\t[  OK  ]\r"
                else
                echo -e "\t\t\t\t\t\t[FAILED]\r"
            fi
	 else 
            echo -e "\t\t\t\tWarning: ${DESC} isn't running"
	    RETVAL=0
	 fi
					    

          # Unload module
          echo -n "Removing ${MODULE} kernel module"
          LOADED=`lsmod | grep -o -e ^${MODULE} -`
          if [ "${LOADED}" = "${MODULE}" ]; then
              modprobe -r "${MODULE}" >/dev/null 2>&1
              RETVAL=$?
              if [ ${RETVAL} -eq 0 ]; then
                  echo -e "\t\t\t\t\t[  OK  ]\r"
                  else
                  echo -e "\t\t\t\t\t[FAILED]\r"
              fi
          return ${RETVAL}
          if [ $RETVAL -eq 0 ]; then
	      rm -f $LOCK
          fi
          else
              echo -e "\t\t\tWarning: ${MODULE} isn't loaded"
              RETVAL=0
          fi
          return ${RETVAL}
}

restart() {
          stop
          RETVAL=$?
          if [ ${RETVAL} -eq 0 ]; then
              PID=$(/sbin/pidof -o %PPID ${NAME})
              start
              RETVAL=$?
              if [ ${RETVAL} -ne 0 ]; then
		  echo "Couldn't start ${DESC} retry in 3sec."
		  sleep 3
		  start
		  RETVAL=$?
              fi
          fi
          return ${RETVAL}
}

# See how we were called.
case "$1" in
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "restart")
        restart
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        ;;
esac

exit ${RETVAL}