make lttctl and lttd interaction more standard : real daemon
[lttv.git] / ltt / branches / poly / lttctl / lttctl.c
index 9845c5c600c81e5a7136550235b0e5697f99c098..19605c2e55888f86cf6cef78db4bce75058ff168 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <signal.h>
 #include <dirent.h>
 #define BUF_SIZE 4194304
 
 enum trace_ctl_op {
+  CTL_OP_CREATE_START,
        CTL_OP_CREATE,
        CTL_OP_DESTROY,
+  CTL_OP_STOP_DESTROY,
        CTL_OP_START,
        CTL_OP_STOP,
        CTL_OP_DAEMON,
@@ -45,12 +48,12 @@ static enum trace_ctl_op op = CTL_OP_NONE;
 static char *channel_root = NULL;
 static char *trace_root = NULL;
 
-static int sigio_received = 0;
+static int sigchld_received = 0;
 
-void handler(int signo)
+void sigchld_handler(int signo)
 {
        printf("signal %d received\n", signo);
-       sigio_received = 1;
+       sigchld_received = 1;
 }
 
 
@@ -62,9 +65,12 @@ void show_arguments(void)
        printf("Please use the following arguments :\n");
        printf("\n");
        printf("-n name       Name of the trace.\n");
-       printf("-c mode       Create trace channels in mode normal or flight recorder.\n");
+       printf("-b            Create trace channels and start tracing (no daemon).\n");
+       printf("-c            Create trace channels.\n");
+       printf("-m mode       Normal or flight recorder mode.\n");
        printf("              Mode values : normal (default) or flight.\n");
        printf("-r            Destroy trace channels.\n");
+       printf("-R            Stop tracing and destroy trace channels.\n");
        printf("-s            Start tracing.\n");
        //printf("              Note : will automatically create a normal trace if "
        //                                                                                      "none exists.\n");
@@ -115,8 +121,12 @@ int parse_arguments(int argc, char **argv)
                                                }
 
                                                break;
+          case 'b':
+            op = CTL_OP_CREATE_START;
                                        case 'c':
                                                op = CTL_OP_CREATE;
+            break;
+                                       case 'm':
                                                if(argn+1 < argc) {
                                                        mode_name = argv[argn+1];
                                                        argn++;
@@ -130,7 +140,7 @@ int parse_arguments(int argc, char **argv)
                                                                ret = -1;
                                                        }
                                                } else {
-                                                               printf("Specify a mode after -c.\n");
+                                                               printf("Specify a mode after -m.\n");
                                                                printf("\n");
                                                                ret = -1;
                                                }
@@ -138,6 +148,9 @@ int parse_arguments(int argc, char **argv)
                                        case 'r':
                                                op = CTL_OP_DESTROY;
                                                break;
+                                       case 'R':
+                                               op = CTL_OP_STOP_DESTROY;
+                                               break;
                                        case 's':
                                                op = CTL_OP_START;
                                                break;
@@ -260,7 +273,7 @@ int create_eventdefs(void)
   struct dirent *entry;
        char *facilities_path = getenv("LTT_FACILITIES");
        if(facilities_path == NULL) facilities_path =
-          "/usr/share/LinuxTraceToolkitViewer/facilities";
+          PACKAGE_DATA_DIR "/" PACKAGE "/facilities";
 
   ret = mkdir(trace_root, S_IRWXU|S_IRWXG|S_IRWXO);
   if(ret == -1 && errno != EEXIST) {
@@ -283,6 +296,12 @@ int create_eventdefs(void)
   
   DIR *facilities_dir = opendir(facilities_path);
   
+  if(facilities_dir == NULL) {
+    perror("Cannot open facilities directory");
+    ret = -1;
+    goto error;
+  }
+
   while((entry = readdir(facilities_dir)) != NULL) {
     if(entry->d_name[0] == '.') continue;
     
@@ -330,7 +349,7 @@ close_dest:
 
   closedir(facilities_dir);
 
-  error:
+error:
   return ret;
 
 }
@@ -344,7 +363,8 @@ int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
        char *lttd_path = getenv("LTT_DAEMON");
        struct sigaction act;
 
-       if(lttd_path == NULL) lttd_path = "lttd";
+       if(lttd_path == NULL) lttd_path = 
+    PACKAGE_BIN_DIR "/lttd";
        
        strcat(channel_path, channel_root);
        strcat(channel_path, "/");
@@ -354,29 +374,33 @@ int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
        ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size, n_subbufs);
        if(ret != 0) goto create_error;
 
-       act.sa_handler = handler;
+       act.sa_handler = sigchld_handler;
        sigemptyset(&(act.sa_mask));
-       sigaddset(&(act.sa_mask), SIGIO);
-       sigaction(SIGIO, &act, NULL);
-       
-       sigio_received = 0;
+       sigaddset(&(act.sa_mask), SIGCHLD);
+       sigaction(SIGCHLD, &act, NULL);
        
        pid = fork();
 
        if(pid > 0) {
+    int status;
                /* parent */
-               while(!sigio_received) pause();
+               while(!(sigchld_received)) pause();
+
+    waitpid(pid, &status, 0);
+    ret = 0;
+    if(WIFEXITED(status))
+      ret = WEXITSTATUS(status);
+    if(ret) goto start_error;
 
-               /* Now the trace is created, go on and create the supplementary files... */
-    
                printf("Creating supplementary trace files\n");
     ret = create_eventdefs();
     if(ret) goto start_error;
 
        } else if(pid == 0) {
                /* child */
-               int ret = 
-                       execlp(lttd_path, lttd_path, "-t", trace_root, "-c", channel_path, "-s", NULL);
+    
+               int ret =       execlp(lttd_path, lttd_path, "-t", trace_root, "-c",
+                     channel_path, "-d", NULL);
                if(ret) {
                        perror("Error in executing the lttd daemon");
                        exit(-1);
@@ -394,6 +418,7 @@ int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
 
        /* error handling */
 start_error:
+  printf("Trace start error\n");
        ret |= lttctl_destroy_trace(handle, trace_name);
 create_error:
        return ret;
@@ -417,6 +442,12 @@ int main(int argc, char ** argv)
        if(handle == NULL) return -1;
        
        switch(op) {
+    case CTL_OP_CREATE_START:
+                       ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
+                                                                                                                               n_subbufs);
+      if(!ret)
+        ret = lttctl_start(handle, trace_name);
+      break;
        case CTL_OP_CREATE:
                        ret = lttctl_create_trace(handle, trace_name, mode, subbuf_size,
                                                                                                                                n_subbufs);
@@ -424,6 +455,11 @@ int main(int argc, char ** argv)
                case CTL_OP_DESTROY:
                        ret = lttctl_destroy_trace(handle, trace_name);
                        break;
+               case CTL_OP_STOP_DESTROY:
+                       ret = lttctl_stop(handle, trace_name);
+      if(!ret)
+                       ret = lttctl_destroy_trace(handle, trace_name);
+                       break;
                case CTL_OP_START:
                        ret = lttctl_start(handle, trace_name);
                        break;
This page took 0.026609 seconds and 4 git commands to generate.