[Pvfs2-cvs] commit by jdenton in pvfs2/src/apps/ucache: ucached.c ucached.h ucached_cmd.c

CVS commit program cvs at parl.clemson.edu
Mon Sep 19 16:56:22 EDT 2011


Update of /projects/cvsroot/pvfs2/src/apps/ucache
In directory parlweb1:/tmp/cvs-serv22529

Modified Files:
      Tag: Orange-Branch
	ucached.c ucached.h ucached_cmd.c 
Log Message:
Changed how ucached and ucached_cmd determine if an instance of the daemon 
    process is already running. Now using popen to call 'ps -e | grep -w ucached'

The daemon now removes leftover FIFOs prior to entering its while loop in case
    the daemon was killed previously (leaving its FIFOs behind)

Now, a response to the cmd caller can be larger than 4096 bytes. since I 
    opened the pipe for non-blocking reads, I can poll until data is sent 
    and continue polling until data is no longer being sent.


Index: ucached.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/apps/ucache/Attic/ucached.c,v
diff -p -u -r1.1.2.1 -r1.1.2.2
--- ucached.c	16 Sep 2011 19:22:01 -0000	1.1.2.1
+++ ucached.c	19 Sep 2011 20:56:21 -0000	1.1.2.2
@@ -1,23 +1,93 @@
 #include "ucached.h"
 #include "shmem_util.h"
 
-/* Globals */
-char buffer[BUFF_SIZE];
-FILE *ucached_log = (FILE *)0;
-/* FIFO File Descriptors */
-int readfd = 0;
-int writefd = 0;
+/* FIFO  */
+static int readfd = 0;  /* Command File Descriptor */
+static int writefd = 0; /* Response File Descriptor */
+static char buffer[BUFF_SIZE]; /* For FIFO reads and writes */
+
+/* Log Globals */
+static FILE *ucached_log = (FILE *)0;
+/* Time Structures For Log */
+static time_t rawtime;
+static struct tm * timeinfo;
 
-time_t rawtime;
-struct tm * timeinfo;
-
-/* Internally Visible Functions */
+/* Forward Function Declarations */
+static int run_as_child(char c, char *str); /* Run as child of ucached */
+static int execute_command(char command, char *str);
 static int create_ucache_shmem();
 static int destroy_ucache_shmem();
-static void print_to_log(char *str);
-static int execute_command(char command, char *str);
-static int run_as_child(char c, char *str);
+static void print_to_log(char *str); /* Logs commands and warnings */
+
+/** Runs the command in a child process */
+static int run_as_child(char c, char *str)
+{
+    pid_t pid;
+    pid = fork();
+    int rc = 0;
+    /* Fork Error? */
+    if(pid < 0)
+    {
+        exit(EXIT_FAILURE);
+    }
+    /* Child Process */
+    else if(pid == 0)
+    {
+        rc = execute_command(c, str);
+        if(rc < 0)
+        {
+            exit(EXIT_FAILURE);
+        }
+        exit(EXIT_SUCCESS);
+    }
+    /* Parent Process */
+    else 
+    {
+        wait(&rc);
+        if(WIFEXITED(rc))
+        {
+            if(WEXITSTATUS(rc) != 0)
+            {
+                return -1;
+            }                  
+        }
+    }
+    return rc;
+}
 
+static int execute_command(char command, char *str)
+{
+    int rc = 0;
+    switch(command)
+    {
+        /* Create the shared memory required by the ucache */
+        case 'c':
+            rc = create_ucache_shmem();
+            break;
+        /* Destroy the shared memory required by the ucache */
+        case 'd':
+            rc = destroy_ucache_shmem();
+            break;
+        /* Close Daemon */
+        case 'x':
+            unlink(FIFO1);
+            unlink(FIFO2);
+            rc = 1;   
+            mywrite(writefd, "SUCCESS\tExiting ucached", buffer);         
+            exit(EXIT_SUCCESS);
+            break;
+        /* Print */
+        case 'p':
+            print_to_log(str);
+            rc = 1;
+            break;
+        default:
+            rc = -1;
+            mywrite(writefd, "FAILURE\tInvalid command character", buffer);
+            break;
+     }
+    return rc;
+}
 
 static int create_ucache_shmem()
 {
@@ -79,76 +149,6 @@ static void print_to_log(char *str)
     fclose(ucached_log);
 }
 
