2 process.c -- process management functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2018 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 /* If zero, don't detach from the terminal. */
33 bool do_detach = true;
36 extern bool use_logfile;
37 extern bool use_syslog;
39 /* Some functions the less gifted operating systems might lack... */
42 static SC_HANDLE manager = NULL;
43 static SC_HANDLE service = NULL;
44 static SERVICE_STATUS status = {0};
45 static SERVICE_STATUS_HANDLE statushandle = 0;
47 static bool install_service(void) {
48 char command[4096] = "\"";
49 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
51 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
54 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
58 HMODULE module = GetModuleHandle(NULL);
59 GetModuleFileName(module, command + 1, sizeof(command) - 1);
60 command[sizeof(command) - 1] = 0;
62 strncat(command, "\"", sizeof(command) - strlen(command));
64 for(char **argp = g_argv + 1; *argp; argp++) {
65 char *space = strchr(*argp, ' ');
66 strncat(command, " ", sizeof(command) - strlen(command));
69 strncat(command, "\"", sizeof(command) - strlen(command));
72 strncat(command, *argp, sizeof(command) - strlen(command));
75 strncat(command, "\"", sizeof(command) - strlen(command));
79 service = CreateService(manager, identname, identname,
80 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
81 command, NULL, NULL, NULL, NULL, NULL);
84 DWORD lasterror = GetLastError();
85 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
87 if(lasterror != ERROR_SERVICE_EXISTS) {
93 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
94 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
96 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
99 if(!StartService(service, 0, NULL)) {
100 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
102 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
110 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID data, LPVOID context) {
116 case SERVICE_CONTROL_INTERROGATE:
117 SetServiceStatus(statushandle, &status);
120 case SERVICE_CONTROL_STOP:
121 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
124 case SERVICE_CONTROL_SHUTDOWN:
125 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
129 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
130 return ERROR_CALL_NOT_IMPLEMENTED;
133 status.dwWaitHint = 1000;
134 status.dwCurrentState = SERVICE_STOP_PENDING;
135 SetServiceStatus(statushandle, &status);
137 if(WSASetEvent(stop_io.event) == FALSE) {
144 VOID WINAPI run_service(DWORD argc, LPTSTR *argv) {
145 extern int main2(int argc, char **argv);
147 status.dwServiceType = SERVICE_WIN32;
148 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
149 status.dwWin32ExitCode = 0;
150 status.dwServiceSpecificExitCode = 0;
151 status.dwCheckPoint = 0;
153 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
156 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
158 status.dwWaitHint = 30000;
159 status.dwCurrentState = SERVICE_START_PENDING;
160 SetServiceStatus(statushandle, &status);
162 status.dwWaitHint = 0;
163 status.dwCurrentState = SERVICE_RUNNING;
164 SetServiceStatus(statushandle, &status);
168 status.dwWaitHint = 0;
169 status.dwCurrentState = SERVICE_STOPPED;
170 SetServiceStatus(statushandle, &status);
176 bool init_service(void) {
177 SERVICE_TABLE_ENTRY services[] = {
178 {identname, run_service},
182 if(!StartServiceCtrlDispatcher(services)) {
183 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
186 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
195 Detach from current terminal
201 signal(SIGPIPE, SIG_IGN);
202 signal(SIGUSR1, SIG_IGN);
203 signal(SIGUSR2, SIG_IGN);
204 signal(SIGWINCH, SIG_IGN);
213 logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
220 exit(!install_service());
227 logmode = LOGMODE_FILE;
228 } else if(use_syslog || do_detach) {
229 logmode = LOGMODE_SYSLOG;
231 logmode = LOGMODE_STDERR;
234 openlogger(identname, logmode);
236 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
237 BUILD_VERSION, BUILD_DATE, BUILD_TIME, debug_level);