]> git.meshlink.io Git - meshlink/blob - src/process.c
Remove support for Subnets.
[meshlink] / src / process.c
1 /*
2     process.c -- process management functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "control.h"
26 #include "edge.h"
27 #include "event.h"
28 #include "logger.h"
29 #include "names.h"
30 #include "net.h"
31 #include "node.h"
32 #include "process.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 /* If zero, don't detach from the terminal. */
37 bool do_detach = true;
38 bool sigalrm = false;
39
40 extern char **g_argv;
41 extern bool use_logfile;
42
43 /* Some functions the less gifted operating systems might lack... */
44
45 #ifdef HAVE_MINGW
46 static SC_HANDLE manager = NULL;
47 static SC_HANDLE service = NULL;
48 static SERVICE_STATUS status = {0};
49 static SERVICE_STATUS_HANDLE statushandle = 0;
50
51 static bool install_service(void) {
52         char command[4096] = "\"";
53         SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
54
55         manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
56         if(!manager) {
57                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
58                 return false;
59         }
60
61         if(!strchr(program_name, '\\')) {
62                 GetCurrentDirectory(sizeof command - 1, command + 1);
63                 strncat(command, "\\", sizeof command - strlen(command));
64         }
65
66         strncat(command, program_name, sizeof command - strlen(command));
67
68         strncat(command, "\"", sizeof command - strlen(command));
69
70         for(char **argp = g_argv + 1; *argp; argp++) {
71                 char *space = strchr(*argp, ' ');
72                 strncat(command, " ", sizeof command - strlen(command));
73
74                 if(space)
75                         strncat(command, "\"", sizeof command - strlen(command));
76
77                 strncat(command, *argp, sizeof command - strlen(command));
78
79                 if(space)
80                         strncat(command, "\"", sizeof command - strlen(command));
81         }
82
83         service = CreateService(manager, identname, identname,
84                         SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
85                         command, NULL, NULL, NULL, NULL, NULL);
86
87         if(!service) {
88                 DWORD lasterror = GetLastError();
89                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
90                 if(lasterror != ERROR_SERVICE_EXISTS)
91                         return false;
92         }
93
94         if(service) {
95                 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
96                 logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
97         } else {
98                 service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
99         }
100
101         if(!StartService(service, 0, NULL))
102                 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
103         else
104                 logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
105
106         return true;
107 }
108
109 DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
110         switch(request) {
111                 case SERVICE_CONTROL_INTERROGATE:
112                         SetServiceStatus(statushandle, &status);
113                         return NO_ERROR;
114                 case SERVICE_CONTROL_STOP:
115                         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
116                         break;
117                 case SERVICE_CONTROL_SHUTDOWN:
118                         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
119                         break;
120                 default:
121                         logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
122                         return ERROR_CALL_NOT_IMPLEMENTED;
123         }
124
125         event_exit();
126         status.dwWaitHint = 30000;
127         status.dwCurrentState = SERVICE_STOP_PENDING;
128         SetServiceStatus(statushandle, &status);
129         return NO_ERROR;
130 }
131
132 VOID WINAPI run_service(DWORD argc, LPTSTR* argv) {
133         extern int main2(int argc, char **argv);
134
135         status.dwServiceType = SERVICE_WIN32;
136         status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
137         status.dwWin32ExitCode = 0;
138         status.dwServiceSpecificExitCode = 0;
139         status.dwCheckPoint = 0;
140
141         statushandle = RegisterServiceCtrlHandlerEx(identname, controlhandler, NULL);
142
143         if (!statushandle) {
144                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "RegisterServiceCtrlHandlerEx", winerror(GetLastError()));
145         } else {
146                 status.dwWaitHint = 30000;
147                 status.dwCurrentState = SERVICE_START_PENDING;
148                 SetServiceStatus(statushandle, &status);
149
150                 status.dwWaitHint = 0;
151                 status.dwCurrentState = SERVICE_RUNNING;
152                 SetServiceStatus(statushandle, &status);
153
154                 main2(argc, argv);
155
156                 status.dwWaitHint = 0;
157                 status.dwCurrentState = SERVICE_STOPPED;
158                 SetServiceStatus(statushandle, &status);
159         }
160
161         return;
162 }
163
164 bool init_service(void) {
165         SERVICE_TABLE_ENTRY services[] = {
166                 {identname, run_service},
167                 {NULL, NULL}
168         };
169
170         if(!StartServiceCtrlDispatcher(services)) {
171                 if(GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
172                         return false;
173                 }
174                 else
175                         logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "StartServiceCtrlDispatcher", winerror(GetLastError()));
176         }
177
178         return true;
179 }
180 #endif
181
182 /*
183   Detach from current terminal
184 */
185 bool detach(void) {
186 #ifndef HAVE_MINGW
187         signal(SIGPIPE, SIG_IGN);
188         signal(SIGUSR1, SIG_IGN);
189         signal(SIGUSR2, SIG_IGN);
190         signal(SIGWINCH, SIG_IGN);
191
192         closelogger();
193 #endif
194
195         if(do_detach) {
196 #ifndef HAVE_MINGW
197                 if(daemon(0, 0)) {
198                         logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno));
199                         return false;
200                 }
201 #else
202                 if(!statushandle)
203                         exit(!install_service());
204 #endif
205         }
206
207         openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
208
209         logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
210                            VERSION, __DATE__, __TIME__, debug_level);
211
212         return true;
213 }
214
215