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