1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
/*
This file is part of GNUnet.
(C) 2005, 2006, 2007 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file src/plugins/daemon/daemon.c
* @brief code for gnunet-gtk gnunetd interaction
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunetgtk_common.h"
#include <GNUnet/gnunet_directories.h>
#include <GNUnet/gnunet_getoption_lib.h>
#include <GNUnet/gnunet_stats_lib.h>
#include <GNUnet/gnunet_util.h>
#include <glib/gerror.h>
#include "daemon.h"
#ifdef WITH_LIBGKSU2
/* Not used because libgksu2 headers have broken depends in Debian
And this is not really needed
#include <libgksu/libgksu.h> */
gboolean gksu_run (gchar * command_line, GError ** error);
#endif
static struct GNUNET_CronManager *cron;
static struct GNUNET_GE_Context *ectx;
static struct GNUNET_GC_Configuration *cfg;
/**
* Do the actual update (in event thread).
*/
static void *
updateAppModelSafe (void *arg)
{
GtkWidget *w;
GtkListStore *model = arg;
w =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"applicationList");
gtk_tree_view_set_model (GTK_TREE_VIEW (w), GTK_TREE_MODEL (model));
gtk_tree_selection_set_mode (gtk_tree_view_get_selection
(GTK_TREE_VIEW (w)), GTK_SELECTION_NONE);
return NULL;
}
/**
* cron job that periodically updates the model for the
* application list.
*/
static void
updateAppModel (void *dummy)
{
struct GNUNET_ClientServerConnection *sock;
GtkTreeIter iter;
char *apps;
char *next;
char *pos;
char *desc;
GtkListStore *model;
model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
apps = NULL;
sock = GNUNET_client_connection_create (ectx, cfg);
if (sock != NULL)
apps =
GNUNET_get_daemon_configuration_value (sock, "GNUNETD", "APPLICATIONS");
if (apps != NULL)
{
next = apps;
do
{
while (*next == ' ')
next++;
pos = next;
while ((*next != '\0') && (*next != ' '))
next++;
if (*next == '\0')
{
next = NULL; /* terminate! */
}
else
{
*next = '\0'; /* add 0-termination for pos */
next++;
}
if (strlen (pos) > 0)
{
desc =
GNUNET_get_daemon_configuration_value (sock, "ABOUT", pos);
gtk_list_store_append (model, &iter);
gtk_list_store_set (model,
&iter,
0, pos, 1, dgettext ("GNUnet", desc), -1);
GNUNET_free_non_null (desc);
}
}
while (next != NULL);
GNUNET_free (apps);
}
GNUNET_client_connection_destroy (sock);
GNUNET_GTK_save_call (&updateAppModelSafe, model);
g_object_unref (model);
}
void *
daemon_doUpdateMenus (void *arg)
{
int ret = *(int *) arg;
static GtkWidget *killEntry = NULL;
static GtkWidget *launchEntry = NULL;
static GtkWidget *statsEntry = NULL;
static GtkWidget *chooser = NULL;
static int once = 1;
static int isLocal;
char *host;
int canStart;
struct GNUNET_GC_Configuration *dcfg;
const char *fn;
char * fnx;
char *user;
if (once)
{
once = 0;
killEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "stopDaemon");
launchEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"startDaemon");
statsEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"statusPixmap");
chooser =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"configfilenameEntry");
GNUNET_GC_get_configuration_value_string (cfg, "NETWORK", "HOST",
"localhost", &host);
if (strncmp (host, "localhost", 9) == 0)
isLocal = TRUE;
else
isLocal = FALSE;
GNUNET_free (host);
}
if (ret == 0)
{
canStart = 0;
fn = gtk_entry_get_text (GTK_ENTRY (chooser));
fnx = NULL;
if (NULL == fn)
GNUNET_GC_get_configuration_value_filename (cfg, "DAEMON",
"CONFIGFILE",
GNUNET_DEFAULT_DAEMON_CONFIG_FILE,
&fnx);
fn = fnx;
if (isLocal && fn)
{
if (GNUNET_disk_file_test (ectx, fn) == GNUNET_YES)
{
dcfg = GNUNET_GC_create ();
canStart = 1;
if (0 != GNUNET_GC_parse_configuration (dcfg, fn))
canStart = 0;
user = NULL;
GNUNET_GC_get_configuration_value_string (dcfg,
"GNUNETD",
"USER", "", &user);
if (strlen (user) > 0)
{
#if defined(WINDOWS) || defined(WITH_LIBGKSU2)
canStart = 1;
#else
struct passwd *pws;
if (NULL == (pws = getpwnam (user)))
{
canStart = 0;
}
else
{
if (pws->pw_uid != getuid ())
canStart = (geteuid () == 0);
else
canStart = 1;
}
#endif
}
GNUNET_free (user);
GNUNET_GC_free (dcfg);
}
}
GNUNET_free_non_null(fnx);
gtk_image_set_from_stock (GTK_IMAGE (statsEntry), GTK_STOCK_YES,
GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_sensitive (killEntry, FALSE);
if (canStart && isLocal)
{
gtk_widget_set_sensitive (launchEntry, TRUE);
gtk_image_set_from_stock (GTK_IMAGE (statsEntry), GTK_STOCK_NO,
GTK_ICON_SIZE_SMALL_TOOLBAR);
}
else
{
gtk_widget_set_sensitive (launchEntry, FALSE);
gtk_image_set_from_stock (GTK_IMAGE (statsEntry),
GTK_STOCK_DIALOG_ERROR,
GTK_ICON_SIZE_SMALL_TOOLBAR);
}
}
else
{
gtk_image_set_from_stock (GTK_IMAGE (statsEntry), GTK_STOCK_YES,
GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_sensitive (killEntry, TRUE);
gtk_widget_set_sensitive (launchEntry, FALSE);
}
return NULL;
}
/**
* Cron job that checks if the daemon is running.
*/
static void
cronCheckDaemon (void *dummy)
{
static int last = -1;
int ret;
if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
ret = 1;
else
ret = 0;
if (last != ret)
{
updateAppModel (NULL);
last = ret;
if (ret)
GNUNET_GTK_display_daemon_status (GNUNET_GTK_STATUS_DAEMONUP, 0);
else
GNUNET_GTK_display_daemon_status (GNUNET_GTK_STATUS_NODAEMON, 0);
}
GNUNET_GTK_save_call (&daemon_doUpdateMenus, &ret);
}
/* in setup.c */
void
on_gnunetdconfigfile_changed_daemon (GtkWidget * wid, gpointer data);
/**
* Launch gnunetd w/ checks
*/
void
on_startDaemon_clicked_daemon (GtkWidget * widget, gpointer data)
{
GtkWidget *launchEntry;
GtkWidget *statsEntry;
GtkWidget *chooser;
char *fn;
char *user;
struct GNUNET_GC_Configuration *dcfg;
int code = 0;
launchEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "startDaemon");
gtk_widget_set_sensitive (launchEntry, FALSE);
if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
{
GNUNET_cron_advance_job (GNUNET_GTK_get_cron_manager (),
&cronCheckDaemon,
15 * GNUNET_CRON_SECONDS, NULL);
return;
}
else
{
GNUNET_GTK_add_log_entry (_("Launching gnunetd...\n"));
statsEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"statusPixmap");
gtk_image_set_from_stock (GTK_IMAGE (statsEntry), GTK_STOCK_EXECUTE,
GTK_ICON_SIZE_SMALL_TOOLBAR);
chooser
= glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"gnunetdconfigFileChooserButton");
fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
GNUNET_GE_BREAK (ectx, fn != NULL);
dcfg = GNUNET_GC_create ();
if (0 != GNUNET_GC_parse_configuration (dcfg, fn))
user = NULL;
GNUNET_GC_get_configuration_value_string (dcfg,
"GNUNETD", "USER", "", &user);
#ifdef WITH_LIBGKSU2
char *command;
GError *gerror = NULL;
struct passwd *pws;
if (strlen (user) > 0)
{
pws = getpwnam (user);
if (pws->pw_uid != getuid ())
{
command = g_strconcat ("gnunetd -c ", fn, NULL);
code = gksu_run (command, &gerror);
GNUNET_free (command);
if (gerror)
{
GNUNET_GTK_add_log_entry (_("Launching gnunetd failed\n"));
g_error_free (gerror);
}
else
{
GNUNET_GTK_add_log_entry (_("Launched gnunetd\n"));
}
g_free (fn);
GNUNET_cron_advance_job (GNUNET_GTK_get_cron_manager (),
&cronCheckDaemon,
15 * GNUNET_CRON_SECONDS, NULL);
return;
}
else
code = GNUNET_daemon_start (ectx, cfg, fn, GNUNET_YES);
}
#else
code = GNUNET_daemon_start (ectx, cfg, fn, GNUNET_YES);
#endif
if (GNUNET_SYSERR != code)
{
GNUNET_GTK_add_log_entry (_("Launched gnunetd\n"));
}
else
{
GNUNET_GTK_add_log_entry (_("Launching gnunetd failed\n"));
}
if (fn != NULL)
g_free (fn);
GNUNET_cron_advance_job (GNUNET_GTK_get_cron_manager (),
&cronCheckDaemon,
15 * GNUNET_CRON_SECONDS, NULL);
}
on_gnunetdconfigfile_changed_daemon (NULL, NULL);
}
/**
* Kill gnunetd
*/
void
on_stopDaemon_clicked_daemon (GtkWidget * widget, gpointer data)
{
GtkWidget *killEntry = NULL;
GtkWidget *statsEntry;
struct GNUNET_ClientServerConnection *sock;
int i;
GNUNET_cron_del_job (cron, &cronCheckDaemon, 15 * GNUNET_CRON_SECONDS,
NULL);
killEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "stopDaemon");
gtk_widget_set_sensitive (killEntry, FALSE);
if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
{
sock = GNUNET_client_connection_create (ectx, cfg);
if (GNUNET_OK !=
GNUNET_client_connection_request_daemon_shutdown (sock))
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new
(NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, _("Error requesting shutdown of gnunetd.\n"));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
else
{
statsEntry =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"statusPixmap");
gtk_image_set_from_stock (GTK_IMAGE (statsEntry), GTK_STOCK_EXECUTE,
GTK_ICON_SIZE_SMALL_TOOLBAR);
GNUNET_GTK_add_log_entry (_("Terminating gnunetd...\n"));
}
GNUNET_client_connection_destroy (sock);
}
for (i = 0; i < 10; i++)
GNUNET_cron_add_job (cron,
&cronCheckDaemon,
(i + 1) * 500 * GNUNET_CRON_MILLISECONDS, 0, NULL);
}
struct GNUNET_GC_Configuration *
daemon_get_cfg_handle()
{
return cfg;
}
void
init_daemon (struct GNUNET_GE_Context *e, struct GNUNET_GC_Configuration *c)
{
GtkWidget *tab;
GtkWidget *apps;
GtkListStore *model;
GtkCellRenderer *renderer;
int col;
char *daemon_config;
ectx = e;
cfg = c;
apps =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"applicationList");
model = gtk_list_store_new (1, G_TYPE_STRING);
gtk_tree_view_set_model (GTK_TREE_VIEW (apps), GTK_TREE_MODEL (model));
renderer = gtk_cell_renderer_text_new ();
col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (apps),
-1,
_("Application"),
renderer,
"text", 0, NULL);
gtk_tree_view_column_set_resizable (gtk_tree_view_get_column
(GTK_TREE_VIEW (apps), col - 1), TRUE);
renderer = gtk_cell_renderer_text_new ();
col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (apps),
-1,
_("Description"),
renderer,
"text", 1, NULL);
gtk_tree_view_column_set_resizable (gtk_tree_view_get_column
(GTK_TREE_VIEW (apps), col - 1), TRUE);
tab =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"daemonScrolledWindow");
gtk_widget_show (tab);
daemon_config = NULL;
GNUNET_GC_get_configuration_value_filename (cfg,
"DAEMON",
"CONFIGFILE",
GNUNET_DEFAULT_DAEMON_CONFIG_FILE,
&daemon_config);
gtk_entry_set_text (GTK_ENTRY
(glade_xml_get_widget
(GNUNET_GTK_get_main_glade_XML (),
"configfilenameEntry")),
daemon_config);
if (GNUNET_YES != GNUNET_disk_file_test (NULL, daemon_config))
{
GNUNET_GTK_add_log_entry (_
("Configuration file for GNUnet daemon `%s' does not exist! Run `gnunet-setup -d'.\n"),
daemon_config);
}
GNUNET_free (daemon_config);
cron = GNUNET_GTK_get_cron_manager ();
GNUNET_cron_add_job (cron, &cronCheckDaemon, 2 * GNUNET_CRON_SECONDS,
15 * GNUNET_CRON_SECONDS, NULL);
GNUNET_cron_add_job (cron, &updateAppModel, 5 * GNUNET_CRON_MINUTES,
5 * GNUNET_CRON_MINUTES, NULL);
}
void
done_daemon ()
{
GtkWidget *w;
GNUNET_cron_del_job (cron, &cronCheckDaemon, 15 * GNUNET_CRON_SECONDS,
NULL);
GNUNET_cron_del_job (cron, &updateAppModel, 5 * GNUNET_CRON_MINUTES, NULL);
w =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"applicationList");
gtk_tree_view_set_model (GTK_TREE_VIEW (w), NULL);
w =
glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
"applicationList");
gtk_tree_view_set_model (GTK_TREE_VIEW (w), NULL);
}
/* end of daemon.c */
|