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