-
Notifications
You must be signed in to change notification settings - Fork 149
/
xrootd.functions
304 lines (269 loc) · 8.64 KB
/
xrootd.functions
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#-------------------------------------------------------------------------------
# Library for handling xrootd daemons on RHEL
# Author: Lukasz Janyst <ljanyst@cern.ch> (09.03.2011)
#-------------------------------------------------------------------------------
. /etc/rc.d/init.d/functions
#-------------------------------------------------------------------------------
# Add the user accounts if needed and set up the directory ownership
#-------------------------------------------------------------------------------
function setupInstallation()
{
XROOTD_USER=$1
XROOTD_GROUP=$2
getent group $XROOTD_GROUP >/dev/null || groupadd -r $XROOTD_GROUP
getent passwd $XROOTD_USER >/dev/null || \
useradd -r -g $XROOTD_GROUP -c "XRootD runtime user" \
-s /sbin/nologin -d /var/spool/xrootd $XROOTD_USER
chown $XROOTD_USER:$XROOTD_GROUP -R /var/spool/xrootd
chown $XROOTD_USER:$XROOTD_GROUP -R /var/log/xrootd
}
#-------------------------------------------------------------------------------
# Check if a file has the right permissions
#-------------------------------------------------------------------------------
function checkFile()
{
FILE=$1
XROOTD_USER=$2
XROOTD_GROUP=$3
if test x"`stat -c %U $FILE`" != x$XROOTD_USER; then
echo "$FILE has wrong user ownership: \"`stat -c %U $FILE`\" instead of \"$XROOTD_USER\""
return 1
fi
}
#-------------------------------------------------------------------------------
# Check a directory and it's permissions
#-------------------------------------------------------------------------------
function checkDirectory()
{
DIRECTORY=$1
XROOTD_USER=$2
XROOTD_GROUP=$3
if test ! -d $DIRECTORY; then
echo "Directory: $DIRECTORY does not exist"
return 1
fi
for i in `find $DIRECTORY`; do
checkFile $i $XROOTD_USER $XROOTD_GROUP
if test $? -ne 0; then
return 1
fi
done
return 0
}
#-------------------------------------------------------------------------------
# Check if the installation is in a sane state
#-------------------------------------------------------------------------------
function checkSanity()
{
XROOTD_USER=$1
XROOTD_GROUP=$2
#-----------------------------------------------------------------------------
# Check if the user account exist
#-----------------------------------------------------------------------------
getent passwd $XROOTD_USER >/dev/null
if test $? -ne 0; then
echo "User account for: $XROOTD_USER doesn't exist"
return 1
fi
getent group $XROOTD_GROUP >/dev/null
if test $? -ne 0; then
echo "Group account for: $XROOTD_GROUP doesn't exist"
return 2
fi
#-----------------------------------------------------------------------------
# We need these directories to be owned by the xroot user for the init
# scripts to work properly, and we can safely change the ownership if
# it is wrong.
#-----------------------------------------------------------------------------
checkDirectory /var/spool/xrootd $XROOTD_USER $XROOTD_GROUP
if test $? -ne 0; then
chown $XROOTD_USER:$XROOTD_GROUP -R /var/spool/xrootd
fi
mkdir -p /var/run/xrootd
chown $XROOTD_USER:$XROOTD_GROUP -R /var/run/xrootd
checkDirectory /var/run/xrootd $XROOTD_USER $XROOTD_GROUP
if test $? -ne 0; then
chown $XROOTD_USER:$XROOTD_GROUP -R /var/run/xrootd
fi
}
#-------------------------------------------------------------------------------
# Start a daemon
#-------------------------------------------------------------------------------
function startDaemon()
{
ulimit -n 65536
DAEMON=$1
EX=$2
XROOTD_USER=$3
XROOTD_GROUP=$4
INSTANCE=$5
PIDFILE="/var/run/xrootd/$DAEMON-$INSTANCE.pid"
# check sanity of the installation
checkSanity $XROOTD_USER $XROOTD_GROUP
if test $? -ne 0; then
echo "Please run: service xrootd setup"
return 1
fi
echo -n "Starting xrootd ($DAEMON, $INSTANCE): "
shift 5
# useful for storing coredumps
cd /var/spool/xrootd
daemon --user $XROOTD_USER --pidfile $PIDFILE $EX $@ -b -s $PIDFILE -n $INSTANCE
RETVAL=$?
echo
return $RETVAL
}
#-------------------------------------------------------------------------------
# Stop a daemon
#-------------------------------------------------------------------------------
function stopDaemon()
{
echo -n "Shutting down xrootd ($1, $5): "
PIDFILE="/var/run/xrootd/$1-$5.pid"
if [ -e $PIDFILE ]; then
killproc -p $PIDFILE $1
RETVAL=$?
echo
return $RETVAL
fi
echo -n "$1-$5 not running"
echo
return 0
}
#-------------------------------------------------------------------------------
# Get the status of a daemon
#-------------------------------------------------------------------------------
function statusOfTheDaemon()
{
PIDFILE="/var/run/xrootd/$1-$5.pid"
echo -n "[$5] "
status -p $PIDFILE $1
return $RETVAL
}
#-------------------------------------------------------------------------------
# Conditionally restart a daemon
#-------------------------------------------------------------------------------
function condrestartDaemon()
{
PIDFILE="/var/run/xrootd/$1-$5.pid"
status -p $PIDFILE $1 > /dev/null
if test $? -ne 0; then
return 0
fi
stopDaemon $@
if test $? -ne 0; then
return 1
fi
startDaemon $@
if test $? -ne 0; then
return 2
fi
return 0
}
#-------------------------------------------------------------------------------
# Do things to daemons
#-------------------------------------------------------------------------------
function handleDaemons()
{
#-----------------------------------------------------------------------------
# Check if the user account is specified
#-----------------------------------------------------------------------------
if test x"$XROOTD_USER" = x; then
XROOTD_USER="daemon"
fi
if test x"$XROOTD_GROUP" = x; then
XROOTD_GROUP="daemon"
fi
#-----------------------------------------------------------------------------
# Determine the command to be run
#-----------------------------------------------------------------------------
COMMAND=$1;
shift
case "$COMMAND" in
'start')
CMD_HANDLER=startDaemon
;;
'stop')
CMD_HANDLER=stopDaemon
;;
'status')
CMD_HANDLER=statusOfTheDaemon
;;
'condrestart')
CMD_HANDLER=condrestartDaemon
;;
'setup')
setupInstallation $XROOTD_USER $XROOTD_GROUP
return $?
;;
*)
echo "Unrecognized command: $COMMAND"
return 1
;;
esac
#-----------------------------------------------------------------------------
# Select the daemon to be started
#-----------------------------------------------------------------------------
DAEMON=$1;
shift
case "$DAEMON" in
'xrootd' | 'cmsd')
EXEC=/usr/bin/$DAEMON
CONFIG_NAME=`echo $DAEMON | tr '[[:lower:]]' '[[:upper:]]'`
;;
'frm_purged')
EXEC=/usr/bin/$DAEMON
CONFIG_NAME=PURD
;;
'frm_xfrd')
EXEC=/usr/bin/$DAEMON
CONFIG_NAME=XFRD
;;
*)
echo "Unrecognized daemon: $DAEMON"
return 1
;;
esac
#-----------------------------------------------------------------------------
# Select the instances to run
#-----------------------------------------------------------------------------
if test $# -eq 0; then
eval INSTANCES=\$${CONFIG_NAME}_INSTANCES
else
INSTANCES=$@
fi
eval INSTANCES_XROOTD3=\$${CONFIG_NAME}3_INSTANCES
INSTANCES=`echo $INSTANCES | tr '[[:upper:]]' '[[:lower:]]'`
INSTANCES_XROOTD3=`echo $INSTANCES_XROOTD3 | tr '[[:upper:]]' '[[:lower:]]'`
#-----------------------------------------------------------------------------
# Exec the command on the instances
#-----------------------------------------------------------------------------
STATUS=0
for INSTANCE in $INSTANCES; do
INSTANCE_UPPER=`echo $INSTANCE | tr '[[:lower:]]' '[[:upper:]]'`
eval OPTS=\$${CONFIG_NAME}_${INSTANCE_UPPER}_OPTIONS
#---------------------------------------------------------------------------
# Check if the instance is xrootd3 or xrootd4, if a xrootd3 binary is
# installed
#---------------------------------------------------------------------------
EXEC_RUN=$EXEC
if test -x $EXEC-3; then
for INS in $INSTANCES_XROOTD3; do
if test $INS = $INSTANCE; then
EXEC_RUN=$EXEC-3
fi
done
fi
#---------------------------------------------------------------------------
# Run the instance
#---------------------------------------------------------------------------
if test x"$OPTS" = x; then
continue
fi
$CMD_HANDLER $DAEMON $EXEC_RUN "$XROOTD_USER" "$XROOTD_GROUP" $INSTANCE "$OPTS"
if test $? -ne 0; then
STATUS=1
fi
done
return $STATUS
}