Daemon detaching implemented.
#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[]={
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
}
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;
}
G_BEGIN_DECLS
extern int optarg_verbose;
+extern int optarg_port;
+extern int optarg_no_fork;
+extern void (*ui_interactive)(void);
G_END_DECLS
#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 */
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)
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;
(*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;
+}
gboolean network_start(gint port);
gboolean network_stop(void);
extern void (*network_notify_hostip)(guint32 hostip_guint32);
+gboolean network_detach(void);
G_END_DECLS
}
}
+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;
/* '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": */
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
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(
network_notify_hostip=ui_gnome_network_notify_hostip;
- return TRUE;
-}
+ ui_interactive=ui_gnome_interactive;
-void ui_gnome_interactive(void)
-{
- gtk_main();
+ return TRUE;
}
<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>
G_BEGIN_DECLS
gboolean ui_gnome_init(void);
-void ui_gnome_interactive(void);
G_END_DECLS
#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;
}
G_BEGIN_DECLS
gboolean ui_line_init(void);
-void ui_line_interactive(void);
G_END_DECLS