/
slash
executable file
·194 lines (166 loc) · 4.86 KB
/
slash
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/sh
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2005 by Open Source Technology Group. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
# /etc/init.d/slash
#
# written by Yazz Atlas for the Slashteam...
# with assists from:
# Jamie McCarthy
# Cliff Wood
# ... and a cast of several.
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin/X11:/usr/X11R6/bin";
# Slash home
PROGNAME="slashd";
DATADIR="/usr/local/slash";
SLASHD="$DATADIR/sbin/$PROGNAME";
SLASHSITE="$DATADIR/slash.sites";
# To figure out where things are...
TZ="GMT";
export TZ;
WHICH_CAT=`which cat`;
WHICH_CUT=`which cut`;
WHICH_PS=`which ps`;
WHICH_GREP=`which grep`;
WHICH_UNAME=`which uname`;
WHICH_AWK=`which awk`;
# if you use sudo, create a file called mysudo in $DATADIR, containing
# your sudo line, like "sudo -u slashdot"
# or "sudo -u foo --shell='/bin/sh'"
MYSUDO=`cat $DATADIR/mysudo 2>/dev/null`
# This prolly ain't 100% accurate, but it should be good enough.
OS=`$WHICH_UNAME | $WHICH_AWK '{print $1}'`;
if [ ! -f $SLASHSITE ] ; then
echo "NOT Starting $PROGNAME: No $SLASHSITE";
exit 0;
fi
GRAB_CONFIG=`$WHICH_CAT $SLASHSITE | $WHICH_CUT -d"#" -f1`;
#
# FUNCTIONS
#
start_slashd () {
# There are differing syntaxes of "su" between OSes
# and even between different distributions of the same
# OS. If you have an OS that isn't listed here (or is
# a different case of one listed here [ie Red Hat Linux,
# Debian Linux]) please add in the necessary logic and
# send in a patch. Thanks!
# if you aren't using GMT for internal dates, please change
# the appropriate lines, below. But why wouldn't you?
if [ "$MYSUDO" ] ; then
TZ=GMT $MYSUDO $SLASHD $VIRTUAL_USER_NAME &
elif [ "$OS" = "FreeBSD" ] ; then
TZ=GMT su $USERNAME -c "$SLASHD $VIRTUAL_USER_NAME" &
elif [ "$OS" = "Linux" ] ; then
su --shell="/bin/sh" - $USERNAME -c "TZ=GMT $SLASHD $VIRTUAL_USER_NAME" &
else
su - $USERNAME -c "TZ=GMT $SLASHD $VIRTUAL_USER_NAME" &
fi
}
break_parts () {
# This is used over and over to break apart the need
# variables.
VIRTUAL_USER_NAME=`echo $server_name | $WHICH_CUT -d":" -f1`;
USERNAME=`echo $server_name | $WHICH_CUT -d":" -f2`;
SITENAME=`echo $server_name | $WHICH_CUT -d":" -f3`;
RUNNINGPID="$DATADIR/site/$SITENAME/logs/$PROGNAME.pid";
}
stop_slashd () {
# This will restart and remove any left over PID files
# since they can't help anyone. Oh wait, they are useful
# to verify is slashd is really running. The keepalive
# choice looks for thoses PID files to see if slashd
# is hanging about.
if [ -f $RUNNINGPID ] ; then
echo -n "Stopping $PROGNAME $VIRTUAL_USER_NAME: ";
if ! $MYSUDO kill `$WHICH_CAT $RUNNINGPID` ; then
echo -n "...using kill -9 to make sure ...";
sleep 3;
$MYSUDO kill -9 `$WHICH_CAT $RUNNINGPID`;
$MYSUDO rm -f $RUNNINGPID;
fi
echo "ok.";
else
echo "$PROGNAME $VIRTUAL_USER_NAME has no PID file";
fi
}
check_variable () {
# Just want to print things out so I can see what we are
# getting.
echo "VIRTUAL_USER_NAME=$VIRTUAL_USER_NAME";
echo "USERNAME=$USERNAME";
echo "SITENAME=$SITENAME";
echo "RUNNINGPID=${RUNNINGPID}";
echo "PID=`cat ${RUNNINGPID}`";
}
#
# MAIN CODE
#
if [ -z "`echo $GRAB_CONFIG | $WHICH_GREP :`" ] ; then
echo "NOT Starting/stopping $PROGNAME: No sites in $SLASHSITE";
exit 0;
fi
case "$1" in
start)
for server_name in $GRAB_CONFIG; do
break_parts;
echo -n "Starting $PROGNAME $VIRTUAL_USER_NAME: ";
start_slashd;
echo "ok PID = $!";
done
;;
stop)
for server_name in $GRAB_CONFIG; do
break_parts;
stop_slashd;
done
;;
keepalive)
for server_name in $GRAB_CONFIG; do
break_parts;
if [ ! -f $RUNNINGPID ] ;then
echo -n "Restarting $PROGNAME $VIRTUAL_USER_NAME, no PID file: ";
start_slashd;
echo "ok PID = $!";
else
# OK, this might not work on every platform since the "ps"
# is different on alot of OSes.
# Once people submit fixes I will pull this into a similar
# check like the one for "su".
if ! $WHICH_PS wh -p `$WHICH_CAT $RUNNINGPID` | $WHICH_GREP $PROGNAME > /dev/null ; then
echo -n "Restarting $PROGNAME $VIRTUAL_USER_NAME, no valid PID file: ";
rm -f $RUNNINGPID;
start_slashd;
echo "ok PID = $!";
fi
fi
done
;;
restart)
$0 stop;
echo -n "Sleeping 10 seconds to be clean: ";
sleep 10;
echo "ok.";
$0 start;
;;
checkvars)
echo "The contents of $SLASHSITE:";
for server_name in $GRAB_CONFIG; do
echo " $server_name";
done
for server_name in $GRAB_CONFIG; do
echo "";
break_parts;
check_variable;
done
echo "";
;;
*)
echo "Debian Usage: /etc/init.d/$0 {start|stop|restart|keepalive|checkvars}";
echo "Redhat Usage: /etc/rc.d/init.d/$0 {start|stop|restart|keepalive|checkvars}";
echo "Other Usage: /usr/local/sbin/$0 {start|stop|restart|keepalive|checkvars}";
exit 1;
;;
esac
exit 0;