From: Iain Patterson Date: Sat, 25 Jan 2014 11:22:11 +0000 (+0000) Subject: Fixed CreateFile() flags when opening log files. X-Git-Tag: v2.22~38 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=ba4831e5eb179e415282a957604f039bceb4804f;p=nssm.git Fixed CreateFile() flags when opening log files. 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. --- diff --git a/io.cpp b/io.cpp index a4f6abb..464e290 100644 --- a/io.cpp +++ b/io.cpp @@ -135,26 +135,14 @@ int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) { return 1; } -HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) { - HANDLE ret; - - /* Try to append to the file first. */ - ret = CreateFile(path, FILE_APPEND_DATA, sharing, attributes, disposition, flags, 0); +HANDLE write_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) { + HANDLE ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0); if (ret) { - SetEndOfFile(ret); + if (SetFilePointer(ret, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER) SetEndOfFile(ret); return ret; } - unsigned long error = GetLastError(); - if (error != ERROR_FILE_NOT_FOUND) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(error), 0); - return (HANDLE) 0; - } - - /* It didn't exist. Create it. */ - ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0); - if (! ret) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0); - + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0); return ret; } @@ -258,7 +246,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } if (si && service->stdout_path[0]) { if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high); - HANDLE stdout_handle = append_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags); + HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags); if (! stdout_handle) return 4; if (service->rotate_files && service->rotate_stdout_online) { @@ -306,7 +294,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } else if (si) { if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high); - HANDLE stderr_handle = append_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags); + HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags); if (! stderr_handle) return 7; if (service->rotate_files && service->rotate_stderr_online) { @@ -560,7 +548,7 @@ unsigned long WINAPI log_and_rotate(void *arg) { } /* Reopen. */ - logger->write_handle = append_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags); + logger->write_handle = write_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags); if (! logger->write_handle) { error = GetLastError(); log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0); diff --git a/io.h b/io.h index d91d39c..cb067eb 100644 --- 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); int set_createfile_parameter(HKEY, TCHAR *, TCHAR *, unsigned long); int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *); -HANDLE append_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long); +HANDLE write_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long); void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long); int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *); void close_output_handles(STARTUPINFO *);