From 491ab7e7fdf8d57ced689ebe90871f4e0d633618 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 4 Dec 2008 10:06:09 +0000 Subject: [PATCH] win32: implement sendmsg, move cmdline handling away from main.c --- src/main.c | 59 ++----------------------------- src/util.c | 2 +- win32/compat_win32.h | 16 +++++++-- win32/win32service.c | 83 +++++++++++++++++++++++++++++++++++++++----- win32/win32service.h | 3 -- 5 files changed, 93 insertions(+), 70 deletions(-) diff --git a/src/main.c b/src/main.c index b8ce734..5e0a318 100644 --- a/src/main.c +++ b/src/main.c @@ -25,10 +25,6 @@ #include #include -#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); @@ -37,24 +33,13 @@ static bool set_defer_accept(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 Assume identity of \n" " -V Show version\n" -" -h Show this help screen and exit\n" -#ifdef WIN32 -" \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) { @@ -646,53 +631,15 @@ static void takeover_part1(void) 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; diff --git a/src/util.c b/src/util.c index e39bfc0..61e0e3b 100644 --- a/src/util.c +++ b/src/util.c @@ -30,9 +30,9 @@ #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 }, diff --git a/win32/compat_win32.h b/win32/compat_win32.h index 536bf5d..382a677 100644 --- a/win32/compat_win32.h +++ b/win32/compat_win32.h @@ -178,14 +178,21 @@ static inline int kill(int pid, int sig) /* 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, @@ -209,4 +216,9 @@ static inline struct tm *w_localtime(const time_t *timep) { } #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_ */ diff --git a/win32/win32service.c b/win32/win32service.c index 5364ff5..c17ac7c 100644 --- a/win32/win32service.c +++ b/win32/win32service.c @@ -45,8 +45,28 @@ static char running_servicename[256]; static DWORD childcount; static char **children_config_files; +static const char *usage_str = +"Usage: %s [OPTION]... config.ini\n" +" -v Increase verbosity\n" +" -u Assume identity of \n" +" -V Show version\n" +" -h Show this help screen and exit\n" +" \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} }; @@ -355,14 +375,8 @@ static void win32_eventlog(int level, const char *msg) 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) @@ -612,4 +626,57 @@ const char *wsa_strerror(int e) } #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); +} + diff --git a/win32/win32service.h b/win32/win32service.h index 3054be9..9abca05 100644 --- a/win32/win32service.h +++ b/win32/win32service.h @@ -9,9 +9,6 @@ *------------------------------------------------------------------------- */ -void win32_servicestart(void); -void win32_serviceconfig(int argc, char *const argv[]); - #define openlog w_openlog #define syslog w_syslog #define closelog w_closelog -- 2.40.0