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