missing include
[lttv.git] / ltt / branches / poly / lttv / modules / gui / tracecontrol / tracecontrol.c
CommitLineData
e7c8534e 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <glib.h>
24#include <string.h>
25#include <gtk/gtk.h>
26#include <gdk/gdk.h>
27#include <gdk/gdkkeysyms.h>
28
29#include <lttv/lttv.h>
30#include <lttv/module.h>
31#include <lttv/hook.h>
e7c8534e 32
33#include <lttvwindow/lttvwindow.h>
34#include <lttvwindow/lttvwindowtraces.h>
35
36#include "hTraceControlInsert.xpm"
381229ee 37#include "TraceControlStart.xpm"
38#include "TraceControlPause.xpm"
39#include "TraceControlStop.xpm"
e7c8534e 40
77ef407f 41#include <sys/types.h>
42#include <unistd.h>
43#include <stdlib.h>
ff430216 44#include <pty.h>
45#include <utmp.h>
46#include <sys/wait.h>
86a65fdb 47#include <sys/poll.h>
6cec4cd2 48#include <errno.h>
77ef407f 49
f6f6abf0 50#define MAX_ARGS_LEN PATH_MAX * 10
e7c8534e 51
52GSList *g_control_list = NULL ;
53
54/*! \file lttv/modules/gui/tracecontrol/tracecontrol.c
55 * \brief Graphic trace start/stop control interface.
56 *
57 * This plugin interacts with lttctl to start/stop tracing. It needs to take the
58 * root password to be able to interact with lttctl.
59 *
60 */
61
62typedef struct _ControlData ControlData;
63
64/*
65 * Prototypes
66 */
67GtkWidget *guicontrol_get_widget(ControlData *tcd);
68ControlData *gui_control(Tab *tab);
69void gui_control_destructor(ControlData *tcd);
70GtkWidget* h_guicontrol(Tab *tab);
71void control_destroy_walk(gpointer data, gpointer user_data);
72
73/*
74 * Callback functions
75 */
76
77ef407f 77static void start_clicked (GtkButton *button, gpointer user_data);
78static void pause_clicked (GtkButton *button, gpointer user_data);
45653836 79static void unpause_clicked (GtkButton *button, gpointer user_data);
77ef407f 80static void stop_clicked (GtkButton *button, gpointer user_data);
e7c8534e 81
ff430216 82
e7c8534e 83/**
84 * @struct _ControlData
85 *
77ef407f 86 * @brief Main structure of gui control
e7c8534e 87 */
88struct _ControlData {
89 Tab *tab; /**< current tab of module */
90
77ef407f 91 GtkWidget *window; /**< window */
e7c8534e 92
77ef407f 93 GtkWidget *main_box; /**< main container */
94 GtkWidget *start_button;
95 GtkWidget *pause_button;
45653836 96 GtkWidget *unpause_button;
77ef407f 97 GtkWidget *stop_button;
98 GtkWidget *username_label;
99 GtkWidget *username_entry;
100 GtkWidget *password_label;
101 GtkWidget *password_entry;
102 GtkWidget *channel_dir_label;
103 GtkWidget *channel_dir_entry;
104 GtkWidget *trace_dir_label;
105 GtkWidget *trace_dir_entry;
106 GtkWidget *trace_name_label;
107 GtkWidget *trace_name_entry;
108 GtkWidget *trace_mode_label;
109 GtkWidget *trace_mode_combo;
110 GtkWidget *start_daemon_label;
111 GtkWidget *start_daemon_check;
45653836 112 GtkWidget *append_label;
113 GtkWidget *append_check;
77ef407f 114 GtkWidget *optional_label;
115 GtkWidget *subbuf_size_label;
116 GtkWidget *subbuf_size_entry;
117 GtkWidget *subbuf_num_label;
118 GtkWidget *subbuf_num_entry;
119 GtkWidget *lttctl_path_label;
120 GtkWidget *lttctl_path_entry;
121 GtkWidget *lttd_path_label;
122 GtkWidget *lttd_path_entry;
123 GtkWidget *fac_path_label;
124 GtkWidget *fac_path_entry;
e7c8534e 125};
126
127/**
128 * @fn GtkWidget* guicontrol_get_widget(ControlData*)
129 *
130 * This function returns the current main widget
131 * used by this module
132 * @param tcd the module struct
133 * @return The main widget
134 */
135GtkWidget*
136guicontrol_get_widget(ControlData *tcd)
137{
77ef407f 138 return tcd->window;
e7c8534e 139}
140
141/**
142 * @fn ControlData* gui_control(Tab*)
143 *
144 * Constructor is used to create ControlData data structure.
145 * @param tab The tab structure used by the widget
146 * @return The Filter viewer data created.
147 */
148ControlData*
149gui_control(Tab *tab)
150{
151 g_debug("filter::gui_control()");
152
153 unsigned i;
154 GtkCellRenderer *renderer;
155 GtkTreeViewColumn *column;
156
157 ControlData* tcd = g_new(ControlData,1);
158
159 tcd->tab = tab;
160
77ef407f 161 tcd->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
162 gtk_window_set_title(GTK_WINDOW(tcd->window), "LTTng Trace Control");
e7c8534e 163 /*
164 * Initiating GtkTable layout
165 * starts with 2 rows and 5 columns and
166 * expands when expressions added
167 */
77ef407f 168 tcd->main_box = gtk_table_new(14,7,FALSE);
169 gtk_table_set_row_spacings(GTK_TABLE(tcd->main_box),5);
170 gtk_table_set_col_spacings(GTK_TABLE(tcd->main_box),5);
e7c8534e 171
77ef407f 172 gtk_container_add(GTK_CONTAINER(tcd->window), GTK_WIDGET(tcd->main_box));
e7c8534e 173
ff430216 174 GList *focus_chain = NULL;
175
381229ee 176 /*
177 * start/pause/stop buttons
178 */
179 GdkPixbuf *pixbuf;
180 GtkWidget *image;
181 pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)TraceControlStart_xpm);
182 image = gtk_image_new_from_pixbuf(pixbuf);
77ef407f 183 tcd->start_button = gtk_button_new_with_label("start");
184 gtk_button_set_image(GTK_BUTTON(tcd->start_button), image);
185 gtk_button_set_alignment(GTK_BUTTON(tcd->start_button), 0.0, 0.0);
186 gtk_widget_show (tcd->start_button);
187 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->start_button,6,7,0,1,GTK_FILL,GTK_FILL,2,2);
381229ee 188
189 pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)TraceControlPause_xpm);
190 image = gtk_image_new_from_pixbuf(pixbuf);
77ef407f 191 tcd->pause_button = gtk_button_new_with_label("pause");
192 gtk_button_set_image(GTK_BUTTON(tcd->pause_button), image);
193 gtk_button_set_alignment(GTK_BUTTON(tcd->pause_button), 0.0, 0.0);
194 gtk_widget_show (tcd->pause_button);
195 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->pause_button,6,7,1,2,GTK_FILL,GTK_FILL,2,2);
381229ee 196
45653836 197 pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)TraceControlPause_xpm);
198 image = gtk_image_new_from_pixbuf(pixbuf);
199 tcd->unpause_button = gtk_button_new_with_label("unpause");
200 gtk_button_set_image(GTK_BUTTON(tcd->unpause_button), image);
201 gtk_button_set_alignment(GTK_BUTTON(tcd->unpause_button), 0.0, 0.0);
202 gtk_widget_show (tcd->unpause_button);
203 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->unpause_button,6,7,2,3,GTK_FILL,GTK_FILL,2,2);
204
381229ee 205 pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)TraceControlStop_xpm);
206 image = gtk_image_new_from_pixbuf(pixbuf);
77ef407f 207 tcd->stop_button = gtk_button_new_with_label("stop");
208 gtk_button_set_image(GTK_BUTTON(tcd->stop_button), image);
209 gtk_button_set_alignment(GTK_BUTTON(tcd->stop_button), 0.0, 0.0);
210 gtk_widget_show (tcd->stop_button);
45653836 211 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->stop_button,6,7,3,4,GTK_FILL,GTK_FILL,2,2);
381229ee 212
e7c8534e 213 /*
214 * First half of the filter window
215 * - textual entry of filter expression
216 * - processing button
217 */
77ef407f 218 tcd->username_label = gtk_label_new("Username:");
219 gtk_widget_show (tcd->username_label);
220 tcd->username_entry = gtk_entry_new();
221 gtk_entry_set_text(GTK_ENTRY(tcd->username_entry),"root");
222 gtk_widget_show (tcd->username_entry);
223 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->username_label,0,2,0,1,GTK_FILL,GTK_FILL,2,2);
224 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->username_entry,2,6,0,1,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
225
226
227
228 tcd->password_label = gtk_label_new("Password:");
229 gtk_widget_show (tcd->password_label);
230 tcd->password_entry = gtk_entry_new();
231 gtk_entry_set_visibility(GTK_ENTRY(tcd->password_entry), FALSE);
232 gtk_widget_show (tcd->password_entry);
233 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->password_label,0,2,1,2,GTK_FILL,GTK_FILL,2,2);
234 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->password_entry,2,6,1,2,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
235
236
237 tcd->channel_dir_label = gtk_label_new("Channel directory:");
238 gtk_widget_show (tcd->channel_dir_label);
239 tcd->channel_dir_entry = gtk_entry_new();
240 gtk_entry_set_text(GTK_ENTRY(tcd->channel_dir_entry),"/mnt/relayfs/ltt");
241 gtk_widget_show (tcd->channel_dir_entry);
242 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->channel_dir_label,0,2,2,3,GTK_FILL,GTK_FILL,2,2);
243 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->channel_dir_entry,2,6,2,3,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
244
245 tcd->trace_dir_label = gtk_label_new("Trace directory:");
246 gtk_widget_show (tcd->trace_dir_label);
247 tcd->trace_dir_entry = gtk_entry_new();
248 gtk_entry_set_text(GTK_ENTRY(tcd->trace_dir_entry),"/tmp/trace1");
249 gtk_widget_show (tcd->trace_dir_entry);
250 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_dir_label,0,2,3,4,GTK_FILL,GTK_FILL,2,2);
251 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_dir_entry,2,6,3,4,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
252
253 tcd->trace_name_label = gtk_label_new("Trace name:");
254 gtk_widget_show (tcd->trace_name_label);
255 tcd->trace_name_entry = gtk_entry_new();
256 gtk_entry_set_text(GTK_ENTRY(tcd->trace_name_entry),"trace");
257 gtk_widget_show (tcd->trace_name_entry);
258 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_name_label,0,2,4,5,GTK_FILL,GTK_FILL,2,2);
259 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_name_entry,2,6,4,5,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
260
261 tcd->trace_mode_label = gtk_label_new("Trace mode ");
262 gtk_widget_show (tcd->trace_mode_label);
263 tcd->trace_mode_combo = gtk_combo_box_new_text();
264 gtk_combo_box_append_text(GTK_COMBO_BOX(tcd->trace_mode_combo),
381229ee 265 "normal");
77ef407f 266 gtk_combo_box_append_text(GTK_COMBO_BOX(tcd->trace_mode_combo),
381229ee 267 "flight recorder");
77ef407f 268 gtk_combo_box_set_active(GTK_COMBO_BOX(tcd->trace_mode_combo), 0);
269 gtk_widget_show (tcd->trace_mode_combo);
270 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_mode_label,0,2,5,6,GTK_FILL,GTK_FILL,2,2);
271 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->trace_mode_combo,2,6,5,6,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
272
273 tcd->start_daemon_label = gtk_label_new("Start daemon ");
274 gtk_widget_show (tcd->start_daemon_label);
275 tcd->start_daemon_check = gtk_check_button_new();
276 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tcd->start_daemon_check), TRUE);
277 gtk_widget_show (tcd->start_daemon_check);
278 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->start_daemon_label,0,2,6,7,GTK_FILL,GTK_FILL,2,2);
279 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->start_daemon_check,2,6,6,7,GTK_FILL,GTK_FILL,0,0);
45653836 280
281 tcd->append_label = gtk_label_new("Append to trace ");
282 gtk_widget_show (tcd->append_label);
283 tcd->append_check = gtk_check_button_new();
284 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tcd->append_check), FALSE);
285 gtk_widget_show (tcd->append_check);
286 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->append_label,0,2,7,8,GTK_FILL,GTK_FILL,2,2);
287 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->append_check,2,6,7,8,GTK_FILL,GTK_FILL,0,0);
288
77ef407f 289
290 tcd->optional_label = gtk_label_new("Optional fields ");
291 gtk_widget_show (tcd->optional_label);
45653836 292 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->optional_label,0,6,8,9,GTK_FILL,GTK_FILL,2,2);
77ef407f 293
294 tcd->subbuf_size_label = gtk_label_new("Subbuffer size:");
295 gtk_widget_show (tcd->subbuf_size_label);
296 tcd->subbuf_size_entry = gtk_entry_new();
297 gtk_widget_show (tcd->subbuf_size_entry);
45653836 298 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->subbuf_size_label,0,2,9,10,GTK_FILL,GTK_FILL,2,2);
299 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->subbuf_size_entry,2,6,9,10,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
77ef407f 300
301 tcd->subbuf_num_label = gtk_label_new("Number of subbuffers:");
302 gtk_widget_show (tcd->subbuf_num_label);
303 tcd->subbuf_num_entry = gtk_entry_new();
304 gtk_widget_show (tcd->subbuf_num_entry);
45653836 305 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->subbuf_num_label,0,2,10,11,GTK_FILL,GTK_FILL,2,2);
306 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->subbuf_num_entry,2,6,10,11,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
77ef407f 307
308 tcd->lttctl_path_label = gtk_label_new("path to lttctl:");
309 gtk_widget_show (tcd->lttctl_path_label);
310 tcd->lttctl_path_entry = gtk_entry_new();
ff430216 311 gtk_entry_set_text(GTK_ENTRY(tcd->lttctl_path_entry),PACKAGE_BIN_DIR "/lttctl");
77ef407f 312 gtk_widget_show (tcd->lttctl_path_entry);
45653836 313 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->lttctl_path_label,0,2,11,12,GTK_FILL,GTK_FILL,2,2);
314 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->lttctl_path_entry,2,6,11,12,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
77ef407f 315
316
317 tcd->lttd_path_label = gtk_label_new("path to lttd:");
318 gtk_widget_show (tcd->lttd_path_label);
319 tcd->lttd_path_entry = gtk_entry_new();
ff430216 320 gtk_entry_set_text(GTK_ENTRY(tcd->lttd_path_entry),PACKAGE_BIN_DIR "/lttd");
77ef407f 321 gtk_widget_show (tcd->lttd_path_entry);
45653836 322 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->lttd_path_label,0,2,12,13,GTK_FILL,GTK_FILL,2,2);
323 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->lttd_path_entry,2,6,12,13,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
381229ee 324
325
77ef407f 326 tcd->fac_path_label = gtk_label_new("path to facilities:");
327 gtk_widget_show (tcd->fac_path_label);
328 tcd->fac_path_entry = gtk_entry_new();
86a65fdb 329 gtk_entry_set_text(GTK_ENTRY(tcd->fac_path_entry),PACKAGE_DATA_DIR "/" PACKAGE "/facilities");
77ef407f 330 gtk_widget_set_size_request(tcd->fac_path_entry, 250, -1);
331 gtk_widget_show (tcd->fac_path_entry);
45653836 332 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->fac_path_label,0,2,13,14,GTK_FILL,GTK_FILL,2,2);
333 gtk_table_attach( GTK_TABLE(tcd->main_box),tcd->fac_path_entry,2,6,13,14,GTK_FILL|GTK_EXPAND|GTK_SHRINK,GTK_FILL,0,0);
ff430216 334
335 focus_chain = g_list_append (focus_chain, tcd->username_entry);
336 focus_chain = g_list_append (focus_chain, tcd->password_entry);
337 focus_chain = g_list_append (focus_chain, tcd->start_button);
338 focus_chain = g_list_append (focus_chain, tcd->pause_button);
45653836 339 focus_chain = g_list_append (focus_chain, tcd->unpause_button);
ff430216 340 focus_chain = g_list_append (focus_chain, tcd->stop_button);
341 focus_chain = g_list_append (focus_chain, tcd->channel_dir_entry);
342 focus_chain = g_list_append (focus_chain, tcd->trace_dir_entry);
343 focus_chain = g_list_append (focus_chain, tcd->trace_name_entry);
344 focus_chain = g_list_append (focus_chain, tcd->trace_mode_combo);
345 focus_chain = g_list_append (focus_chain, tcd->start_daemon_check);
45653836 346 focus_chain = g_list_append (focus_chain, tcd->append_check);
ff430216 347 focus_chain = g_list_append (focus_chain, tcd->subbuf_size_entry);
348 focus_chain = g_list_append (focus_chain, tcd->subbuf_num_entry);
349 focus_chain = g_list_append (focus_chain, tcd->lttctl_path_entry);
350 focus_chain = g_list_append (focus_chain, tcd->lttd_path_entry);
351 focus_chain = g_list_append (focus_chain, tcd->fac_path_entry);
352
353 gtk_container_set_focus_chain(GTK_CONTAINER(tcd->main_box), focus_chain);
354
382e4b92 355 g_list_free(focus_chain);
356
77ef407f 357 g_signal_connect(G_OBJECT(tcd->start_button), "clicked",
358 (GCallback)start_clicked, tcd);
359 g_signal_connect(G_OBJECT(tcd->pause_button), "clicked",
360 (GCallback)pause_clicked, tcd);
45653836 361 g_signal_connect(G_OBJECT(tcd->unpause_button), "clicked",
362 (GCallback)unpause_clicked, tcd);
77ef407f 363 g_signal_connect(G_OBJECT(tcd->stop_button), "clicked",
364 (GCallback)stop_clicked, tcd);
e7c8534e 365
366 /*
367 * show main container
368 */
77ef407f 369 gtk_widget_show(tcd->main_box);
370 gtk_widget_show(tcd->window);
e7c8534e 371
372
373 g_object_set_data_full(
77ef407f 374 G_OBJECT(guicontrol_get_widget(tcd)),
375 "control_viewer_data",
e7c8534e 376 tcd,
377 (GDestroyNotify)gui_control_destructor);
378
379 g_control_list = g_slist_append(
380 g_control_list,
381 tcd);
382
383 return tcd;
384}
385
386
387/**
388 * @fn void gui_control_destructor(ControlData*)
389 *
390 * Destructor for the filter gui module
391 * @param tcd The module structure
392 */
393void
394gui_control_destructor(ControlData *tcd)
395{
396 Tab *tab = tcd->tab;
397
398 /* May already been done by GTK window closing */
77ef407f 399 if(GTK_IS_WIDGET(guicontrol_get_widget(tcd))){
e7c8534e 400 g_info("widget still exists");
401 }
402// if(tab != NULL) {
403// lttvwindow_unregister_traceset_notify(tcd->tab,
404// filter_traceset_changed,
405// filter_viewer_data);
406// }
407 lttvwindowtraces_background_notify_remove(tcd);
408
409 g_control_list = g_slist_remove(g_control_list, tcd);
410
411 g_free(tcd);
412}
413
29e34d6c 414static int execute_command(const gchar *command, const gchar *username,
45653836 415 const gchar *password, const gchar *lttd_path, const gchar *fac_path)
77ef407f 416{
ff430216 417 pid_t pid;
418 int fdpty;
419 pid = forkpty(&fdpty, NULL, NULL, NULL);
29e34d6c 420 int retval = 0;
77ef407f 421
422 if(pid > 0) {
423 /* parent */
ff430216 424 gchar buf[256];
425 int status;
426 ssize_t count;
427 /* discuss with su */
428 struct timeval timeout;
429 timeout.tv_sec = 1;
430 timeout.tv_usec = 0;
ff430216 431
86a65fdb 432 struct pollfd pollfd;
433 int num_rdy;
434 int num_hup = 0;
435
436
437 /* Read the output from the child terminal before the prompt. If no data in
438 * 200 ms, we stop reading to give the password */
439 g_info("Reading from child console...");
1f1a8b9c 440 sleep(1); /* make sure the child is ready */
86a65fdb 441 while(1) {
442 pollfd.fd = fdpty;
75e2f396 443 pollfd.events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
86a65fdb 444
445 num_rdy = poll(&pollfd, 1, 200);
446#if 0
447 if(num_rdy == -1) {
448 perror("Poll error");
449 goto wait_child;
450 }
451#endif //0
452
453 /* Timeout : stop waiting for chars */
454 if(num_rdy == 0) break;
455
456 switch(pollfd.revents) {
457 case POLLERR:
458 g_warning("Error returned in polling fd\n");
459 num_hup++;
460 break;
461 case POLLHUP:
462 g_info("Polling FD : hung up.");
463 num_hup++;
464 break;
465 case POLLNVAL:
466 g_warning("Polling fd tells it is not open");
467 num_hup++;
468 break;
469 case POLLPRI:
470 case POLLIN:
471 count = read (fdpty, buf, 256);
472 if(count > 0) {
473 buf[count] = '\0';
474 printf("%s", buf);
475 } else if(count == -1) {
476 perror("Error in read");
477 goto wait_child;
478 }
479 break;
480 }
481 if(num_hup > 0) {
482 g_warning("Child hung up too fast");
483 goto wait_child;
484 }
485 }
486
487 /* Write the password */
ff430216 488 g_info("Got su prompt, now writing password...");
ff430216 489 int ret;
490 ret = write(fdpty, password, strlen(password));
491 if(ret < 0) perror("Error in write");
492 ret = write(fdpty, "\n", 1);
493 if(ret < 0) perror("Error in write");
494 fsync(fdpty);
495
86a65fdb 496 /* Take the output from the terminal and show it on the real console */
497 g_info("Getting data from child terminal...");
498 while(1) {
499 int num_hup = 0;
500 pollfd.fd = fdpty;
75e2f396 501 pollfd.events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
86a65fdb 502
503 num_rdy = poll(&pollfd, 1, -1);
504#if 0
505 if(num_rdy == -1) {
506 perror("Poll error");
507 goto wait_child;
508 }
509#endif //0
510 if(num_rdy == 0) break;
511
512 switch(pollfd.revents) {
513 case POLLERR:
514 g_warning("Error returned in polling fd\n");
515 num_hup++;
516 break;
517 case POLLHUP:
518 g_info("Polling FD : hung up.");
519 num_hup++;
520 break;
521 case POLLNVAL:
522 g_warning("Polling fd tells it is not open");
523 num_hup++;
524 break;
525 case POLLPRI:
526 case POLLIN:
527 count = read (fdpty, buf, 256);
528 if(count > 0) {
529 buf[count] = '\0';
530 printf("%s", buf);
531 } else if(count == -1) {
532 perror("Error in read");
533 goto wait_child;
534 }
535 break;
ff430216 536 }
86a65fdb 537 if(num_hup > 0) goto wait_child;
538 }
539wait_child:
540 g_info("Waiting for child exit...");
541
542 ret = waitpid(pid, &status, 0);
6cec4cd2 543
544 if(ret == -1) {
545 g_warning("An error occured in wait : %s",
546 strerror(errno));
547 } else {
548 if(WIFEXITED(status))
549 if(WEXITSTATUS(status) != 0) {
550 retval = WEXITSTATUS(status);
551 g_warning("An error occured in the su command : %s",
552 strerror(retval));
553 }
554 }
77ef407f 555
86a65fdb 556 g_info("Child exited.");
77ef407f 557
558 } else if(pid == 0) {
f6f6abf0 559 /* Setup environment variables */
77ef407f 560 if(strcmp(lttd_path, "") != 0)
561 setenv("LTT_DAEMON", lttd_path, 1);
562 if(strcmp(fac_path, "") != 0)
563 setenv("LTT_FACILITIES", fac_path, 1);
f6f6abf0 564
45653836 565 g_message("Executing (as %s) : %s\n", username, command);
ff430216 566
45653836 567 execlp("su", "su", "-p", "-c", command, username, NULL);
568 exit(-1); /* not supposed to happen! */
569
570 //gint ret = execvp();
571
572 } else {
573 /* error */
574 g_warning("Error happened when forking for su");
575 }
86a65fdb 576
29e34d6c 577 return retval;
45653836 578}
86a65fdb 579
86a65fdb 580
45653836 581/* Callbacks */
f6f6abf0 582
45653836 583void start_clicked (GtkButton *button, gpointer user_data)
584{
585 ControlData *tcd = (ControlData*)user_data;
f6f6abf0 586
45653836 587 const gchar *username = gtk_entry_get_text(GTK_ENTRY(tcd->username_entry));
588 const gchar *password = gtk_entry_get_text(GTK_ENTRY(tcd->password_entry));
589 const gchar *channel_dir =
590 gtk_entry_get_text(GTK_ENTRY(tcd->channel_dir_entry));
591 const gchar *trace_dir = gtk_entry_get_text(GTK_ENTRY(tcd->trace_dir_entry));
592 const gchar *trace_name =
593 gtk_entry_get_text(GTK_ENTRY(tcd->trace_name_entry));
594
382e4b92 595 const gchar *trace_mode_sel;
596 GtkTreeIter iter;
597
598 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(tcd->trace_mode_combo), &iter);
599 gtk_tree_model_get(
600 gtk_combo_box_get_model(GTK_COMBO_BOX(tcd->trace_mode_combo)),
601 &iter, 0, &trace_mode_sel, -1);
602 //const gchar *trace_mode_sel =
603 //2.6+ gtk_combo_box_get_active_text(GTK_COMBO_BOX(tcd->trace_mode_combo));
45653836 604 const gchar *trace_mode;
605 if(strcmp(trace_mode_sel, "normal") == 0)
606 trace_mode = "normal";
607 else
608 trace_mode = "flight";
609
610 gboolean start_daemon =
611 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tcd->start_daemon_check));
f6f6abf0 612
45653836 613 gboolean append =
614 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tcd->append_check));
615
616 const gchar *subbuf_size =
617 gtk_entry_get_text(GTK_ENTRY(tcd->subbuf_size_entry));
618 const gchar *subbuf_num =
619 gtk_entry_get_text(GTK_ENTRY(tcd->subbuf_num_entry));
620 const gchar *lttctl_path =
621 gtk_entry_get_text(GTK_ENTRY(tcd->lttctl_path_entry));
622 const gchar *lttd_path = gtk_entry_get_text(GTK_ENTRY(tcd->lttd_path_entry));
623 const gchar *fac_path = gtk_entry_get_text(GTK_ENTRY(tcd->fac_path_entry));
624
625
626 /* Setup arguments to su */
627 /* child */
628 gchar args[MAX_ARGS_LEN];
629 gint args_left = MAX_ARGS_LEN - 1; /* for \0 */
630
631 args[0] = '\0';
632
633 /* Command */
634 strncat(args, "exec", args_left);
635 args_left = MAX_ARGS_LEN - strlen(args) - 1;
636
637 /* space */
638 strncat(args, " ", args_left);
639 args_left = MAX_ARGS_LEN - strlen(args) - 1;
640
641 if(strcmp(lttctl_path, "") == 0)
642 strncat(args, "lttctl", args_left);
643 else
644 strncat(args, lttctl_path, args_left);
645 args_left = MAX_ARGS_LEN - strlen(args) - 1;
646
647 /* space */
648 strncat(args, " ", args_left);
649 args_left = MAX_ARGS_LEN - strlen(args) - 1;
650
651 /* channel dir */
652 strncat(args, "-l ", args_left);
653 args_left = MAX_ARGS_LEN - strlen(args) - 1;
654 strncat(args, channel_dir, args_left);
655 args_left = MAX_ARGS_LEN - strlen(args) - 1;
656
657 /* space */
658 strncat(args, " ", args_left);
659 args_left = MAX_ARGS_LEN - strlen(args) - 1;
660
661 /* trace dir */
662 strncat(args, "-t ", args_left);
663 args_left = MAX_ARGS_LEN - strlen(args) - 1;
664 strncat(args, trace_dir, args_left);
665 args_left = MAX_ARGS_LEN - strlen(args) - 1;
666
667 /* space */
668 strncat(args, " ", args_left);
669 args_left = MAX_ARGS_LEN - strlen(args) - 1;
670
671 /* name */
672 strncat(args, "-n ", args_left);
673 args_left = MAX_ARGS_LEN - strlen(args) - 1;
674 strncat(args, trace_name, args_left);
675 args_left = MAX_ARGS_LEN - strlen(args) - 1;
676
677 /* space */
678 strncat(args, " ", args_left);
679 args_left = MAX_ARGS_LEN - strlen(args) - 1;
680
681 /* trace mode */
682 strncat(args, "-m ", args_left);
683 args_left = MAX_ARGS_LEN - strlen(args) - 1;
684 strncat(args, trace_mode, args_left);
685 args_left = MAX_ARGS_LEN - strlen(args) - 1;
686
687 /* space */
688 strncat(args, " ", args_left);
689 args_left = MAX_ARGS_LEN - strlen(args) - 1;
690
691 /* Start daemon ? */
692 if(start_daemon) {
693 strncat(args, "-d", args_left);
f6f6abf0 694 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 695 } else {
696 /* Simply create the channel and then start tracing */
697 strncat(args, "-b", args_left);
f6f6abf0 698 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 699 }
700
f6f6abf0 701
45653836 702 /* Append to trace ? */
703 if(append) {
f6f6abf0 704 /* space */
705 strncat(args, " ", args_left);
706 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 707 strncat(args, "-a", args_left);
f6f6abf0 708 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 709 }
710
711 /* optional arguments */
712 /* subbuffer size */
713 if(strcmp(subbuf_size, "") != 0) {
f6f6abf0 714 /* space */
715 strncat(args, " ", args_left);
716 args_left = MAX_ARGS_LEN - strlen(args) - 1;
717
45653836 718 strncat(args, "-z ", args_left);
f6f6abf0 719 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 720 strncat(args, subbuf_size, args_left);
f6f6abf0 721 args_left = MAX_ARGS_LEN - strlen(args) - 1;
45653836 722 }
f6f6abf0 723
45653836 724 /* number of subbuffers */
725 if(strcmp(subbuf_num, "") != 0) {
f6f6abf0 726 /* space */
727 strncat(args, " ", args_left);
728 args_left = MAX_ARGS_LEN - strlen(args) - 1;
729
45653836 730 strncat(args, "-x ", args_left);
731 args_left = MAX_ARGS_LEN - strlen(args) - 1;
732 strncat(args, subbuf_num, args_left);
733 args_left = MAX_ARGS_LEN - strlen(args) - 1;
734 }
ff430216 735
77ef407f 736
29e34d6c 737 int retval = execute_command(args, username, password, lttd_path, fac_path);
738
739 if(retval) {
740 gchar msg[256];
741 guint msg_left = 256;
742
743 strcpy(msg, "A problem occured when executing the su command : ");
744 msg_left = 256 - strlen(msg) - 1;
745 strncat(msg, strerror(retval), msg_left);
746 GtkWidget *dialogue =
747 gtk_message_dialog_new(
748 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
749 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
750 GTK_MESSAGE_ERROR,
751 GTK_BUTTONS_OK,
752 msg);
753 gtk_dialog_run(GTK_DIALOG(dialogue));
754 gtk_widget_destroy(dialogue);
755 }
77ef407f 756
757}
758
759
760void pause_clicked (GtkButton *button, gpointer user_data)
761{
762 ControlData *tcd = (ControlData*)user_data;
763
45653836 764 const gchar *username = gtk_entry_get_text(GTK_ENTRY(tcd->username_entry));
765 const gchar *password = gtk_entry_get_text(GTK_ENTRY(tcd->password_entry));
766 const gchar *trace_name =
767 gtk_entry_get_text(GTK_ENTRY(tcd->trace_name_entry));
768 const gchar *lttd_path = "";
769 const gchar *fac_path = "";
770
771 const gchar *lttctl_path =
772 gtk_entry_get_text(GTK_ENTRY(tcd->lttctl_path_entry));
77ef407f 773
45653836 774 /* Setup arguments to su */
775 /* child */
776 gchar args[MAX_ARGS_LEN];
777 gint args_left = MAX_ARGS_LEN - 1; /* for \0 */
778
779 args[0] = '\0';
780
781 /* Command */
782 strncat(args, "exec", args_left);
783 args_left = MAX_ARGS_LEN - strlen(args) - 1;
784
785 /* space */
786 strncat(args, " ", args_left);
787 args_left = MAX_ARGS_LEN - strlen(args) - 1;
788
789 if(strcmp(lttctl_path, "") == 0)
790 strncat(args, "lttctl", args_left);
791 else
792 strncat(args, lttctl_path, args_left);
793 args_left = MAX_ARGS_LEN - strlen(args) - 1;
794
795 /* space */
796 strncat(args, " ", args_left);
797 args_left = MAX_ARGS_LEN - strlen(args) - 1;
798
799 /* name */
800 strncat(args, "-n ", args_left);
801 args_left = MAX_ARGS_LEN - strlen(args) - 1;
802 strncat(args, trace_name, args_left);
803 args_left = MAX_ARGS_LEN - strlen(args) - 1;
804
805 /* space */
806 strncat(args, " ", args_left);
807 args_left = MAX_ARGS_LEN - strlen(args) - 1;
808
809 /* Simply pause tracing */
810 strncat(args, "-q", args_left);
811 args_left = MAX_ARGS_LEN - strlen(args) - 1;
812
29e34d6c 813 int retval = execute_command(args, username, password, lttd_path, fac_path);
814 if(retval) {
815 gchar msg[256];
816 guint msg_left = 256;
817
818 strcpy(msg, "A problem occured when executing the su command : ");
819 msg_left = 256 - strlen(msg) - 1;
820 strncat(msg, strerror(retval), msg_left);
821 GtkWidget *dialogue =
822 gtk_message_dialog_new(
823 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
824 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
825 GTK_MESSAGE_ERROR,
826 GTK_BUTTONS_OK,
827 msg);
828 gtk_dialog_run(GTK_DIALOG(dialogue));
829 gtk_widget_destroy(dialogue);
830 }
831
45653836 832}
833
834void unpause_clicked (GtkButton *button, gpointer user_data)
835{
836 ControlData *tcd = (ControlData*)user_data;
837
838 const gchar *username = gtk_entry_get_text(GTK_ENTRY(tcd->username_entry));
839 const gchar *password = gtk_entry_get_text(GTK_ENTRY(tcd->password_entry));
840 const gchar *trace_name =
841 gtk_entry_get_text(GTK_ENTRY(tcd->trace_name_entry));
842 const gchar *lttd_path = "";
843 const gchar *fac_path = "";
844
845 const gchar *lttctl_path =
846 gtk_entry_get_text(GTK_ENTRY(tcd->lttctl_path_entry));
847
848 /* Setup arguments to su */
849 /* child */
850 gchar args[MAX_ARGS_LEN];
851 gint args_left = MAX_ARGS_LEN - 1; /* for \0 */
852
853 args[0] = '\0';
854
855 /* Command */
856 strncat(args, "exec", args_left);
857 args_left = MAX_ARGS_LEN - strlen(args) - 1;
858
859 /* space */
860 strncat(args, " ", args_left);
861 args_left = MAX_ARGS_LEN - strlen(args) - 1;
862
863 if(strcmp(lttctl_path, "") == 0)
864 strncat(args, "lttctl", args_left);
865 else
866 strncat(args, lttctl_path, args_left);
867 args_left = MAX_ARGS_LEN - strlen(args) - 1;
868
869 /* space */
870 strncat(args, " ", args_left);
871 args_left = MAX_ARGS_LEN - strlen(args) - 1;
872
873 /* name */
874 strncat(args, "-n ", args_left);
875 args_left = MAX_ARGS_LEN - strlen(args) - 1;
876 strncat(args, trace_name, args_left);
877 args_left = MAX_ARGS_LEN - strlen(args) - 1;
878
879 /* space */
880 strncat(args, " ", args_left);
881 args_left = MAX_ARGS_LEN - strlen(args) - 1;
882
883 /* Simply unpause tracing */
884 strncat(args, "-s", args_left);
885 args_left = MAX_ARGS_LEN - strlen(args) - 1;
886
29e34d6c 887 int retval = execute_command(args, username, password, lttd_path, fac_path);
888 if(retval) {
889 gchar msg[256];
890 guint msg_left = 256;
891
892 strcpy(msg, "A problem occured when executing the su command : ");
893 msg_left = 256 - strlen(msg) - 1;
894 strncat(msg, strerror(retval), msg_left);
895 GtkWidget *dialogue =
896 gtk_message_dialog_new(
897 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
898 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
899 GTK_MESSAGE_ERROR,
900 GTK_BUTTONS_OK,
901 msg);
902 gtk_dialog_run(GTK_DIALOG(dialogue));
903 gtk_widget_destroy(dialogue);
904 }
905
77ef407f 906}
907
908void stop_clicked (GtkButton *button, gpointer user_data)
909{
910 ControlData *tcd = (ControlData*)user_data;
911
45653836 912 const gchar *username = gtk_entry_get_text(GTK_ENTRY(tcd->username_entry));
913 const gchar *password = gtk_entry_get_text(GTK_ENTRY(tcd->password_entry));
914 const gchar *trace_name =
915 gtk_entry_get_text(GTK_ENTRY(tcd->trace_name_entry));
916 const gchar *lttd_path = "";
917 const gchar *fac_path = "";
918
919 const gchar *lttctl_path =
920 gtk_entry_get_text(GTK_ENTRY(tcd->lttctl_path_entry));
8321ae6a 921 const gchar *trace_dir = gtk_entry_get_text(GTK_ENTRY(tcd->trace_dir_entry));
45653836 922
923 /* Setup arguments to su */
924 /* child */
925 gchar args[MAX_ARGS_LEN];
926 gint args_left = MAX_ARGS_LEN - 1; /* for \0 */
927
928 args[0] = '\0';
929
930 /* Command */
931 strncat(args, "exec", args_left);
932 args_left = MAX_ARGS_LEN - strlen(args) - 1;
933
934 /* space */
935 strncat(args, " ", args_left);
936 args_left = MAX_ARGS_LEN - strlen(args) - 1;
937
938 if(strcmp(lttctl_path, "") == 0)
939 strncat(args, "lttctl", args_left);
940 else
941 strncat(args, lttctl_path, args_left);
942 args_left = MAX_ARGS_LEN - strlen(args) - 1;
943
944 /* space */
945 strncat(args, " ", args_left);
946 args_left = MAX_ARGS_LEN - strlen(args) - 1;
947
948 /* name */
949 strncat(args, "-n ", args_left);
950 args_left = MAX_ARGS_LEN - strlen(args) - 1;
951 strncat(args, trace_name, args_left);
952 args_left = MAX_ARGS_LEN - strlen(args) - 1;
953
954 /* space */
955 strncat(args, " ", args_left);
956 args_left = MAX_ARGS_LEN - strlen(args) - 1;
957
958 /* Simply stop tracing and destroy channel */
959 strncat(args, "-R", args_left);
960 args_left = MAX_ARGS_LEN - strlen(args) - 1;
961
29e34d6c 962 int retval = execute_command(args, username, password, lttd_path, fac_path);
963 if(retval) {
964 gchar msg[256];
965 guint msg_left = 256;
966
967 strcpy(msg, "A problem occured when executing the su command : ");
968 msg_left = 256 - strlen(msg) - 1;
969 strncat(msg, strerror(retval), msg_left);
970 GtkWidget *dialogue =
971 gtk_message_dialog_new(
972 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
973 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
974 GTK_MESSAGE_ERROR,
975 GTK_BUTTONS_OK,
976 msg);
977 gtk_dialog_run(GTK_DIALOG(dialogue));
978 gtk_widget_destroy(dialogue);
979 return;
980 }
981
77ef407f 982
8321ae6a 983 /* Ask to the user if he wants to open the trace in a new window */
984 GtkWidget *dialogue;
985 GtkWidget *label;
986 gint id;
987
988 dialogue = gtk_dialog_new_with_buttons("Open trace ?",
989 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))),
990 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
991 GTK_STOCK_YES,GTK_RESPONSE_ACCEPT,
992 GTK_STOCK_NO,GTK_RESPONSE_REJECT,
993 NULL);
994 label = gtk_label_new("Do you want to open the trace in LTTV ?");
995 gtk_widget_show(label);
996
997 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialogue)->vbox),
998 label);
999
1000 id = gtk_dialog_run(GTK_DIALOG(dialogue));
1001
1002 switch(id){
1003 case GTK_RESPONSE_ACCEPT:
1004 {
1005 create_main_window_with_trace(trace_dir);
1006 }
1007 break;
1008 case GTK_RESPONSE_REJECT:
1009 default:
1010 break;
1011 }
1012 gtk_widget_destroy(dialogue);
1013
77ef407f 1014}
1015
e7c8534e 1016
1017/**
1018 * @fn GtkWidget* h_guicontrol(Tab*)
1019 *
1020 * Control Module's constructor hook
1021 *
1022 * This constructor is given as a parameter to the menuitem and toolbar button
1023 * registration. It creates the list.
1024 * @param tab A pointer to the parent window.
1025 * @return The widget created.
1026 */
1027GtkWidget *
1028h_guicontrol(Tab *tab)
1029{
1030 ControlData* f = gui_control(tab) ;
1031
1032 return NULL;
1033}
1034
1035/**
1036 * @fn static void init()
1037 *
1038 * This function initializes the Filter Viewer functionnality through the
1039 * gtkTraceSet API.
1040 */
1041static void init() {
1042
1043 lttvwindow_register_constructor("guicontrol",
1044 "/",
1045 "Insert Tracing Control Module",
1046 hTraceControlInsert_xpm,
1047 "Insert Tracing Control Module",
1048 h_guicontrol);
1049}
1050
1051/**
1052 * @fn void control_destroy_walk(gpointer,gpointer)
1053 *
1054 * Initiate the destruction of the current gui module
1055 * on the GTK Interface
1056 */
1057void
1058control_destroy_walk(gpointer data, gpointer user_data)
1059{
1060 ControlData *tcd = (ControlData*)data;
1061
1062 g_debug("traceontrol.c : control_destroy_walk, %p", tcd);
1063
1064 /* May already have been done by GTK window closing */
1065 if(GTK_IS_WIDGET(guicontrol_get_widget(tcd)))
1066 gtk_widget_destroy(guicontrol_get_widget(tcd));
1067}
1068
1069/**
1070 * @fn static void destroy()
1071 * @brief plugin's destroy function
1072 *
1073 * This function releases the memory reserved by the module and unregisters
1074 * everything that has been registered in the gtkTraceSet API.
1075 */
1076static void destroy() {
1077
1078 g_slist_foreach(g_control_list, control_destroy_walk, NULL );
1079
1080 lttvwindow_unregister_constructor(h_guicontrol);
1081
1082}
1083
1084
1085LTTV_MODULE("guitracecontrol", "Trace Control Window", \
1086 "Graphical module that let user control kernel tracing", \
1087 init, destroy, "lttvwindow")
1088
This page took 0.066943 seconds and 4 git commands to generate.