Hi Alan,

The attached export file changes nanoprobe to use glib2 option processing.

It may be too big a change for you to take right now.

nanoprobe compiles & links correctly on windows with this change.

I left so old code within ifdef RHM blocks for reference. You could
remove these blocks if you wish.

Roger
# HG changeset patch
# User Roger Massey <[email protected]>
# Date 1364323124 21600
# Node ID b800e54233515e51e83bd59ecf157c54cde986ce
# Parent  a8b6ad2734b37804a60c0d1ca2e621f69598cf2e
Change nonoprobe to use glib2 command line option parser. With this change
nanoprobe builds clean on windows. It doesn't work on windows, just coimpiles & 
links.

diff -r a8b6ad2734b3 -r b800e5423351 clientlib/nanoprobe.c
--- a/clientlib/nanoprobe.c     Sun Mar 24 08:32:26 2013 -0600
+++ b/clientlib/nanoprobe.c     Tue Mar 26 12:38:44 2013 -0600
@@ -56,7 +56,7 @@
 void (*nanoprobe_comealive_agent)(HbListener*, guint64 howlate)        = NULL;
 NanoHbStats            nano_hbstats = {0U, 0U, 0U, 0U, 0U};
 gboolean               nano_connected = FALSE;
-int                    errcount = 0;
+WINEXPORT int                  errcount = 0;
 GMainLoop*             mainloop = NULL;
 
 FSTATIC void           nanoobey_sendexpecthb(AuthListener*, FrameSet* fs, 
NetAddr*);
diff -r a8b6ad2734b3 -r b800e5423351 include/misc.h
--- a/include/misc.h    Sun Mar 24 08:32:26 2013 -0600
+++ b/include/misc.h    Tue Mar 26 12:38:44 2013 -0600
@@ -42,15 +42,15 @@
 char * proj_get_sysname(void);                 ///< Return a malloced string 
of the system name
 
 /** Make a daemon process out of this process*/
-void daemonize_me(gboolean stay_in_foreground  ///< TRUE == don't fork
+WINEXPORT void daemonize_me(gboolean stay_in_foreground        ///< TRUE == 
don't fork
 ,                const char * dirtorunin       ///< Directory to cd to before 
running
 ,                const char* pidfile);         ///< pathname of pid file, or 
NULL
-void assimilation_openlog(const char* logname);                                
///< Open logs in our style (syslog)
-PidRunningStat are_we_already_running(const char * pidfile, int* pid); ///< 
Determine service status
-guint          pidrunningstat_to_status(PidRunningStat);               ///< 
Convert PidRunningStat to an exit code for status
+WINEXPORT void assimilation_openlog(const char* logname);                      
        ///< Open logs in our style (syslog)
+WINEXPORT PidRunningStat are_we_already_running(const char * pidfile, int* 
pid);       ///< Determine service status
+WINEXPORT guint                pidrunningstat_to_status(PidRunningStat);       
        ///< Convert PidRunningStat to an exit code for status
 gboolean       create_pid_file(const char * pidfile);                  ///< 
Create pid file - return TRUE on success
-void           remove_pid_file(const char * pidfile);                  ///< 
Remove pid file we created (if we created one)
-int            kill_pid_service(const char * pidfile, int signal);     ///< 
Issue given signal to the pidfile-indicated running process
+WINEXPORT void         remove_pid_file(const char * pidfile);                  
///< Remove pid file we created (if we created one)
+WINEXPORT int          kill_pid_service(const char * pidfile, int signal);     
///< Issue given signal to the pidfile-indicated running process
 void           rmpid_and_exit_on_signal(const char * pidfile, int signal);     
///< Issue given signal to the pidfile-indicated running process
 #endif /* MISC_H */
 ///@}
diff -r a8b6ad2734b3 -r b800e5423351 include/nanoprobe.h
--- a/include/nanoprobe.h       Sun Mar 24 08:32:26 2013 -0600
+++ b/include/nanoprobe.h       Tue Mar 26 12:38:44 2013 -0600
@@ -46,8 +46,8 @@
 WINEXPORT gboolean             nano_initiate_shutdown(void);
 WINEXPORT void                 nanoprobe_report_upstream(guint16 reporttype, 
NetAddr* who, const char * sysname, guint64 howlate);
 extern const char *            procname;       ///< process name
-extern int                     errcount;       ///< error count
-extern GMainLoop*              mainloop;
+WINEXPORT extern int                   errcount;       ///< error count
+WINEXPORT extern GMainLoop*            mainloop;
 extern gboolean                        nano_shutting_down;
 
 // Override this if you want to set up a non-standard deadtime agent. */
diff -r a8b6ad2734b3 -r b800e5423351 nanoprobe/nanoprobe.c
--- a/nanoprobe/nanoprobe.c     Sun Mar 24 08:32:26 2013 -0600
+++ b/nanoprobe/nanoprobe.c     Tue Mar 26 12:38:44 2013 -0600
@@ -29,7 +29,9 @@
 #include <string.h>
 #include <signal.h>
 #include <errno.h>
+#ifdef RHM
 #include <getopt.h>
+#endif
 #include <framesettypes.h>
 #include <frameset.h>
 #include <ctype.h>
@@ -50,13 +52,26 @@
 DEBUGDECLARATIONS
 
 #ifdef WIN32
+#define SEP "\\"
 #      undef HAS_FORK
+WINEXPORT int errcount;
+WINEXPORT GMainLoop*           mainloop;
+WINEXPORT void         remove_pid_file(const char * pidfile);
+WINEXPORT void daemonize_me(gboolean stay_in_foreground, const char * 
dirtorunin, const char* pidfile);
+WINEXPORT PidRunningStat are_we_already_running(const char * pidfile, int* 
pid);
+WINEXPORT int          kill_pid_service(const char * pidfile, int signal);
 #else
+#define SEP "/"
 #      define  HAS_FORK
 #endif
 
 #define                PIDFILENAME STD_PID_DIR "/nanoprobe"
 
+const char *           localaddr = NULL;
+const char *           cmaaddr = NULL;
+const char *           procname = "nanoprobe";
+void ignore_signal(int signum);
+
 gint64         pktcount = 0;
 NetIO*         nettransport;
 NetGSource*    netpkt;
@@ -110,7 +125,12 @@
        UNREF(fs);
        return TRUE;
 }
