2 process.c -- process management functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2013 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 boe, LPVOID bah) {
120 case SERVICE_CONTROL_INTERROGATE:
121 SetServiceStatus(statushandle, &status);
124 case SERVICE_CONTROL_STOP:
125 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
128 case SERVICE_CONTROL_SHUTDOWN:
129 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
133 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
134 return ERROR_CALL_NOT_IMPLEMENTED;
137 status.dwWaitHint = 1000;
138 status.dwCurrentState = SERVICE_STOP_PENDING;
139 SetServiceStatus(statushandle, &status);
141 if(WSASetEvent(stop_io.event) == FALSE) {
148 VOID WINAPI run_service(DWORD argc, LPTSTR *argv) {
149 extern int main2(int argc, char **argv);
151 status.dwServiceType = SERVICE_WIN32;
152 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
153 status.dwWin32ExitCode = 0;
154 status.dwServiceSpecificExitCode = 0;
155 status.dwCheckPoint = 0;
157 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
160 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
162 status.dwWaitHint = 30000;
163 status.dwCurrentState = SERVICE_START_PENDING;
164 SetServiceStatus(statushandle, &status);
166 status.dwWaitHint = 0;
167 status.dwCurrentState = SERVICE_RUNNING;
168 SetServiceStatus(statushandle, &status);
172 status.dwWaitHint = 0;
173 status.dwCurrentState = SERVICE_STOPPED;
174 SetServiceStatus(statushandle, &status);
180 bool init_service(void) {
181 SERVICE_TABLE_ENTRY services[] = {
182 {identname, run_service},
186 if(!StartServiceCtrlDispatcher(services)) {
187 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
190 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
199 Detach from current terminal
205 signal(SIGPIPE, SIG_IGN);
206 signal(SIGUSR1, SIG_IGN);
207 signal(SIGUSR2, SIG_IGN);
208 signal(SIGWINCH, SIG_IGN);
217 logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
224 exit(!install_service());
231 logmode = LOGMODE_FILE;
232 } else if(use_syslog || do_detach) {
233 logmode = LOGMODE_SYSLOG;
235 logmode = LOGMODE_STDERR;
238 openlogger(identname, logmode);
240 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
241 BUILD_VERSION, BUILD_DATE, BUILD_TIME, debug_level);