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"
36 /* If zero, don't detach from the terminal. */
37 bool do_detach = true;
40 extern char *identname;
42 extern bool use_logfile;
44 /* Some functions the less gifted operating systems might lack... */
47 extern char *identname;
48 extern char *program_name;
51 static SC_HANDLE manager = NULL;
52 static SC_HANDLE service = NULL;
53 static SERVICE_STATUS status = {0};
54 static SERVICE_STATUS_HANDLE statushandle = 0;
56 static bool install_service(void) {
57 char command[4096] = "\"";
60 SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
62 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
64 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
68 if(!strchr(program_name, '\\')) {
69 GetCurrentDirectory(sizeof command - 1, command + 1);
70 strncat(command, "\\", sizeof command - strlen(command));
73 strncat(command, program_name, sizeof command - strlen(command));
75 strncat(command, "\"", sizeof command - strlen(command));
77 for(argp = g_argv + 1; *argp; argp++) {
78 space = strchr(*argp, ' ');
79 strncat(command, " ", sizeof command - strlen(command));
82 strncat(command, "\"", sizeof command - strlen(command));
84 strncat(command, *argp, sizeof command - strlen(command));
87 strncat(command, "\"", sizeof command - strlen(command));
90 service = CreateService(manager, identname, identname,
91 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
92 command, NULL, NULL, NULL, NULL, NULL);
95 DWORD lasterror = GetLastError();
96 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
97 if(lasterror != ERROR_SERVICE_EXISTS)
102 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
103 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
105 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
108 if(!StartService(service, 0, NULL))
109 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
111 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
116 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
118 case SERVICE_CONTROL_INTERROGATE:
119 SetServiceStatus(statushandle, &status);
121 case SERVICE_CONTROL_STOP:
122 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
124 case SERVICE_CONTROL_SHUTDOWN:
125 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
128 logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", request);
129 return ERROR_CALL_NOT_IMPLEMENTED;
132 event_loopexit(NULL);
133 status.dwWaitHint = 30000;
134 status.dwCurrentState = SERVICE_STOP_PENDING;
135 SetServiceStatus(statushandle, &status);
139 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
141 extern int main2(int argc, char **argv);
144 status.dwServiceType = SERVICE_WIN32;
145 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
146 status.dwWin32ExitCode = 0;
147 status.dwServiceSpecificExitCode = 0;
148 status.dwCheckPoint = 0;
150 statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
153 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
156 status.dwWaitHint = 30000;
157 status.dwCurrentState = SERVICE_START_PENDING;
158 SetServiceStatus(statushandle, &status);
160 status.dwWaitHint = 0;
161 status.dwCurrentState = SERVICE_RUNNING;
162 SetServiceStatus(statushandle, &status);
164 err = main2(argc, argv);
166 status.dwWaitHint = 0;
167 status.dwCurrentState = SERVICE_STOPPED;
168 //status.dwWin32ExitCode = err;
169 SetServiceStatus(statushandle, &status);
175 bool init_service(void) {
176 SERVICE_TABLE_ENTRY services[] = {
177 {identname, run_service},
181 if(!StartServiceCtrlDispatcher(services)) {
182 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
186 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
194 Detach from current terminal
198 signal(SIGPIPE, SIG_IGN);
199 signal(SIGUSR1, SIG_IGN);
200 signal(SIGUSR2, SIG_IGN);
201 signal(SIGWINCH, SIG_IGN);
209 fprintf(stderr, "Couldn't detach from terminal: %s",
215 exit(!install_service());
219 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
221 logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
222 VERSION, __DATE__, __TIME__, debug_level);
227 bool execute_script(const char *name, char **envp) {
234 len = xasprintf(&scriptname, "\"%s" SLASH "%s\"", confbase, name);
236 len = xasprintf(&scriptname, "\"%s" SLASH "%s.bat\"", confbase, name);
241 scriptname[len - 1] = '\0';
244 /* First check if there is a script */
246 if(access(scriptname + 1, F_OK)) {
252 logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
255 /* Set environment */
257 for(i = 0; envp[i]; i++)
261 scriptname[len - 1] = '\"';
262 status = system(scriptname);
266 /* Unset environment */
268 for(i = 0; envp[i]; i++) {
269 char *e = strchr(envp[i], '=');
271 char p[e - envp[i] + 1];
272 strncpy(p, envp[i], e - envp[i]);
273 p[e - envp[i]] = '\0';
280 if(WIFEXITED(status)) { /* Child exited by itself */
281 if(WEXITSTATUS(status)) {
282 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
283 name, WEXITSTATUS(status));
286 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
287 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
288 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
290 } else { /* Something strange happened */
291 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
295 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));