#include <signal.h>
#include <getopt.h>
-#ifdef WIN32
-#include "win32service.h"
-#endif
-
static bool set_mode(ConfElem *elem, const char *val, PgSocket *console);
static const char *get_mode(ConfElem *elem);
static bool set_auth(ConfElem *elem, const char *val, PgSocket *console);
static const char *usage_str =
"Usage: %s [OPTION]... config.ini\n"
-#ifndef WIN32
" -d Run in background (as a daemon)\n"
" -R Do a online restart\n"
" -q Run quietly\n"
-#endif
" -v Increase verbosity\n"
" -u <username> Assume identity of <username>\n"
" -V Show version\n"
-" -h Show this help screen and exit\n"
-#ifdef WIN32
-" <windows service registration>\n"
-" -regservice [servicename]\n"
-" -unregservice [servicename]\n"
-" -listengines [servicename]\n"
-" -addengine [servicename] config.ini\n"
-" -delengine [servicename] config.ini\n"
-#endif
-"";
+" -h Show this help screen and exit\n";
static void usage(int err, char *exe)
{
event_base_free(evtmp);
}
-#ifdef WIN32
-static void win32_startup(int argc, char *argv[])
-{
- WSADATA wsaData;
-
- /* parse cmdline */
- if (argc >= 2 && !strcmp(argv[1], "-service"))
- {
- win32_servicestart();
- exit(0);
- }
- if (argc >= 2 && !strcmp(argv[1], "-subservice"))
- {
- cf_quiet = 1;
- argc--;
- argv++;
- }
- if (argc >= 2 && argc <= 4 && (
- !strcmp(argv[1], "-regservice") ||
- !strcmp(argv[1], "-unregservice") ||
- !strcmp(argv[1], "-addengine") ||
- !strcmp(argv[1], "-delengine") ||
- !strcmp(argv[1], "-listengines")))
- {
- win32_serviceconfig(argc, argv);
- exit(0);
- }
-
- if (WSAStartup(MAKEWORD(2,0), &wsaData))
- fatal("Cannot start the network subsystem");
-}
-#endif
-
/* boot everything */
int main(int argc, char *argv[])
{
int c;
bool did_takeover = false;
char *arg_username = NULL;
-#ifndef WIN32
- const char *flags = "avhV" "qdRu:";
-#else
- const char *flags = "avhV";
- win32_startup(argc, argv);
-#endif
- while ((c = getopt(argc, argv, flags)) != EOF) {
+ /* parse cmdline */
+ while ((c = getopt(argc, argv, "qvhdVRu:")) != EOF) {
switch (c) {
case 'R':
cf_reboot = 1;
#include "md5.h"
+static int syslog_started = 0;
static int log_fd = 0;
-static int syslog_started = 0;
struct FacName { const char *name; int code; };
static struct FacName facility_names [] = {
{ "auth", LOG_AUTH },
/* sendmsg is not used */
static inline int sendmsg(int s, const struct msghdr *m, int flags)
{
- return -1;
+ if (m->msg_iovlen != 1) {
+ errno = EINVAL;
+ return -1;
+ }
+ return send(s, m->msg_iov[0].iov_base,
+ m->msg_iov[0].iov_len, flags);
}
/* recvmsg() is, but only with one iov */
static inline int recvmsg(int s, struct msghdr *m, int flags)
{
- if (m->msg_iovlen != 1)
+ if (m->msg_iovlen != 1) {
+ errno = EINVAL;
return -1;
+ }
if (m->msg_controllen)
m->msg_controllen = 0;
return recv(s, m->msg_iov[0].iov_base,
}
#define localtime(a) w_localtime(a)
+/* redirect main() */
+#define main(a,b) real_main(a,b)
+int real_main(int argc, char *argv[]);
+
+
#endif /* _CONFIG_WIN32_ */
static DWORD childcount;
static char **children_config_files;
+static const char *usage_str =
+"Usage: %s [OPTION]... config.ini\n"
+" -v Increase verbosity\n"
+" -u <username> Assume identity of <username>\n"
+" -V Show version\n"
+" -h Show this help screen and exit\n"
+" <windows service registration>\n"
+" -regservice [servicename]\n"
+" -unregservice [servicename]\n"
+" -listengines [servicename]\n"
+" -addengine [servicename] config.ini\n"
+" -delengine [servicename] config.ini\n"
+"";
+
+static void usage(int err, char *exe)
+{
+ printf(usage_str, basename(exe));
+ exit(err);
+}
+
/* Start running as a service */
-void win32_servicestart(void)
+static void win32_servicestart(void)
{
SERVICE_TABLE_ENTRY st[] = { {servicename, win32_servicemain}, {NULL, NULL} };
ReportEvent(evtHandle, elevel, 0, 0, NULL, 1, 0, (const char **)&msg, NULL);
}
-static void usage(int err, char *exe)
-{
- fprintf(stderr, "Bad usage, see -h for help\n");
- exit(1);
-}
-
/* Deal with service and engine registration and unregistration */
-void win32_serviceconfig(int argc, char *const argv[])
+static void win32_serviceconfig(int argc, char *const argv[])
{
if (!strcmp(argv[1], "-regservice")) {
if (argc != 2 && argc != 3)
}
#define strerror(x) w_strerror(x)
+static void manage_win32_service(int argc, char *argv[])
+{
+ /* parse cmdline */
+ if (argc >= 2 && !strcmp(argv[1], "-service"))
+ {
+ win32_servicestart();
+ exit(0);
+ }
+ if (argc >= 2 && !strcmp(argv[1], "-subservice"))
+ {
+ cf_quiet = 1;
+ argc--;
+ argv++;
+ }
+ if (argc >= 2 && argc <= 4 && (
+ !strcmp(argv[1], "-regservice") ||
+ !strcmp(argv[1], "-unregservice") ||
+ !strcmp(argv[1], "-addengine") ||
+ !strcmp(argv[1], "-delengine") ||
+ !strcmp(argv[1], "-listengines")))
+ {
+ win32_serviceconfig(argc, argv);
+ exit(0);
+ }
+}
+
+#undef main
+int main(int argc, char *argv[])
+{
+ int i, j;
+ WSADATA wsaData;
+
+ manage_win32_service(argc, argv);
+
+ /* check if regular arguments are in allowed list */
+ for (i = 1; i < argc; i++) {
+ char *p = argv[0];
+ if (p[0] != '-')
+ continue;
+ for (j = 1; p[j]; j++) {
+ if (!strchr("avhV", p[j]))
+ usage(1, argv[0]);
+ }
+ }
+
+ /* initialize socket subsystem */
+ if (WSAStartup(MAKEWORD(2,0), &wsaData))
+ fatal("Cannot start the network subsystem");
+
+ /* call actual main() */
+ return real_main(argc, argv);
+}
+
*-------------------------------------------------------------------------
*/
-void win32_servicestart(void);
-void win32_serviceconfig(int argc, char *const argv[]);
-
#define openlog w_openlog
#define syslog w_syslog
#define closelog w_closelog