[Pvfs2-cvs] commit by sampson in pvfs2/src/client/windows/client-service: cert.c client-service.h config.c fs.c fs.h service-main.c

CVS commit program cvs at parl.clemson.edu
Wed Jun 8 15:11:29 EDT 2011


Update of /projects/cvsroot/pvfs2/src/client/windows/client-service
In directory parlweb1:/tmp/cvs-serv5707/src/client/windows/client-service

Modified Files:
      Tag: windows-client
	cert.c client-service.h config.c fs.c fs.h service-main.c 
Log Message:
Windows client: added Event Log error reporting


Index: cert.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/cert.c,v
diff -p -u -r1.1.2.15 -r1.1.2.16
--- cert.c	31 May 2011 21:45:56 -0000	1.1.2.15
+++ cert.c	8 Jun 2011 19:11:28 -0000	1.1.2.16
@@ -37,6 +37,17 @@ void openssl_cleanup()
     ERR_remove_state(0);
 }
 
+/* certificate error reporting */
+static void report_cert_error(char *message)
+{
+    /* debug the message */
+    DbgPrint("   %s\n", message);
+
+    /* write to Event Log */
+    report_error_event(message, FALSE);
+
+}
+
 /* load certificate from file (PEM format) */
 static unsigned long load_cert_from_file(char *path, 
                                          X509 **cert)

Index: client-service.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/client-service.h,v
diff -p -u -r1.1.2.7 -r1.1.2.8
--- client-service.h	7 Jun 2011 21:32:00 -0000	1.1.2.7
+++ client-service.h	8 Jun 2011 19:11:28 -0000	1.1.2.8
@@ -42,4 +42,7 @@ typedef struct
 
 void DbgPrint(char *format, ...);
 
+BOOL report_error_event(char *message, 
+                        BOOL startup);
+
 #endif

Index: config.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/config.c,v
diff -p -u -r1.1.2.14 -r1.1.2.15
--- config.c	7 Jun 2011 21:32:00 -0000	1.1.2.14
+++ config.c	8 Jun 2011 19:11:28 -0000	1.1.2.15
@@ -178,7 +178,8 @@ static int parse_ldap_option(PORANGEFS_O
 
     if (options->user_mode != USER_MODE_LDAP)
     {
-        strncpy(error_msg, "Specify \"user-mode ldap\" before other ldap options\n", 
+        strncpy(error_msg, "Configuration file (fatal): "
+            "Specify \"user-mode ldap\" before other ldap options", 
             error_msg_len);
         goto parse_ldap_option_exit;
     }
@@ -261,7 +262,8 @@ static int parse_ldap_option(PORANGEFS_O
             options->ldap.search_scope = LDAP_SCOPE_SUBTREE;
         else
         {
-            strncpy(error_msg, "ldap-search-scope must be onelevel or subtree\n", error_msg_len);
+            strncpy(error_msg, "Configuration file (fatal): "
+                "ldap-search-scope must be onelevel or subtree", error_msg_len);
             goto parse_ldap_option_exit;
         }
 
@@ -309,13 +311,15 @@ static int parse_ldap_option(PORANGEFS_O
     }
     else
     {
-        _snprintf(error_msg, error_msg_len, "Invalid option %s\n", option);
+        _snprintf(error_msg, error_msg_len, "Configuration file (fatal): "
+            "Unknown option %s", option);
     }
 
 parse_ldap_option_exit:
 
     if (ret != 0 && strlen(error_msg) == 0)
-        _snprintf(error_msg, error_msg_len, "Could not parse option %s\n", option);
+        _snprintf(error_msg, error_msg_len, "Configuration file (fatal): "
+            "Could not parse option %s", option);
 
     return ret;
 }
@@ -398,7 +402,10 @@ int get_config(PORANGEFS_OPTIONS options
                 token = strtok(NULL, " \t");
                 if (token == NULL)
                 {
-                    _snprintf(error_msg, error_msg_len, "user-mode option must be list, certificate, or ldap\n");                    
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "user-mode option must be list, certificate, "
+                        "or ldap");
                     ret = -1;
                     goto get_config_exit;
                 }
@@ -416,7 +423,10 @@ int get_config(PORANGEFS_OPTIONS options
                 }
                 else
                 {
-                    _snprintf(error_msg, error_msg_len, "user-mode option must be list, certificate, or ldap\n");
+                    _snprintf(error_msg, error_msg_len,
+                        "Configuration file (fatal): "
+                        "user-mode option must be list, certificate, "
+                        "or ldap");
                     ret = -1;
                     goto get_config_exit;
                 }
@@ -425,20 +435,27 @@ int get_config(PORANGEFS_OPTIONS options
             {
                 if (options->user_mode == USER_MODE_NONE)
                 {
-                    _snprintf(error_msg, error_msg_len, "user option: specify 'user-mode list' above user option\n");
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "user option: specify 'user-mode list' above user "
+                        "option");
                     ret = -1;
                     goto get_config_exit;
                 }
                 else if (options->user_mode != USER_MODE_LIST)
                 {
-                    _snprintf(error_msg, error_msg_len, "user option: not legal with current user mode\n");
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "user option: not legal with current user mode");
                     ret = -1;
                     goto get_config_exit;
                 }
 
                 if (parse_user() != 0)
                 {
-                    _snprintf(error_msg, error_msg_len, "user option: parse error\n");
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "user option: parse error");
                     ret = -1;
                     goto get_config_exit;
                 }
@@ -456,7 +473,9 @@ int get_config(PORANGEFS_OPTIONS options
                 }
                 else
                 {
-                    _snprintf(error_msg, error_msg_len, "cert-dir-prefix option: parse error\n");
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "cert-dir-prefix option: parse error");
                     ret = -1;
                     goto get_config_exit;
                 }
