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