-static int execute_command(char command, char *str)
-{
-    int rc = 0;
-    switch(command)
-    {
-        /* Create the shared memory required by the ucache */
-        case 'c':
-            rc = create_ucache_shmem();
-            break;
-        /* Destroy the shared memory required by the ucache */
-        case 'd':
-            rc = destroy_ucache_shmem();
-            break;
-        /* Close Daemon */
-        case 'x':
-            unlink(FIFO1);
-            unlink(FIFO2);
-            rc = 1;   
-            mywrite(writefd, "SUCCESS\tExiting ucached", buffer);         
-            exit(EXIT_SUCCESS);
-            break;
-        /* Print */
-        case 'p':
-            print_to_log(str);
-            rc = 1;
-            break;
-       default:
-            rc = -1;
-            //exit(EXIT_SUCCESS);
-            break;
-     }
-    return rc;
-}
-
-/** Runs the command in a child process */
-static int run_as_child(char c, char *str)
-{
-    pid_t pid;
-    pid = fork();
-    int rc = 0;
-    /* Fork Error? */
-    if(pid < 0)
-    {
-        exit(EXIT_FAILURE);
-    }
-    /* Child Process */
-    else if(pid == 0)
-    {
-        rc = execute_command(c, str);
-        if(rc < 0)
-        {
-            exit(EXIT_FAILURE);
-        }
-        exit(EXIT_SUCCESS);
-    }
-    /* Parent Process */
-    else 
-    {
-        wait(&rc);
-        if(WIFEXITED(rc))
-        {
-            if(WEXITSTATUS(rc) != 0)
-            {
-                return -1;
-            }                  
-        }
-    }
-    return rc;
-}
-
 /** This program should be run as root on startup to initialize the shared 
  * memory segments required by the user cache in PVFS. 
  */
