Attached is an export file of changes which allow clientlib & serverlib to build on windows.
The executables need more work. Alan, feel free to change or ignore as you see fit. Roger
# HG changeset patch # User Roger Massey <[email protected]> # Date 1363893289 21600 # Node ID 2196db3eb8c03e917d1f0eeed919d4ba5caa9ffb # Parent 3f859593fdcb4ff1651eb8588eef173d2bf692b7 These changes allow clientlib & serverlib to compile cleanly on windows. The executables will not compile until more work is done. This is another small step. diff -r 3f859593fdcb -r 2196db3eb8c0 clientlib/misc.c --- a/clientlib/misc.c Wed Mar 20 14:20:19 2013 -0600 +++ b/clientlib/misc.c Thu Mar 21 13:14:49 2013 -0600 @@ -35,8 +35,13 @@ #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> +#ifndef WIN32 #include <sys/time.h> #include <sys/resource.h> +#else +#include <Windows.h> +#include <WinBase.h> +#endif #include <signal.h> #include <misc.h> @@ -58,15 +63,25 @@ char * proj_get_sysname(void) { - return g_strdup("Put Windows Code Here!"); +// BOOL WINAPI GetComputerName(_Out_ LPTSTR lpBuffer, _Inout_ LPDWORD lpnSize); + + char sn[MAX_COMPUTERNAME_LENGTH + 1]; + DWORD snsize = sizeof(sn); + BOOL ret; + + ret = GetComputerName((LPSTR) sn, &snsize); + if(ret) { + return g_strdup(sn); + } + + return g_strdup("GetComputerName failed"); } -# error "Need to replace code above with real Windows code..." # else # error "Need some function to get our computer name!" # endif #endif - +#ifndef WIN32 /// Make us into a proper daemon. void daemonize_me( gboolean stay_in_foreground, ///<[in] TRUE to not make a background job @@ -150,11 +165,30 @@ close(j); } } +#else +void +daemonize_me( gboolean stay_in_foreground, ///<[in] TRUE to not make a background job + const char* dirtorunin, ///<[in] Directory to cd to or NULL for default (/) + const char* pidfile) ///<[in] Pathname of pidfile or NULL for no pidfile +{ + if (pidfile) { + if (are_we_already_running(pidfile, NULL) == PID_RUNNING) { + g_message("Already running."); + exit(0); + } + } + // Exit if we can't create the requested pidfile + if (!create_pid_file(pidfile)) { + exit(1); + } +} +#endif static gboolean syslog_opened = FALSE; void assimilation_openlog(const char* logname) { +#ifndef WIN32 const int syslog_options = LOG_PID|LOG_NDELAY; const int syslog_facility = LOG_DAEMON; @@ -174,6 +208,7 @@ g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL); openlog(assim_syslogid, syslog_options, syslog_facility); syslog_opened = TRUE; +#endif } void assimilation_logger(const gchar *log_domain, ///< What domain are we logging to? @@ -181,6 +216,16 @@ const gchar *message, ///< What should we log gpointer ignored) ///< Ignored { +#ifdef WIN32 +#define LOG_INFO 6 +#define LOG_DEBUG 7 +#define LOG_NOTICE 5 +#define LOG_WARNING 4 +#define LOG_ERR 3 +#define LOG_CRIT 2 +#define LOG_ALERT 1 +#define LOG_EMERG 0 +#endif int syslogprio = LOG_INFO; const char * prefix = "INFO:"; @@ -212,19 +257,36 @@ syslogprio = LOG_EMERG; // Or maybe LOG_CRIT ? prefix = "EMERG"; } +#ifndef WIN32 syslog(syslogprio, "%s:%s %s", prefix , log_domain == NULL ? "" : log_domain , message); +#else + { + char msg[256]; + g_snprintf(msg, sizeof(msg), "%s: %s:%s %s\n",assim_syslogid, prefix + , log_domain == NULL ? "" : log_domain + , message); + OutputDebugString((LPCSTR) msg); + } +#endif fprintf(stderr, "%s: %s:%s %s\n", assim_syslogid, prefix , log_domain == NULL ? "" : log_domain , message); } - +#ifdef WIN32 +#define SEP '\\' +//TODO: these will be replaced when windows functionality cathes up +#define PROCSELFEXE "/" +#define PROCOTHEREXE "/" +#else +#define SEP '/' +#define PROCSELFEXE "/proc/self/exe" +#define PROCOTHEREXE "/proc/%d/exe" +#endif #define MAXPIDLEN 16 #define MAXPATH 256 -#define PROCSELFEXE "/proc/self/exe" -#define PROCOTHEREXE "/proc/%d/exe" static gboolean created_pid_file = FALSE; @@ -235,11 +297,15 @@ { char * pidcontents; // Contents of the pid file int pid; // Pid from the pid file - char pidexename[sizeof(PROCOTHEREXE)+16]; // Name of /proc entry for 'pid' char* ourexepath; // Pathname of our executable char* ourexecmd; // command name of our executable char* pidexepath; // Pathname of the 'pid' executable char* pidexecmd; // command name the 'pid' executable +#ifdef WIN32 + char w_ourexepath[MAXPATH]; + int nSize = MAXPATH-1, ret; +#endif + char pidexename[sizeof(PROCOTHEREXE)+16]; // Name of /proc entry for 'pid' //g_debug("%s.%d: PID file path [%s]", __FUNCTION__, __LINE__, pidfile); if (pidarg) { @@ -268,7 +334,12 @@ *pidarg = pid; } // Is it still running? - if (kill(pid, 0) < 0 && errno != EPERM) { +#ifdef WIN32 + if(TerminateProcess((void *)pid, 0) == 0) +#else + if (kill(pid, 0) < 0 && errno != EPERM) +#endif + { g_debug("%s.%d: PID %d is not running", __FUNCTION__, __LINE__, pid); return PID_DEAD; } @@ -276,12 +347,21 @@ // That is, is it the same executable as we are? // So, what is the pathname of our executable? +#ifndef WIN32 ourexepath = g_file_read_link(PROCSELFEXE, NULL); +#else + ret = GetModuleFileName(NULL, w_ourexepath, nSize); + if(ret == 0) { + g_debug("%s.%d: GetModuleFileName failed %d", __FUNCTION__, __LINE__, GetLastError()); + return(PID_DEAD); + } + ourexepath = g_strdup(w_ourexepath); +#endif if (NULL == ourexepath) { return PID_RUNNING; } - if (strrchr(ourexepath, '/') != NULL) { - ourexecmd = strrchr(ourexepath, '/')+1; + if (strrchr(ourexepath, SEP) != NULL) { + ourexecmd = strrchr(ourexepath, SEP)+1; }else{ ourexecmd = ourexepath; } @@ -293,8 +373,8 @@ g_free(ourexepath); ourexepath = NULL; return PID_RUNNING; } - if (strrchr(pidexepath, '/') != NULL) { - pidexecmd = strrchr(pidexepath, '/')+1; + if (strrchr(pidexepath, SEP) != NULL) { + pidexecmd = strrchr(pidexepath, SEP)+1; }else{ pidexecmd = pidexepath; } @@ -321,13 +401,22 @@ GError* errptr = NULL; PidRunningStat pstat; - - g_debug("%s.%d: Creating pid file %s for pid %d", __FUNCTION__, __LINE__, pidfile, getpid()); +#if _MSC_VER +__declspec(dllimport) +__out +void * +__stdcall +GetCurrentProcess(); +#define GETPID GetCurrentProcessId() +#else +#define GETPID getpid() +#endif + g_debug("%s.%d: Creating pid file %s for pid %d", __FUNCTION__, __LINE__, pidfile, GETPID); pstat = are_we_already_running(pidfile, NULL); if (PID_RUNNING == pstat) { return FALSE; } - g_snprintf(pidbuf, sizeof(pidbuf), "%6d\n", getpid()); + g_snprintf(pidbuf, sizeof(pidbuf), "%6d\n", GETPID); if (pstat == PID_DEAD || pstat == PID_NOTUS) { g_debug("%s.%d: Unlinking dead pid file %s", __FUNCTION__, __LINE__, pidfile); g_unlink(pidfile); @@ -336,10 +425,12 @@ if (g_file_set_contents(pidfile, pidbuf, strlen(pidbuf), &errptr)) { g_debug("%s.%d: Successfully set file %s to content [%s]" , __FUNCTION__, __LINE__, pidfile, pidbuf); +#ifndef WIN32 if (chmod(pidfile, 0644) < 0) { g_warning("%s.%d: Could not chmod pid file %s to 0644", __FUNCTION__, __LINE__ , pidfile); } +#endif created_pid_file = TRUE; return TRUE; } @@ -355,7 +446,7 @@ remove_pid_file(const char * pidfile) { if (created_pid_file) { - unlink(pidfile); + g_unlink(pidfile); } } @@ -368,24 +459,37 @@ pidstat = are_we_already_running(pidfile, &service_pid); if (pidstat == PID_RUNNING) { +#ifndef WIN32 return kill((pid_t)service_pid, signal); +#else + if(TerminateProcess((HANDLE) service_pid, signal) != 0) { + g_unlink(pidfile); + return(-1); + } +#endif } - unlink(pidfile); // No harm in removing it... + g_unlink(pidfile); // No harm in removing it... return 0; } static const char * saved_pidfile = NULL; void -rmpid_and_exit_on_signal(const char * pidfile, int signal) +rmpid_and_exit_on_signal(const char * pidfile, int signal_in) { +#ifndef WIN32 struct sigaction sigact; +#endif if (pidfile != NULL) { saved_pidfile = pidfile; } +#ifndef WIN32 memset(&sigact, 0, sizeof(sigact)); sigact.sa_handler = catch_pid_signal; - sigaction(signal, &sigact, NULL); + sigaction(signal_in, &sigact, NULL); +#else + signal(signal_in, catch_pid_signal); +#endif } FSTATIC void catch_pid_signal(int unused_signum) diff -r 3f859593fdcb -r 2196db3eb8c0 clientlib/nanoprobe.c --- a/clientlib/nanoprobe.c Wed Mar 20 14:20:19 2013 -0600 +++ b/clientlib/nanoprobe.c Thu Mar 21 13:14:49 2013 -0600 @@ -138,7 +138,7 @@ // Add the system name - if any... if (systemnm != NULL) { CstringFrame* usf = cstringframe_new(FRAMETYPE_HOSTNAME, 0); - usf->baseclass.setvalue(&usf->baseclass, strdup(systemnm), strlen(systemnm)+1 + usf->baseclass.setvalue(&usf->baseclass, g_strdup(systemnm), strlen(systemnm)+1 , frame_default_valuefinalize); frameset_append_frame(fs, &usf->baseclass); UNREF2(usf); @@ -293,7 +293,7 @@ case FRAMETYPE_HBINTERVAL: iframe = CASTTOCLASS(IntFrame, frame); - sendinterval = iframe->getint(iframe); + sendinterval = (guint16) iframe->getint(iframe); break; case FRAMETYPE_IPPORT: if (0 == sendinterval) { @@ -549,7 +549,7 @@ case FRAMETYPE_CINTVAL: { // Integer value to set 'paramname' to IntFrame* intf = CASTTOCLASS(IntFrame, frame); g_return_if_fail(paramname != NULL); - cfg->setint(cfg, paramname, intf->getint(intf)); + cfg->setint(cfg, paramname, (gint)intf->getint(intf)); paramname = NULL; } break; @@ -698,7 +698,7 @@ case FRAMETYPE_DISCINTERVAL: { // Discovery interval IntFrame* intf = CASTTOCLASS(IntFrame, frame); - interval = intf->getint(intf); + interval = (guint)intf->getint(intf); DEBUGMSG3("%s - got DISCOVERYINTERVAL %d", __FUNCTION__, interval); } break; @@ -879,7 +879,7 @@ // Put in the JSON discovery text jsontext = context->getstring(context, cfgname); csf = cstringframe_new(FRAMETYPE_JSDISCOVER, 0); - csf->baseclass.setvalue(&csf->baseclass, strdup(jsontext), strlen(jsontext)+1 + csf->baseclass.setvalue(&csf->baseclass, g_strdup(jsontext), strlen(jsontext)+1 , frame_default_valuefinalize); frameset_append_frame(fs, &csf->baseclass); diff -r 3f859593fdcb -r 2196db3eb8c0 clientlib/netio.c --- a/clientlib/netio.c Wed Mar 20 14:20:19 2013 -0600 +++ b/clientlib/netio.c Thu Mar 21 13:14:49 2013 -0600 @@ -34,6 +34,11 @@ #ifdef _MSC_VER # include <winsock2.h> # include <ws2tcpip.h> +#include <ws2ipdef.h> +#define ip_mreqn ip_mreq +#define imr_address imr_multiaddr +#define s6_addr16 u.Word +#define close closesocket #else # include <sys/socket.h> # include <netinet/in.h> @@ -232,7 +237,7 @@ ///< <255 Unrestricted in scope. Global. { int ttl = ttlin; - return setsockopt(self->getfd(self), IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl) == 0); + return setsockopt(self->getfd(self), IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl) == 0); } /// Member function that returns TRUE if input is ready to be read @@ -592,7 +597,12 @@ FREE(msgbuf); msgbuf = NULL; return NULL; } - + +#ifdef WIN32 +#define __in6_u u +#define __u6_addr8 Byte +#endif + if (memcmp(srcaddr->sin6_addr.__in6_u.__u6_addr8, v4any, sizeof(v4any)) == 0) { //const guint8 localhost[16] = CONST_IPV6_LOOPBACK; const guint8 localhost[16] = {CONST_IPV6_IPV4SPACE, 127, 0, 0, 1}; @@ -673,7 +683,9 @@ #ifdef IPV6_V6ONLY +#ifndef _MSC_VER # include <netdb.h> +#endif /// Return TRUE if our OS supports dual ipv4/ipv6 sockets. That is, /// can a single socket receive and send both ipv4 and ipv6 packets? @@ -692,7 +704,9 @@ return retval; } proto = getprotobyname("ipv6"); +#ifdef HAVE_ENDPROTOENT endprotoent(); +#endif g_return_val_if_fail(proto != NULL, FALSE); sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); @@ -700,7 +714,7 @@ optlen = sizeof(retval); optval = TRUE; - if (getsockopt(sockfd, proto->p_proto, IPV6_V6ONLY, &optval, &optlen) < 0) { + if (getsockopt(sockfd, proto->p_proto, IPV6_V6ONLY, (char *)&optval, &optlen) < 0) { g_warning("%s.%d: getsockopt failed: %s", __FUNCTION__, __LINE__ , g_strerror(errno)); close(sockfd); @@ -718,7 +732,7 @@ // This might be OK for other OSes too... if (optval) { optval = FALSE; - if (setsockopt(sockfd, proto->p_proto, IPV6_V6ONLY, &optval, &optlen) < 0) { + if (setsockopt(sockfd, proto->p_proto, IPV6_V6ONLY, (char *)&optval, optlen) < 0) { /// @todo this isn't perfect yet - see Microsoft note for ipv6-only stacks /// (presuming someone would disable ipv4 support from their machine) optval = TRUE; diff -r 3f859593fdcb -r 2196db3eb8c0 include/misc.h --- a/include/misc.h Wed Mar 20 14:20:19 2013 -0600 +++ b/include/misc.h Thu Mar 21 13:14:49 2013 -0600 @@ -23,7 +23,9 @@ */ #ifndef _MISC_H #include <projectcommon.h> +#ifndef WIN32 #include <syslog.h> +#endif #include <stdio.h> #define STD_PID_DIR "/var/run" diff -r 3f859593fdcb -r 2196db3eb8c0 include/nanoprobe.h --- a/include/nanoprobe.h Wed Mar 20 14:20:19 2013 -0600 +++ b/include/nanoprobe.h Thu Mar 21 13:14:49 2013 -0600 @@ -39,9 +39,9 @@ extern NanoHbStats nano_hbstats; extern gboolean nano_connected; -void nano_start_full(const char *initdiscoverpath, guint discover_interval +WINEXPORT void nano_start_full(const char *initdiscoverpath, guint discover_interval , NetGSource* io, ConfigContext* config); -void nano_shutdown(gboolean statreport); +WINEXPORT void nano_shutdown(gboolean statreport); WINEXPORT PacketDecoder* nano_packet_decoder(void); WINEXPORT gboolean nano_initiate_shutdown(void); WINEXPORT void nanoprobe_report_upstream(guint16 reporttype, NetAddr* who, const char * sysname, guint64 howlate); diff -r 3f859593fdcb -r 2196db3eb8c0 include/projectcommon.h.in --- a/include/projectcommon.h.in Wed Mar 20 14:20:19 2013 -0600 +++ b/include/projectcommon.h.in Thu Mar 21 13:14:49 2013 -0600 @@ -54,8 +54,12 @@ #include <glib.h> #if !HAVE_G_UNLINK /* #if (GLIB_MINOR_VERSION < 6) - but this doesn't work as I expected */ +#ifdef WIN32 +#define g_unlink(arg) _unlink(arg) +#else # define g_unlink(arg) unlink(arg) #endif +#endif #ifndef g_info # define g_info(...) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__) @@ -87,6 +91,8 @@ #cmakedefine HAVE_GETCOMPUTERNAME #cmakedefine HAVE_KILL #cmakedefine HAVE_UNAME +#cmakedefine HAVE_G_GET_REAL_TIME +#cmakedefine HAVE_G_GET_MONOTONIC_TIME #ifndef HAVE_G_GET_REAL_TIME WINEXPORT gint64 g_get_real_time(void);
_______________________________________________ Assimilation mailing list - Discovery-Driven Monitoring [email protected] http://lists.community.tummy.com/cgi-bin/mailman/listinfo/assimilation http://assimmon.org/
