xref: /OK3568_Linux_fs/external/rkscript/bootanim (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/sh
2#
3# Usage:
4# bootanim start [timeout]
5# bootanim stop
6#
7# Example hook:
8# root@RK3588:/# more /etc/bootanim.d/gst-bootanim.sh
9# #!/bin/sh
10# touch ${TAG_FILE:-/dev/null}
11# gst-play-1.0 /etc/bootanim.d/bootanim.mp4 -q --no-interactive&
12
13export TAG_FILE=/tmp/.bootanim
14rm -f $TAG_FILE
15
16SCRIPT=/usr/bin/bootanim
17HOOK_DIR=/etc/bootanim.d/
18PID_FILE=/tmp/bootanim.pid
19LOG_FILE=/tmp/bootanim.log
20TIMEOUT=${2:-${BOOTANIM_DEFAULT_TIMEOUT:-5}}
21
22pid_by_sid()
23{
24	sed "s/(.*)//" /proc/*/stat | cut -d' ' -f1,6 | grep -w "$1$" |
25		cut -d' ' -f1 || true
26}
27
28sid_by_pid()
29{
30	sed "s/(.*)//" /proc/$1/stat | cut -d' ' -f6
31}
32
33bootanim_init()
34{
35	# Save bootanim's pid
36	echo $$ > $PID_FILE
37
38	# Freeze display service
39	touch /dev/null $XSERVER_FREEZE_DISPLAY $WESTON_FREEZE_DISPLAY
40}
41
42bootanim_deinit()
43{
44	# Unfreeze display service
45	rm -rf $XSERVER_FREEZE_DISPLAY $WESTON_FREEZE_DISPLAY
46
47	rm -rf "$PID_FILE"
48}
49
50bootanim_start()
51{
52	[ -d $HOOK_DIR ] || return
53
54	echo "Starting bootanim..."
55
56	bootanim_init
57
58	# Start bootanim hooks
59	for hook in $(find $HOOK_DIR -maxdepth 1 -name "*.sh"); do
60		echo "Starting hook: $hook..."
61		$hook
62		echo "Started hook: $hook..."
63	done
64
65	if [ ! -e "$TAG_FILE" ]; then
66		echo "No animation started..."
67		bootanim_deinit
68		return
69	fi
70
71	# Timeout killer
72	sleep $TIMEOUT
73	bootanim_stop
74}
75
76children_pid()
77{
78	[ -f $PID_FILE ] || return
79
80	SID=$(cat $PID_FILE)
81	[ "$SID" ] || return
82
83	pid_by_sid $SID | grep -wv $SID || true
84}
85
86bootanim_stop()
87{
88	echo "Stopping bootanim..."
89
90	# Parse children pid
91	CPID=$(children_pid)
92
93	if [ -z "$CPID" ]; then
94		bootanim_deinit
95		return
96	fi
97
98	{
99		# Pause animation -> unfreeze display -> kill animation
100		kill -STOP $CPID &>/dev/null || true
101		bootanim_deinit
102		sleep .1
103		kill -9 $CPID &>/dev/null || true
104	}&
105	wait
106}
107
108case "$1" in
109	start|"")
110		# Make sure that we own this session (pid equals sid)
111		if [ $(sid_by_pid $$) != $$ ] ||
112			[ $(realpath "$0") != $SCRIPT ] ; then
113			setsid $SCRIPT $@
114		else
115			{
116				bootanim_stop || true
117				bootanim_start
118			} 2>&1 | tee -a $LOG_FILE&
119		fi
120		;;
121	stop)
122		bootanim_stop 2>&1 | tee -a $LOG_FILE
123		;;
124	*)
125		echo "Usage: $0 [start|stop]"
126		;;
127esac
128
129:
130