Fixed CreateFile() flags when opening log files.
authorIain Patterson <me@iain.cx>
Sat, 25 Jan 2014 11:22:11 +0000 (11:22 +0000)
committerIain Patterson <me@iain.cx>
Sat, 25 Jan 2014 21:57:25 +0000 (21:57 +0000)
Opening a file with FILE_APPEND_DATA access is not appropriate when not
truncating existing files or opening new files.  Redirected I/O would be
buffered and output would not be immediately visible to readers.

io.cpp
io.h

diff --git a/io.cpp b/io.cpp
index a4f6abb..464e290 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -135,26 +135,14 @@ int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) {
   return 1;\r
 }\r
 \r
-HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {\r
-  HANDLE ret;\r
-\r
-  /* Try to append to the file first. */\r
-  ret = CreateFile(path, FILE_APPEND_DATA, sharing, attributes, disposition, flags, 0);\r
+HANDLE write_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {\r
+  HANDLE ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
   if (ret) {\r
-    SetEndOfFile(ret);\r
+    if (SetFilePointer(ret, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER) SetEndOfFile(ret);\r
     return ret;\r
   }\r
 \r
-  unsigned long error = GetLastError();\r
-  if (error != ERROR_FILE_NOT_FOUND) {\r
-    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(error), 0);\r
-    return (HANDLE) 0;\r
-  }\r
-\r
-  /* It didn't exist.  Create it. */\r
-  ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
-  if (! ret) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0);\r
-\r
+  log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0);\r
   return ret;\r
 }\r
 \r
@@ -258,7 +246,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   }\r
   if (si && service->stdout_path[0]) {\r
     if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-    HANDLE stdout_handle = append_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
+    HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
     if (! stdout_handle) return 4;\r
 \r
     if (service->rotate_files && service->rotate_stdout_online) {\r
@@ -306,7 +294,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     }\r
     else if (si) {\r
       if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-      HANDLE stderr_handle = append_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags);\r
+      HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags);\r
       if (! stderr_handle) return 7;\r
 \r
       if (service->rotate_files && service->rotate_stderr_online) {\r
@@ -560,7 +548,7 @@ unsigned long WINAPI log_and_rotate(void *arg) {
           }\r
 \r
           /* Reopen. */\r
-          logger->write_handle = append_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags);\r
+          logger->write_handle = write_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags);\r
           if (! logger->write_handle) {\r
             error = GetLastError();\r
             log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0);\r
diff --git a/io.h b/io.h
index d91d39c..cb067eb 100644 (file)
--- a/io.h
+++ b/io.h
@@ -27,7 +27,7 @@ typedef struct {
 int get_createfile_parameters(HKEY, TCHAR *, TCHAR *, unsigned long *, unsigned long, unsigned long *, unsigned long, unsigned long *, unsigned long);\r
 int set_createfile_parameter(HKEY, TCHAR *, TCHAR *, unsigned long);\r
 int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *);\r
-HANDLE append_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long);\r
+HANDLE write_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long);\r
 void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long);\r
 int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *);\r
 void close_output_handles(STARTUPINFO *);\r