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