1 /*
2  * Copyright (C) 2005           Dizzy
3  * Copyright (C) 2005           Olaf Freyer (aaron@cs.tu-berlin.de)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 #include "common/setup_before.h"
20 #include "setup.h"
21 
22 #ifdef HAVE_STDDEF_H
23 # include <stddef.h>
24 #else
25 # ifndef NULL
26 #  define NULL ((void *)0)
27 # endif
28 #endif
29 #include <stdio.h>
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #else
33 # ifdef HAVE_STRINGS_H
34 #  include <strings.h>
35 # endif
36 # ifdef HAVE_MEMORY_H
37 #  include <memory.h>
38 # endif
39 #endif
40 #include "compat/memset.h"
41 #ifdef WIN32
42 # include "win32/service.h"
43 #endif
44 #include "common/conf.h"
45 #include "common/xalloc.h"
46 #include "version.h"
47 #include "cmdline.h"
48 #include "compat/strcasecmp.h"
49 #include "common/eventlog.h"
50 #include "common/setup_after.h"
51 
52 static struct {
53 #ifdef DO_DAEMONIZE
54 	unsigned foreground;
55 #endif
56 	const char *preffile;
57 	const char *logfile;
58 	unsigned debug;
59 } cmdline_config;
60 
61 
62 static unsigned exitflag;
63 static const char *progname;
64 
65 
66 static int conf_set_preffile(const char *valstr);
67 static int conf_setdef_preffile(void);
68 
69 static int conf_set_logfile(const char * valstr);
70 static int conf_setdef_logfile(void);
71 
72 static int conf_set_help(const char * valstr);
73 static int conf_setdef_help(void);
74 
75 static int conf_set_version(const char * valstr);
76 static int conf_setdef_version(void);
77 
78 static int conf_set_foreground(const char * valstr);
79 static int conf_setdef_foreground(void);
80 
81 static int conf_set_debug(const char * valstr);
82 static int conf_setdef_debug(void);
83 
84 #ifdef WIN32
85 static int conf_set_service(char const * valstr);
86 static int conf_setdef_service(void);
87 
88 static int conf_set_servaction(const char * valstr);
89 static int conf_setdef_servaction(void);
90 #endif
91 
92 
93 static t_conf_entry conftab[]={
94 	{ "c",          conf_set_preffile,      NULL, conf_setdef_preffile},
95 	{ "config",     conf_set_preffile,      NULL, conf_setdef_preffile},
96 	{ "l",          conf_set_logfile,       NULL, conf_setdef_logfile   },
97 	{ "log",        conf_set_logfile,       NULL, conf_setdef_logfile   },
98 	{ "h",          conf_set_help,          NULL, conf_setdef_help      },
99 	{ "help",       conf_set_help,          NULL, conf_setdef_help      },
100 	{ "usage",      conf_set_help,          NULL, conf_setdef_help      },
101 	{ "v",          conf_set_version,       NULL, conf_setdef_version   },
102 	{ "version",    conf_set_version,       NULL, conf_setdef_version   },
103 #ifdef DO_DAEMONIZE
104 	{ "f",	        conf_set_foreground,    NULL, conf_setdef_foreground},
105 	{ "foreground", conf_set_foreground,    NULL, conf_setdef_foreground},
106 #endif
107 	{ "D",          conf_set_debug,         NULL, conf_setdef_debug     },
108 	{ "debug",      conf_set_debug,         NULL, conf_setdef_debug     },
109 #ifdef WIN32
110 	{ "service",    conf_set_service,       NULL, conf_setdef_service   },
111 	{ "s",		conf_set_servaction,	NULL, conf_setdef_servaction},
112 #endif
113 	{ NULL,         NULL,                   NULL, NULL                  }
114 };
115 
116 
cmdline_load(int argc,char ** argv)117 extern int cmdline_load(int argc, char** argv)
118 {
119 	int res;
120 
121 	if (argc<1 || !argv || !argv[0]) {
122 		fprintf(stderr,"bad arguments\n");
123 		return -1;
124 	}
125 
126 	exitflag = 0;
127 	progname = argv[0];
128 
129 	res = conf_load_cmdline(argc, argv, conftab);
130 	if (res < 0) return -1;
131 	return exitflag ? 0 : 1;
132 }
133 
cmdline_unload(void)134 extern void cmdline_unload(void)
135 {
136 	conf_unload(conftab);
137 }
138 
139 
140 
usage(void)141 static void usage(void)
142 {
143 	fprintf(stderr,
144 		"Usage: %s [<options>]\n"
145 		"    -c FILE, --config=FILE   use FILE as configuration file (default is " D2CS_DEFAULT_CONF_FILE ")\n"
146 		"    -l FILE, --log=FILE      set log to FILE\n"
147 #ifdef DO_DAEMONIZE
148 		"    -f, --foreground:        don't daemonize\n"
149 #endif
150 		"    -D, --debug:             run in debug mode (run in foreground and log to stdout)\n"
151 		"    -h, --help, --usage      show this information and exit\n"
152 		"    -v, --version:           print version number and exit\n"
153 #ifdef WIN32
154 		"    Running as service functions:\n"
155 		"    --service                run as service\n"
156 		"    -s install               install service\n"
157 		"    -s uninstall             uninstall service\n"
158 #endif
159 		"\n"
160 		"Notes:\n"
161 		"	1.You should always use absolute path here for all FILE names\n\n",
162 		progname);
163 }
164 
165 #ifdef DO_DAEMONIZE
cmdline_get_foreground(void)166 extern int cmdline_get_foreground(void)
167 {
168 	return cmdline_config.foreground;
169 }
170 
conf_set_foreground(const char * valstr)171 static int conf_set_foreground(const char *valstr)
172 {
173 	return conf_set_bool(&cmdline_config.foreground, valstr, 0);
174 }
175 
conf_setdef_foreground(void)176 static int conf_setdef_foreground(void)
177 {
178 	return conf_set_bool(&cmdline_config.foreground, NULL, 0);
179 }
180 #endif
181 
182 
cmdline_get_preffile(void)183 extern const char* cmdline_get_preffile(void)
184 {
185 	return cmdline_config.preffile;
186 }
187 
conf_set_preffile(const char * valstr)188 static int conf_set_preffile(const char *valstr)
189 {
190 	return conf_set_str(&cmdline_config.preffile, valstr, NULL);
191 }
192 
conf_setdef_preffile(void)193 static int conf_setdef_preffile(void)
194 {
195 	return conf_set_str(&cmdline_config.preffile, NULL, D2CS_DEFAULT_CONF_FILE);
196 }
197 
198 
cmdline_get_logfile(void)199 extern const char* cmdline_get_logfile(void)
200 {
201 	return cmdline_config.logfile;
202 }
203 
conf_set_logfile(const char * valstr)204 static int conf_set_logfile(const char *valstr)
205 {
206 	return conf_set_str(&cmdline_config.logfile, valstr, NULL);
207 }
208 
conf_setdef_logfile(void)209 static int conf_setdef_logfile(void)
210 {
211 	return conf_set_str(&cmdline_config.logfile, NULL, NULL);
212 }
213 
214 
conf_set_debug(const char * valstr)215 static int conf_set_debug(const char *valstr)
216 {
217 	conf_set_bool(&cmdline_config.debug, valstr, 0);
218 	if (cmdline_config.debug) eventlog_set_debugmode(1);
219 #ifdef DO_DAEMONIZE
220 	cmdline_config.foreground = 1;
221 #endif
222 	return 0;
223 }
224 
conf_setdef_debug(void)225 static int conf_setdef_debug(void)
226 {
227 	return conf_set_bool(&cmdline_config.debug, NULL, 0);
228 }
229 
230 
conf_set_help(const char * valstr)231 static int conf_set_help(const char *valstr)
232 {
233 	unsigned tmp = 0;
234 
235 	conf_set_bool(&tmp, valstr, 0);
236 	if (tmp) {
237 		usage();
238 		exitflag = 1;
239 	}
240 
241 	return 0;
242 }
243 
conf_setdef_help(void)244 static int conf_setdef_help(void)
245 {
246 	return 0;
247 }
248 
249 
conf_set_version(const char * valstr)250 static int conf_set_version(const char *valstr)
251 {
252 	unsigned tmp = 0;
253 
254 	conf_set_bool(&tmp, valstr, 0);
255 	if (tmp) {
256 		printf(D2CS_VERSION"\n");
257 		exitflag = 1;
258 	}
259 
260 	return 0;
261 }
262 
conf_setdef_version(void)263 static int conf_setdef_version(void)
264 {
265 	return 0;
266 }
267 
268 
269 #ifdef WIN32
conf_set_service(const char * valstr)270 static int conf_set_service(const char *valstr)
271 {
272 	unsigned tmp = 0;
273 
274 	conf_set_bool(&tmp, valstr, 0);
275 	if (tmp) {
276 		Win32_ServiceRun();
277 		exitflag = 1;
278 	}
279 
280 	return 0;
281 }
282 
conf_setdef_service(void)283 static int conf_setdef_service(void)
284 {
285 	return 0;
286 }
287 
288 
conf_set_servaction(const char * valstr)289 static int conf_set_servaction(const char *valstr)
290 {
291 	const char* tmp = NULL;
292 
293 	conf_set_str(&tmp, valstr, NULL);
294 
295 	if (tmp) {
296 		if (!strcasecmp(tmp, "install")) {
297 			fprintf(stderr, "Installing service");
298 			Win32_ServiceInstall();
299 		} else if (!strcasecmp(tmp, "uninstall")) {
300 			fprintf(stderr, "Uninstalling service");
301 			Win32_ServiceUninstall();
302 		} else {
303 			fprintf(stderr, "Unknown service action '%s'\n", tmp);
304 		}
305 
306 		exitflag = 1;
307 		xfree((void *)tmp);
308 	}
309 
310 	return 0;
311 }
312 
conf_setdef_servaction(void)313 static int conf_setdef_servaction(void)
314 {
315 	return 0;
316 }
317 
318 #endif
319