Command-line version implemented.
authorshort <>
Fri, 21 May 2004 17:46:28 +0000 (17:46 +0000)
committershort <>
Fri, 21 May 2004 17:46:28 +0000 (17:46 +0000)
Daemon detaching implemented.

src/main.c
src/main.h
src/network.c
src/network.h
src/ui-gnome.c
src/ui-gnome.glade
src/ui-gnome.h
src/ui-line.c
src/ui-line.h

index 0f6b384..f649a3e 100644 (file)
 #include <libgnome/gnome-program.h>
 #include <libgnomeui/gnome-ui-init.h>
 
+#include "main.h"      /* self */
 #include "ui-gnome.h"
+#include "ui-line.h"
+#include "network.h"
+
+
+/* Config: */
+#define LOCAL_PORT_DEFAULT 9201
 
 
-int optarg_verbose;
 static int optarg_text;
+int optarg_verbose;
+int optarg_port=LOCAL_PORT_DEFAULT;
+static int optarg_start;
+static int optarg_stop;
+int optarg_no_fork;
+
+void (*ui_interactive)(void);
 
 static const struct poptOption popt_table[]={
 
@@ -49,9 +62,18 @@ static const struct poptOption popt_table[]={
                        argDescrip: (argDescripP), \
                }
 
-               UDPGATE_POPT(0  ,"text"            ,POPT_ARG_NONE  ,&optarg_text   ,0,
+               UDPGATE_POPT(0  ,"text"   ,POPT_ARG_NONE                          ,&optarg_text   ,0,
                                N_("Disable Gnome UI; --text must be first argument"),NULL),
-               UDPGATE_POPT('v',"verbose"         ,POPT_ARG_NONE  ,&optarg_verbose,0,N_("Display additional debug information"),NULL),
+               UDPGATE_POPT('v',"verbose",POPT_ARG_NONE                          ,&optarg_verbose,0,
+                               N_("Display additional debug information"),NULL),
+               UDPGATE_POPT('p',"port"   ,POPT_ARG_INT |POPT_ARGFLAG_SHOW_DEFAULT,&optarg_port   ,0,
+                               N_("Listen on this UDP port"),NULL),
+               UDPGATE_POPT('s',"start"  ,POPT_ARG_NONE                          ,&optarg_start  ,0,
+                               N_("Start the daemon"),NULL),
+               UDPGATE_POPT('S',"stop"   ,POPT_ARG_NONE                          ,&optarg_stop   ,0,
+                               N_("Stop the daemon"),NULL),
+               UDPGATE_POPT('1',"no-fork",POPT_ARG_NONE                          ,&optarg_no_fork,0,
+                               N_("Do not detach from the current process"),NULL),
 
 #undef UDPGATE_POPT
                POPT_TABLEEND
@@ -167,21 +189,24 @@ guint handler_id;
                }
 
        is_interactive=(1
-/*                     && ! optarg_scan_path_list
-                       && ! optarg_scan_disks_quick
-                       && ! optarg_scan_disks 
-                       && ! optarg_microsoft_com */ );
+                       && !optarg_start
+                       && !optarg_stop);
 
        /* Initialize UI here to catch all GLog errors below. */
        if (is_interactive
                        && (no_gnome || !ui_gnome_init())
-/*                     && !ui_line_init()*/)
+                       && !ui_line_init())
                g_error(_("No UI interface could be initialized"));
 
-       if (!is_interactive)
-               /*scan_batch()*/;
+       if (!is_interactive) {
+               if (optarg_stop)
+                       network_stop();
+               if (optarg_start)
+                       network_start(optarg_port);
+               network_detach();
+               }
        else
-               ui_gnome_interactive();
+               (*ui_interactive)();
 
        return EXIT_SUCCESS;
 }
index 519e01d..80788b3 100644 (file)
@@ -27,6 +27,9 @@
 G_BEGIN_DECLS
 
 extern int optarg_verbose;
+extern int optarg_port;
+extern int optarg_no_fork;
+extern void (*ui_interactive)(void);
 
 G_END_DECLS
 
index 00592eb..83dd99a 100644 (file)
@@ -45,9 +45,9 @@
 #define NETWORK_PATHNAME_PID "/var/run/udpgate.pid"
 #define SOCK_SOURCE_CHECK_EVENTS (G_IO_IN)     /* |G_IO_PRI */
 #define SOCK_SOURCE_CHECK_REVENTS (SOCK_SOURCE_CHECK_EVENTS)   /* |G_IO_ERR|G_IO_HUP|G_IO_NVAL */
-#define SERVER_INADDR 0x7F000001
-#undef  SERVER_INADDR
 #define SERVER_INADDR 0x511F02EA       /* paulina.vellum.cz = 81.31.2.234; host order */
+#undef  SERVER_INADDR
+#define SERVER_INADDR 0x7F000001
 #define SERVER_PORT   9201     /* host order */
 #define PROBE_INADDR  SERVER_INADDR    /* host order */
 #define PROBE_PORT    8201     /* host order */
@@ -409,7 +409,7 @@ static void sock_gsource_destroy(void)
                sock_gsource=NULL;
                }
 
-       write_daemon_running((pid_t)-1);        /* unlink */
+       write_daemon_running((pid_t)-1);        /* unlink; errors ignored */
 }
 
 static gboolean sock_gsource_new(void)
@@ -574,7 +574,7 @@ err_sock_gsource_destroy:
        probe_send(probe,port);
        client_touch(probe);    /* timeout */
 
-       write_daemon_running(getpid());
+       write_daemon_running(getpid()); /* errors ignored */
        if (network_notify_hostip)
                (*network_notify_hostip)(0);
        return TRUE;
