Subversion Repositories Tewi

Rev

Rev 90 | Rev 92 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: main.c 91 2024-09-19 13:15:32Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
43 nishi 5
#include "../config.h"
6
 
3 nishi 7
#include <stdio.h>
8
#include <stdbool.h>
9
#include <string.h>
16 nishi 10
#include <signal.h>
3 nishi 11
 
43 nishi 12
#ifndef NO_SSL
6 nishi 13
#include <openssl/opensslv.h>
43 nishi 14
#endif
6 nishi 15
 
3 nishi 16
#include <cm_log.h>
62 nishi 17
#include <cm_string.h>
3 nishi 18
 
4 nishi 19
#include "tw_config.h"
8 nishi 20
#include "tw_server.h"
3 nishi 21
#include "tw_version.h"
22
 
51 nishi 23
#ifdef __MINGW32__
24
#include <windows.h>
25
#endif
26
 
3 nishi 27
extern bool cm_do_log;
18 nishi 28
extern struct tw_config config;
62 nishi 29
extern FILE* logfile;
3 nishi 30
 
18 nishi 31
char tw_server[2048];
32
 
62 nishi 33
int startup(int argc, char** argv);
34
 
35
#ifdef SERVICE
36
SERVICE_STATUS status;
37
SERVICE_STATUS_HANDLE status_handle;
38
 
70 nishi 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;
62 nishi 45
	}
46
	SetServiceStatus(status_handle, &status);
47
}
48
 
70 nishi 49
void WINAPI servmain(DWORD argc, LPSTR* argv) {
62 nishi 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);
70 nishi 63
	if(st != -1) {
62 nishi 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
 
91 nishi 78
void sig(int sign){
79
	printf("%d\n", sign);
80
	exit(0);
81
}
82
 
3 nishi 83
int main(int argc, char** argv) {
62 nishi 84
	logfile = stderr;
85
#ifdef SERVICE
86
	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
87
	StartServiceCtrlDispatcher(table);
88
#else
89
	int st = startup(argc, argv);
90
	if(st != -1) return st;
91
	tw_server_loop();
92
#endif
93
}
94
 
70 nishi 95
int startup(int argc, char** argv) {
3 nishi 96
	int i;
18 nishi 97
	const char* confpath = PREFIX "/etc/tewi.conf";
70 nishi 98
	if(argv != NULL) {
62 nishi 99
		for(i = 1; i < argc; i++) {
100
			if(argv[i][0] == '-') {
101
				if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
102
					if(!cm_do_log) {
103
						cm_do_log = true;
70 nishi 104
#ifndef NO_SSL
62 nishi 105
						cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
70 nishi 106
#else
62 nishi 107
						cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
70 nishi 108
#endif
62 nishi 109
					} else {
110
						cm_do_log = true;
111
					}
112
				} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
113
					i++;
114
					if(argv[i] == NULL) {
115
						fprintf(stderr, "Missing argument\n");
116
						return 1;
117
					}
118
					confpath = argv[i];
119
				} else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
120
					printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
121
					printf("Under public domain.\n");
122
					printf("Original by 2024 Nishi\n");
123
					printf("\n");
124
					printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
125
					printf("--config  | -C config     : Specify config\n");
126
					printf("--verbose | -v            : Verbose mode\n");
127
					printf("--version | -V            : Version information\n");
128
					return 0;
3 nishi 129
				} else {
62 nishi 130
					fprintf(stderr, "Unknown option: %s\n", argv[i]);
4 nishi 131
					return 1;
132
				}
3 nishi 133
			}
134
		}
135
	}
6 nishi 136
	tw_config_init();
18 nishi 137
	if(tw_config_read(confpath) != 0) {
4 nishi 138
		fprintf(stderr, "Could not read the config\n");
139
		return 1;
140
	}
8 nishi 141
	if(tw_server_init() != 0) {
142
		fprintf(stderr, "Could not initialize the server\n");
143
		return 1;
144
	}
18 nishi 145
	sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
62 nishi 146
	char* r = cm_strcat(tw_server, " running...");
147
	cm_force_log(r);
148
	free(r);
16 nishi 149
#ifndef __MINGW32__
150
	signal(SIGCHLD, SIG_IGN);
90 nishi 151
	signal(SIGPIPE, SIG_IGN);
152
	signal(SIGCONT, SIG_IGN);
91 nishi 153
	int i;
154
	for(i = 1; i < 32; i++) signal(i, sig);
50 nishi 155
#else
156
	SetConsoleTitle(tw_server);
16 nishi 157
#endif
62 nishi 158
	return -1;
3 nishi 159
}