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"
35 /* If zero, don't detach from the terminal. */
36 bool do_detach = true;
40 extern bool use_logfile;
42 /* Some functions the less gifted operating systems might lack... */
45 static SC_HANDLE manager = NULL;
46 static SC_HANDLE service = NULL;
47 static SERVICE_STATUS status = {0};
48 static SERVICE_STATUS_HANDLE statushandle = 0;
50 static bool install_service(void) {
51 char command[4096] = "\"";
52 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
54 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
56 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
60 if(!strchr(program_name, '\\')) {
61 GetCurrentDirectory(sizeof command - 1, command + 1);
62 strncat(command, "\\", sizeof command - strlen(command));
65 strncat(command, program_name, sizeof command - strlen(command));
67 strncat(command, "\"", sizeof command - strlen(command));
69 for(char **argp = g_argv + 1; *argp; argp++) {
70 char *space = strchr(*argp, ' ');
71 strncat(command, " ", sizeof command - strlen(command));
74 strncat(command, "\"", sizeof command - strlen(command));
76 strncat(command, *argp, sizeof command - strlen(command));
79 strncat(command, "\"", sizeof command - strlen(command));
82 service = CreateService(manager, identname, identname,
83 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
84 command, NULL, NULL, NULL, NULL, NULL);
87 DWORD lasterror = GetLastError();
88 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
89 if(lasterror != ERROR_SERVICE_EXISTS)
94 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
95 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
97 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
100 if(!StartService(service, 0, NULL))
101 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
103 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
108 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
110 case SERVICE_CONTROL_INTERROGATE:
111 SetServiceStatus(statushandle, &status);
113 case SERVICE_CONTROL_STOP:
114 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
116 case SERVICE_CONTROL_SHUTDOWN:
117 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
120 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
121 return ERROR_CALL_NOT_IMPLEMENTED;
125 status.dwWaitHint = 30000;
126 status.dwCurrentState = SERVICE_STOP_PENDING;
127 SetServiceStatus(statushandle, &status);
131 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
132 extern int main2(int argc, char **argv);
134 status.dwServiceType = SERVICE_WIN32;
135 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
136 status.dwWin32ExitCode = 0;
137 status.dwServiceSpecificExitCode = 0;
138 status.dwCheckPoint = 0;
140 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
143 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
145 status.dwWaitHint = 30000;
146 status.dwCurrentState = SERVICE_START_PENDING;
147 SetServiceStatus(statushandle, &status);
149 status.dwWaitHint = 0;
150 status.dwCurrentState = SERVICE_RUNNING;
151 SetServiceStatus(statushandle, &status);
155 status.dwWaitHint = 0;
156 status.dwCurrentState = SERVICE_STOPPED;
157 SetServiceStatus(statushandle, &status);
163 bool init_service(void) {
164 SERVICE_TABLE_ENTRY services[] = {
165 {identname, run_service},
169 if(!StartServiceCtrlDispatcher(services)) {
170 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
174 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
182 Detach from current terminal
186 signal(SIGPIPE, SIG_IGN);
187 signal(SIGUSR1, SIG_IGN);
188 signal(SIGUSR2, SIG_IGN);
189 signal(SIGWINCH, SIG_IGN);
197 logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
202 exit(!install_service());
206 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
208 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
209 VERSION, __DATE__, __TIME__, debug_level);