From 211f9c64a1c533d99c3c05c4d7b1bcaffdb7dbff Mon Sep 17 00:00:00 2001 From: short <> Date: Fri, 21 May 2004 17:46:28 +0000 Subject: [PATCH] Command-line version implemented. Daemon detaching implemented. --- src/main.c | 47 ++++++++++++++++++++++++++++++++++++----------- src/main.h | 3 +++ src/network.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- src/network.h | 1 + src/ui-gnome.c | 21 +++++++++++++++------ src/ui-gnome.glade | 2 +- src/ui-gnome.h | 1 - src/ui-line.c | 15 ++++++++++----- src/ui-line.h | 1 - 9 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/main.c b/src/main.c index 0f6b384..f649a3e 100644 --- a/src/main.c +++ b/src/main.c @@ -30,11 +30,24 @@ #include #include +#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; } diff --git a/src/main.h b/src/main.h index 519e01d..80788b3 100644 --- a/src/main.h +++ b/src/main.h @@ -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 diff --git a/src/network.c b/src/network.c index 00592eb..83dd99a 100644 --- a/src/network.c +++ b/src/network.c @@ -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; +} diff --git a/src/network.h b/src/network.h index b9bd212..d6a7797 100644 --- a/src/network.h +++ b/src/network.h @@ -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 diff --git a/src/ui-gnome.c b/src/ui-gnome.c index 56d4d26..f0b2bb6 100644 --- a/src/ui-gnome.c +++ b/src/ui-gnome.c @@ -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; } diff --git a/src/ui-gnome.glade b/src/ui-gnome.glade index e37e9e8..f61026b 100644 --- a/src/ui-gnome.glade +++ b/src/ui-gnome.glade @@ -91,7 +91,7 @@ Generally you cannot use this program in firewalled networks although try to ask True True 5 - 9201 + True * False diff --git a/src/ui-gnome.h b/src/ui-gnome.h index 3eaba34..4a32d5e 100644 --- a/src/ui-gnome.h +++ b/src/ui-gnome.h @@ -27,7 +27,6 @@ G_BEGIN_DECLS gboolean ui_gnome_init(void); -void ui_gnome_interactive(void); G_END_DECLS diff --git a/src/ui-line.c b/src/ui-line.c index 472a3b0..e512ac8 100644 --- a/src/ui-line.c +++ b/src/ui-line.c @@ -21,16 +21,21 @@ #include "ui-line.h" /* self */ #include -#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; } diff --git a/src/ui-line.h b/src/ui-line.h index 705f8d9..6751814 100644 --- a/src/ui-line.h +++ b/src/ui-line.h @@ -27,7 +27,6 @@ G_BEGIN_DECLS gboolean ui_line_init(void); -void ui_line_interactive(void); G_END_DECLS -- 1.8.3.1