-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhatd
executable file
·107 lines (93 loc) · 2.23 KB
/
hatd
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
#!/usr/bin/env bash
# Wrapper script that starts/stops the hat-daemon
# by calling daemon_front.py/client.py directly
# with proper argument(s).
export PATH='/bin:/usr/bin:/sbin:/usr/sbin'
print_msg() {
printf '%s\n' "$@"
}
PYTHON_BIN="$(command -v python3)"
[ -z "${PYTHON_BIN}" ] && print_msg "hatd requires python3" >&2 \
&& exit 1
HATD_RUN_DIR='/var/run/hatd'
HATD_PID_FILE='/var/run/hatd/hatd.pid'
DAEMON_FRONT='hat/daemon_front.py' # relative
CLIENT='hat/client.py' # relative
# Daemon running->ret=0, Not running->ret=1
check_daemon() {
if [ ! -f "${HATD_PID_FILE}" ]; then
return 1
else
pid="$(cat <"${HATD_PID_FILE}")"
if ! out="$(ps -p "$pid" -o cmd= >/dev/null 2>&1)"; then
return 1
else
case "hat/daemon_front.py" in
*${out}*)
return 0
;;
*)
return 1
;;
esac
fi
fi
}
# Create or unlink necessary files (mostly FIFOs)
mk_rm_files() {
files=(
"${HATD_RUN_DIR}/ipc/runner_in.fifo"
"${HATD_RUN_DIR}/ipc/runner_out.fifo"
"${HATD_RUN_DIR}/ipc/daemon_in.fifo"
"${HATD_RUN_DIR}/ipc/daemon_out.fifo"
"${HATD_PID_FILE}"
)
if [[ $1 == "create" ]]; then
for file in "${files[@]}"; do
rm -f "${file}"
if [[ $file == *_in.fifo ]]; then
mkfifo "${file}" && chmod 0660 "${file}"
elif [[ $file == *_out.fifo ]]; then
mkfifo "${file}" && chmod 0640 "${file}"
else
: >"${file}" && chmod 0644 "${file}"
fi
done
elif [[ $1 == "remove" ]]; then
rm -f "${files[@]}"
rm -f "${HATD_RUN_DIR}"/locks/._*
fi
}
# Create relevant dirs and meke them SETGID
create_setgid_dirs() {
for dir in ipc locks; do
mkdir -p "${HATD_RUN_DIR}/${dir}"
chown :hatd "${HATD_RUN_DIR}/${dir}"
chmod 0770 "${HATD_RUN_DIR}/${dir}"
chmod g+s "${HATD_RUN_DIR}/${dir}"
done
chmod 0755 "${HATD_RUN_DIR}"
}
case "$1" in
"start")
if check_daemon; then
print_msg "Daemon is already running" && exit 0
else
create_setgid_dirs
mk_rm_files "create"
exec "${PYTHON_BIN}" "${DAEMON_FRONT}" start
fi
;;
"stop")
if check_daemon; then
mk_rm_files "remove"
exec "${PYTHON_BIN}" "${CLIENT}" stop_daemon
else
print_msg "Daemon is not running" && exit 0
fi
;;
*)
print_msg "Valid arguments are start/stop" >&2
exit 2
;;
esac