1 | /* $Id: main.c 168 2024-09-26 20:24:04Z 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 | #include <stdlib.h>
|
---|
12 |
|
---|
13 | #ifndef NO_SSL
|
---|
14 | #include <openssl/opensslv.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <cm_log.h>
|
---|
18 | #include <cm_string.h>
|
---|
19 |
|
---|
20 | #include "tw_config.h"
|
---|
21 | #include "tw_server.h"
|
---|
22 | #include "tw_version.h"
|
---|
23 |
|
---|
24 | #ifdef __MINGW32__
|
---|
25 | #include <windows.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | extern bool cm_do_log;
|
---|
29 | extern struct tw_config config;
|
---|
30 | extern FILE* logfile;
|
---|
31 |
|
---|
32 | char tw_server[2048];
|
---|
33 |
|
---|
34 | int startup(int argc, char** argv);
|
---|
35 |
|
---|
36 | #ifdef SERVICE
|
---|
37 | SERVICE_STATUS status;
|
---|
38 | SERVICE_STATUS_HANDLE status_handle;
|
---|
39 |
|
---|
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;
|
---|
46 | }
|
---|
47 | SetServiceStatus(status_handle, &status);
|
---|
48 | }
|
---|
49 |
|
---|
50 | void WINAPI servmain(DWORD argc, LPSTR* argv) {
|
---|
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);
|
---|
64 | if(st != -1) {
|
---|
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 |
|
---|
79 | int main(int argc, char** argv) {
|
---|
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 | return 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int startup(int argc, char** argv) {
|
---|
93 | int i;
|
---|
94 | const char* confpath = PREFIX "/etc/tewi.conf";
|
---|
95 | if(argv != NULL) {
|
---|
96 | for(i = 1; i < argc; i++) {
|
---|
97 | if(argv[i][0] == '-') {
|
---|
98 | if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
|
---|
99 | if(!cm_do_log) {
|
---|
100 | cm_do_log = true;
|
---|
101 | #ifndef NO_SSL
|
---|
102 | cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
---|
103 | #else
|
---|
104 | cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
---|
105 | #endif
|
---|
106 | } else {
|
---|
107 | cm_do_log = true;
|
---|
108 | }
|
---|
109 | } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
|
---|
110 | i++;
|
---|
111 | if(argv[i] == NULL) {
|
---|
112 | fprintf(stderr, "Missing argument\n");
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 | confpath = argv[i];
|
---|
116 | } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
|
---|
117 | i++;
|
---|
118 | if(argv[i] == NULL) {
|
---|
119 | fprintf(stderr, "Missing argument\n");
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 | if(logfile != NULL && logfile != stderr) {
|
---|
123 | fclose(logfile);
|
---|
124 | }
|
---|
125 | logfile = fopen(argv[i], "a");
|
---|
126 | if(logfile == NULL) {
|
---|
127 | fprintf(stderr, "Failed to open logfile\n");
|
---|
128 | return 1;
|
---|
129 | }
|
---|
130 | } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
|
---|
131 | printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
|
---|
132 | printf("Under public domain.\n");
|
---|
133 | printf("Original by 2024 Nishi\n");
|
---|
134 | printf("\n");
|
---|
135 | printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
|
---|
136 | printf("--config | -C config : Specify config\n");
|
---|
137 | printf("--logfile | -l logfile : Specify logfile\n");
|
---|
138 | printf("--verbose | -v : Verbose mode\n");
|
---|
139 | printf("--version | -V : Version information\n");
|
---|
140 | return 0;
|
---|
141 | } else {
|
---|
142 | fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | tw_config_init();
|
---|
149 | if(tw_config_read(confpath) != 0) {
|
---|
150 | fprintf(stderr, "Could not read the config\n");
|
---|
151 | return 1;
|
---|
152 | }
|
---|
153 | if(tw_server_init() != 0) {
|
---|
154 | fprintf(stderr, "Could not initialize the server\n");
|
---|
155 | return 1;
|
---|
156 | }
|
---|
157 | sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
---|
158 | char* r = cm_strcat(tw_server, " running...");
|
---|
159 | cm_force_log(r);
|
---|
160 | free(r);
|
---|
161 | #ifndef __MINGW32__
|
---|
162 | signal(SIGCHLD, SIG_IGN);
|
---|
163 | signal(SIGPIPE, SIG_IGN);
|
---|
164 | #else
|
---|
165 | SetConsoleTitle(tw_server);
|
---|
166 | #endif
|
---|
167 | return -1;
|
---|
168 | }
|
---|