/*
Firebird BBS for Windows NT/2K
Copyright (C) 2000, COMMAN,Kang Xiao-ning, kxn@student.cs.tsinghua.edu.c
n
Windows NT/2000 crontab service for Firebird NT
*/
#include <windows.h>
#include <stdlib.h>
#include <process.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
//#include <sys/cygwin.h>
//#include "pathnames.h"
//#include "config.h"
#define MY_BBS_HOME "/home/bbs"
SERVICE_STATUS FBServiceStatus;
SERVICE_STATUS_HANDLE FBServiceStatusHandle;
void __stdcall FBServiceStart (DWORD argc, LPTSTR argv);
WINBOOL __stdcall FBServiceCtrlHandler (DWORD opcode);
DWORD ExecuteInnd(DWORD argc,LPTSTR argv);
void __stdcall FBServiceStart (DWORD argc, LPTSTR argv)
{
DWORD status;
FBServiceStatus.dwServiceType = SERVICE_WIN32;
FBServiceStatus.dwCurrentState = SERVICE_START_PENDING;
FBServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
FBServiceStatus.dwWin32ExitCode = 0;
FBServiceStatus.dwServiceSpecificExitCode = 0;
FBServiceStatus.dwCheckPoint = 0;
FBServiceStatus.dwWaitHint = 0;
FBServiceStatusHandle = RegisterServiceCtrlHandler(
"FirebirdNTInnd",
FBServiceCtrlHandler);
if (FBServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
MessageBox(0,"RegisterServiceCtrlHandler failed","Error",MB_OK);
return;
}
status = ExecuteInnd(argc,argv);
if (status != 0)
{
FBServiceStatus.dwCurrentState = SERVICE_STOPPED;
FBServiceStatus.dwCheckPoint = 0;
FBServiceStatus.dwWaitHint = 0;
FBServiceStatus.dwWin32ExitCode = status;
FBServiceStatus.dwServiceSpecificExitCode = status;
SetServiceStatus (FBServiceStatusHandle, &FBServiceStatus);
return;
}
FBServiceStatus.dwCurrentState = SERVICE_RUNNING;
FBServiceStatus.dwCheckPoint = 0;
FBServiceStatus.dwWaitHint = 0;
SetServiceStatus (FBServiceStatusHandle, &FBServiceStatus);
return;
}
int stop_innd()
{
FILE *fp ;
char buf[255];
pid_t thepid;
buf[0]='\0';
sprintf(buf,"/home/bbs/tmp/innd.pid");
fp = fopen(buf,"r");
if (fp)
{
fgets(buf,255,fp);
strtok(buf,"\r\n ");
thepid = atoi(buf);
if (thepid && thepid!=getpid()) kill(thepid,9);
fclose(fp);
sprintf(buf,"/home/bbs/tmp/innd.pid");
unlink(buf);
return 0;
}
return 2;
}
WINBOOL __stdcall FBServiceCtrlHandler (DWORD opcode)
{
switch(opcode)
{
case SERVICE_CONTROL_STOP:
stop_innd();
FBServiceStatus.dwCurrentState = SERVICE_STOPPED;
FBServiceStatus.dwCheckPoint = 0;
FBServiceStatus.dwWaitHint = 0;
FBServiceStatus.dwWin32ExitCode = 0;//mysystem(MY_BBS_HOME"/b
in/shutdowncrond.exe");
SetServiceStatus (FBServiceStatusHandle, &FBServiceStatus);
break;
default:
break;
}
}
#include <sys/cygwin.h>
DWORD ExecuteInnd(DWORD argc,LPTSTR argv)
{
/* DWORD dwThreadID;
HANDLE hThread=CreateThread(NULL,0,ExecuteCronThread,NULL,0,&dwThreadID);
if (hThread)
{
CloseHandle(hThread);
return 0;
}
else return GetLastError();*/
char buf[1024],buf2[256];
//#ifdef InndNum
buf2[0]='\0';
sprintf(buf2,MY_BBS_HOME"/inn/innd.exe");
cygwin_conv_to_win32_path(buf2,buf);
//#else
// cygwin_conv_to_win32_path(MY_BBS_HOME"/inn2/innd.exe",buf);
//#endif
if (WinExec(buf,SW_HIDE)>31) return 0; else return 127;
}
void help(void)
{
printf("Firebird BBS NT Innd service -- by glutton \n");
printf("Usage: innsvc [/i] [/u]\n");
printf(" /i: Install Firebird NT Innd service\n");
printf(" /u: Uninstall Service\n");
}
int main(int argc, char *argv[])
{
SC_HANDLE hSCHandle;
SC_HANDLE hService;
char buf[1024];
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ "FirebirdNTInnd", FBServiceStart },
{ NULL, NULL }
};
GetModuleFileName(NULL,buf,1024);
if (argc > 1)
{
if (argv[1][0] != '/' && argv[1][0] != '-')
{
help();
return 0;
}
switch (argv[1][1])
{
case 'i':
case 'u':
hSCHandle = OpenSCManager( NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (!hSCHandle)
{
printf("Opening SC Manager Failed\n");
return -1;
}
if (argv[1][1] == 'i')
{
hService =CreateService(
hSCHandle,
"FirebirdNTInnd",
"FirebirdNTInnd",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
buf,
NULL,
NULL,
NULL,
NULL,
NULL);
if (hService == NULL)
printf("CreateService Failed!\n");
else
printf("CreateService SUCCESS.\n");
CloseServiceHandle(hService);
}
else
{
hService = OpenService(
hSCHandle, // SCManager database
"FirebirdNTInnd", // name of service
DELETE); // only need DELETE access
if (hService == NULL)
printf("OpenService Failed!\n");
if (! DeleteService(hService) )
printf("DeleteService Failed!\n");
else
printf("DeleteService SUCCESS\n");
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCHandle);
break;
default :
help();
break;
}
return 0;
}
/* else
{
help();
return -1;
}*/
if (!StartServiceCtrlDispatcher( DispatchTable))
{
MessageBox(0,"Failed starting Firebird BBS Crontab Service!\nDon't execute
this program directly","Error",MB_OK|MB_ICONERROR);
}
return 0;
}
--
FROM 218.197.235.188