2010-08-31 17:47:57 +08:00
|
|
|
// Copyright (c) 2004-2009 Sergey Lyubka
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
2010-09-13 04:59:31 +08:00
|
|
|
|
2010-05-04 04:46:42 +08:00
|
|
|
#if defined(_WIN32)
|
2010-09-13 04:59:31 +08:00
|
|
|
#define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005
|
2010-08-31 17:47:57 +08:00
|
|
|
#else
|
|
|
|
#define _XOPEN_SOURCE 600 // For PATH_MAX on linux
|
|
|
|
#endif
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2010-08-30 19:54:46 +08:00
|
|
|
#include <sys/stat.h>
|
2010-05-04 04:46:42 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2010-06-25 23:23:17 +08:00
|
|
|
#include <stddef.h>
|
2010-09-07 04:42:05 +08:00
|
|
|
#include <stdarg.h>
|
2010-05-04 04:46:42 +08:00
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winsvc.h>
|
2010-08-31 07:12:16 +08:00
|
|
|
#define PATH_MAX MAX_PATH
|
|
|
|
#define S_ISDIR(x) ((x) & _S_IFDIR)
|
2010-09-07 04:42:05 +08:00
|
|
|
#define DIRSEP '\\'
|
2010-09-13 04:59:31 +08:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#define sleep(x) Sleep((x) * 1000)
|
2010-09-07 18:00:10 +08:00
|
|
|
#define WINCDECL __cdecl
|
2010-05-04 04:46:42 +08:00
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
2010-09-07 04:42:05 +08:00
|
|
|
#include <unistd.h>
|
2010-05-04 04:46:42 +08:00
|
|
|
#define DIRSEP '/'
|
2010-09-07 18:00:10 +08:00
|
|
|
#define WINCDECL
|
2010-09-07 04:42:05 +08:00
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
#define MAX_OPTIONS 40
|
2010-05-04 04:46:42 +08:00
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
static int exit_flag;
|
2010-09-19 20:40:16 +08:00
|
|
|
static char server_name[40]; // Set by init_server_name()
|
|
|
|
static char config_file[PATH_MAX]; // Set by process_command_line_arguments()
|
|
|
|
static struct mg_context *ctx; // Set by start_mongoose()
|
2010-05-04 04:46:42 +08:00
|
|
|
|
|
|
|
#if !defined(CONFIG_FILE)
|
2010-09-13 04:59:31 +08:00
|
|
|
#define CONFIG_FILE "mongoose.conf"
|
2010-05-04 04:46:42 +08:00
|
|
|
#endif /* !CONFIG_FILE */
|
|
|
|
|
2010-09-07 18:00:10 +08:00
|
|
|
static void WINCDECL signal_handler(int sig_num) {
|
2010-09-27 05:20:20 +08:00
|
|
|
exit_flag = sig_num;
|
2010-05-04 04:46:42 +08:00
|
|
|
}
|
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
static void die(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char msg[200];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
MessageBox(NULL, msg, "Error", MB_OK);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-08-30 05:30:49 +08:00
|
|
|
static void show_usage_and_exit(void) {
|
|
|
|
const char **names;
|
2010-08-30 20:10:10 +08:00
|
|
|
int i;
|
2010-08-30 05:30:49 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka\n", mg_version());
|
|
|
|
fprintf(stderr, "Usage:\n");
|
|
|
|
fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
|
|
|
|
fprintf(stderr, " mongoose <config_file>\n");
|
|
|
|
fprintf(stderr, " mongoose [-option value ...]\n");
|
2010-08-30 20:10:10 +08:00
|
|
|
fprintf(stderr, "OPTIONS:\n");
|
2010-08-30 05:30:49 +08:00
|
|
|
|
|
|
|
names = mg_get_valid_option_names();
|
2010-08-31 15:01:14 +08:00
|
|
|
for (i = 0; names[i] != NULL; i += 3) {
|
|
|
|
fprintf(stderr, " -%s %s (default: \"%s\")\n",
|
|
|
|
names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]);
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
|
|
|
fprintf(stderr, "See http://code.google.com/p/mongoose/wiki/MongooseManual"
|
|
|
|
" for more details.\n");
|
2010-08-30 20:10:10 +08:00
|
|
|
fprintf(stderr, "Example:\n mongoose -s cert.pem -p 80,443s -d no\n");
|
2010-08-30 05:30:49 +08:00
|
|
|
exit(EXIT_FAILURE);
|
2010-06-25 23:23:17 +08:00
|
|
|
}
|
|
|
|
|
2010-08-30 19:54:46 +08:00
|
|
|
static void verify_document_root(const char *root) {
|
|
|
|
const char *p, *path;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
path = root;
|
|
|
|
if ((p = strchr(root, ',')) != NULL && (size_t) (p - root) < sizeof(buf)) {
|
2011-02-16 00:40:52 +08:00
|
|
|
memcpy(buf, root, p - root);
|
|
|
|
buf[p - root] = '\0';
|
2010-08-30 19:54:46 +08:00
|
|
|
path = buf;
|
|
|
|
}
|
|
|
|
|
2010-08-30 21:59:21 +08:00
|
|
|
if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
|
2011-02-16 00:40:52 +08:00
|
|
|
die("Invalid root directory: [%s]: %s", root, strerror(errno));
|
2010-08-30 19:54:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-30 05:30:49 +08:00
|
|
|
static char *sdup(const char *str) {
|
|
|
|
char *p;
|
2011-04-25 05:40:08 +08:00
|
|
|
if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
|
2010-08-30 05:30:49 +08:00
|
|
|
strcpy(p, str);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2010-06-25 23:23:17 +08:00
|
|
|
|
2010-08-30 05:30:49 +08:00
|
|
|
static void set_option(char **options, const char *name, const char *value) {
|
|
|
|
int i;
|
|
|
|
|
2010-08-30 21:59:21 +08:00
|
|
|
if (!strcmp(name, "document_root") || !(strcmp(name, "r"))) {
|
2010-08-30 19:54:46 +08:00
|
|
|
verify_document_root(value);
|
|
|
|
}
|
|
|
|
|
2010-08-30 05:30:49 +08:00
|
|
|
for (i = 0; i < MAX_OPTIONS - 3; i++) {
|
|
|
|
if (options[i] == NULL) {
|
|
|
|
options[i] = sdup(name);
|
|
|
|
options[i + 1] = sdup(value);
|
|
|
|
options[i + 2] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == MAX_OPTIONS - 3) {
|
2010-09-07 04:42:05 +08:00
|
|
|
die("%s", "Too many options specified");
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
2010-06-25 23:23:17 +08:00
|
|
|
}
|
|
|
|
|
2010-08-30 05:30:49 +08:00
|
|
|
static void process_command_line_arguments(char *argv[], char **options) {
|
2010-09-19 20:40:16 +08:00
|
|
|
char line[512], opt[512], val[512], *p;
|
2010-08-30 21:59:21 +08:00
|
|
|
FILE *fp = NULL;
|
2010-08-30 05:30:49 +08:00
|
|
|
size_t i, line_no = 0;
|
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
options[0] = NULL;
|
|
|
|
|
|
|
|
// Should we use a config file ?
|
2010-08-30 21:59:21 +08:00
|
|
|
if (argv[1] != NULL && argv[2] == NULL) {
|
2010-09-19 20:40:16 +08:00
|
|
|
snprintf(config_file, sizeof(config_file), "%s", argv[1]);
|
2010-09-13 04:59:31 +08:00
|
|
|
} else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
|
2010-09-03 11:01:26 +08:00
|
|
|
// No command line flags specified. Look where binary lives
|
2010-09-19 20:40:16 +08:00
|
|
|
snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE);
|
2010-09-13 04:59:31 +08:00
|
|
|
} else {
|
2010-09-19 20:40:16 +08:00
|
|
|
snprintf(config_file, sizeof(config_file), "%.*s%c%s",
|
2010-09-07 18:00:10 +08:00
|
|
|
(int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
2010-09-13 04:59:31 +08:00
|
|
|
|
|
|
|
fp = fopen(config_file, "r");
|
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
// If config file was set in command line and open failed, exit
|
2010-09-13 04:59:31 +08:00
|
|
|
if (argv[1] != NULL && argv[2] == NULL && fp == NULL) {
|
2010-09-07 04:42:05 +08:00
|
|
|
die("Cannot open config file %s: %s", config_file, strerror(errno));
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
// Load config file settings first
|
2010-08-30 05:30:49 +08:00
|
|
|
if (fp != NULL) {
|
2010-08-30 21:59:21 +08:00
|
|
|
fprintf(stderr, "Loading config file %s\n", config_file);
|
2010-08-30 05:30:49 +08:00
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
// Loop over the lines in config file
|
2010-08-30 05:30:49 +08:00
|
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
|
|
|
|
|
|
|
line_no++;
|
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
// Ignore empty lines and comments
|
2010-08-30 05:30:49 +08:00
|
|
|
if (line[0] == '#' || line[0] == '\n')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
|
2010-09-07 04:42:05 +08:00
|
|
|
die("%s: line %d is invalid", config_file, (int) line_no);
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
|
|
|
set_option(options, opt, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) fclose(fp);
|
2010-09-14 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now handle command line flags. They override config file settings.
|
|
|
|
for (i = 1; argv[i] != NULL; i += 2) {
|
|
|
|
if (argv[i][0] != '-' || argv[i + 1] == NULL) {
|
|
|
|
show_usage_and_exit();
|
2010-08-30 21:59:21 +08:00
|
|
|
}
|
2010-09-14 18:17:09 +08:00
|
|
|
set_option(options, &argv[i][1], argv[i + 1]);
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
2010-05-04 04:46:42 +08:00
|
|
|
}
|
|
|
|
|
2010-09-13 04:59:31 +08:00
|
|
|
static void init_server_name(void) {
|
|
|
|
snprintf(server_name, sizeof(server_name), "Mongoose web server v.%s",
|
|
|
|
mg_version());
|
|
|
|
}
|
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
static void start_mongoose(int argc, char *argv[]) {
|
2010-09-14 18:17:09 +08:00
|
|
|
char *options[MAX_OPTIONS];
|
2010-08-30 05:30:49 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Edit passwords file if -A option is specified */
|
|
|
|
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
|
|
|
|
if (argc != 6) {
|
|
|
|
show_usage_and_exit();
|
|
|
|
}
|
2011-02-19 00:40:58 +08:00
|
|
|
exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
|
2010-08-30 05:30:49 +08:00
|
|
|
EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show usage if -h or --help options are specified */
|
|
|
|
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
|
|
|
|
show_usage_and_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update config based on command line arguments */
|
|
|
|
process_command_line_arguments(argv, options);
|
|
|
|
|
|
|
|
/* Setup signal handler: quit on Ctrl-C */
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
|
|
|
|
/* Start Mongoose */
|
2010-10-27 06:37:44 +08:00
|
|
|
ctx = mg_start(NULL, NULL, (const char **) options);
|
2010-08-30 05:30:49 +08:00
|
|
|
for (i = 0; options[i] != NULL; i++) {
|
|
|
|
free(options[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2010-09-07 04:42:05 +08:00
|
|
|
die("%s", "Failed to start Mongoose. Maybe some options are "
|
|
|
|
"assigned bad values?\nTry to run with '-e error_log.txt' "
|
|
|
|
"and check error_log.txt for more information.");
|
2010-08-30 05:30:49 +08:00
|
|
|
}
|
2010-09-07 04:42:05 +08:00
|
|
|
}
|
2010-08-30 05:30:49 +08:00
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
static SERVICE_STATUS ss;
|
|
|
|
static SERVICE_STATUS_HANDLE hStatus;
|
2010-09-27 08:06:05 +08:00
|
|
|
static const char *service_magic_argument = "--";
|
2010-08-30 05:30:49 +08:00
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
static void WINAPI ControlHandler(DWORD code) {
|
|
|
|
if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
|
|
|
|
ss.dwWin32ExitCode = 0;
|
|
|
|
ss.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
}
|
|
|
|
SetServiceStatus(hStatus, &ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain(void) {
|
|
|
|
ss.dwServiceType = SERVICE_WIN32;
|
|
|
|
ss.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
|
|
|
|
hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
|
|
|
|
SetServiceStatus(hStatus, &ss);
|
|
|
|
|
|
|
|
while (ss.dwCurrentState == SERVICE_RUNNING) {
|
|
|
|
Sleep(1000);
|
|
|
|
}
|
|
|
|
mg_stop(ctx);
|
|
|
|
|
|
|
|
ss.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
ss.dwWin32ExitCode = (DWORD) -1;
|
|
|
|
SetServiceStatus(hStatus, &ss);
|
|
|
|
}
|
|
|
|
|
2010-09-13 04:59:31 +08:00
|
|
|
#define ID_TRAYICON 100
|
|
|
|
#define ID_QUIT 101
|
|
|
|
#define ID_EDIT_CONFIG 102
|
|
|
|
#define ID_SEPARATOR 103
|
|
|
|
#define ID_INSTALL_SERVICE 104
|
|
|
|
#define ID_REMOVE_SERVICE 105
|
|
|
|
#define ID_ICON 200
|
|
|
|
static NOTIFYICONDATA TrayIcon;
|
|
|
|
|
|
|
|
static void edit_config_file(void) {
|
|
|
|
const char **names, *value;
|
|
|
|
FILE *fp;
|
|
|
|
int i;
|
|
|
|
char cmd[200];
|
|
|
|
|
|
|
|
// Create config file if it is not present yet
|
|
|
|
if ((fp = fopen(config_file, "r")) != NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
} else if ((fp = fopen(config_file, "a+")) != NULL) {
|
|
|
|
fprintf(fp,
|
|
|
|
"# Mongoose web server configuration file.\n"
|
|
|
|
"# Lines starting with '#' and empty lines are ignored.\n"
|
|
|
|
"# For detailed description of every option, visit\n"
|
|
|
|
"# http://code.google.com/p/mongoose/wiki/MongooseManual\n\n");
|
|
|
|
names = mg_get_valid_option_names();
|
|
|
|
for (i = 0; names[i] != NULL; i += 3) {
|
|
|
|
value = mg_get_option(ctx, names[i]);
|
|
|
|
fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2010-09-07 04:42:05 +08:00
|
|
|
}
|
|
|
|
|
2010-09-13 04:59:31 +08:00
|
|
|
snprintf(cmd, sizeof(cmd), "notepad.exe %s", config_file);
|
|
|
|
WinExec(cmd, SW_SHOW);
|
|
|
|
}
|
2010-09-07 04:42:05 +08:00
|
|
|
|
2010-09-14 18:17:09 +08:00
|
|
|
static void show_error(void) {
|
|
|
|
char buf[256];
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
buf, sizeof(buf), NULL);
|
|
|
|
MessageBox(NULL, buf, "Error", MB_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manage_service(int action) {
|
|
|
|
static const char *service_name = "Mongoose";
|
|
|
|
SC_HANDLE hSCM = NULL, hService = NULL;
|
2010-09-27 08:06:05 +08:00
|
|
|
SERVICE_DESCRIPTION descr = {server_name};
|
|
|
|
char path[PATH_MAX + 20]; // Path to executable plus magic argument
|
2010-09-14 18:17:09 +08:00
|
|
|
int success = 1;
|
|
|
|
|
|
|
|
if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
|
|
|
|
GENERIC_WRITE : GENERIC_READ)) == NULL) {
|
|
|
|
success = 0;
|
|
|
|
show_error();
|
|
|
|
} else if (action == ID_INSTALL_SERVICE) {
|
|
|
|
GetModuleFileName(NULL, path, sizeof(path));
|
2010-09-27 08:06:05 +08:00
|
|
|
strncat(path, " ", sizeof(path));
|
|
|
|
strncat(path, service_magic_argument, sizeof(path));
|
2010-09-14 18:17:09 +08:00
|
|
|
hService = CreateService(hSCM, service_name, service_name,
|
|
|
|
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
|
|
|
|
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
|
|
|
|
path, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
if (hService) {
|
2010-09-27 08:06:05 +08:00
|
|
|
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
|
2010-09-14 18:17:09 +08:00
|
|
|
} else {
|
|
|
|
show_error();
|
|
|
|
}
|
|
|
|
} else if (action == ID_REMOVE_SERVICE) {
|
|
|
|
if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
|
|
|
|
!DeleteService(hService)) {
|
|
|
|
show_error();
|
|
|
|
}
|
|
|
|
} else if ((hService = OpenService(hSCM, service_name,
|
|
|
|
SERVICE_QUERY_STATUS)) == NULL) {
|
|
|
|
success = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:06:05 +08:00
|
|
|
CloseServiceHandle(hService);
|
|
|
|
CloseServiceHandle(hSCM);
|
2010-09-14 18:17:09 +08:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-09-07 04:42:05 +08:00
|
|
|
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
|
|
|
|
LPARAM lParam) {
|
2010-09-13 04:59:31 +08:00
|
|
|
static SERVICE_TABLE_ENTRY service_table[] = {
|
|
|
|
{server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2010-09-14 18:17:09 +08:00
|
|
|
int service_installed;
|
2010-09-27 08:06:05 +08:00
|
|
|
char buf[200], *service_argv[] = {__argv[0], NULL};
|
2010-09-13 04:59:31 +08:00
|
|
|
POINT pt;
|
|
|
|
HMENU hMenu;
|
2010-09-07 04:42:05 +08:00
|
|
|
|
|
|
|
switch (msg) {
|
2010-09-13 04:59:31 +08:00
|
|
|
case WM_CREATE:
|
2010-09-27 08:06:05 +08:00
|
|
|
if (__argv[1] != NULL &&
|
|
|
|
!strcmp(__argv[1], service_magic_argument)) {
|
|
|
|
start_mongoose(1, service_argv);
|
|
|
|
StartServiceCtrlDispatcher(service_table);
|
2010-09-13 04:59:31 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-27 08:06:05 +08:00
|
|
|
} else {
|
|
|
|
start_mongoose(__argc, __argv);
|
2010-09-13 04:59:31 +08:00
|
|
|
}
|
|
|
|
break;
|
2010-09-07 04:42:05 +08:00
|
|
|
case WM_COMMAND:
|
|
|
|
switch (LOWORD(wParam)) {
|
|
|
|
case ID_QUIT:
|
2010-09-13 04:59:31 +08:00
|
|
|
mg_stop(ctx);
|
|
|
|
Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
case ID_EDIT_CONFIG:
|
|
|
|
edit_config_file();
|
2010-09-07 04:42:05 +08:00
|
|
|
break;
|
2010-09-14 18:17:09 +08:00
|
|
|
case ID_INSTALL_SERVICE:
|
|
|
|
case ID_REMOVE_SERVICE:
|
|
|
|
manage_service(LOWORD(wParam));
|
|
|
|
break;
|
2010-09-07 04:42:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WM_USER:
|
|
|
|
switch (lParam) {
|
|
|
|
case WM_RBUTTONUP:
|
|
|
|
case WM_LBUTTONUP:
|
|
|
|
case WM_LBUTTONDBLCLK:
|
|
|
|
hMenu = CreatePopupMenu();
|
2010-09-13 04:59:31 +08:00
|
|
|
AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
|
|
|
|
AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
|
2010-09-14 18:17:09 +08:00
|
|
|
service_installed = manage_service(0);
|
|
|
|
snprintf(buf, sizeof(buf), "NT service: %s installed",
|
|
|
|
service_installed ? "" : "not");
|
|
|
|
AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
|
|
|
|
AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
|
|
|
|
ID_INSTALL_SERVICE, "Install");
|
|
|
|
AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
|
|
|
|
ID_REMOVE_SERVICE, "Deinstall");
|
2010-09-13 04:59:31 +08:00
|
|
|
AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
|
|
|
|
AppendMenu(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file");
|
|
|
|
AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
|
2010-09-07 04:42:05 +08:00
|
|
|
GetCursorPos(&pt);
|
2010-09-19 22:19:16 +08:00
|
|
|
SetForegroundWindow(hWnd);
|
2010-09-07 04:42:05 +08:00
|
|
|
TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
|
2010-09-19 22:19:16 +08:00
|
|
|
PostMessage(hWnd, WM_NULL, 0, 0);
|
2010-09-07 04:42:05 +08:00
|
|
|
DestroyMenu(hMenu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
|
|
|
|
WNDCLASS cls;
|
|
|
|
HWND hWnd;
|
|
|
|
MSG msg;
|
|
|
|
|
2010-09-13 04:59:31 +08:00
|
|
|
init_server_name();
|
2010-09-07 04:42:05 +08:00
|
|
|
memset(&cls, 0, sizeof(cls));
|
2010-09-13 04:59:31 +08:00
|
|
|
cls.lpfnWndProc = (WNDPROC) WindowProc;
|
2010-09-07 04:42:05 +08:00
|
|
|
cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
cls.lpszClassName = server_name;
|
|
|
|
|
|
|
|
RegisterClass(&cls);
|
|
|
|
hWnd = CreateWindow(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
|
|
|
|
0, 0, 0, 0, NULL, NULL, NULL, NULL);
|
|
|
|
ShowWindow(hWnd, SW_HIDE);
|
|
|
|
|
2010-09-13 04:59:31 +08:00
|
|
|
TrayIcon.cbSize = sizeof(TrayIcon);
|
|
|
|
TrayIcon.uID = ID_TRAYICON;
|
|
|
|
TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
|
|
|
|
TrayIcon.hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON),
|
|
|
|
IMAGE_ICON, 16, 16, 0);
|
|
|
|
TrayIcon.hWnd = hWnd;
|
|
|
|
snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
|
|
|
|
TrayIcon.uCallbackMessage = WM_USER;
|
|
|
|
Shell_NotifyIcon(NIM_ADD, &TrayIcon);
|
|
|
|
|
|
|
|
while (GetMessage(&msg, hWnd, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
2010-09-07 04:42:05 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-13 04:59:31 +08:00
|
|
|
#else
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
init_server_name();
|
2010-09-07 04:42:05 +08:00
|
|
|
start_mongoose(argc, argv);
|
|
|
|
printf("%s started on port(s) %s with web root [%s]\n",
|
|
|
|
server_name, mg_get_option(ctx, "listening_ports"),
|
|
|
|
mg_get_option(ctx, "document_root"));
|
2010-08-30 05:30:49 +08:00
|
|
|
while (exit_flag == 0) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
printf("Exiting on signal %d, waiting for all threads to finish...",
|
|
|
|
exit_flag);
|
|
|
|
fflush(stdout);
|
|
|
|
mg_stop(ctx);
|
|
|
|
printf("%s", " done.\n");
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2010-05-04 04:46:42 +08:00
|
|
|
}
|
2010-09-13 04:59:31 +08:00
|
|
|
#endif /* _WIN32 */
|