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 static void memory_full(int size) {
44 logger(LOG_ERR, "Memory exhausted (couldn't allocate %d bytes), exitting.", size);
48 /* Some functions the less gifted operating systems might lack... */
51 extern char *identname;
52 extern char *program_name;
55 static SC_HANDLE manager = NULL;
56 static SC_HANDLE service = NULL;
57 static SERVICE_STATUS status = {0};
58 static SERVICE_STATUS_HANDLE statushandle = 0;
60 bool install_service(void) {
61 char command[4096] = "\"";
64 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
66 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
68 logger(LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
72 if(!strchr(program_name, '\\')) {
73 GetCurrentDirectory(sizeof command - 1, command + 1);
74 strncat(command, "\\", sizeof command - strlen(command));
77 strncat(command, program_name, sizeof command - strlen(command));
79 strncat(command, "\"", sizeof command - strlen(command));
81 for(argp = g_argv + 1; *argp; argp++) {
82 space = strchr(*argp, ' ');
83 strncat(command, " ", sizeof command - strlen(command));
86 strncat(command, "\"", sizeof command - strlen(command));
88 strncat(command, *argp, sizeof command - strlen(command));
91 strncat(command, "\"", sizeof command - strlen(command));
94 service = CreateService(manager, identname, identname,
95 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
96 command, NULL, NULL, NULL, NULL, NULL);
99 DWORD lasterror = GetLastError();
100 logger(LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
101 if(lasterror != ERROR_SERVICE_EXISTS)
106 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
107 logger(LOG_INFO, "%s service installed", identname);
109 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
112 if(!StartService(service, 0, NULL))
113 logger(LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
115 logger(LOG_INFO, "%s service started", identname);
120 bool remove_service(void) {
121 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
123 logger(LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
127 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
130 logger(LOG_ERR, "Could not open %s service: %s", identname, winerror(GetLastError()));
134 if(!ControlService(service, SERVICE_CONTROL_STOP, &status))
135 logger(LOG_ERR, "Could not stop %s service: %s", identname, winerror(GetLastError()));
137 logger(LOG_INFO, "%s service stopped", identname);
139 if(!DeleteService(service)) {
140 logger(LOG_ERR, "Could not remove %s service: %s", identname, winerror(GetLastError()));
144 logger(LOG_INFO, "%s service removed", identname);
149 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
151 case SERVICE_CONTROL_INTERROGATE:
152 SetServiceStatus(statushandle, &status);
154 case SERVICE_CONTROL_STOP:
155 logger(LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
157 case SERVICE_CONTROL_SHUTDOWN:
158 logger(LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
161 logger(LOG_WARNING, "Got unexpected request %d", request);
162 return ERROR_CALL_NOT_IMPLEMENTED;
165 event_loopexit(NULL);
166 status.dwWaitHint = 30000;
167 status.dwCurrentState = SERVICE_STOP_PENDING;
168 SetServiceStatus(statushandle, &status);
172 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
174 extern int main2(int argc, char **argv);
177 status.dwServiceType = SERVICE_WIN32;
178 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
179 status.dwWin32ExitCode = 0;
180 status.dwServiceSpecificExitCode = 0;
181 status.dwCheckPoint = 0;
183 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
186 logger(LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
189 status.dwWaitHint = 30000;
190 status.dwCurrentState = SERVICE_START_PENDING;
191 SetServiceStatus(statushandle, &status);
193 status.dwWaitHint = 0;
194 status.dwCurrentState = SERVICE_RUNNING;
195 SetServiceStatus(statushandle, &status);
197 err = main2(argc, argv);
199 status.dwWaitHint = 0;
200 status.dwCurrentState = SERVICE_STOPPED;
201 //status.dwWin32ExitCode = err;
202 SetServiceStatus(statushandle, &status);
208 bool init_service(void) {
209 SERVICE_TABLE_ENTRY services[] = {
210 {identname, run_service},
214 if(!StartServiceCtrlDispatcher(services)) {
215 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
219 logger(LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
227 Detach from current terminal
239 fprintf(stderr, "Couldn't detach from terminal: %s",
245 exit(install_service());
249 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
251 logger(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
252 VERSION, __DATE__, __TIME__, debug_level);
257 bool execute_script(const char *name, char **envp) {
264 len = xasprintf(&scriptname, "\"%s/%s\"", confbase, name);
266 len = xasprintf(&scriptname, "\"%s/%s.bat\"", confbase, name);
271 scriptname[len - 1] = '\0';
274 /* First check if there is a script */
276 if(access(scriptname + 1, F_OK)) {
282 ifdebug(STATUS) logger(LOG_INFO, "Executing script %s", name);
285 /* Set environment */
287 for(i = 0; envp[i]; i++)
291 scriptname[len - 1] = '\"';
292 status = system(scriptname);
296 /* Unset environment */
298 for(i = 0; envp[i]; i++) {
299 char *e = strchr(envp[i], '=');
301 char p[e - envp[i] + 1];
302 strncpy(p, envp[i], e - envp[i]);
303 p[e - envp[i]] = '\0';
310 if(WIFEXITED(status)) { /* Child exited by itself */
311 if(WEXITSTATUS(status)) {
312 logger(LOG_ERR, "Script %s exited with non-zero status %d",
313 name, WEXITSTATUS(status));
316 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
317 logger(LOG_ERR, "Script %s was killed by signal %d (%s)",
318 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
320 } else { /* Something strange happened */
321 logger(LOG_ERR, "Script %s terminated abnormally", name);
325 logger(LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
339 static RETSIGTYPE ignore_signal_handler(int a) {
340 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignored signal %d (%s)", a, strsignal(a));
345 void (*handler)(int);
347 {SIGPIPE, ignore_signal_handler},
348 {SIGCHLD, ignore_signal_handler},
353 void setup_signals(void) {
356 struct sigaction act = {NULL};
358 sigemptyset(&act.sa_mask);
360 for(i = 0; sighandlers[i].signal; i++) {
361 act.sa_handler = sighandlers[i].handler;
362 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
363 fprintf(stderr, "Installing signal handler for signal %d (%s) failed: %s\n",
364 sighandlers[i].signal, strsignal(sighandlers[i].signal),