-
Notifications
You must be signed in to change notification settings - Fork 1
/
fission
executable file
·622 lines (508 loc) · 18.1 KB
/
fission
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env sh
### config
FISSION_BINARY=$(readlink -f "${0}")
FISSION_PATH=$(dirname "${FISSION_BINARY}")
FISSION_CALL=$(basename "${0}")
#FISSION_VERBOSE="false"
#FISSION_SHELL=""
__FISSION_SILENT="false"
__FISSION_FILE="/etc/fission/fission.json"
__FISSION_OVERLAY_DIR="/etc/fission/overlays"
__FISSION_RUNSVDIR="/etc/service"
__FISSION_LOGDIR="/var/log"
__FISSION_RUNSVDIR_CMD="setsid runsvdir -P ${__FISSION_RUNSVDIR}"
__FISSION_PID1_CMD="tini -g --"
__FISSION_PID1_CMD_DOCKER_INIT="docker-init -s -g --"
__FISSION_SIGKILL_TIMEOUT=5
__FISSION_SILENT_REDIRECT=""
__FISSION_STDERR="true"
__FISSION_STDERR_TARGET="/proc/1/fd/2"
__FISSION_LOG_TARGET="/proc/1/fd/1"
### MULTICALL SCRIPT
# -> see end of file
__FISSION_MULTICALL_ALIASES="fission-spawn fission-run fission-runsv fission-auxmain fission-getconf"
### helpers/methods
log()
{
if ! ${__FISSION_SILENT}; then
printf "%s\n" "$@" | awk '$0="> "$0' >${__FISSION_LOG_TARGET}
fi
}
log_verbose()
{
if ${FISSION_VERBOSE}; then
printf "%s\n" "$@" | awk '$0="# "$0' >${__FISSION_LOG_TARGET}
fi
}
log_multicall()
{
log_verbose "called as ${FISSION_CALL}"
}
_passback()
{
while [ 0 -lt $# ]; do
printf '%s='"'"'%s'"';" "$1" "$(eval echo '${'${1}'}')"
shift
done
}
set_shell_opts()
{
# disable job control and notification
set +o monitor
set +o notify
}
common_config()
{
# verbosity
if [ "${FISSION_VERBOSE}" = "" ]; then
FISSION_VERBOSE="false"
fi
# silent
__FISSION_SILENT=$(echo "${FISSION_CONFIG}" | jq '.silent // false')
# disable silence when verbose
if ${FISSION_VERBOSE}; then
__FISSION_SILENT="false"
fi
# redirect all command output on silence
if ${__FISSION_SILENT}; then
__FISSION_SILENT_REDIRECT=">/dev/null"
fi
# stderr forwarding
__FISSION_STDERR=$(echo "${FISSION_CONFIG}" | jq -e 'if has("stderr") then .stderr else true end')
## program options
# awk
__FISSION_AWK_OPTIONS=""
if [ -z "$(awk -W interactive '' 2>&1)" ]; then
__FISSION_AWK_OPTIONS="-W interactive"
fi
__FISSION_AWK_FLUSH=""
if [ -z "$(echo 'test awk' | awk '$1; fflush()' | uniq -d)" ]; then
__FISSION_AWK_FLUSH='; fflush()'
fi
log_multicall
log_verbose "common config: $(_passback __FISSION_AWK_FLUSH __FISSION_AWK_OPTIONS __FISSION_STDERR __FISSION_SILENT FISSION_VERBOSE)"
_passback __FISSION_AWK_FLUSH __FISSION_AWK_OPTIONS __FISSION_STDERR __FISSION_SILENT FISSION_VERBOSE
}
## supervision helpers
proc_group_childs()
{
ps -e -o pgid,pid 2>/dev/null | awk -v pgid=${1} '$1 == pgid {print $2}' | tr '\n' ' '
}
proc_childs()
{
ps -e -o ppid,pid 2>/dev/null | awk -v ppid=${1} '$1 == ppid {print $2}' | tr '\n' ' '
}
# clean background jobs, but send SIGKILL
# send SIGKILL to all pids passed as parameter
killup()
{
if [ "${1}" == "--ec" ]; then
shift
EXIT_CODE=${1}
shift
fi
PIDS=""
for PID in ${@}; do
if [ -d /proc/${PID} ]; then
PIDS="${PIDS} ${PID}"
fi
done
if [ ! -z "${PIDS}" ]; then
log "escalating to SIGKILL after ${__FISSION_SIGKILL_TIMEOUT}s for pids: ${PIDS}"
echo ${PIDS} | tr ' ' '\n' | xargs -i kill -KILL {} &>/dev/null
fi
if [ ! -z "${EXIT_CODE}" ]; then
exit ${EXIT_CODE}
fi
}
# schedule SIGKILL for pids
schedule_killup()
{
if [ "${1}" == "--ec" ]; then
log_verbose "scheduling SIGKILL with final exit code: ${2}"
__EXIT_CODE_PARAM="--ec ${2}"
shift; shift
fi
log_verbose "scheduling SIGKILL for pids: $(echo ${@})"
( (sleep ${__FISSION_SIGKILL_TIMEOUT}; killup ${__EXIT_CODE_PARAM} ${@}) & )
unset __EXIT_CODE_PARAM
}
# cleanup helper/signal-handler to terminate (SIGTERM) jobs of current shell + escalation to SIGKILL after timeout
cleanup()
{
# wait for output stream sync from prior jobs
sleep 0.1
# get running jobs
jobs &>/dev/null
PIDS=$(jobs -p 2>/dev/null)
if [ ! -z "${PIDS}" ]; then
# terminate background jobs (using xargs prevents calling kill without argument)
echo ${PIDS} | tr ' ' '\n' | xargs -i kill -TERM {} &>/dev/null
# schedule SIGKILL handler in background with delay
schedule_killup $( [ ! -z "${1}" ] && echo "--ec ${1}" ) ${PIDS}
log_verbose "waiting for response to SIGTERM from pids: $(echo ${PIDS})"
wait $(echo ${PIDS}) &>/dev/null
# wait for output stream sync from prior jobs
sleep 0.1
if [ ! -z "${1}" ]; then
log "forwarding exit code: ${1}"
exit ${1}
fi
fi
}
# rewrite signals to terminate runsvdir
runsvdir_term()
{
# suppress job output
jobs 2>/dev/null &>/dev/null
RUNSVDIR_PID="$(ps -e -o pid,comm | awk '/runsvdir/ { print $1 }')"
if [ ! -z "${RUNSVDIR_PID}" ]; then
kill -HUP ${RUNSVDIR_PID} &>/dev/null
wait ${RUNSVDIR_PID} $(proc_childs ${RUNSVDIR_PID}) &>/dev/null
fi
}
### MULTICALL FUNCTIONS
## get config as json string
get_conf()
{
LC_COLLATE=C
__FISSION_OVERLAYS="$(if [ -d ${__FISSION_OVERLAY_DIR} ]; then echo ${__FISSION_OVERLAY_DIR}/*; fi)"
### fetch fission config
# build config from overlays
# jq -rs 'reduce .[] as $item ({}; . * $item) | with_entries(.value = (.value | with_entries(select( .value != null ))))' ${__FISSION_FILE} ${__FISSION_OVERLAYS}
# pre jq 1.6 compatible
jq -rs 'reduce .[] as $item ({}; . * $item) | walk( if type == "object" then with_entries(select(.value != null)) else . end)' ${__FISSION_FILE} ${__FISSION_OVERLAYS}
}
## INIT STEP 1
# check usage
# build/get config
# replace pid1
setup_init() #${@}
{
# check arguments
__FISSION_USAGE="usage: fission command [options] [-- command2 [options]]"
if [ ${#} -eq 0 ]; then
printf "%s\n" "${__FISSION_USAGE}"
exit 1
fi
if [ "${1}" = "--" ]; then
printf "%s\n" "${__FISSION_USAGE}"
exit 1
fi
# link multicall aliases
for __FISSION_ALIAS in ${__FISSION_MULTICALL_ALIASES}; do
ln -s ${FISSION_BINARY} ${FISSION_PATH}/${__FISSION_ALIAS}
done
# update path to use local binaries
export PATH=${FISSION_PATH}:${PATH}
# check fission.json exists
if [ ! -f "${__FISSION_FILE}" ] ; then
printf "configuration file %s missing\n" ${__FISSION_FILE}
exit 1
fi
# test fission.json and overlays
LC_COLLATE=C
__FISSION_OVERLAYS="$(if [ -d ${__FISSION_OVERLAY_DIR} ]; then echo ${__FISSION_OVERLAY_DIR}/*; fi)"
for __FISSION_CONFIG_FILE in ${__FISSION_FILE} ${__FISSION_OVERLAYS}; do
if ! jq '.' "${__FISSION_CONFIG_FILE}" > /dev/null; then
printf "cannot parse %s; review jq error output above\n" ${__FISSION_CONFIG_FILE}
exit 1
fi
if [ "$(jq -r 'type' ${__FISSION_CONFIG_FILE})" != "object" ]; then
printf "configuration file %s is not an object\n" ${__FISSION_CONFIG_FILE}
exit 1
fi
done
### fetch fission config
export FISSION_CONFIG="$(${FISSION_SHELL} ${FISSION_PATH}/fission-getconf)"
# setup common configuration variables
eval $(common_config)
## logging
# postponed logging after config has been read to respect silence flag
log_verbose "silent: ${__FISSION_SILENT}"
log_verbose "stderr: ${__FISSION_STDERR}"
log_verbose "using config:"
log_verbose "$(echo ${FISSION_CONFIG} | jq -C .)"
log_verbose "created multicall aliases: ${__FISSION_MULTICALL_ALIASES}"
log "updating PATH as ${FISSION_PATH}:"'${PATH}'
### replace PID1
if [ $$ -eq 1 ]; then
# is PID1: replace with init process
log "spawning ${__FISSION_PID1_CMD} as PID1..."
exec ${__FISSION_PID1_CMD} "${FISSION_PATH}/fission-spawn" "${@}"
else
# is not PID1:
# is using docker-init (tinit): respawn with "docker-init -g"
# --> pstree: docker-init (1) -- docker-init -g (2) -- fission-spwan ${@} (3)
if sh -c "ps -e -o pid,comm | awk '"'/1[[:space:]]*docker-init/{di=1} END{ exit !di }'"'"; then
log "detected docker --init - using additional docker-init as group subreaper with PID=2"
exec ${__FISSION_PID1_CMD_DOCKER_INIT} "${FISSION_PATH}/fission-spawn" "${@}"
else
# is using unknown init process: WARN but execute
log "detected unknown init process in PID1; executing nonethless..."
exec /usr/bin/env sh "${FISSION_PATH}/fission-spawn" "${@}"
fi
fi
}
## INIT STEP 2
# respawn with shell from /etc/passwd
select_shell()
{
# setup common configuration variables
eval $(common_config)
# respawn with shell from /etc/passwd
if [ -z ${FISSION_SHELL} ]; then
export FISSION_SHELL=$(getent passwd $(whoami) | cut -d ':' -f 7)
log "replacing '/usr/bin/env sh' as executor with ${FISSION_SHELL}..."
fi
exec ${FISSION_SHELL} ${FISSION_PATH}/fission-run "${@}"
}
## INIT STEP 3
# setup environment
# run init scripts
# start services
# run main & aux process(es)
run()
{
# setup common configuration variables
eval $(common_config)
### load env
# TODO: use jq @sh quoting?
log "loading env..."
for __FISSION_ENV_VAR in $(echo "${FISSION_CONFIG}" | jq -r '.env // empty | keys | .[]'); do
__FISSION_ENV_VAR_VAL=$(echo "${FISSION_CONFIG}" | jq -r ".env.\"${__FISSION_ENV_VAR}\"")
log "- ${__FISSION_ENV_VAR}"
log_verbose '`-: '"${__FISSION_ENV_VAR_VAL}"
export ${__FISSION_ENV_VAR}="${__FISSION_ENV_VAR_VAL}"
unset __FISSION_ENV_VAR_VAL
done
### source init scripts
log "running init tasks..."
for __FISSION_INIT in $(echo "${FISSION_CONFIG}" | jq -rS '.init // empty | keys | .[]'); do
__FISSION_INIT_TARGET=$(echo "${FISSION_CONFIG}" | jq -r ".init.\"${__FISSION_INIT}\"")
log_verbose "${__FISSION_INIT}"
log_verbose "${__FISSION_INIT_TARGET}"
if [ -e ${__FISSION_INIT_TARGET} -o "${__FISSION_INIT_TARGET}" = "" ]; then
if [ -d ${__FISSION_INIT_TARGET} ]; then
# is directory
log "+ ${__FISSION_INIT_TARGET}"
# execute in directory
__PWD_CACHE="${PWD}"
log_verbose "executing in ${__FISSION_INIT_TARGET}"
# pushd
cd "${__FISSION_INIT_TARGET}"
for __FISSION_INIT_TARGET_SCRIPT in $(find -L ${__FISSION_INIT_TARGET} -type f -executable -name '*' | sort -f); do
if [ -e "${__FISSION_INIT_TARGET_SCRIPT}" ] ; then
log "|- ${__FISSION_INIT_TARGET_SCRIPT}"
. "${__FISSION_INIT_TARGET_SCRIPT}" ${__FISSION_SILENT_REDIRECT}
fi
done
# popd
cd "${__PWD_CACHE}"
else
# is file
log "- ${__FISSION_INIT_TARGET}"
# execute in directory
# pushd
__PWD_CACHE="${PWD}"
__FISSION_INIT_TARGET_DIR="$(dirname ${__FISSION_INIT_TARGET})"
log_verbose "executing in ${__FISSION_INIT_TARGET_DIR}"
cd "${__FISSION_INIT_TARGET_DIR}"
. "${__FISSION_INIT_TARGET}" ${__FISSION_SILENT_REDIRECT}
# popd
cd "${__PWD_CACHE}"
fi
else
log "- [ERR] ${__FISSION_INIT_TARGET} does not exist!"
fi
unset __FISSION_INIT_TARGET
done
### create services & logging
# logging with stdout and stderr to file + stderr to screen
log "creating services..."
mkdir -p ${__FISSION_RUNSVDIR}
for __FISSION_SERVICE in $(echo "${FISSION_CONFIG}" | jq -r '.services // empty | keys | .[]'); do
# log "creating service: ${__FISSION_SERVICE}"
log "- ${__FISSION_SERVICE}"
__FISSION_SERVICE_CMDL=$(echo "${FISSION_CONFIG}" | jq -r ".services.\"${__FISSION_SERVICE}\"")
log_verbose '`-: '"${__FISSION_SERVICE_CMDL}"
mkdir -p "${__FISSION_RUNSVDIR}/${__FISSION_SERVICE}/log"
# (re)create service file
read -r -d '' __SV_SCRIPT <<-EOF
#!${FISSION_SHELL}
exec ${FISSION_SHELL} ${FISSION_PATH}/fission-runsv ${__FISSION_SERVICE_CMDL}
EOF
__SV_SCRIPT=${__SV_SCRIPT}
echo "${__SV_SCRIPT}" > "${__FISSION_RUNSVDIR}/${__FISSION_SERVICE}/run"
chmod +x "${__FISSION_RUNSVDIR}/${__FISSION_SERVICE}/run"
# (re)create logger
read -r -d '' __SV_LOG_SCRIPT <<-EOF
#!${FISSION_SHELL}
exec 2>${__FISSION_STDERR_TARGET}
# will be killed by broken pipes?
trap ' ' SIGINT SIGTERM
setsid svlogd -tt ${__FISSION_LOGDIR}/${__FISSION_SERVICE} 2>&1 | setsid awk ${__FISSION_AWK_OPTIONS} -v name="${__FISSION_SERVICE}" '{\$1=""; \$2=""; sub(/^\s*/, "[" name "] "); print ${__FISSION_AWK_FLUSH}}' >&2
#> ${__FISSION_STDERR_TARGET}
EOF
__SV_LOG_SCRIPT=${__SV_LOG_SCRIPT}
echo "${__SV_LOG_SCRIPT}" > "${__FISSION_RUNSVDIR}/${__FISSION_SERVICE}/log/run"
chmod +x "${__FISSION_RUNSVDIR}/${__FISSION_SERVICE}/log/run"
mkdir -p "${__FISSION_LOGDIR}/${__FISSION_SERVICE}"
# enable forwarding
if ${__FISSION_STDERR}; then
echo 'eE:*' > ${__FISSION_LOGDIR}/${__FISSION_SERVICE}/config
fi
unset __FISSION_SERVICE_CMDL
unset __SV_SCRIPT
unset __SV_LOG_SCRIPT
done
### run services
# runsvdir_term will be called from trap to rewrite SIGTERM to SIGHUP for runsvdir
log "starting services..."
${__FISSION_RUNSVDIR_CMD} ${__FISSION_SILENT_REDIRECT} &
# wait to start runsvdir
# TODO: find better solution
sleep 2
# wait for services
log "waiting for services..."
for __FISSION_SERVICE in $(ls ${__FISSION_RUNSVDIR}); do
# wait 30 seconds for service to come up
sv -w 30 check ${__FISSION_RUNSVDIR}/${__FISSION_SERVICE} &>/dev/null
if [ $? -ne 0 ]; then
log "service ${__FISSION_SERVICE} not starting up! exiting"
exit 1
fi
log_verbose "service ${__FISSION_SERVICE} up"
done
### exec
# install trap to ignore signals from init:
# all subprocesses should receive a sigint individually by using tini -g (group) option
trap 'runsvdir_term; cleanup' SIGINT SIGTERM
# build primary command till '--' or end of options
while [ ${#} -ne 0 ] && [ "${1}" != "--" ]; do
__FISSION_PRIMARY="${__FISSION_PRIMARY} \"${1}\""
shift
done
log_verbose "main cmdl: ${__FISSION_PRIMARY}"
# has an auxilliary command after -- ?
if [ "${1}" = "--" ] && [ ${#} -gt 1 ]; then
# run primary command in background
log "starting main process in background [logging to ${__FISSION_LOGDIR}/app]..."
${FISSION_SHELL} ${FISSION_PATH}/fission-auxmain "${__FISSION_PRIMARY}" &
shift
# build auxilliary command
# __FISSION_AUXILLIARY=${@}
while [ ${#} -ne 0 ]; do
__FISSION_AUXILLIARY="${__FISSION_AUXILLIARY} \"${1}\""
shift
done
log_verbose "aux cmdl: ${__FISSION_AUXILLIARY}"
# wait for main process (keep logging in order)
sleep 0.5
# run aux process
log "starting aux process..."
eval ${__FISSION_AUXILLIARY}
__FISSION_EXIT_CODE=$?
else
# run primary command in foreground and replace process
log "starting main process..."
# TODO: remove shell call
eval ${__FISSION_PRIMARY}
__FISSION_EXIT_CODE=$?
fi
# # send sigterm and/or wait for main process
log "foreground process exited sending SIGTERM and waiting for services and background operations..."
# shutdown
runsvdir_term
cleanup ${__FISSION_EXIT_CODE}
}
### log/stderr redirection
# will both run command:
# - in new backgrounded shell
# - shell as new process group leader
# -> allows signal forwarding as this process and handlers/traps stay active
# -> allows to forwarding signals to all child processes (including tee) by group
## run command and handle stderr redirection
# TODO: remove additional shell call?
runsv_redirect()
{
eval $(common_config)
read -r -d '' __SV_CMDLINE <<-EOF
{ { exec ${@} | setsid awk ${__FISSION_AWK_OPTIONS} '\$0="O: "\$0 ${__FISSION_AWK_FLUSH}' >&127; } 2>&1 | setsid awk ${__FISSION_AWK_OPTIONS} '\$0="E: "\$0 ${__FISSION_AWK_FLUSH}' >&127 ; } 127>&1
EOF
__SV_CMDLINE=${__SV_CMDLINE}
${FISSION_SHELL} -c "${__SV_CMDLINE}" &
# get group id
__PROC_GROUP_LEADER_PID=$!
__PROC_GROUP_ID=$(cat /proc/${__PROC_GROUP_LEADER_PID}/stat | cut -d ' ' -f 5)
__PROC_GROUP_CHILD_PIDS="$(proc_group_childs ${__PROC_GROUP_ID})"
# install trap
# - make SIGINT and SIGTERM send SIGTERM to child pgroup
# - keeps tee running; will exit on broken pipe
trap "kill -- -${__PROC_GROUP_ID} &>/dev/null; wait $(proc_group_childs ${__PROC_GROUP_ID}) &>/dev/null; cleanup" SIGINT SIGTERM
# wait for command to finish (keep process running and forward signals)
wait $(proc_group_childs ${__PROC_GROUP_ID}) &>/dev/null
wait &>/dev/null
cleanup
}
## run main command and handle stderr redirection in presence of auxilliary command
main_logger()
{
eval $(common_config)
__MAIN_LOGGER_NAME="main"
mkdir -p ${__FISSION_LOGDIR}/app
# enable forwarding
if ${__FISSION_STDERR}; then
echo 'eE:*' > ${__FISSION_LOGDIR}/app/config
fi
# create FIFO to be used as "anonymous pipe"
# attach to file descriptor
# remove fifo link as cleanup step
# file descriptor will stay open as long as the shell
#------------------------------------------------------
__PIPE=/tmp/$(mktemp -u fifo.XXXXXX)
mkfifo ${__PIPE}
exec 127<>${__PIPE}
# rm ${__PIPE}
#------------------------------------------------------
set_shell_opts
read -r -d '' __MAIN_CMDLINE <<-EOF
{ exec ${@} | setsid awk ${__FISSION_AWK_OPTIONS} '\$0="O: "\$0 ${__FISSION_AWK_FLUSH}' >&127; } 2>&1 | setsid awk ${__FISSION_AWK_OPTIONS} '\$0="E: "\$0 ${__FISSION_AWK_FLUSH}' >&127
EOF
__MAIN_CMDLINE=${__MAIN_CMDLINE}
setsid ${FISSION_SHELL} -c "trap ' ' SIGINT SIGTERM; ${__MAIN_CMDLINE}" &
# get group id
__PROC_GROUP_LEADER_PID=$!
__PROC_GROUP_ID=$(cat /proc/${__PROC_GROUP_LEADER_PID}/stat | cut -d ' ' -f 5)
__PROC_GROUP_CHILD_PIDS="$(proc_group_childs ${__PROC_GROUP_ID})"
log_verbose "main process running in background shell with pid: ${__PROC_GROUP_LEADER_PID}; pgid:${__PROC_GROUP_ID}; child pids: ${__PROC_GROUP_CHILD_PIDS}"
# attach logger
read -r -d '' __LOGGER_CMDLINE <<-EOF
exec 2>${__FISSION_STDERR_TARGET}
svlogd -tt ${__FISSION_LOGDIR}/app 2>&1 | awk ${__FISSION_AWK_OPTIONS} -v name="${__MAIN_LOGGER_NAME}" '{\$1=""; \$2=""; sub(/^\s*/, "[" name "] "); print ${__FISSION_AWK_FLUSH}}' >&2
#> ${__FISSION_STDERR_TARGET}
EOF
__LOGGER_CMDLINE=${__LOGGER_CMDLINE}
setsid ${FISSION_SHELL} -c "${__LOGGER_CMDLINE}" <${__PIPE} &
# install trap
# - make SIGINT and SIGTERM send SIGTERM to child pgroup
# - keeps tee running; will exit on broken pipe
# - keeps svlogd running; will be terminated after succesfull wait on group leader
trap "trap ' ' SIGINT SIGTERM; log_verbose 'executing background main process signal handler'; kill -- -${__PROC_GROUP_ID} &>/dev/null; wait $(proc_group_childs ${__PROC_GROUP_ID}) &>/dev/null; log_verbose 'background main process signal handler waited childs -> cleanup'; cleanup" SIGINT SIGTERM
# wait for command to finish
wait $(proc_group_childs ${__PROC_GROUP_ID}) &>/dev/null
# terminate remaining background commands (svlogd)
cleanup
}
### MAIN
set_shell_opts
### multicall selector
case "${FISSION_CALL}" in
"fission") setup_init "${@}" ;;
"fission-spawn") select_shell "${@}" ;;
"fission-run") run "${@}" ;;
"fission-runsv") runsv_redirect "${@}" ;;
"fission-auxmain") main_logger "${@}" ;;
"fission-getconf") get_conf "${@}" ;;
*) echo "fission called as unknown command" >&2; exit 1;;
esac