@@ -472,7 +491,9 @@ int get_config(PORANGEFS_OPTIONS options
                 }
                 else
                 {
-                    _snprintf(error_msg, error_msg_len, "ca-path option: parse error\n");
+                    _snprintf(error_msg, error_msg_len, 
+                        "Configuration file (fatal): "
+                        "ca-path option: parse error\n");
                     ret = -1;
                     goto get_config_exit;
                 }
@@ -512,13 +533,16 @@ int get_config(PORANGEFS_OPTIONS options
             }
             else if (!strnicmp(token, "ldap", 4))
             {
-                ret = parse_ldap_option(options, line, token, error_msg, error_msg_len);
+                ret = parse_ldap_option(options, line, token, error_msg, 
+                    error_msg_len);
                 if (ret != 0)
                     goto get_config_exit;
             }
             else
             {
-                _snprintf(error_msg, error_msg_len, "Unknown option %s\n", token);
+                _snprintf(error_msg, error_msg_len, 
+                    "Configuration file (fatal): "
+                    "Unknown option %s", token);
                 ret = -1;
                 goto get_config_exit;
             }
@@ -527,8 +551,9 @@ int get_config(PORANGEFS_OPTIONS options
 
     if (options->user_mode == USER_MODE_NONE)
     {
-        _snprintf(error_msg, error_msg_len, "Must specify user-mode (list, "
-            "certificate or ldap)\n");
+        _snprintf(error_msg, error_msg_len, 
+            "Configuration file (fatal): "
+            "Must specify user-mode (list, certificate or ldap)");
         ret = -1;
         goto get_config_exit;
     }
@@ -536,17 +561,19 @@ int get_config(PORANGEFS_OPTIONS options
     if (options->user_mode == USER_MODE_LDAP &&
         (strlen(options->ldap.host) == 0 ||
          strlen(options->ldap.search_root) == 0))
-    {
-        _snprintf(error_msg, error_msg_len, "Missing ldap option: ldap-host, "
-            "or ldap-search-root\n");
+    {        
+        _snprintf(error_msg, error_msg_len, 
+            "Configuration file (fatal): "
+            "Missing ldap option: ldap-host, or ldap-search-root");
         ret = -1;
     }
 
     /* gossip can only print to either a file or stderr */
     if (options->debug_stderr && debug_file_flag)
     {
-        _snprintf(error_msg, error_msg_len, "Cannot specify both debug-stderr "
-            "and debug-file\n");
+        _snprintf(error_msg, error_msg_len, 
+            "Configuration file (fatal): "
+            "Cannot specify both debug-stderr and debug-file");
         ret = -1;
     }
 

Index: fs.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/fs.c,v
diff -p -u -r1.1.2.17 -r1.1.2.18
--- fs.c	13 Apr 2011 22:01:30 -0000	1.1.2.17
+++ fs.c	8 Jun 2011 19:11:29 -0000	1.1.2.18
@@ -31,15 +31,19 @@ int split_path(char *fs_path, 
 }
 
 /* initialize file systems */
-int fs_initialize(const char *tabfile)
+int fs_initialize(const char *tabfile, 
+                  char *error_msg,
+                  size_t error_msg_len)
 {
     int ret, i, found_one = 0;
+    char errbuf[256];
 
     /* read tab file */
     tab = PVFS_util_parse_pvfstab(tabfile);
     if (!tab)
     {
-        fprintf(stderr, "Error: failed to parse pvfstab\n");
+        _snprintf(error_msg, error_msg_len, "fs_initialize: failed to parse %s", 
+            tabfile);
         return -1;
     }
 
@@ -48,8 +52,9 @@ int fs_initialize(const char *tabfile)
     ret = PVFS_sys_initialize(GOSSIP_NO_DEBUG);
     if (ret < 0)
     {
-        PVFS_perror("PVFS_sys_initialize", ret);
-        return -1;
+        PVFS_strerror_r(ret, errbuf, 256);
+        _snprintf(error_msg, error_msg_len, "PVFS_sys_initialize: %s", errbuf);
+        return ret;
     }
     
     /* initialize file systems */
@@ -62,8 +67,8 @@ int fs_initialize(const char *tabfile)
 
     if (!found_one)
     {
-        fprintf(stderr, "Error: could not initialize any file systems "
-            "from %s\n", tab->tabfile_name);
+        _snprintf(error_msg, error_msg_len, "fs_initialize: could not initialize any "
+            "file systems from %s", tab->tabfile_name);
      
         PVFS_sys_finalize();
         return -1;

Index: fs.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/fs.h,v
diff -p -u -r1.1.2.12 -r1.1.2.13
--- fs.h	24 Mar 2011 21:32:57 -0000	1.1.2.12
+++ fs.h	8 Jun 2011 19:11:29 -0000	1.1.2.13
@@ -6,7 +6,9 @@
 
 #include "pvfs2.h"
 
-int fs_initialize(const char *tabfile);
+int fs_initialize(const char *tabfile,
+                  char *error_msg,
+                  size_t error_msg_len);
 
 struct PVFS_sys_mntent *fs_get_mntent(PVFS_fs_id fs_id);
 

Index: service-main.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/client/windows/client-service/Attic/service-main.c,v
diff -p -u -r1.1.2.17 -r1.1.2.18
--- service-main.c	7 Jun 2011 21:32:00 -0000	1.1.2.17
+++ service-main.c	8 Jun 2011 19:11:29 -0000	1.1.2.18
@@ -16,6 +16,7 @@
 #include "cert.h"
 #include "user-cache.h"
 #include "ldap-support.h"
+#include "messages.h"
 
 #define WIN32ServiceName           "orangefs-client"
 #define WIN32ServiceDisplayName    "OrangeFS Client"
@@ -34,6 +35,7 @@ int is_running = 0;
 int run_service = 0;  
 
 HANDLE hthread, hcache_thread;
+HANDLE hevent_log = NULL;
 
 DWORD thread_start(PORANGEFS_OPTIONS options);
 DWORD thread_stop();
@@ -105,6 +107,98 @@ void close_service_log()
     }
 }
 
+/* Open our Event Log entry (from registry) */
+DWORD init_event_log()
+{
+    hevent_log = RegisterEventSource(NULL, "OrangeFS Client");
+    return GetLastError();
+}
+
+/* Report an error to the Event Log, service log (file), and stderr. The 
+   entire text of the message is displayed without modification. */
+BOOL report_error_event(char *message, BOOL startup)
+{
+    char *strings[1];
+
+    /* startup errors also go to service log or stderr */
+    if (startup)
+    {
+        if (run_service)
+        {
+            service_debug("%s\n", message);
+        }
+        else
+        {
+            fprintf(stderr, "%s\n", message);
+        }
+    }
+
+    if (hevent_log != NULL)
+    {
+        strings[0] = message;
+
+        return ReportEvent(hevent_log, EVENTLOG_ERROR_TYPE, 0, 
+            MSG_GENERIC_ERROR, NULL, 1, 0, strings, NULL);
+    }
+
+    return FALSE;
+}
+
+/* Return the Windows error message for the specified code.
+   The returned string must be freed with LocalFree. */
+LPTSTR get_windows_message(DWORD err)
+{
+    LPVOID msg_buf;
+
+    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
+                  FORMAT_MESSAGE_FROM_SYSTEM |
+                  FORMAT_MESSAGE_IGNORE_INSERTS,
+                  NULL,
+                  err,
+                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                  (LPTSTR) &msg_buf,
+                  0, NULL);
+
+    return (LPTSTR) msg_buf;
+}
+
+/* Report a Windows error to the Event Log 
+   Format is {prefix}{windows msg} ({err}) */
+BOOL report_windows_error(char *prefix, DWORD err)
+{
+    LPTSTR win_msg, message;
+    size_t msg_len;
+    BOOL ret;
+
+    win_msg = get_windows_message(err);
+    if (win_msg == NULL)
+        return FALSE;
+
+    /* remove trailing \r\n from win_msg */
+    if (win_msg[strlen(win_msg)-1] == '\n')
+        win_msg[strlen(win_msg)-1] = '\0';
+    if (win_msg[strlen(win_msg)-1] == '\r')
+        win_msg[strlen(win_msg)-1] = '\0';
+
+    msg_len = strlen(prefix)+strlen(win_msg)+16;
+    message = (LPTSTR) LocalAlloc(0, msg_len);
+    _snprintf(message, msg_len, "%s%s (%u)", prefix, win_msg, err);
+
+    ret = report_error_event(message, TRUE);
+
+    LocalFree(message);
+    LocalFree(win_msg);
+
+    return ret;
+}
+
+/* Close our Event Log source */
+void close_event_log()
+{
+    if (hevent_log != NULL)
+        DeregisterEventSource(hevent_log);
+}
+
 BOOL check_mount_point(const char *mount_point)
 {
     const char *slash;
@@ -125,7 +219,7 @@ BOOL check_mount_point(const char *mount
     mask = GetLogicalDrives();
     if (mask == 0)
     {
-        fprintf(stderr, "GetLogicalDrives failed: %u\n", GetLastError());
+        report_windows_error("GetLogicalDrives failed: ", GetLastError());
         return FALSE;
     }
 
@@ -331,9 +425,9 @@ void WINAPI service_main(DWORD argc, cha
 
     if (ret != 0)
     {
-        service_debug(error_msg);
-        service_debug("Could not parse config file: exiting\n");
+        report_error_event(error_msg, TRUE);
         close_service_log();
+        close_event_log();
         return;
     }
 
@@ -367,8 +461,11 @@ void WINAPI service_main(DWORD argc, cha
         ret = cache_thread_start();
         if (ret != 0)
         {
-            service_debug("Could not start cache thread: %u\n", ret);
+            _snprintf(error_msg, sizeof(error_msg), "Fatal init error: could "
+                "not start cache thread: %u", ret);
+            report_error_event(error_msg, TRUE);
             close_service_log();
+            close_event_log();
             free(options);
             
             return;
@@ -404,14 +501,15 @@ void WINAPI service_main(DWORD argc, cha
     }
     else
     {
-        service_debug("RegisterServiceCtrlHandler failed: %u\n", GetLastError());
-        /* TODO: error reporting */
+        report_windows_error("RegisterServiceCtrlHandler failed: ", GetLastError());
     }
 
     qhash_destroy_and_finalize(user_cache, struct user_entry, hash_link, free);
     
     close_service_log();
 
+    close_event_log();
+
     free(options);
 }
 
@@ -435,7 +533,7 @@ DWORD thread_start(PORANGEFS_OPTIONS opt
     else
     {
         err = GetLastError();
-        service_debug("CreateThread failed: %u\n", err);
+        report_windows_error("CreateThread (main) failed: ", err);
     }
 
     service_debug("thread_start exit\n");
@@ -453,7 +551,7 @@ DWORD thread_stop()
     if (!TerminateThread(hthread, 0))
     {
         err = GetLastError();
-        service_debug("TerminateThread failed: %u\n", err);
+        report_windows_error("TerminateThread (main) failed: ", err);
     }
 
     service_debug("thread_stop exit\n");
@@ -474,7 +572,10 @@ DWORD cache_thread_start()
                                  NULL);
     
     if (hcache_thread == NULL)
+    {
         err = GetLastError();
+        report_windows_error("CreateThread (user cache) failed: ", err);
+    }
 
     return err;
 }
@@ -485,7 +586,10 @@ DWORD cache_thread_stop()
 
     if (hcache_thread != NULL)
         if (!TerminateThread(hcache_thread, 0))
+        {
             err = GetLastError();
+            report_windows_error("TerminateThread (user cache) failed: ", err);
+        }
 
     return err;
 }
@@ -493,7 +597,8 @@ DWORD cache_thread_stop()
 DWORD WINAPI main_loop(LPVOID poptions)
 {
     PORANGEFS_OPTIONS options = (PORANGEFS_OPTIONS) poptions;
-    char *tabfile, exe_path[MAX_PATH], *p;
+    char *tabfile, exe_path[MAX_PATH], *p, error_msg[256],
+         event_msg[512];
     FILE *f;
     int ret, malloc_flag = 0;
 
@@ -525,22 +630,20 @@ DWORD WINAPI main_loop(LPVOID poptions)
                 strcat(tabfile, "\\pvfs2tab");
             }
         }
-        else
-        {
-           fprintf(stderr, "GetModuleFileName failed: %u\n", GetLastError());
-        }
     }
 
     /* init file systems */
     if (tabfile)
     {
         service_debug("Using tabfile: %s\n", tabfile);
-        ret = fs_initialize(tabfile);
+        ret = fs_initialize(tabfile, error_msg, 256);
     }
     else
-        ret = ERROR_FILE_NOT_FOUND;
+    {
+        report_windows_error("GetModuleFileName failed: ", GetLastError());
+    }
 
-    /* run dokan operations */
+    /*** main loop - run dokan client ***/
     if (ret == 0)
     {
         dokan_loop(options);
@@ -550,8 +653,9 @@ DWORD WINAPI main_loop(LPVOID poptions)
     }
     else 
     {
-        service_debug("fs_initialize returned %d\n", ret);
-        fprintf(stderr, "fs_initialize returned %d\n", ret);
+        _snprintf(event_msg, sizeof(event_msg), "Fatal init error: %s",
+            error_msg);        
+        report_error_event(event_msg, TRUE);
     }
 
     if (malloc_flag)
@@ -609,13 +713,18 @@ int main(int argc, char **argv, char **e
       }
   }
 
+  /* init event log */
+  if ((err = init_event_log()) != 0)
+      /* since we can't log to event log, log to stderr */
+      fprintf(stderr, "Could not open event log: %u\n", err);
+
   /* initialize OpenSSL */
   openssl_init();
 
   /* initialize LDAP */
   if (PVFS_ldap_init() != 0)
   {
-      fprintf(stderr, "LDAP could not be initialized\n");
+      report_error_event("Fatal error: LDAP could not be initialized", TRUE);
       return 1;
   }
 
@@ -635,8 +744,7 @@ int main(int argc, char **argv, char **e
 
       /* get options from config file */
       if (get_config(options, error_msg, 512) != 0)
-      {
-          fprintf(stderr, error_msg);
+      {          
           err = 1;
           goto main_exit;
       }
@@ -677,7 +785,8 @@ int main(int argc, char **argv, char **e
 
       if (!check_mount_point(options->mount_point))
       {
-          fprintf(stderr, "Drive already in use\n");
+          _snprintf(error_msg, sizeof(error_msg), "Fatal error: %s already "
+              "in use", options->mount_point);
           err = 1;
           goto main_exit;
       }
@@ -686,7 +795,7 @@ int main(int argc, char **argv, char **e
       err = cache_thread_start();
       if (err != 0)
       {
-          fprintf(stderr, "User cache thread did not start: %u\n", err);
+          sprintf(error_msg, "Fatal error: user cache thread did not start");
           goto main_exit;
       }
 
@@ -694,7 +803,11 @@ int main(int argc, char **argv, char **e
 
       /* process requests */
       err = main_loop(options);
-      
+      if (err != 0)
+      {
+          sprintf(error_msg, "Main loop exited with error code: %d", err);
+      }
+
       printf("main_loop exited: %d\n", err);
 
       cache_thread_stop();
@@ -703,11 +816,18 @@ int main(int argc, char **argv, char **e
 
 main_exit:
 
+      if (err != 0)
+      {          
+          report_error_event(error_msg, TRUE);
+      }
+
       qhash_destroy_and_finalize(user_cache, struct user_entry, hash_link, free);
 
       PVFS_ldap_cleanup();
 
       openssl_cleanup();
+
+      close_event_log();
 
       free(options);
   }



More information about the Pvfs2-cvs mailing list