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