-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_service.in
377 lines (368 loc) · 10.1 KB
/
create_service.in
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/sh
#
# $Id: create_service.in,v 1.29 2023-06-30 10:16:44+05:30 Cprogrammer Exp mbhangui $
#
check_update_if_diff()
{
val=`cat $1 2>/dev/null`
if [ ! " $val" = " $2" ] ; then
echo $2 > $1
fi
}
add_service()
{
if [ $# -ne 1 ] ; then
echo "USAGE: create_service --servicedir=path --user=username \\"
echo " --lcdhost=host --lcdport=port || --lcdfifo=fifo \\"
echo " --add-service" 1>&2
return 1
fi
userid=$1
case "$SYSTEM" in
Linux)
user=$(getent passwd $userid 2>/dev/null| cut -d: -f1)
if [ $? -ne 0 ] ; then
if [ $? -eq 2 ] ; then
echo "No such userid $userid" 1>&2
return 1
fi
return 1
fi
;;
Darwin)
user=$(dscl . -list /Users UniqueID|grep -w $userid|awk '{print $1}')
if [ -z "$user" ] ; then
echo "No such userid $userid" 1>&2
return 1
fi
;;
*)
echo "Configure/setup startup manually" 1>&2
exit 1
;;
esac
if [ -z "$servicedir" ] ; then
echo "--servicedir not specified" 1>&2
exit 1
fi
if [ ! -d $logdir ] ; then
mkdir -p $logdir
chown qmaill:nofiles $logdir
fi
mkdir -p $servicedir/$service_name/log
mkdir -p $servicedir/$service_name/variables
echo "$prog_args" > $servicedir/$service_name/variables/.options
if [ ! -f $servicedir/$service_name/variables/NO_DB_UPDATE ] ; then
> $servicedir/$service_name/variables/NO_DB_UPDATE
fi
check_update_if_diff $servicedir/$service_name/variables/AUTO_RATING 0
check_update_if_diff $servicedir/$service_name/variables/MPD_HOST 127.0.0.1
check_update_if_diff $servicedir/$service_name/variables/MPD_PORT 6600
if [ -n "$lcdfifo" ] ; then
> $servicedir/$service_name/variables/LCD_HOST
> $servicedir/$service_name/variables/LCD_PORT
check_update_if_diff $servicedir/$service_name/variables/LCD_FIFO $lcdfifo
elif [ -n "$lcdhost" -a -n "$lcdport" ] ; then
> $servicedir/$service_name/variables/LCD_FIFO
check_update_if_diff $servicedir/$service_name/variables/LCD_HOST $lcdhost
check_update_if_diff $servicedir/$service_name/variables/LCD_PORT $lcdport
else
> $servicedir/$service_name/variables/LCD_FIFO
> $servicedir/$service_name/variables/LCD_HOST
> $servicedir/$service_name/variables/LCD_PORT
fi
check_update_if_diff $servicedir/$service_name/variables/MPDEV_TMPDIR "/tmp/mpdev"
sock=$(grep "^bind_to_address" @sysconfdir@/mpd.conf | grep sock|awk '{print $2}'|sed 's{"{{g')
if [ -n "$sock" ] ; then
check_update_if_diff $servicedir/$service_name/variables/MPD_SOCKET $sock
elif [ -S /run/mpd/mpd.sock ] ; then
check_update_if_diff $servicedir/$service_name/variables/MPD_SOCKET "/run/mpd/mpd.sock"
fi
case "$SYSTEM" in
Linux)
home=$(getent passwd $user|cut -d: -f6)
;;
Darwin)
home=$(dscl . -list /Users NFSHomeDirectory|grep $user|awk '{print $2}')
;;
esac
if [ -n "$home" ] ; then
check_update_if_diff $servicedir/$service_name/variables/PATH "/bin:/usr/bin:@prefix@/bin:$home/.mpdev:/usr/local/bin:/opt/local/bin"
check_update_if_diff $servicedir/$service_name/variables/HOME "$home"
if [ "$SYSTEM" = "Linux" ] ; then
check_update_if_diff $servicedir/$service_name/variables/XDG_RUNTIME_DIR "$home"
fi
fi
mkdir -p $home/.mpdev
for i in player playpause output mixer lcd_display
do
if [ ! -f $home/.mpdev/$i ] ; then
cp @mpdevlibexecdir@/$i $home/.mpdev
elif [ ! -f $home/.mpdev/."$i".nooverwrite ] ; then
cp @mpdevlibexecdir@/$i $home/.mpdev
fi
done
case "$SYSTEM" in
Linux)
t_uid=$(getent passwd $user|cut -d: -f3)
t_gid=$(getent passwd $user|cut -d: -f4)
;;
Darwin)
t_uid=$(dscl . -read /Users/$user UniqueID|cut -d: -f2|sed 's{ {{g')
t_gid=$(dscl . -read /Users/$user PrimaryGroupID|cut -d: -f2|sed 's{ {{g')
;;
esac
chown -R $t_uid:$t_gid $home/.mpdev
(
echo "#!/bin/sh"
echo "# generated log script for $host on `date`"
echo "# by the below command"
echo "# $prog_args"
echo "exec @prefix@/bin/setuidgid qmaill \\"
if [ -f @prefix@/sbin/multilog ] ; then
echo "@prefix@/sbin/multilog t $logdir/$service_name"
else
echo "@prefix@/bin/multilog t $logdir/$service_name"
fi
) > /service/$service_name/log/run
chmod +x /service/$service_name/log/run
(
echo "#!/bin/sh"
echo "# generated run script for $host on `date`"
echo "# by the below command"
echo "# $prog_args"
echo "exec 2>&1"
echo
echo "user=$user"
echo "home=$home"
echo
echo "for i in lastfm librefm"
echo "do"
echo " echo \"clearing \$i status\""
echo " @prefix@/bin/setuidgid \$user @prefix@/bin/envdir \\"
echo " variables @prefix@/bin/\$i-scrobbler --stop"
echo " find \$home/.config/\$i-scrobbler -name '*'.log -ctime +7 -exec /bin/rm -f {} \;"
echo "done"
echo "for i in player playpause output mixer"
echo "do"
echo " if [ -f \$home/.mpdev/.\$i.nooverwrite ] ; then"
echo " continue"
echo " fi"
echo " diff @mpdevlibexecdir@/\$i \$home/.mpdev/\$i > /dev/null 2>&1"
echo " if [ \$? -ne 0 ] ; then"
echo " cp @mpdevlibexecdir@/\$i \$home/.mpdev/\$i"
echo " fi"
echo "done"
echo "exec @prefix@/bin/setuidgid -s \$user @prefix@/bin/envdir \\"
echo " variables @prefix@/bin/mpdev -v"
) > /service/$service_name/run
chmod +x /service/$service_name/run
(
echo "#!/bin/sh"
echo "# generated run script for $host on `date`"
echo "# by the below command"
echo "# $prog_args"
echo "exec 2>&1"
echo
echo "user=$user"
echo "home=$home"
echo "for i in lastfm librefm"
echo "do"
echo " echo \"clearing \$i status\""
echo " @prefix@/bin/setuidgid \$user @prefix@/bin/envdir \\"
echo " variables @prefix@/bin/\$i-scrobbler --stop"
echo "done"
) > /service/$service_name/shutdown
chmod +x /service/$service_name/shutdown
}
del_service()
{
if [ ! -d $servicedir/$service_name ] ; then
return 0
fi
mv $servicedir/$service_name $servicedir/."$service_name"
svc -dx $servicedir/."$service_name" $servicedir/."$service_name"/log > /dev/null
sleep 5
/bin/rm -rf $servicedir/."$service_name" $servicedir/."$service_name"/log > /dev/null
}
usage()
{
prog_args=""
if [ -f /usr/bin/less ] ; then
MORE=/usr/bin/less
else
MORE=/usr/bin/more
fi
echo "Press ENTER for options, Cntrl C to quit" 1>&2
read key
$MORE <<EOF
Usage: create_service [OPTIONS]
create_service --servicedir=dir --add-service
or
create_service --servicedir=dir --del-service
EOF
exit $1
}
selinux_module()
{
if [ -f /usr/sbin/selinuxenabled ] ; then
selinuxenabled
if [ $? -ne 0 ] ; then
return 1
fi
else
return 1
fi
if [ -f @sysconfdir@/mpdev/mpd_local.te ] ; then
outdir=$(mktemp -dt mpdevXXXXXXXXXX)
checkmodule -M -m -o $outdir/mpd_local.mod @sysconfdir@/mpdev/mpd_local.te
semodule_package -o $outdir/mpd_local.pp -m $outdir/mpd_local.mod
semodule -i $outdir/mpd_local.pp
fi
}
################################# Main ##################################
if test $# -eq 0; then
usage 1
fi
service_name=mpdev
logdir=/var/log/svc
servicedir=/service
lcdhost=""
lcdport=""
lcdfifo=""
prog_args="$0 $*"
SYSTEM=$(uname -s)
while test $# -gt 0; do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
;;
*) optarg=
;;
esac
case "$1" in
--servicedir=*)
servicedir=$optarg
;;
--user=*)
user=$optarg
;;
--lcdhost=*)
lcdhost=$optarg
;;
--lcdport=*)
lcdport=$optarg
;;
--lcdfifo=*)
lcdfifo=$optarg
;;
--add-service)
if [ -z "$user" ] ; then
echo "UNIX user not specified" 1>&2
else
add_service "$user"
selinux_module
fi
;;
--del-service)
del_service
;;
--selinux)
selinux_module
;;
*)
echo "invalid option [$1]"
read key
usage 1
;;
esac
shift
done
#
# $Log: create_service.in,v $
# Revision 1.29 2023-06-30 10:16:44+05:30 Cprogrammer
# added --lcdfifo argument to create LCD_FIFO env variable
#
# Revision 1.28 2023-06-26 23:17:24+05:30 Cprogrammer
# added --lcdhost, --lcdport to add LCD_HOST, LCD_PORT env variables
#
# Revision 1.27 2023-06-25 11:15:13+05:30 Cprogrammer
# added lcd_display script
#
# Revision 1.26 2023-05-18 08:49:31+05:30 Cprogrammer
# added selinux module
#
# Revision 1.25 2022-11-26 20:40:05+05:30 Cprogrammer
# added supplementary group when running mpdev
#
# Revision 1.24 2022-06-24 09:30:12+05:30 Cprogrammer
# use relative path for variables directory
#
# Revision 1.23 2022-06-23 21:56:43+05:30 Cprogrammer
# added lastfm-scrobbler --stop, librefm-scrobbler --stop for cleanup
#
# Revision 1.22 2022-06-23 09:05:36+05:30 Cprogrammer
# Mac OSX port
#
# Revision 1.21 2022-06-20 01:00:29+05:30 Cprogrammer
# use directories set by ./configure
#
# Revision 1.20 2021-09-26 02:57:43+05:30 Cprogrammer
# change ownership of $HOME/.mpdev to uid 1000
#
# Revision 1.19 2021-09-09 17:10:03+05:30 Cprogrammer
# added mixer script
#
# Revision 1.18 2021-09-09 12:17:03+05:30 Cprogrammer
# added script output
#
# Revision 1.17 2021-04-25 10:22:15+05:30 Cprogrammer
# remove cleanup of /tmp/mpdev as purpose is solved by having it as TMPFS
#
# Revision 1.16 2021-04-19 21:16:09+05:30 Cprogrammer
# added MPDEV_TMPDIR variable
#
# Revision 1.15 2021-04-15 12:26:20+05:30 Cprogrammer
# fixed cleanup of /tmp/mpdev
#
# Revision 1.14 2021-04-14 21:49:57+05:30 Cprogrammer
# do cleanup only once
#
# Revision 1.13 2021-04-14 13:19:21+05:30 Cprogrammer
# fixed .options
#
# Revision 1.12 2021-04-14 09:45:58+05:30 Cprogrammer
# update variables only if changed
#
# Revision 1.11 2021-04-10 19:13:30+05:30 Cprogrammer
# create variables/.options file
#
# Revision 1.10 2021-02-25 15:40:13+05:30 Cprogrammer
# use relative path for variables to allow renaming of service directory
#
# Revision 1.9 2021-02-25 15:13:08+05:30 Cprogrammer
# added cleanup of /tmp/mpdev and logs in scrobbles directory
#
# Revision 1.8 2021-01-07 12:20:08+05:30 Cprogrammer
# fixed copy
#
# Revision 1.7 2020-08-30 10:56:28+05:30 Cprogrammer
# update player, playpause script on startup
#
# Revision 1.6 2020-08-18 08:20:14+05:30 Cprogrammer
# copy playpause script
#
# Revision 1.5 2020-08-17 22:15:20+05:30 Cprogrammer
# set default rating to 0
#
# Revision 1.4 2020-07-23 10:47:04+05:30 Cprogrammer
# fixed path variable
#
# Revision 1.3 2020-07-23 01:48:43+05:30 Cprogrammer
# fix for raspberry pi, dietpi
#
# Revision 1.2 2020-07-22 15:51:03+05:30 Cprogrammer
# added AUTO_RATING env variable
#
# Revision 1.1 2020-07-19 18:17:00+05:30 Cprogrammer
# Initial revision
#