| 1725 | } |
| 1726 | |
| 1727 | static void WINAPI |
| 1728 | pgwin32_ServiceMain(DWORD argc, LPTSTR *argv) |
| 1729 | { |
| 1730 | PROCESS_INFORMATION pi; |
| 1731 | DWORD ret; |
| 1732 | |
| 1733 | /* Initialize variables */ |
| 1734 | status.dwWin32ExitCode = S_OK; |
| 1735 | status.dwCheckPoint = 0; |
| 1736 | status.dwWaitHint = 60000; |
| 1737 | status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
| 1738 | status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE; |
| 1739 | status.dwServiceSpecificExitCode = 0; |
| 1740 | status.dwCurrentState = SERVICE_START_PENDING; |
| 1741 | |
| 1742 | memset(&pi, 0, sizeof(pi)); |
| 1743 | |
| 1744 | read_post_opts(); |
| 1745 | |
| 1746 | /* Register the control request handler */ |
| 1747 | if ((hStatus = RegisterServiceCtrlHandler(register_servicename, pgwin32_ServiceHandler)) == (SERVICE_STATUS_HANDLE) 0) |
| 1748 | return; |
| 1749 | |
| 1750 | if ((shutdownEvent = CreateEvent(NULL, true, false, NULL)) == NULL) |
| 1751 | return; |
| 1752 | |
| 1753 | /* Start the postmaster */ |
| 1754 | pgwin32_SetServiceStatus(SERVICE_START_PENDING); |
| 1755 | if (!CreateRestrictedProcess(pgwin32_CommandLine(false), &pi, true)) |
| 1756 | { |
| 1757 | pgwin32_SetServiceStatus(SERVICE_STOPPED); |
| 1758 | return; |
| 1759 | } |
| 1760 | postmasterPID = pi.dwProcessId; |
| 1761 | postmasterProcess = pi.hProcess; |
| 1762 | CloseHandle(pi.hThread); |
| 1763 | |
| 1764 | if (do_wait) |
| 1765 | { |
| 1766 | write_eventlog(EVENTLOG_INFORMATION_TYPE, _("Waiting for server startup...\n")); |
| 1767 | if (wait_for_postmaster_start(postmasterPID, true) != POSTMASTER_READY) |
| 1768 | { |
| 1769 | write_eventlog(EVENTLOG_ERROR_TYPE, _("Timed out waiting for server startup\n")); |
| 1770 | pgwin32_SetServiceStatus(SERVICE_STOPPED); |
| 1771 | return; |
| 1772 | } |
| 1773 | write_eventlog(EVENTLOG_INFORMATION_TYPE, _("Server started and accepting connections\n")); |
| 1774 | } |
| 1775 | |
| 1776 | pgwin32_SetServiceStatus(SERVICE_RUNNING); |
| 1777 | |
| 1778 | /* Wait for quit... */ |
| 1779 | ret = WaitForMultipleObjects(2, shutdownHandles, FALSE, INFINITE); |
| 1780 | |
| 1781 | pgwin32_SetServiceStatus(SERVICE_STOP_PENDING); |
| 1782 | switch (ret) |
| 1783 | { |
| 1784 | case WAIT_OBJECT_0: /* shutdown event */ |
nothing calls this directly
no test coverage detected