@@ -156,11 +156,25 @@ int main(int argc, char **argv)
 {
     int rc = 0;
 
-    /* Start ucached if not already started */
-    if(open(FIFO1, O_WRONLY) != -1)
-    {
+    /* Continue ucached if it's the only ucached */
+    char ps_buff1[256];
+    char ps_buff2[256];
+    FILE *pipe = popen("ps -e | grep -w ucached", "r");
+    /* Should catch 1 line result, but not 2 */
+    rc = (int)fgets(ps_buff1, 256, pipe);
+    rc = (int)fgets(ps_buff2, 256, pipe); /* Should be zero if only 1 ucached */
+    if(rc == 0)
+    {
+        /* Remove old FIFOs in case daemon was killed last time */
+        remove(FIFO1);
+        remove(FIFO2);
+    }
+    else
+    {  
         puts("FAILURE: Daemon already started");
-        return 1;
+        puts(ps_buff1);
+        puts(ps_buff2);
+        exit(EXIT_FAILURE);
     }
 
     /* Daemonize! */
@@ -169,7 +183,7 @@ int main(int argc, char **argv)
     /* remove previous log file */   
     remove(LOG);
 
-    run_as_child('p', "ucached running...\t\t");
+    run_as_child('p', "ucached running...\t\t\t");
     
     /* Start up with shared memory initialized */
     run_as_child('c', NULL);
@@ -196,9 +210,10 @@ int main(int argc, char **argv)
         if(myread(readfd, buffer) > 0)
         {            
             c = buffer[0];
+            char buff[128];
+            /* Valid Command */
             if(c == 'c' || c == 'd' || c == 'x')
             {
-                char buff[128];
                 snprintf(buff, 128, "Command Received: %c\t\t\t", c);
                 run_as_child('p', buff);                    
                 /* If creating shmem segments, do so in a child process to 
@@ -214,6 +229,14 @@ int main(int argc, char **argv)
                     rc = execute_command(c, NULL);
                     CHECK_RC(rc);
                 }
+            }
+            /* Invalid Command */
+            else
+            {
+                snprintf(buff, 128, "Invalid Command Received: %c\t\t", c);
+                run_as_child('p', buff);  
+                rc = -1;
+                CHECK_RC(rc);  
             }
             c = 0;
             memset(buffer, 0, BUFF_SIZE);            

Index: ucached.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/apps/ucache/Attic/ucached.h,v
diff -p -u -r1.1.2.1 -r1.1.2.2
--- ucached.h	16 Sep 2011 19:22:01 -0000	1.1.2.1
+++ ucached.h	19 Sep 2011 20:56:21 -0000	1.1.2.2
@@ -33,14 +33,14 @@ void mywrite(int writefd, const char *sr
 #define LOCKS_SIZE (513 * 24)
 
 /* Choose which reponse to send to caller */
-#define CHECK_RC(rc)                            \
-    if(rc >= 0)                                 \
-    {                                           \
-       mywrite(writefd, "SUCCESS", buffer);     \
-    }                                           \
-    else                                        \
-    {                                           \
-        mywrite(writefd, "FAILURE", buffer);    \
-    }                                           \
+#define CHECK_RC(rc)                                                        \
+    if(rc >= 0)                                                             \
+    {                                                                       \
+       mywrite(writefd, "SUCCESS", buffer);                                 \
+    }                                                                       \
+    else                                                                    \
+    {                                                                       \
+        mywrite(writefd, "FAILURE: check log: /tmp/ucached.log", buffer);   \
+    }                                                                       \
 
 #endif

Index: ucached_cmd.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/apps/ucache/Attic/ucached_cmd.c,v
diff -p -u -r1.1.2.1 -r1.1.2.2
--- ucached_cmd.c	16 Sep 2011 19:22:01 -0000	1.1.2.1
+++ ucached_cmd.c	19 Sep 2011 20:56:21 -0000	1.1.2.2
@@ -1,6 +1,7 @@
 #include "ucached.h"
 
 /*
+ * s = start ucached
  * c = create shared memory for ucache
  * d = destroy shared memory for ucache
  * x = exit ucached
@@ -9,7 +10,7 @@ int main(int argc, char **argv)
 {
     if(argc!=2)
     {
-        printf("usage: ./ucache_cmd <command>\n");
+        printf("usage: ./ucache_cmd <command char>\n");
         return 0; 
     }
 
@@ -17,17 +18,21 @@ int main(int argc, char **argv)
 
     if(argv[1][0] == 's')
     {
-        /* Start ucached if not already started */
-        if(open(FIFO1, O_WRONLY) == -1)
+        char ps_buff[256];
+        FILE *pipe = popen("ps -e | grep -w ucached", "r");
+        rc = (int)fgets(ps_buff, 256, pipe);
+        if(rc == 0)
         {
+            remove(FIFO1);
+            remove(FIFO2);
             /* Crank up the daemon since it's not running */
-            
-            puts("SUCCESS: Daemon started");
             rc = system("./ucached");
+            puts("SUCCESS: Daemon started");
         }
         else
-        {
+        {  
             puts("FAILURE: Daemon already started");
+            puts(ps_buff);
         }
         return 1;
     }
@@ -40,14 +45,27 @@ int main(int argc, char **argv)
 
     /* Open FIFOs for use */
     writefd = open(FIFO1, O_WRONLY);
-    readfd = open(FIFO2, O_RDONLY);
+    /* Non-blocking since response could excede 4096 chars */
+    readfd = open(FIFO2, O_RDONLY | O_NONBLOCK); 
 
     /* Send Command to Daemon */
     mywrite(writefd, &argv[1][0], buffer);
 
     /* Collect Response */
-    myread(readfd, buffer);
+    int count = 0;
+    /*  */
+    while((count = myread(readfd, buffer)) == -1)
+    {
+    }
+    /* Now output the response until no more chars can be read */
     puts(buffer);
+    memset(buffer, 0, BUFF_SIZE);
+
+    while((count = myread(readfd, buffer)) > 0)
+    {
+        puts(buffer);
+        memset(buffer, 0, BUFF_SIZE);
+    }
 
     /* Close FIFO when done */
     close(readfd);



More information about the Pvfs2-cvs mailing list