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"
38 /* If zero, don't detach from the terminal. */
39 bool do_detach = true;
43 extern bool use_logfile;
45 /* Some functions the less gifted operating systems might lack... */
48 static SC_HANDLE manager = NULL;
49 static SC_HANDLE service = NULL;
50 static SERVICE_STATUS status = {0};
51 static SERVICE_STATUS_HANDLE statushandle = 0;
53 static bool install_service(void) {
54 char command[4096] = "\"";
55 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
57 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
59 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
63 if(!strchr(program_name, '\\')) {
64 GetCurrentDirectory(sizeof command - 1, command + 1);
65 strncat(command, "\\", sizeof command - strlen(command));
68 strncat(command, program_name, sizeof command - strlen(command));
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));
79 strncat(command, *argp, sizeof command - strlen(command));
82 strncat(command, "\"", sizeof command - strlen(command));
85 service = CreateService(manager, identname, identname,
86 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
87 command, NULL, NULL, NULL, NULL, NULL);
90 DWORD lasterror = GetLastError();
91 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
92 if(lasterror != ERROR_SERVICE_EXISTS)
97 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
98 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
100 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
103 if(!StartService(service, 0, NULL))
104 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
106 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
113 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
115 case SERVICE_CONTROL_INTERROGATE:
116 SetServiceStatus(statushandle, &status);
118 case SERVICE_CONTROL_STOP:
119 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
121 case SERVICE_CONTROL_SHUTDOWN:
122 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
125 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
126 return ERROR_CALL_NOT_IMPLEMENTED;
129 status.dwWaitHint = 1000;
130 status.dwCurrentState = SERVICE_STOP_PENDING;
131 SetServiceStatus(statushandle, &status);
132 if (WSASetEvent(stop_io.event) == FALSE)
137 static void stop_handler(void *data, int flags) {
141 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
142 extern int main2(int argc, char **argv);
144 io_add_event(&stop_io, stop_handler, NULL, WSACreateEvent());
145 if (stop_io.event == FALSE)
148 status.dwServiceType = SERVICE_WIN32;
149 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
150 status.dwWin32ExitCode = 0;
151 status.dwServiceSpecificExitCode = 0;
152 status.dwCheckPoint = 0;
154 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
157 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
159 status.dwWaitHint = 30000;
160 status.dwCurrentState = SERVICE_START_PENDING;
161 SetServiceStatus(statushandle, &status);
163 status.dwWaitHint = 0;
164 status.dwCurrentState = SERVICE_RUNNING;
165 SetServiceStatus(statushandle, &status);
169 status.dwWaitHint = 0;
170 status.dwCurrentState = SERVICE_STOPPED;
171 SetServiceStatus(statushandle, &status);
174 if (WSACloseEvent(stop_io.event) == FALSE)
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) {
191 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
199 Detach from current terminal
203 signal(SIGPIPE, SIG_IGN);
204 signal(SIGUSR1, SIG_IGN);
205 signal(SIGUSR2, SIG_IGN);
206 signal(SIGWINCH, SIG_IGN);
214 logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
219 exit(!install_service());
223 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
225 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
226 VERSION, __DATE__, __TIME__, debug_level);