0.4
[lttv.git] / ltt / branches / poly / lttv / modules / gui / tracecontrol / tracecontrol.c
index 6e09947ab3c7df5f13201f6f2e4ffa8a79f596cb..1407aa88bbd4850429f2e356c38af5a51b6b63dd 100644 (file)
@@ -45,6 +45,7 @@
 #include <utmp.h>
 #include <sys/wait.h>
 #include <sys/poll.h>
+#include <errno.h>
 
 #define MAX_ARGS_LEN PATH_MAX * 10
 
@@ -408,12 +409,13 @@ gui_control_destructor(ControlData *tcd)
   g_free(tcd);
 }
 
-static void execute_command(const gchar *command, const gchar *username,
+static int execute_command(const gchar *command, const gchar *username,
     const gchar *password, const gchar *lttd_path, const gchar *fac_path)
 {
   pid_t pid;
   int fdpty;
   pid = forkpty(&fdpty, NULL, NULL, NULL);
+  int retval = 0;
 
   if(pid > 0) {
     /* parent */
@@ -433,9 +435,10 @@ static void execute_command(const gchar *command, const gchar *username,
     /* Read the output from the child terminal before the prompt. If no data in
      * 200 ms, we stop reading to give the password */
     g_info("Reading from child console...");
+    sleep(1); /* make sure the child is ready */
     while(1) {
       pollfd.fd = fdpty;
-      pollfd.events = POLLIN|POLLPRI;
+      pollfd.events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
 
       num_rdy = poll(&pollfd, 1, 200);
 #if 0
@@ -493,7 +496,7 @@ static void execute_command(const gchar *command, const gchar *username,
     while(1) {
       int num_hup = 0;
       pollfd.fd = fdpty;
-      pollfd.events = POLLIN|POLLPRI;
+      pollfd.events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
 
       num_rdy = poll(&pollfd, 1, -1);
 #if 0
@@ -535,11 +538,18 @@ wait_child:
     g_info("Waiting for child exit...");
     
     ret = waitpid(pid, &status, 0);
-
-    if(WIFEXITED(ret))
-      if(WEXITSTATUS(ret) != 0)
-       g_warning("An error occured in the su command : %s",
-           strerror(WEXITSTATUS(ret)));
+    
+    if(ret == -1) {
+      g_warning("An error occured in wait : %s",
+          strerror(errno));
+    } else {
+      if(WIFEXITED(status))
+        if(WEXITSTATUS(status) != 0) {
+          retval = WEXITSTATUS(status);
+          g_warning("An error occured in the su command : %s",
+              strerror(retval));
+        }
+    }
 
     g_info("Child exited.");
 
@@ -562,6 +572,7 @@ wait_child:
     g_warning("Error happened when forking for su");
   }
 
+  return retval;
 }
 
 
@@ -714,8 +725,25 @@ void start_clicked (GtkButton *button, gpointer user_data)
   }
 
   
-  execute_command(args, username, password, lttd_path, fac_path);
-
+  int retval = execute_command(args, username, password, lttd_path, fac_path);
+
+  if(retval) {
+    gchar msg[256];
+    guint msg_left = 256;
+
+    strcpy(msg, "A problem occured when executing the su command : ");
+    msg_left = 256 - strlen(msg) - 1;
+    strncat(msg, strerror(retval), msg_left);
+    GtkWidget *dialogue = 
+      gtk_message_dialog_new(
+        GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
+        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+        GTK_MESSAGE_ERROR,
+        GTK_BUTTONS_OK,
+        msg);
+    gtk_dialog_run(GTK_DIALOG(dialogue));
+    gtk_widget_destroy(dialogue);
+  }
   
 }
 
@@ -773,8 +801,25 @@ void pause_clicked (GtkButton *button, gpointer user_data)
   strncat(args, "-q", args_left);
   args_left = MAX_ARGS_LEN - strlen(args) - 1;
 
-  execute_command(args, username, password, lttd_path, fac_path);
-
+  int retval = execute_command(args, username, password, lttd_path, fac_path);
+  if(retval) {
+    gchar msg[256];
+    guint msg_left = 256;
+
+    strcpy(msg, "A problem occured when executing the su command : ");
+    msg_left = 256 - strlen(msg) - 1;
+    strncat(msg, strerror(retval), msg_left);
+    GtkWidget *dialogue = 
+      gtk_message_dialog_new(
+        GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
+        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+        GTK_MESSAGE_ERROR,
+        GTK_BUTTONS_OK,
+        msg);
+    gtk_dialog_run(GTK_DIALOG(dialogue));
+    gtk_widget_destroy(dialogue);
+  }
 }
 
 void unpause_clicked (GtkButton *button, gpointer user_data)
@@ -830,7 +875,25 @@ void unpause_clicked (GtkButton *button, gpointer user_data)
   strncat(args, "-s", args_left);
   args_left = MAX_ARGS_LEN - strlen(args) - 1;
 
-  execute_command(args, username, password, lttd_path, fac_path);
+  int retval = execute_command(args, username, password, lttd_path, fac_path);
+  if(retval) {
+    gchar msg[256];
+    guint msg_left = 256;
+
+    strcpy(msg, "A problem occured when executing the su command : ");
+    msg_left = 256 - strlen(msg) - 1;
+    strncat(msg, strerror(retval), msg_left);
+    GtkWidget *dialogue = 
+      gtk_message_dialog_new(
+        GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
+        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+        GTK_MESSAGE_ERROR,
+        GTK_BUTTONS_OK,
+        msg);
+    gtk_dialog_run(GTK_DIALOG(dialogue));
+    gtk_widget_destroy(dialogue);
+  }
 }
 
 void stop_clicked (GtkButton *button, gpointer user_data)
@@ -887,8 +950,26 @@ void stop_clicked (GtkButton *button, gpointer user_data)
   strncat(args, "-R", args_left);
   args_left = MAX_ARGS_LEN - strlen(args) - 1;
 
-  execute_command(args, username, password, lttd_path, fac_path);
-
+  int retval = execute_command(args, username, password, lttd_path, fac_path);
+  if(retval) {
+    gchar msg[256];
+    guint msg_left = 256;
+
+    strcpy(msg, "A problem occured when executing the su command : ");
+    msg_left = 256 - strlen(msg) - 1;
+    strncat(msg, strerror(retval), msg_left);
+    GtkWidget *dialogue =
+      gtk_message_dialog_new(
+        GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
+        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+        GTK_MESSAGE_ERROR,
+        GTK_BUTTONS_OK,
+        msg);
+    gtk_dialog_run(GTK_DIALOG(dialogue));
+    gtk_widget_destroy(dialogue);
+    return;
+  }
 
   /* Ask to the user if he wants to open the trace in a new window */
   GtkWidget *dialogue;
This page took 0.024802 seconds and 4 git commands to generate.