From fbbb2d0b2aba4ddc42651096b20dd4a7ca4a33f2 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Wed, 21 Sep 2022 23:04:57 +0800
Subject: [PATCH] Merge branch 'master' of ssh://master.iot-yun.club:2280/raspberrypi

---
 iotd/lylib/util_proc.c |   94 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 74 insertions(+), 20 deletions(-)

diff --git a/iotd/lylib/util_proc.c b/iotd/lylib/util_proc.c
index a1af244..700bb65 100644
--- a/iotd/lylib/util_proc.c
+++ b/iotd/lylib/util_proc.c
@@ -97,7 +97,7 @@
  * *****************************************************************************/
 void daemonize(int nochdir, int noclose)
 { 
-    int retval, fd; 
+    int rv, fd; 
     int i; 
     
     /*  already a daemon */ 
@@ -105,11 +105,11 @@
         return; 
     
     /*  fork error */
-    retval = fork(); 
-    if (retval < 0) exit(1); 
+    rv = fork(); 
+    if (rv < 0) exit(1); 
     
     /*  parent process exit */
-    if (retval > 0)
+    if (rv > 0)
         exit(0); 
     
     /*  obtain a new process session group */
@@ -141,6 +141,49 @@
     
     return;
 }
+
+/* ****************************************************************************
+ * FunctionName: check_set_program_running
+ * Description : check program already running or not, if not then run it and 
+ *               record pid into $pidfile
+ * Inputs      : daemon:  set program running in daemon or not
+ *               pid_file:The record PID file path
+ * Output      : NONE
+ * Return      : 0: Record successfully  Else: Failure
+ * *****************************************************************************/
+
+int check_set_program_running(int daemon, char *pidfile)
+{
+    if( !pidfile )
+        return 0;
+
+    if( check_daemon_running(pidfile) ) 
+    {
+        log_err("Program already running, process exit now");
+        return -1;
+    }
+
+    if( daemon ) 
+    {
+        if( set_daemon_running(pidfile) < 0 )
+        {
+            log_err("set program running as daemon failure\n");
+            return -2;
+        }
+    }
+    else
+    {
+        if( record_daemon_pid(pidfile) < 0 )
+        {
+            log_err("record program running PID failure\n");
+            return -3;
+        }
+    }
+
+    return 0;
+}
+
+
 
 /* ****************************************************************************
  * FunctionName: record_daemon_pid
@@ -229,11 +272,11 @@
  * *****************************************************************************/
 int check_daemon_running(const char *pid_file)
 {
-    int retVal = -1; 
+    int rv = -1; 
     struct stat fStatBuf;
 
-    retVal = stat(pid_file, &fStatBuf); 
-    if (0 == retVal) 
+    rv = stat(pid_file, &fStatBuf); 
+    if (0 == rv) 
     { 
         pid_t pid = -1; 
         printf("PID record file \"%s\" exist.\n", pid_file);
@@ -241,7 +284,7 @@
         pid = get_daemon_pid(pid_file);
         if (pid > 0)  /*  Process pid exist */
         { 
-            if ((retVal = kill(pid, 0)) == 0) 
+            if ((rv = kill(pid, 0)) == 0) 
             { 
                 printf("Program with PID[%d] seems running.\n", pid); 
                 return 1; 
@@ -327,34 +370,45 @@
 /* start a new thread to run $thread_workbody point function  */
 int thread_start(pthread_t *thread_id, thread_body_t thread_workbody, void *thread_arg)
 {
-    int        retval = 0;
+    int                rv = 0;
+    pthread_t          tid;
 
-    pthread_attr_t thread_attr; 
+    pthread_attr_t     thread_attr; 
 
     /* Initialize the thread  attribute */
-    retval = pthread_attr_init(&thread_attr); 
-    if(retval)
+    rv = pthread_attr_init(&thread_attr); 
+    if(rv)
         return -1;
 
     /* Set the stack size of the thread */
-    retval = pthread_attr_setstacksize(&thread_attr, 120 * 1024); 
-    if(retval)
+    rv = pthread_attr_setstacksize(&thread_attr, 120 * 1024); 
+    if(rv)
         goto CleanUp;
     
     /* Set thread to detached state:Don`t need pthread_join */
-    retval = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); 
-    if(retval)
+    rv = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); 
+    if(rv)
         goto CleanUp;
     
     /* Create the thread */
-    retval = pthread_create(thread_id, &thread_attr, thread_workbody, thread_arg);
-    if(retval)
+    rv = pthread_create(&tid, &thread_attr, thread_workbody, thread_arg);
+    if(rv)
         goto CleanUp;
-    
+
 CleanUp:
+
+
+    if( thread_id )
+    {
+        if( rv )
+            *thread_id = 0;
+        else
+            *thread_id = tid;
+    }
+
     /* Destroy the  attributes  of  thread */
     pthread_attr_destroy(&thread_attr); 
-    return retval;
+    return rv;
 }
 
 

--
Gitblit v1.9.1