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