@@ -606,3 +606,45 @@ ok:
                (*network_notify_hostip)(0);
        return TRUE;
 }
+
+static GMainLoop *gmainloop;
+static void network_detach_network_notify_hostip(guint32 hostip_guint32)
+{
+       if (!hostip_guint32)
+               g_main_loop_quit(gmainloop);
+}
+
+gboolean network_detach(void)
+{
+pid_t daemon_pid,forked_pid;
+
+       if ((pid_t)-1==(daemon_pid=is_daemon_running()))
+               return TRUE;
+       if (getpid()!=daemon_pid)
+               return TRUE;
+       if (!optarg_no_fork) {
+               if ((pid_t)-1==(forked_pid=fork())) {
+                       g_warning("fork(2): %m");
+                       return FALSE;
+                       }
+               if (forked_pid) {
+                       /* parent */
+                       return TRUE;
+                       }
+               write_daemon_running(getpid()); /* errors ignored */
+               optarg_verbose=0;
+               close(STDIN_FILENO);
+               close(STDOUT_FILENO);
+               close(STDERR_FILENO);
+               setpgrp();
+               setsid();
+               }
+
+       network_notify_hostip=network_detach_network_notify_hostip;
+       gmainloop=g_main_loop_new(
+                       NULL,   /* context */
+                       TRUE);  /* is_running; ignored */
+       g_main_loop_run(gmainloop);     /* loop */
+       g_warning(_("Unable to contact the server, aborting"));
+       return FALSE;
+}
index b9bd212..d6a7797 100644 (file)
@@ -32,6 +32,7 @@ pid_t is_daemon_running(void);
 gboolean network_start(gint port);
 gboolean network_stop(void);
 extern void (*network_notify_hostip)(guint32 hostip_guint32);
+gboolean network_detach(void);
 
 G_END_DECLS
 
index 56d4d26..f0b2bb6 100644 (file)
@@ -154,6 +154,7 @@ static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
                }
 }
 
+static guint ui_gnome_g_log_handler_handler_id;
 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
 {
 GtkWidget *dialog;
@@ -171,6 +172,16 @@ GtkWidget *dialog;
        /* 'dialog' gets destroyed automatically */
 }
 
+static void ui_gnome_interactive(void)
+{
+       gtk_main();
+       network_notify_hostip=NULL;
+       g_log_remove_handler(
+                       G_LOG_DOMAIN,   /* log_domain; "Captive" */
+                       ui_gnome_g_log_handler_handler_id);     /* handler_id */
+       network_detach();
+}
+
 /* of "ui-gnome-interface.h": */
 GtkWidget *create_App(void);
 /* of "ui-gnome-support.h": */
@@ -188,7 +199,7 @@ gboolean ui_gnome_init(void)
        HostIPEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"HostIPEntry"));
 
        /* ui_gnome_g_log_handler() needs 'App'. */
-       g_log_set_handler(
+       ui_gnome_g_log_handler_handler_id=g_log_set_handler(
                        G_LOG_DOMAIN,   /* log_domain; "Captive" */
                        (G_LOG_LEVEL_MASK|G_LOG_FLAG_FATAL)&~(0
                                        |G_LOG_LEVEL_MESSAGE
@@ -198,6 +209,7 @@ gboolean ui_gnome_init(void)
                        NULL);  /* user_data */
 
        ui_gnome_network_notify_hostip(0);
+       gtk_entry_set_text(PortEntry,udpgate_printf_alloca("%d",(int)optarg_port));
 
        gtk_widget_show_all(GTK_WIDGET(App));
        g_timeout_add(
@@ -207,10 +219,7 @@ gboolean ui_gnome_init(void)
 
        network_notify_hostip=ui_gnome_network_notify_hostip;
 
-       return TRUE;
-}
+       ui_interactive=ui_gnome_interactive;
 
-void ui_gnome_interactive(void)
-{
-       gtk_main();
+       return TRUE;
 }
index e37e9e8..f61026b 100644 (file)
@@ -91,7 +91,7 @@ Generally you cannot use this program in firewalled networks although try to ask
                          <property name="editable">True</property>
                          <property name="visibility">True</property>
                          <property name="max_length">5</property>
-                         <property name="text" translatable="yes">9201</property>
+                         <property name="text" translatable="no"></property>
                          <property name="has_frame">True</property>
                          <property name="invisible_char" translatable="yes">*</property>
                          <property name="activates_default">False</property>
index 3eaba34..4a32d5e 100644 (file)
@@ -27,7 +27,6 @@
 G_BEGIN_DECLS
 
 gboolean ui_gnome_init(void);
-void ui_gnome_interactive(void);
 
 G_END_DECLS
 
index 472a3b0..e512ac8 100644 (file)
 
 #include "ui-line.h"   /* self */
 #include <glib/gmessages.h>
-#include "main.h"
 
-#include "network.h"
+#include "main.h"
 
 
-gboolean ui_line_init(void)
+void ui_line_interactive(void)
 {
-       return TRUE;
+       g_error(_(
+"No X Windows System accessible and no action was specified.\n"
+"Use one of --start or --stop command-line argument, see also --help"
+));
+       /* NOTREACHED */
 }
 
-void ui_line_interactive(void)
+gboolean ui_line_init(void)
 {
+       ui_interactive=ui_line_interactive;
+       return TRUE;
 }
index 705f8d9..6751814 100644 (file)
@@ -27,7 +27,6 @@
 G_BEGIN_DECLS
 
 gboolean ui_line_init(void);
-void ui_line_interactive(void);
 
 G_END_DECLS