2024-09-13 08:38:39 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2024-09-13 15:09:52 +00:00
|
|
|
#define SOURCE
|
|
|
|
|
2024-09-18 09:19:03 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2024-09-13 09:06:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2024-09-13 15:09:52 +00:00
|
|
|
#include <signal.h>
|
2024-09-13 09:06:44 +00:00
|
|
|
|
2024-09-18 09:19:03 +00:00
|
|
|
#ifndef NO_SSL
|
2024-09-13 10:28:20 +00:00
|
|
|
#include <openssl/opensslv.h>
|
2024-09-18 09:19:03 +00:00
|
|
|
#endif
|
2024-09-13 10:28:20 +00:00
|
|
|
|
2024-09-13 09:06:44 +00:00
|
|
|
#include <cm_log.h>
|
2024-09-18 20:02:26 +00:00
|
|
|
#include <cm_string.h>
|
2024-09-13 09:06:44 +00:00
|
|
|
|
2024-09-13 09:39:33 +00:00
|
|
|
#include "tw_config.h"
|
2024-09-13 11:18:23 +00:00
|
|
|
#include "tw_server.h"
|
2024-09-13 09:06:44 +00:00
|
|
|
#include "tw_version.h"
|
|
|
|
|
2024-09-18 11:06:58 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2024-09-13 09:06:44 +00:00
|
|
|
extern bool cm_do_log;
|
2024-09-14 00:42:40 +00:00
|
|
|
extern struct tw_config config;
|
2024-09-18 20:02:26 +00:00
|
|
|
extern FILE* logfile;
|
2024-09-14 00:42:40 +00:00
|
|
|
|
|
|
|
char tw_server[2048];
|
2024-09-13 09:06:44 +00:00
|
|
|
|
2024-09-18 20:02:26 +00:00
|
|
|
int startup(int argc, char** argv);
|
|
|
|
|
|
|
|
#ifdef SERVICE
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
SERVICE_STATUS_HANDLE status_handle;
|
|
|
|
|
2024-09-19 09:23:45 +00:00
|
|
|
void WINAPI servhandler(DWORD control) {
|
|
|
|
switch(control) {
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
break;
|
2024-09-18 20:02:26 +00:00
|
|
|
}
|
|
|
|
SetServiceStatus(status_handle, &status);
|
|
|
|
}
|
|
|
|
|
2024-09-19 09:23:45 +00:00
|
|
|
void WINAPI servmain(DWORD argc, LPSTR* argv) {
|
2024-09-18 20:02:26 +00:00
|
|
|
logfile = fopen(PREFIX "/logs/tewi.log", "a");
|
|
|
|
if(logfile == NULL) logfile = stderr;
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
status.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
|
|
|
|
if(status_handle == NULL) return;
|
|
|
|
if(SetServiceStatus(status_handle, &status) == 0) return;
|
|
|
|
int st = startup(argc, argv);
|
2024-09-19 09:23:45 +00:00
|
|
|
if(st != -1) {
|
2024-09-18 20:02:26 +00:00
|
|
|
status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
status.dwServiceSpecificExitCode = st;
|
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(status_handle, &status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
SetServiceStatus(status_handle, &status);
|
|
|
|
tw_server_loop();
|
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(status_handle, &status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-09-13 09:06:44 +00:00
|
|
|
int main(int argc, char** argv) {
|
2024-09-18 20:02:26 +00:00
|
|
|
logfile = stderr;
|
|
|
|
#ifdef SERVICE
|
|
|
|
SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
|
|
|
|
StartServiceCtrlDispatcher(table);
|
2024-09-18 09:19:03 +00:00
|
|
|
#else
|
2024-09-18 20:02:26 +00:00
|
|
|
int st = startup(argc, argv);
|
|
|
|
if(st != -1) return st;
|
|
|
|
tw_server_loop();
|
2024-09-18 09:19:03 +00:00
|
|
|
#endif
|
2024-09-18 20:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 09:23:45 +00:00
|
|
|
int startup(int argc, char** argv) {
|
2024-09-18 20:02:26 +00:00
|
|
|
int i;
|
|
|
|
const char* confpath = PREFIX "/etc/tewi.conf";
|
2024-09-19 09:23:45 +00:00
|
|
|
if(argv != NULL) {
|
2024-09-18 20:02:26 +00:00
|
|
|
for(i = 1; i < argc; i++) {
|
|
|
|
if(argv[i][0] == '-') {
|
|
|
|
if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
|
|
|
|
if(!cm_do_log) {
|
|
|
|
cm_do_log = true;
|
2024-09-19 09:23:45 +00:00
|
|
|
#ifndef NO_SSL
|
2024-09-18 20:02:26 +00:00
|
|
|
cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
2024-09-19 09:23:45 +00:00
|
|
|
#else
|
2024-09-18 20:02:26 +00:00
|
|
|
cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
2024-09-19 09:23:45 +00:00
|
|
|
#endif
|
2024-09-18 20:02:26 +00:00
|
|
|
} else {
|
|
|
|
cm_do_log = true;
|
|
|
|
}
|
|
|
|
} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
|
|
|
|
i++;
|
|
|
|
if(argv[i] == NULL) {
|
|
|
|
fprintf(stderr, "Missing argument\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
confpath = argv[i];
|
|
|
|
} else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
|
|
|
|
printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
|
|
|
|
printf("Under public domain.\n");
|
|
|
|
printf("Original by 2024 Nishi\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
|
|
|
|
printf("--config | -C config : Specify config\n");
|
|
|
|
printf("--verbose | -v : Verbose mode\n");
|
|
|
|
printf("--version | -V : Version information\n");
|
|
|
|
return 0;
|
2024-09-13 09:06:44 +00:00
|
|
|
} else {
|
2024-09-18 20:02:26 +00:00
|
|
|
fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
2024-09-13 09:39:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-09-13 09:06:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 10:28:20 +00:00
|
|
|
tw_config_init();
|
2024-09-14 00:42:40 +00:00
|
|
|
if(tw_config_read(confpath) != 0) {
|
2024-09-13 09:39:33 +00:00
|
|
|
fprintf(stderr, "Could not read the config\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-09-13 11:18:23 +00:00
|
|
|
if(tw_server_init() != 0) {
|
|
|
|
fprintf(stderr, "Could not initialize the server\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-09-14 00:42:40 +00:00
|
|
|
sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
2024-09-18 20:02:26 +00:00
|
|
|
char* r = cm_strcat(tw_server, " running...");
|
|
|
|
cm_force_log(r);
|
|
|
|
free(r);
|
2024-09-13 15:09:52 +00:00
|
|
|
#ifndef __MINGW32__
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
2024-09-18 11:06:12 +00:00
|
|
|
#else
|
|
|
|
SetConsoleTitle(tw_server);
|
2024-09-13 15:09:52 +00:00
|
|
|
#endif
|
2024-09-18 20:02:26 +00:00
|
|
|
return -1;
|
2024-09-13 09:06:44 +00:00
|
|
|
}
|