-
+FSTATIC void
+ignore_signal( int signum)
+{
+       signum--; //get rid of unused warning
+       return;
+}
 /// Signal reception function - signals stop by here...
 FSTATIC void
 catch_a_signal(int signum)
@@ -122,6 +142,7 @@
                case SIGTERM:
                        sigterm = TRUE;
                        break;
+#ifndef WIN32
                case SIGHUP:
                        sighup = TRUE;
                        break;
@@ -133,6 +154,7 @@
                        proj_class_decr_debug(NULL);
                        sigusr2 = TRUE;
                        break;
+#endif
        }
 }
 
@@ -164,6 +186,8 @@
        fprintf(stderr, "Legal arguments are:\n");
        fprintf(stderr, "\t-c --cmaaddr <address:port-of-CMA>\n");
        fprintf(stderr, "\t-b --bind <address:port-to-listen-on-locally>\n");
+       fprintf(stderr, "\t-t --ttl  <multi cast ttl (default == 31)>\n");
+#ifndef WIN32
 #ifdef HAS_FORK
        fprintf(stderr, "\t-f --foreground (stay in foreground.)\n");
 #endif
@@ -171,6 +195,9 @@
        fprintf(stderr, "\t-p --pidfile <pid-file-pathname>.\n");
        fprintf(stderr, "\t-s --status (report nanoprobe status)\n");
        fprintf(stderr, "\t-d --debug (increment debug level)\n");
