2 process.c -- process management functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2011 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;
39 extern char *identname;
41 extern bool use_logfile;
43 /* Some functions the less gifted operating systems might lack... */
46 extern char *identname;
47 extern char *program_name;
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 bool install_service(void) {
56 char command[4096] = "\"";
59 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
61 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
63 logger(LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
67 if(!strchr(program_name, '\\')) {
68 GetCurrentDirectory(sizeof command - 1, command + 1);
69 strncat(command, "\\", sizeof command - strlen(command));
72 strncat(command, program_name, sizeof command - strlen(command));
74 strncat(command, "\"", sizeof command - strlen(command));
76 for(argp = g_argv + 1; *argp; argp++) {
77 space = strchr(*argp, ' ');
78 strncat(command, " ", sizeof command - strlen(command));
81 strncat(command, "\"", sizeof command - strlen(command));
83 strncat(command, *argp, sizeof command - strlen(command));
86 strncat(command, "\"", sizeof command - strlen(command));
89 service = CreateService(manager, identname, identname,
90 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
91 command, NULL, NULL, NULL, NULL, NULL);
94 DWORD lasterror = GetLastError();
95 logger(LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
96 if(lasterror != ERROR_SERVICE_EXISTS)
101 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
102 logger(LOG_INFO, "%s service installed", identname);
104 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
107 if(!StartService(service, 0, NULL))
108 logger(LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
110 logger(LOG_INFO, "%s service started", identname);
115 bool remove_service(void) {
116 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
118 logger(LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
122 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
125 logger(LOG_ERR, "Could not open %s service: %s", identname, winerror(GetLastError()));
129 if(!ControlService(service, SERVICE_CONTROL_STOP, &status))
130 logger(LOG_ERR, "Could not stop %s service: %s", identname, winerror(GetLastError()));
132 logger(LOG_INFO, "%s service stopped", identname);
134 if(!DeleteService(service)) {
135 logger(LOG_ERR, "Could not remove %s service: %s", identname, winerror(GetLastError()));
139 logger(LOG_INFO, "%s service removed", identname);
144 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
146 case SERVICE_CONTROL_INTERROGATE:
147 SetServiceStatus(statushandle, &status);
149 case SERVICE_CONTROL_STOP:
150 logger(LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
152 case SERVICE_CONTROL_SHUTDOWN:
153 logger(LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
156 logger(LOG_WARNING, "Got unexpected request %d", request);
157 return ERROR_CALL_NOT_IMPLEMENTED;
160 event_loopexit(NULL);
161 status.dwWaitHint = 30000;
162 status.dwCurrentState = SERVICE_STOP_PENDING;
163 SetServiceStatus(statushandle, &status);
167 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
169 extern int main2(int argc, char **argv);
172 status.dwServiceType = SERVICE_WIN32;
173 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
174 status.dwWin32ExitCode = 0;
175 status.dwServiceSpecificExitCode = 0;
176 status.dwCheckPoint = 0;
178 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
181 logger(LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
184 status.dwWaitHint = 30000;
185 status.dwCurrentState = SERVICE_START_PENDING;
186 SetServiceStatus(statushandle, &status);
188 status.dwWaitHint = 0;
189 status.dwCurrentState = SERVICE_RUNNING;
190 SetServiceStatus(statushandle, &status);
192 err = main2(argc, argv);
194 status.dwWaitHint = 0;
195 status.dwCurrentState = SERVICE_STOPPED;
196 //status.dwWin32ExitCode = err;
197 SetServiceStatus(statushandle, &status);
203 bool init_service(void) {
204 SERVICE_TABLE_ENTRY services[] = {
205 {identname, run_service},
209 if(!StartServiceCtrlDispatcher(services)) {
210 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
214 logger(LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
222 Detach from current terminal
226 signal(SIGALRM, SIG_IGN);
227 signal(SIGPIPE, SIG_IGN);
228 signal(SIGUSR1, SIG_IGN);
229 signal(SIGUSR2, SIG_IGN);
230 signal(SIGWINCH, SIG_IGN);
238 fprintf(stderr, "Couldn't detach from terminal: %s",
244 exit(install_service());
248 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
250 logger(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
251 VERSION, __DATE__, __TIME__, debug_level);
256 bool execute_script(const char *name, char **envp) {
263 len = xasprintf(&scriptname, "\"%s/%s\"", confbase, name);
265 len = xasprintf(&scriptname, "\"%s/%s.bat\"", confbase, name);
270 scriptname[len - 1] = '\0';
273 /* First check if there is a script */
275 if(access(scriptname + 1, F_OK)) {
281 ifdebug(STATUS) logger(LOG_INFO, "Executing script %s", name);
284 /* Set environment */
286 for(i = 0; envp[i]; i++)
290 scriptname[len - 1] = '\"';
291 status = system(scriptname);
295 /* Unset environment */
297 for(i = 0; envp[i]; i++) {
298 char *e = strchr(envp[i], '=');
300 char p[e - envp[i] + 1];
301 strncpy(p, envp[i], e - envp[i]);
302 p[e - envp[i]] = '\0';
309 if(WIFEXITED(status)) { /* Child exited by itself */
310 if(WEXITSTATUS(status)) {
311 logger(LOG_ERR, "Script %s exited with non-zero status %d",
312 name, WEXITSTATUS(status));
315 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
316 logger(LOG_ERR, "Script %s was killed by signal %d (%s)",
317 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
319 } else { /* Something strange happened */
320 logger(LOG_ERR, "Script %s terminated abnormally", name);
324 logger(LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));