summaryrefslogtreecommitdiffstats
path: root/scull/scull.init
blob: e0523ce456f126677ccb48b6051aa4dd1bbd6cdd (plain) (blame)
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
#!/bin/bash
# Sample init script for the a driver module <rubini@linux.it>

DEVICE="scull"
SECTION="misc"

# The list of filenames and minor numbers: $PREFIX is prefixed to all names
PREFIX="scull"
FILES="     0 0         1 1         2 2        3 3    priv 16 
        pipe0 32    pipe1 33    pipe2 34   pipe3 35
       single 48      uid 64     wuid 80"

INSMOD=/sbin/insmod; # use /sbin/modprobe if you prefer

function device_specific_post_load () {
    true; # fill at will
}
function device_specific_pre_unload () {
    true; # fill at will
}

# Everything below this line should work unchanged for any char device.
# Obviously, however, no options on the command line: either in
# /etc/${DEVICE}.conf or /etc/modules.conf (if modprobe is used)

# Optional configuration file: format is
#    owner  <ownername>
#    group  <groupname>
#    mode   <modename>
#    options <insmod options>
CFG=/etc/${DEVICE}.conf

# kernel version, used to look for modules
KERNEL=`uname -r`

#FIXME: it looks like there is no misc section. Where should it be?
MODDIR="/lib/modules/${KERNEL}/kernel/drivers/${SECTION}"
if [ ! -d $MODDIR ]; then MODDIR="/lib/modules/${KERNEL}/${SECTION}"; fi

# Root or die
if [ "$(id -u)" != "0" ]
then
  echo "You must be root to load or unload kernel modules"
  exit 1
fi

# Read configuration file
if [ -r $CFG ]; then
    OWNER=`awk "\\$1==\"owner\" {print \\$2}" $CFG`
    GROUP=`awk "\\$1==\"group\" {print \\$2}" $CFG`
    MODE=`awk "\\$1==\"mode\" {print \\$2}" $CFG`
    # The options string may include extra blanks or only blanks
    OPTIONS=`sed -n '/^options / s/options //p' $CFG`
fi


# Create device files
function create_files () {
    cd /dev
    local devlist=""
    local file
    while true; do
	if [ $# -lt 2 ]; then break; fi
	file="${DEVICE}$1"
	mknod $file c $MAJOR $2
	devlist="$devlist $file"
	shift 2
    done
    if [ -n "$OWNER" ]; then chown $OWNER $devlist; fi
    if [ -n "$GROUP" ]; then chgrp $GROUP $devlist; fi
    if [ -n "$MODE"  ]; then chmod $MODE  $devlist; fi
}

# Remove device files
function remove_files () {
    cd /dev
    local devlist=""
    local file
    while true; do
	if [ $# -lt 2 ]; then break; fi
	file="${DEVICE}$1"
	devlist="$devlist $file"
	shift 2
    done
    rm -f $devlist
}

# Load and create files
function load_device () {
    
    if [ -f $MODDIR/$DEVICE.o ]; then
	devpath=$MODDIR/$DEVICE.o
    else if [ -f ./$DEVICE.o ]; then
	devpath=./$DEVICE.o
    else
	devpath=$DEVICE; # let insmod/modprobe guess
    fi; fi
    if [ "$devpath" != "$DEVICE" ]; then
	echo -n " (loading file $devpath)"
    fi

    if $INSMOD $devpath $OPTIONS; then
	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
	remove_files $FILES
	create_files $FILES
	device_specific_post_load
    else
	echo " FAILED!"
     fi
}

# Unload and remove files
function unload_device () {
    device_specific_pre_unload 
    /sbin/rmmod $DEVICE
    remove_files $FILES
}


case "$1" in
  start)
     echo -n "Loading $DEVICE"
     load_device
     echo "."
     ;;
  stop)
     echo -n "Unloading $DEVICE"
     unload_device
     echo "."
     ;;
  force-reload|restart)
     echo -n "Reloading $DEVICE"
     unload_device
     load_device
     echo "."
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|force-reload}"
     exit 1
esac

exit 0