+#else
+       fprintf(stderr, "\t-d --debug <set debug level>\n");
+#endif
 }
 
 /**
@@ -181,19 +208,44 @@
 int
 main(int argc, char **argv)
 {
-       const char              defaultCMAaddr[] = CMAADDR;
-       const char              defaultlocaladdress [] = NANOLISTENADDR;
+       static char             defaultCMAaddr[] = CMAADDR;
+       static char             defaultlocaladdress [] = NANOLISTENADDR;
        SignFrame*              signature = signframe_new(G_CHECKSUM_SHA256, 0);
        Listener*               otherlistener;
        ConfigContext*          config = configcontext_new(0);
        PacketDecoder*          decoder = nano_packet_decoder();
+#ifndef WIN32
        struct sigaction        sigact;
-       const char *            cmaaddr = defaultCMAaddr;
-       const char *            localaddr = defaultlocaladdress;
+#endif
+       static char *           localaddr = defaultlocaladdress;
+       static char *           cmaaddr = defaultCMAaddr;
+       static int debug = 0;
        gboolean                anyportpermitted = TRUE;
-       int                     c;
-       int                     mcast_ttl = 31;
-       gboolean                stay_in_foreground = FALSE;
+       static int                      mcast_ttl = 31;
+       static gboolean         stay_in_foreground = FALSE;
+       static gboolean         dostatusonly = FALSE;
+       static gboolean         dokillonly = FALSE;
+       static char*            pidfile = NULL;
+       gboolean                bindret;
+
+       GError *error = NULL;
+       static GOptionEntry     long_options[] = {
+               {"bind",        'b', 0, G_OPTION_ARG_STRING, &localaddr, 
"<address:port-to-listen-on-locally>", NULL},
+               {"cmaaddr",     'c', 0, G_OPTION_ARG_STRING, &cmaaddr,  
"<address:port-of-CMA>", NULL},
+               {"debug",       'd', 0, G_OPTION_ARG_INT,   &debug,   " set 
debug level", NULL},
+               {"ttl",         't', 0, G_OPTION_ARG_INT, &mcast_ttl, 
"<multicast-ttl> (default is 31)",        NULL},
+               {"kill",    'k', 0, G_OPTION_ARG_NONE, &dokillonly, "send 
SIGTERM to the running service", NULL},
+               {"pidfile", 'p', 0, G_OPTION_ARG_STRING, &pidfile, 
"<pid-file-pathname>", NULL},
+               {"status",  's', 0, G_OPTION_ARG_NONE, &dostatusonly, "report 
nanoprobe status", NULL},
+#ifdef HAS_FORK
+               {"foreground", 'f', 0, G_OPTION_ARG_NONE, &stay_in_foreground, 
"stay in foreground", NULL},
+#endif
+               {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
+       };
+
+       GOptionContext *context = g_option_context_new("- start nanoprobe");
+       g_option_context_add_main_entries(context, long_options, NULL);
+#ifdef RHM
        static struct option    long_options[] = {
                {"bind",        required_argument,      0,      'b'},
                {"cmaaddr",     required_argument,      0,      'c'},
@@ -212,12 +264,19 @@
        gboolean                dostatusonly = FALSE;
        gboolean                dokillonly = FALSE;
        int                     option_index = 0;
-       gboolean                bindret;
-       const char*             pidfile = NULL;
+#endif
        /// @todo initialize from a setup file - initial IP address:port, debug 
- anything else?
 
 
+       if(!(g_option_context_parse(context, &argc, &argv, &error)))
+       {
+               g_print("option parsing failed %s\n", error->message);
+               usage(argv[0]);
+               exit(1);
+       }
+       
        BINDDEBUG(NanoprobeMain);
+#ifdef RHM
        while (moreopts) {
                c = getopt_long(argc, argv, "b:c:dfkl:p:st:", long_options, 
&option_index);
                switch(c) {
@@ -275,8 +334,43 @@
                usage(argv[0]);
                exit(1);
        }
+#endif
+
+       if(debug > 0 && debug <= 5) {
+               while(debug--) {
+                       proj_class_incr_debug(NULL);
+               }
+       }
+
        if (pidfile == NULL) {
-               pidfile = PIDFILENAME;
+#ifdef WIN32
+
+               {const char * const *dirs;
+                char *w_pidfile;
+                PidRunningStat ret;
+                       dirs = g_get_system_config_dirs();
+                       w_pidfile = g_build_filename(dirs[0], procname, NULL);
+                       fprintf(stderr, "$s: pidfile = %s\n", "nanoprobe", 
w_pidfile);
+                       ret = are_we_already_running(w_pidfile, NULL);
+                       if(dostatusonly) {
+                               g_free(w_pidfile);
+                               exit(pidrunningstat_to_status(ret));
+                       }
+                       if (dokillonly) {
+                               int rc = kill_pid_service(w_pidfile, SIGTERM);
+                               g_free(w_pidfile);
+                               if (rc != 0) {
+                                       fprintf(stderr, "%s: could not stop 
service [%s]\n", "nanoprobe", g_strerror(errno));
+                                       exit(1);
+                               }
+                               exit(0);
+                       }
+                       daemonize_me(stay_in_foreground, SEP, w_pidfile);
+                       g_free(w_pidfile);
+               }
+       }
+#else
+               pidfile = (char *)PIDFILENAME;
        }
 
        if (dostatusonly) {
@@ -290,24 +384,38 @@
                }
                exit(0);
        }
+       daemonize_me(stay_in_foreground, SEP, pidfile);
+#endif
 
-       daemonize_me(stay_in_foreground, "/", pidfile);
+
        assimilation_openlog(argv[0]);
 
        if (!netio_is_dual_ipv4v6_stack()) {
                g_warning("This OS DOES NOT support dual ipv4/v6 sockets - this 
may not work!!");
        }
+#ifdef WIN32
+       signal(SIGTERM, catch_a_signal);
+#else
        memset(&sigact, 0,  sizeof(sigact));
        sigact.sa_handler = catch_a_signal;
        sigaction(SIGTERM, &sigact, NULL);
+#endif
        if (stay_in_foreground) {
+#ifdef WIN32
+               signal(SIGINT, catch_a_signal);
+#else
                struct sigaction        oldact;
                sigaction(SIGINT,  &sigact, &oldact); // Need to check to see 
if it's already blocked.
                if (oldact.sa_handler == SIG_IGN) {
                        // OOPS - put it back like it was
                        sigaction(SIGINT, &oldact, NULL);
                }
+#endif
        }else{
+#ifdef WIN32
+               signal(SIGINT, ignore_signal);
+       }
+#else
                // Always ignore SIGINT when in the background
                struct sigaction        ignoreme;
                memset(&ignoreme, 0,  sizeof(ignoreme));
@@ -316,6 +424,7 @@
        }
        sigaction(SIGUSR1, &sigact, NULL);
        sigaction(SIGUSR2, &sigact, NULL);
+#endif
 
        config->setframe(config, CONFIGNAME_OUTSIG, &signature->baseclass);
 
_______________________________________________
Assimilation mailing list - Discovery-Driven Monitoring
[email protected]
http://lists.community.tummy.com/cgi-bin/mailman/listinfo/assimilation
http://assimmon.org/

Reply via email to