aboutsummaryrefslogtreecommitdiff
path: root/system/cdemu-daemon/rc.cdemud
blob: e9c7ba08b8790ec73cf52bd6c269cd18dde97093 (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
#!/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"
MODULE="vhba"
CTL_DEVICE="/dev/vhba_ctl"
PID=$(/sbin/pidof -o %PPID ${NAME})

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"
            exit 1
        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"
            exit 1
        fi
    fi

    # Start daemon
    # ... we don't really start the daemon, this is done by dbus
    return ${RETVAL}
}

stop() 
{
    # Stopping daemon
    echo -n "Stopping ${DESC}"
    if [ -n "${PID}" ] ;then
        kill ${PID}
        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
        sleep 2
    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
    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}