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.
24 #include "connection.h"
39 /* If zero, don't detach from the terminal. */
40 bool do_detach = true;
44 extern bool use_logfile;
45 extern bool use_syslog;
47 /* Some functions the less gifted operating systems might lack... */
50 static SC_HANDLE manager = NULL;
51 static SC_HANDLE service = NULL;
52 static SERVICE_STATUS status = {0};
53 static SERVICE_STATUS_HANDLE statushandle = 0;
55 static bool install_service(void) {
56 char command[4096] = "\"";
57 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
59 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
62 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
66 HMODULE module = GetModuleHandle(NULL);
67 GetModuleFileName(module, command + 1, sizeof(command) - 1);
68 command[sizeof(command) - 1] = 0;
70 strncat(command, "\"", sizeof(command) - strlen(command));
72 for(char **argp = g_argv + 1; *argp; argp++) {
73 char *space = strchr(*argp, ' ');
74 strncat(command, " ", sizeof(command) - strlen(command));
77 strncat(command, "\"", sizeof(command) - strlen(command));
80 strncat(command, *argp, sizeof(command) - strlen(command));
83 strncat(command, "\"", sizeof(command) - strlen(command));
87 service = CreateService(manager, identname, identname,
88 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
89 command, NULL, NULL, NULL, NULL, NULL);
92 DWORD lasterror = GetLastError();
93 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
95 if(lasterror != ERROR_SERVICE_EXISTS) {
101 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
102 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
104 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
107 if(!StartService(service, 0, NULL)) {
108 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
110 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
118 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID data, LPVOID context) {
124 case SERVICE_CONTROL_INTERROGATE:
125 SetServiceStatus(statushandle, &status);
128 case SERVICE_CONTROL_STOP:
129 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
132 case SERVICE_CONTROL_SHUTDOWN:
133 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
137 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
138 return ERROR_CALL_NOT_IMPLEMENTED;
141 status.dwWaitHint = 1000;
142 status.dwCurrentState = SERVICE_STOP_PENDING;
143 SetServiceStatus(statushandle, &status);
145 if(WSASetEvent(stop_io.event) == FALSE) {
152 VOID WINAPI run_service(DWORD argc, LPTSTR *argv) {
153 extern int main2(int argc, char **argv);
155 status.dwServiceType = SERVICE_WIN32;
156 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
157 status.dwWin32ExitCode = 0;
158 status.dwServiceSpecificExitCode = 0;
159 status.dwCheckPoint = 0;
161 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
164 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
166 status.dwWaitHint = 30000;
167 status.dwCurrentState = SERVICE_START_PENDING;
168 SetServiceStatus(statushandle, &status);
170 status.dwWaitHint = 0;
171 status.dwCurrentState = SERVICE_RUNNING;
172 SetServiceStatus(statushandle, &status);
176 status.dwWaitHint = 0;
177 status.dwCurrentState = SERVICE_STOPPED;
178 SetServiceStatus(statushandle, &status);
184 bool init_service(void) {
185 SERVICE_TABLE_ENTRY services[] = {
186 {identname, run_service},
190 if(!StartServiceCtrlDispatcher(services)) {
191 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
194 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
203 Detach from current terminal
209 signal(SIGPIPE, SIG_IGN);
210 signal(SIGUSR1, SIG_IGN);
211 signal(SIGUSR2, SIG_IGN);
212 signal(SIGWINCH, SIG_IGN);
221 logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
228 exit(!install_service());
235 logmode = LOGMODE_FILE;
236 } else if(use_syslog || do_detach) {
237 logmode = LOGMODE_SYSLOG;
239 logmode = LOGMODE_STDERR;
242 openlogger(identname, logmode);
244 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
245 BUILD_VERSION, BUILD_DATE, BUILD_TIME, debug_level);