From 691117006ce042c001dcbee3a7ea02f0c9c36675 Mon Sep 17 00:00:00 2001
From: "shuning.han" <shuning.han@sophgo.com>
Date: Thu, 7 Nov 2024 20:19:46 +0800
Subject: [PATCH] update rp scripts

Signed-off-by: shuning.han <shuning.han@sophgo.com>
---
 ramdisk/overlay/rp/etc/init.d/S30rpdaemond | 81 ++++++++++++++++++++++
 ramdisk/overlay/rp/etc/init.d/S60rpenv     | 34 ---------
 2 files changed, 81 insertions(+), 34 deletions(-)
 create mode 100755 ramdisk/overlay/rp/etc/init.d/S30rpdaemond
 delete mode 100644 ramdisk/overlay/rp/etc/init.d/S60rpenv

diff --git a/ramdisk/overlay/rp/etc/init.d/S30rpdaemond b/ramdisk/overlay/rp/etc/init.d/S30rpdaemond
new file mode 100755
index 00000000..70e01839
--- /dev/null
+++ b/ramdisk/overlay/rp/etc/init.d/S30rpdaemond
@@ -0,0 +1,81 @@
+#!/bin/sh
+#
+# Script to load modules, manage, and start rp_daemon
+#
+
+# Set environment variables
+export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/opt/lib
+[ -f /opt/bin/rp_daemon ] || exit 0
+
+umask 077
+
+# Log file path
+LOGFILE="/var/log/rp_daemon.log"
+
+# Function to load a module with a delay
+load_module() {
+    echo "Loading module \$1..." | tee -a $LOGFILE
+    insmod \$1
+    if [ $? -ne 0 ]; then
+        echo "Failed to load module \$1" | tee -a $LOGFILE
+        exit 1
+    fi
+    # Wait for 5 seconds
+    sleep 5
+}
+
+# Function to start rp_daemon
+start() {
+    echo "Starting module loading process..." | tee -a $LOGFILE
+
+    # Load modules with pauses in between
+    load_module /opt/ko/rp_daemon.ko
+    load_module /opt/ko/soph_base.ko
+    load_module /opt/ko/soph_vpss.ko
+    load_module /opt/ko/soph_vc_drv.ko
+
+    # Trigger mdev to populate /dev based on loaded modules
+    echo "Triggering mdev..." | tee -a $LOGFILE
+    mdev -s
+
+    # Start the rp_daemon process
+    echo "Starting rp_daemon..." | tee -a $LOGFILE
+    /opt/bin/rp_daemon &
+    echo $! > /var/run/rp_daemon.pid
+    echo "All processes started successfully." | tee -a $LOGFILE
+}
+
+# Function to stop rp_daemon
+stop() {
+    echo "Stopping rp_daemon..." | tee -a $LOGFILE
+    kill $(cat /var/run/rp_daemon.pid)
+    rm -f /var/run/rp_daemon.pid
+    echo "rp_daemon stopped successfully." | tee -a $LOGFILE
+}
+
+# Function to restart rp_daemon
+restart() {
+    echo "Restarting rp_daemon..." | tee -a $LOGFILE
+    stop
+    sleep 2
+    start
+}
+
+# Handle command line arguments
+case "\$1" in
+    start)
+        start
+        ;;
+    stop)
+        stop
+        ;;
+    restart)
+        restart
+        ;;
+    *)
+        echo "Usage: $0 {start|stop|restart}"
+        exit 1
+        ;;
+esac
+
+exit 0
diff --git a/ramdisk/overlay/rp/etc/init.d/S60rpenv b/ramdisk/overlay/rp/etc/init.d/S60rpenv
deleted file mode 100644
index fbe195b2..00000000
--- a/ramdisk/overlay/rp/etc/init.d/S60rpenv
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/bin/sh
-#
-# Script to load modules and start rp_daemon
-#
-
-# Set environment variables
-export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/opt/lib
-export PATH=$PATH:/opt/bin
-
-# Function to load a module with a delay
-load_module() {
-    echo "Loading module \$1..."
-    insmod \$1
-    # Wait for 5 seconds
-    sleep 5
-}
-
-echo "Starting module loading process..."
-
-# Load modules with pauses in between
-load_module /opt/ko/rp_daemon.ko
-load_module /opt/ko/soph_base.ko
-load_module /opt/ko/soph_vpss.ko
-load_module /opt/ko/soph_vc_drv.ko
-
-# Trigger mdev to populate /dev based on loaded modules
-echo "Triggering mdev..."
-mdev -s
-
-# Start the rp_daemon process
-echo "Starting rp_daemon..."
-rp_daemon
-
-echo "All processes started successfully."