Changeset 62 in Main
- Timestamp:
- Sep 19, 2024, 5:02:26 AM (2 months ago)
- Location:
- trunk
- Files:
-
- 3 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/cm_log.h
r3 r62 5 5 6 6 void cm_log(const char* name, const char* log, ...); 7 void cm_force_log(const char* log); 7 8 8 9 #endif -
trunk/Common/log.c
r12 r62 5 5 #include "cm_string.h" 6 6 7 #include <time.h> 7 8 #include <stdio.h> 8 9 #include <stdbool.h> … … 11 12 #include <stdarg.h> 12 13 14 FILE* logfile; 15 13 16 bool cm_do_log = false; 14 17 15 18 #define LOGNAME_LENGTH 12 19 20 void cm_force_log(const char* log) { 21 time_t t = time(NULL); 22 struct tm* tm = localtime(&t); 23 char date[513]; 24 strftime(date, 512, "%a %b %d %H:%M:%S %Z %Y", tm); 25 fprintf(logfile, "[%s] %s\n", date, log); 26 fflush(logfile); 27 } 16 28 17 29 void cm_log(const char* name, const char* log, ...) { … … 57 69 } 58 70 59 fprintf( stderr, "%s %s\n", namebuf, result);71 fprintf(logfile, "%s %s\n", namebuf, result); 60 72 va_end(args); 61 73 -
trunk/Module/Makefile
r40 r62 15 15 16 16 clean: 17 rm -f *.o *.so *.a 17 rm -f *.o *.so *.a *.dll -
trunk/Platform/win32.mk
r54 r62 12 12 LIB = .dll 13 13 PREOBJS = tewi.res 14 INSTALLER = install.exe -
trunk/Platform/win64.mk
r54 r62 12 12 LIB = .dll 13 13 PREOBJS = tewi.res 14 INSTALLER = install.exe -
trunk/Server/Makefile
r54 r62 8 8 OBJS = version.o main.o config.o server.o http.o module.o strptime.o $(EXTOBJS) $(PREOBJS) 9 9 10 all: tewi$(EXEC) $(INSTALLER) 11 12 install.exe: tewi$(EXEC) install.nsi 13 makensis install.nsi 10 all: tewi$(EXEC) 14 11 15 12 tewi$(EXEC): $(OBJS) ../Common/common.a -
trunk/Server/install.nsi
r61 r62 6 6 Icon "tewi.ico" 7 7 LicenseData ../LICENSE 8 9 !include "LogicLib.nsh" 10 !include "Sections.nsh" 11 8 12 Page license 13 Page components 9 14 Page instfiles 10 15 UninstPage uninstConfirm … … 18 23 SetOutPath "$INSTDIR" 19 24 File /oname=LICENSE.txt "../LICENSE" 20 SetOutPath "$INSTDIR\bin"21 File "tewi.exe"22 25 SetOutPath "$INSTDIR\modules" 23 26 File "../Module/*.dll" … … 43 46 SectionEnd 44 47 48 Section "Install the executable only" SEL_EXEC 49 SetOutPath "$INSTDIR\bin" 50 File "../tewi.exe" 51 WriteINIStr $INSTDIR\install.ini uninstall service false 52 SectionEnd 53 54 Section /o "Install the service too (NT-only)" SEL_SERVICE 55 WriteINIStr $INSTDIR\install.ini uninstall service true 56 FileOpen $9 $INSTDIR\install.bat w 57 FileWrite $9 '"$SYSDIR\sc.exe" stop "TewiHTTPd"$\r$\n' 58 FileClose $9 59 nsExec::Exec '"$INSTDIR\install.bat"' 60 Pop $0 61 DetailPrint "Waiting for 1s so service can stop..." 62 Sleep 1000 63 CreateDirectory "$INSTDIR\logs" 64 SetOutPath "$INSTDIR\bin" 65 File "../tewi.exe" 66 File /oname=tewisrv.exe "../tewi-service.exe" 67 FileOpen $9 $INSTDIR\install.bat w 68 FileWrite $9 '"$SYSDIR\sc.exe" delete "TewiHTTPd"$\r$\n' 69 FileWrite $9 '"$SYSDIR\sc.exe" create "TewiHTTPd" DisplayName= "Tewi HTTPd" binpath= "$INSTDIR\bin\tewisrv.exe" start= "auto"$\r$\n' 70 FileWrite $9 '"$SYSDIR\sc.exe" start "TewiHTTPd"$\r$\n' 71 FileClose $9 72 nsExec::Exec '"$INSTDIR\install.bat"' 73 Pop $0 74 Delete $INSTDIR\install.bat 75 SectionEnd 76 77 Function .onInit 78 StrCpy $1 ${SEL_EXEC} 79 FunctionEnd 80 81 Function .onSelChange 82 !insertmacro StartRadioButtons $1 83 !insertmacro RadioButton ${SEL_EXEC} 84 !insertmacro RadioButton ${SEL_SERVICE} 85 !insertmacro EndRadioButtons 86 FunctionEnd 87 45 88 Section "Uninstall" 89 ReadINIStr $8 $INSTDIR\install.ini uninstall service 90 ${If} $8 == "true" 91 FileOpen $9 $INSTDIR\uninstall.bat w 92 FileWrite $9 '"$SYSDIR\sc.exe" stop "TewiHTTPd"$\r$\n' 93 FileClose $9 94 nsExec::Exec '"$INSTDIR\uninstall.bat"' 95 Pop $0 96 FileOpen $9 $INSTDIR\uninstall.bat w 97 DetailPrint "Waiting for 1s so service can stop..." 98 Sleep 1000 99 FileWrite $9 '"$SYSDIR\sc.exe" delete "TewiHTTPd"$\r$\n' 100 FileClose $9 101 nsExec::Exec '"$INSTDIR\uninstall.bat"' 102 Pop $0 103 Delete $INSTDIR\uninstall.bat 104 ${EndIf} 105 46 106 RMDir /r "$INSTDIR" 47 107 RMDir /r "$SMPROGRAMS\Tewi HTTPd" -
trunk/Server/main.c
r51 r62 15 15 16 16 #include <cm_log.h> 17 #include <cm_string.h> 17 18 18 19 #include "tw_config.h" … … 26 27 extern bool cm_do_log; 27 28 extern struct tw_config config; 29 extern FILE* logfile; 28 30 29 31 char tw_server[2048]; 30 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 31 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){ 32 91 int i; 33 92 const char* confpath = PREFIX "/etc/tewi.conf"; 34 for(i = 1; i < argc; i++) { 35 if(argv[i][0] == '-') { 36 if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) { 37 if(!cm_do_log) { 38 cm_do_log = true; 39 #ifndef NO_SSL 40 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT); 41 #else 42 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version()); 43 #endif 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; 44 124 } else { 45 cm_do_log = true; 46 } 47 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) { 48 i++; 49 if(argv[i] == NULL) { 50 fprintf(stderr, "Missing argument\n"); 125 fprintf(stderr, "Unknown option: %s\n", argv[i]); 51 126 return 1; 52 127 } 53 confpath = argv[i];54 } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {55 printf("Tewi HTTPd Tewi/%s\n", tw_get_version());56 printf("Under public domain.\n");57 printf("Original by 2024 Nishi\n");58 printf("\n");59 printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);60 printf("--config | -C config : Specify config\n");61 printf("--verbose | -v : Verbose mode\n");62 printf("--version | -V : Version information\n");63 return 0;64 } else {65 fprintf(stderr, "Unknown option: %s\n", argv[i]);66 return 1;67 128 } 68 129 } … … 78 139 } 79 140 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension); 80 cm_log("Daemon", "Ready, server: %s", tw_server); 141 char* r = cm_strcat(tw_server, " running..."); 142 cm_force_log(r); 143 free(r); 81 144 #ifndef __MINGW32__ 82 145 signal(SIGCHLD, SIG_IGN); … … 84 147 SetConsoleTitle(tw_server); 85 148 #endif 86 tw_server_loop();149 return -1; 87 150 } -
trunk/Server/server.c
r61 r62 32 32 #include <winsock2.h> 33 33 #include <process.h> 34 #include <windows.h> 34 35 35 36 #include "strptime.h" … … 697 698 } 698 699 700 #ifdef SERVICE 701 extern SERVICE_STATUS status; 702 extern SERVICE_STATUS_HANDLE status_handle; 703 #endif 704 699 705 void tw_server_loop(void) { 700 706 struct timeval tv; … … 710 716 if(ret == -1) { 711 717 break; 718 }else if(ret == 0){ 719 #ifdef SERVICE 720 if(status.dwCurrentState == SERVICE_STOP_PENDING){ 721 break; 722 } 723 #endif 712 724 } else if(ret > 0) { 713 725 /* connection */
Note:
See TracChangeset
for help on using the changeset viewer.