[2] | 1 | /* $Id: main.c 117 2024-09-21 08:47:51Z nishi $ */
|
---|
| 2 |
|
---|
[16] | 3 | #define SOURCE
|
---|
| 4 |
|
---|
[43] | 5 | #include "../config.h"
|
---|
| 6 |
|
---|
[3] | 7 | #include <stdio.h>
|
---|
| 8 | #include <stdbool.h>
|
---|
| 9 | #include <string.h>
|
---|
[16] | 10 | #include <signal.h>
|
---|
[116] | 11 | #include <stdlib.h>
|
---|
[3] | 12 |
|
---|
[43] | 13 | #ifndef NO_SSL
|
---|
[6] | 14 | #include <openssl/opensslv.h>
|
---|
[43] | 15 | #endif
|
---|
[6] | 16 |
|
---|
[3] | 17 | #include <cm_log.h>
|
---|
[62] | 18 | #include <cm_string.h>
|
---|
[3] | 19 |
|
---|
[4] | 20 | #include "tw_config.h"
|
---|
[8] | 21 | #include "tw_server.h"
|
---|
[3] | 22 | #include "tw_version.h"
|
---|
| 23 |
|
---|
[51] | 24 | #ifdef __MINGW32__
|
---|
| 25 | #include <windows.h>
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
[3] | 28 | extern bool cm_do_log;
|
---|
[18] | 29 | extern struct tw_config config;
|
---|
[62] | 30 | extern FILE* logfile;
|
---|
[3] | 31 |
|
---|
[18] | 32 | char tw_server[2048];
|
---|
| 33 |
|
---|
[62] | 34 | int startup(int argc, char** argv);
|
---|
| 35 |
|
---|
| 36 | #ifdef SERVICE
|
---|
| 37 | SERVICE_STATUS status;
|
---|
| 38 | SERVICE_STATUS_HANDLE status_handle;
|
---|
| 39 |
|
---|
[70] | 40 | void WINAPI servhandler(DWORD control) {
|
---|
| 41 | switch(control) {
|
---|
| 42 | case SERVICE_CONTROL_STOP:
|
---|
| 43 | case SERVICE_CONTROL_SHUTDOWN:
|
---|
| 44 | status.dwCurrentState = SERVICE_STOP_PENDING;
|
---|
| 45 | break;
|
---|
[62] | 46 | }
|
---|
| 47 | SetServiceStatus(status_handle, &status);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[70] | 50 | void WINAPI servmain(DWORD argc, LPSTR* argv) {
|
---|
[62] | 51 | logfile = fopen(PREFIX "/logs/tewi.log", "a");
|
---|
| 52 | if(logfile == NULL) logfile = stderr;
|
---|
| 53 | status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
---|
| 54 | status.dwCurrentState = SERVICE_START_PENDING;
|
---|
| 55 | status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
---|
| 56 | status.dwWin32ExitCode = NO_ERROR;
|
---|
| 57 | status.dwServiceSpecificExitCode = 0;
|
---|
| 58 | status.dwCheckPoint = 0;
|
---|
| 59 | status.dwWaitHint = 0;
|
---|
| 60 | status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
|
---|
| 61 | if(status_handle == NULL) return;
|
---|
| 62 | if(SetServiceStatus(status_handle, &status) == 0) return;
|
---|
| 63 | int st = startup(argc, argv);
|
---|
[70] | 64 | if(st != -1) {
|
---|
[62] | 65 | status.dwWin32ExitCode = NO_ERROR;
|
---|
| 66 | status.dwServiceSpecificExitCode = st;
|
---|
| 67 | status.dwCurrentState = SERVICE_STOPPED;
|
---|
| 68 | SetServiceStatus(status_handle, &status);
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 | status.dwCurrentState = SERVICE_RUNNING;
|
---|
| 72 | SetServiceStatus(status_handle, &status);
|
---|
| 73 | tw_server_loop();
|
---|
| 74 | status.dwCurrentState = SERVICE_STOPPED;
|
---|
| 75 | SetServiceStatus(status_handle, &status);
|
---|
| 76 | }
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
[3] | 79 | int main(int argc, char** argv) {
|
---|
[62] | 80 | logfile = stderr;
|
---|
| 81 | #ifdef SERVICE
|
---|
| 82 | SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
|
---|
| 83 | StartServiceCtrlDispatcher(table);
|
---|
| 84 | #else
|
---|
| 85 | int st = startup(argc, argv);
|
---|
| 86 | if(st != -1) return st;
|
---|
| 87 | tw_server_loop();
|
---|
| 88 | #endif
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[70] | 91 | int startup(int argc, char** argv) {
|
---|
[3] | 92 | int i;
|
---|
[18] | 93 | const char* confpath = PREFIX "/etc/tewi.conf";
|
---|
[70] | 94 | if(argv != NULL) {
|
---|
[62] | 95 | for(i = 1; i < argc; i++) {
|
---|
| 96 | if(argv[i][0] == '-') {
|
---|
| 97 | if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
|
---|
| 98 | if(!cm_do_log) {
|
---|
| 99 | cm_do_log = true;
|
---|
[70] | 100 | #ifndef NO_SSL
|
---|
[62] | 101 | cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
---|
[70] | 102 | #else
|
---|
[62] | 103 | cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
---|
[70] | 104 | #endif
|
---|
[62] | 105 | } else {
|
---|
| 106 | cm_do_log = true;
|
---|
| 107 | }
|
---|
| 108 | } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
|
---|
| 109 | i++;
|
---|
| 110 | if(argv[i] == NULL) {
|
---|
| 111 | fprintf(stderr, "Missing argument\n");
|
---|
| 112 | return 1;
|
---|
| 113 | }
|
---|
| 114 | confpath = argv[i];
|
---|
[117] | 115 | } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
|
---|
| 116 | i++;
|
---|
| 117 | if(argv[i] == NULL) {
|
---|
| 118 | fprintf(stderr, "Missing argument\n");
|
---|
| 119 | return 1;
|
---|
| 120 | }
|
---|
| 121 | if(logfile != NULL && logfile != stderr) {
|
---|
| 122 | fclose(logfile);
|
---|
| 123 | }
|
---|
| 124 | logfile = fopen(argv[i], "a");
|
---|
| 125 | if(logfile == NULL) {
|
---|
| 126 | fprintf(stderr, "Failed to open logfile\n");
|
---|
| 127 | return 1;
|
---|
| 128 | }
|
---|
[62] | 129 | } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
|
---|
| 130 | printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
|
---|
| 131 | printf("Under public domain.\n");
|
---|
| 132 | printf("Original by 2024 Nishi\n");
|
---|
| 133 | printf("\n");
|
---|
| 134 | printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
|
---|
| 135 | printf("--config | -C config : Specify config\n");
|
---|
| 136 | printf("--verbose | -v : Verbose mode\n");
|
---|
| 137 | printf("--version | -V : Version information\n");
|
---|
| 138 | return 0;
|
---|
[3] | 139 | } else {
|
---|
[62] | 140 | fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
---|
[4] | 141 | return 1;
|
---|
| 142 | }
|
---|
[3] | 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
[6] | 146 | tw_config_init();
|
---|
[18] | 147 | if(tw_config_read(confpath) != 0) {
|
---|
[4] | 148 | fprintf(stderr, "Could not read the config\n");
|
---|
| 149 | return 1;
|
---|
| 150 | }
|
---|
[8] | 151 | if(tw_server_init() != 0) {
|
---|
| 152 | fprintf(stderr, "Could not initialize the server\n");
|
---|
| 153 | return 1;
|
---|
| 154 | }
|
---|
[18] | 155 | sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
---|
[62] | 156 | char* r = cm_strcat(tw_server, " running...");
|
---|
| 157 | cm_force_log(r);
|
---|
| 158 | free(r);
|
---|
[16] | 159 | #ifndef __MINGW32__
|
---|
| 160 | signal(SIGCHLD, SIG_IGN);
|
---|
[90] | 161 | signal(SIGPIPE, SIG_IGN);
|
---|
[50] | 162 | #else
|
---|
| 163 | SetConsoleTitle(tw_server);
|
---|
[16] | 164 | #endif
|
---|
[62] | 165 | return -1;
|
---|
[3] | 166 | }
|
---|