X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=io.cpp;h=41139a099a8d887926eb34f9786d62a5025a5e6a;hb=c03d51cbbfe9f4f8a28dbabbfc068573a1842b74;hp=a4f6abb5df09304551f1a1aa5e436ee96ab44318;hpb=fa2f3fe4a81e6958717ae05f6d37af2da91bcd66;p=nssm.git diff --git a/io.cpp b/io.cpp index a4f6abb..41139a0 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; } @@ -243,12 +231,40 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign } int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { - bool set_flags = false; + /* Allocate a new console so we get a fresh stdin, stdout and stderr. */ + if (si && ! service->no_console) { + FreeConsole(); + AllocConsole(); + banner(); + + /* Set a title like "[NSSM] Jenkins" */ + TCHAR displayname[SERVICE_NAME_LENGTH]; + unsigned long len = _countof(displayname); + SC_HANDLE services = open_service_manager(); + if (services) { + if (! GetServiceDisplayName(services, service->name, displayname, &len)) ZeroMemory(displayname, sizeof(displayname)); + CloseServiceHandle(services); + } + if (! displayname[0]) _sntprintf_s(displayname, _countof(displayname), _TRUNCATE, _T("%s"), service->name); - /* Standard security attributes allowing inheritance. */ - SECURITY_ATTRIBUTES attributes; - ZeroMemory(&attributes, sizeof(attributes)); - attributes.bInheritHandle = true; + TCHAR title[65535]; + _sntprintf_s(title, _countof(title), _TRUNCATE, _T("[%s] %s"), NSSM, displayname); + SetConsoleTitle(title); + } + + /* stdin */ + if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) { + service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0; + ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR)); + return 1; + } + if (si && service->stdin_path[0]) { + si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, 0, service->stdin_disposition, service->stdin_flags, 0); + if (! si->hStdInput) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0); + return 2; + } + } /* stdout */ if (get_createfile_parameters(key, NSSM_REG_STDOUT, service->stdout_path, &service->stdout_sharing, NSSM_STDOUT_SHARING, &service->stdout_disposition, NSSM_STDOUT_DISPOSITION, &service->stdout_flags, NSSM_STDOUT_FLAGS)) { @@ -258,7 +274,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) { @@ -278,8 +294,6 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } service->rotate_stdout_online = NSSM_ROTATE_OFFLINE; } - - set_flags = true; } /* stderr */ @@ -306,7 +320,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) { @@ -326,62 +340,38 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; } - - set_flags = true; } } - /* stdin */ - if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) { - service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0; - ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR)); - return 1; - } - if (si && service->stdin_path[0]) { - if (str_equiv(service->stdin_path, _T("|"))) { - /* Fake stdin with a pipe. */ - if (set_flags) { - /* - None of this is necessary if we aren't redirecting stdout and/or - stderr as well. - - If we don't redirect any handles the application will start and be - quite happy with its console. If we start it with - STARTF_USESTDHANDLES set it will interpret a NULL value for - hStdInput as meaning no input. Because the service starts with - no stdin we can't just pass GetStdHandle(STD_INPUT_HANDLE) to - the application. - - The only way we can successfully redirect the application's output - while preventing programs which exit after reading all input from - exiting prematurely is to create a pipe between ourselves and the - application but write nothing to it. - */ - if (! CreatePipe(&si->hStdInput, &service->stdin_pipe, 0, 0)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_STDIN_CREATEPIPE_FAILED, service->name, error_string(GetLastError()), 0); - return 2; - } - SetHandleInformation(si->hStdInput, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); - } - } - else { - si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, &attributes, service->stdin_disposition, service->stdin_flags, 0); - if (! si->hStdInput) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0); - return 2; - } - - set_flags = true; - } - } - - if (! set_flags) return 0; + if (! si) return 0; /* We need to set the startup_info flags to make the new handles inheritable by the new process. */ - if (si) si->dwFlags |= STARTF_USESTDHANDLES; + si->dwFlags |= STARTF_USESTDHANDLES; + + if (service->no_console) return 0; + + /* Redirect other handles. */ + if (! si->hStdInput) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE), GetCurrentProcess(), &si->hStdInput, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_INPUT_HANDLE"), _T("stdin"), error_string(GetLastError()), 0); + return 8; + } + } + if (! si->hStdOutput) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE), GetCurrentProcess(), &si->hStdOutput, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_OUTPUT_HANDLE"), _T("stdout"), error_string(GetLastError()), 0); + return 9; + } + } + if (! si->hStdError) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_ERROR_HANDLE"), _T("stderr"), error_string(GetLastError()), 0); + return 10; + } + } return 0; } @@ -560,7 +550,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);