source: Main/trunk/Server/main.c@ 182

Last change on this file since 182 was 182, checked in by Nishi, on Sep 27, 2024 at 9:55:12 PM

psp

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: main.c 182 2024-09-27 12:55:12Z 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#ifdef _PSP
29#include <pspkernel.h>
30#include <pspdebug.h>
31
32PSP_MODULE_INFO("Tewi HTTPd", PSP_MODULE_USER, 1, 1);
33PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
34
35#define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
36#endif
37
38extern bool cm_do_log;
39extern struct tw_config config;
40extern FILE* logfile;
41
42char tw_server[2048];
43
44int startup(int argc, char** argv);
45
46#ifdef SERVICE
47SERVICE_STATUS status;
48SERVICE_STATUS_HANDLE status_handle;
49
50void 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;
56 }
57 SetServiceStatus(status_handle, &status);
58}
59
60void WINAPI servmain(DWORD argc, LPSTR* argv) {
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);
74 if(st != -1) {
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
89int main(int argc, char** argv) {
90 logfile = stderr;
91#ifdef SERVICE
92 SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
93 StartServiceCtrlDispatcher(table);
94#else
95#ifdef _PSP
96 pspDebugScreenInit();
97 pspDebugScreenSetXY(0, 0);
98#endif
99 int st = startup(argc, argv);
100 if(st != -1) return st;
101 tw_server_loop();
102#endif
103 return 0;
104}
105
106int startup(int argc, char** argv) {
107 int i;
108 const char* confpath = PREFIX "/etc/tewi.conf";
109 if(argv != NULL) {
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;
115#ifndef NO_SSL
116 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
117#else
118 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
119#endif
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];
130#ifndef _PSP
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 }
145#endif
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]);
152 printf("--config | -C config : Specify config\n");
153#ifndef _PSP
154 printf("--logfile | -l logfile : Specify logfile\n");
155#endif
156 printf("--verbose | -v : Verbose mode\n");
157 printf("--version | -V : Version information\n");
158 return 0;
159 } else {
160 fprintf(stderr, "Unknown option: %s\n", argv[i]);
161 return 1;
162 }
163 }
164 }
165 }
166 tw_config_init();
167 if(tw_config_read(confpath) != 0) {
168 fprintf(stderr, "Could not read the config\n");
169 return 1;
170 }
171 if(tw_server_init() != 0) {
172 fprintf(stderr, "Could not initialize the server\n");
173 return 1;
174 }
175 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
176 char* r = cm_strcat(tw_server, " running...");
177 cm_force_log(r);
178 free(r);
179#ifndef __MINGW32__
180 signal(SIGCHLD, SIG_IGN);
181 signal(SIGPIPE, SIG_IGN);
182#else
183 SetConsoleTitle(tw_server);
184#endif
185 return -1;
186}
Note: See TracBrowser for help on using the repository browser.