-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from thin-edge/add-missing-sysvinit-yocto-defi…
…nitions fix(sysvinit-yocto): add missing configuration and log service definitions
- Loading branch information
Showing
3 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
services/sysvinit-yocto/init.d/tedge-configuration-plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#!/bin/sh | ||
### BEGIN INIT INFO | ||
# Provides: tedge-configuration-plugin | ||
# Required-Start: $remote_fs $syslog | ||
# Required-Stop: $remote_fs $syslog | ||
# Default-Start: 2 3 4 5 | ||
# Default-Stop: 0 1 6 | ||
# Short-Description: Thin-edge device configuration management | ||
# Description: Thin-edge device configuration management | ||
### END INIT INFO | ||
|
||
dir="/var" | ||
DAEMON="/usr/bin/tedge-configuration-plugin" | ||
DAEMON_USER="root" | ||
DAEMON_ARGS="" | ||
|
||
name="tedge-configuration-plugin" | ||
PIDFILE=/run/lock/$name.lock | ||
stdout_log="/var/log/$name.log" | ||
stderr_log="/var/log/$name.err" | ||
|
||
user_exists() { | ||
if command -V getent > /dev/null 2>&1; then | ||
getent passwd "$1" >/dev/null | ||
else | ||
# Fallback to plain grep, as busybox does not have getent | ||
grep -q "^${1}:" /etc/passwd | ||
fi | ||
} | ||
|
||
# Check if sudo or su is installed, otherwise don't use it | ||
SUDO="" | ||
if [ -n "$DAEMON_USER" ]; then | ||
if user_exists "$DAEMON_USER"; then | ||
if command -V sudo >/dev/null 2>&1; then | ||
SUDO="sudo" | ||
if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then | ||
SUDO="sudo -u $DAEMON_USER" | ||
fi | ||
elif command -V runuser >/dev/null 2>&1; then | ||
SUDO="runuser" | ||
if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then | ||
SUDO="runuser -u $DAEMON_USER --" | ||
fi | ||
elif command -V su >/dev/null 2>&1; then | ||
# Note: using su requires a user which can login | ||
# su -s /bin/sh -c '$DAEMON $DAEMON_AGS' $DAEMON_USER | ||
SUDO="su" | ||
if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then | ||
SUDO="su - $DAEMON_USER" | ||
fi | ||
fi | ||
else | ||
echo "WARNING: Daemon user does not exist, so starting the service as current user. DAEMON_USER=$DAEMON_USER" | ||
fi | ||
fi | ||
|
||
get_pid() { cat "$PIDFILE"; } | ||
|
||
is_running() { | ||
# shellcheck disable=SC2009 | ||
# Use ps/grep fallback as busybox does not support the "ps -p" option | ||
if command -V pidof >/dev/null 2>&1; then | ||
pidof tedge-configuration-plugin >/dev/null | ||
else | ||
PROCESSES=$(ps -x || ps) | ||
[ -f "$PIDFILE" ] && (echo "$PROCESSES" | grep "^[[:blank:]]*$(get_pid)" >/dev/null 2>&1) | ||
fi | ||
} | ||
|
||
case "$1" in | ||
start) | ||
if is_running; then | ||
echo "Already started" | ||
else | ||
echo "Starting $name (using '$SUDO $DAEMON $DAEMON_ARGS')" | ||
cd "$dir" || (echo "Failed changing directory"; exit 1) | ||
$SUDO $DAEMON $DAEMON_ARGS >> "$stdout_log" 2>> "$stderr_log" & | ||
# TODO: is it ok to let the process create the pidfile itself? | ||
# echo $! > "$PIDFILE" | ||
|
||
i=10 | ||
printf "Waiting for %s.." "$name" | ||
while [ $i -gt 0 ]; do | ||
if is_running; then | ||
break | ||
fi | ||
printf "." | ||
i=$((i-1)) | ||
sleep 1 | ||
done | ||
|
||
if ! is_running; then | ||
echo "Unable to start, see $stdout_log and $stderr_log" | ||
exit 1 | ||
else | ||
echo "started" | ||
fi | ||
fi | ||
;; | ||
stop) | ||
if is_running; then | ||
printf "Stopping (pid=%s) %s.." "$(get_pid)" "$name" | ||
kill "$(get_pid)" | ||
i=30 | ||
while [ $i -gt 0 ]; do | ||
if ! is_running; then | ||
break | ||
fi | ||
printf "." | ||
i=$((i-1)) | ||
sleep 1 | ||
done | ||
echo | ||
|
||
if is_running; then | ||
echo "Not stopped; may still be shutting down or shutdown may have failed" | ||
exit 1 | ||
else | ||
echo "stopped" | ||
if [ -f "$PIDFILE" ]; then | ||
rm -f "$PIDFILE" | ||
fi | ||
fi | ||
else | ||
echo "Not running" | ||
fi | ||
;; | ||
# reload) | ||
# if is_running; then | ||
# echo "Reloading configuration" | ||
# kill -HUP "$(get_pid)" | ||
# else | ||
# echo "Not running" | ||
# fi | ||
# ;; | ||
restart) | ||
"$0" stop | ||
if is_running; then | ||
echo "Unable to stop, will not attempt to start" | ||
exit 1 | ||
fi | ||
"$0" start | ||
;; | ||
status) | ||
if is_running; then | ||
echo "Running" | ||
else | ||
echo "Stopped" | ||
exit 1 | ||
fi | ||
;; | ||
*) | ||
echo "Usage: $0 {start|stop|restart|status}" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters