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