UCHEBA/5 СЕМ/БСИТ/2 ЛАБА/bsit2.c

210 lines
5.4 KiB
C

#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#define serviceName _T("AAA_ARCHIVER")
#define logfileName "C:\\Users\\user\\Desktop\\bsit2\\x64\\Debug\\log.txt"
#define Path _T("C:/Users/user/Desktop/bsit2/x64/Debug/bsit2.exe")
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE hStatus;
LPTSTR servicePath = Path;
HANDLE hServiceEvent;
void ServiceMain(DWORD argc, LPTSTR* argv);
void ControlHandler(DWORD request);
int addLogMessage(char* str, int code);
int installService();
int removeService();
int startService();
int initService();
int _tmain(int argc, _TCHAR* argv[]) {
if (argc - 1 == 0) {
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{(LPWSTR)serviceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL}
};
if (!StartServiceCtrlDispatcher(ServiceTable)) {
addLogMessage("Error: StartServiceCtrlDispatcher", GetLastError());
}
}
else if (wcscmp(argv[argc - 1], _T("stop")) == 0) {
installService();
}
else if (wcscmp(argv[argc - 1], _T("install")) == 0) {
installService();
}
else if (wcscmp(argv[argc - 1], _T("remove")) == 0) {
removeService();
}
else if (wcscmp(argv[argc - 1], _T("start")) == 0) {
startService();
}
}
int installService() {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_CREATE_SERVICE);
if (!hSCManager) {
addLogMessage("Error: Can't open Service Control Manager", GetLastError());
return -1;
}
SC_HANDLE hService = CreateService(
hSCManager,
serviceName,
serviceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
servicePath,
NULL, NULL, NULL, NULL, NULL
);
if (!hService) {
int err = GetLastError();
switch (err) {
case ERROR_ACCESS_DENIED:
addLogMessage("Error: ERROR_ACCESS_DENIED", err);
break;
case ERROR_CIRCULAR_DEPENDENCY:
addLogMessage("Error: ERROR_CIRCULAR_DEPENDENCY", err);
break;
case ERROR_DUPLICATE_SERVICE_NAME:
addLogMessage("Error: ERROR_DUPLICATE_SERVICE_NAME", err);
break;
case ERROR_INVALID_HANDLE:
addLogMessage("Error: ERROR_INVALID_HANDLE", err);
break;
case ERROR_INVALID_NAME:
addLogMessage("Error: ERROR_INVALID_NAME", err);
break;
case ERROR_INVALID_PARAMETER:
addLogMessage("Error: ERROR_INVALID_PARAMETER", err);
break;
case ERROR_INVALID_SERVICE_ACCOUNT:
addLogMessage("Error: ERROR_INVALID_SERVICE_ACCOUNT", err);
break;
case ERROR_SERVICE_EXISTS:
addLogMessage("Error: ERROR_SERVICE_EXISTS", err);
break;
default:
addLogMessage("Error: Undefined", err);
}
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("Success install service!", 0);
return 0;
}
int removeService() {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if (!hSCManager) {
addLogMessage("Error: Can't open Service Control Manager", GetLastError());
return -1;
}
SC_HANDLE hService = OpenService(hSCManager, serviceName,
SERVICE_STOP | DELETE);
if (!hService) {
addLogMessage("Error: Can't remove service", GetLastError());
CloseServiceHandle(hSCManager);
return -1;
}
DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("Success remove service!", 0);
return 0;
}
int startService() {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_CREATE_SERVICE);
SC_HANDLE hService = OpenService(hSCManager, serviceName,
SERVICE_START);
if (!StartService(hService, 0, NULL)) {
CloseServiceHandle(hSCManager);
addLogMessage("Error: Can't start service", GetLastError());
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return 0;
}
int initService() {
int result = addLogMessage("Service started!", 0);
return result;
}
void ControlHandler(DWORD request) {
switch (request)
{
case SERVICE_CONTROL_STOP:
addLogMessage("Stopped.", 0);
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &serviceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
addLogMessage("Shutdown.", 0);
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &serviceStatus);
return;
default:
break;
}
SetServiceStatus(hStatus, &serviceStatus);
return;
}
void ServiceMain(DWORD argc, LPTSTR* argv) {
addLogMessage("ii", 1);
int error;
int i = 0;
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler(serviceName,
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0) {
return;
}
error = initService();
if (error) {
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &serviceStatus);
return;
}
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hStatus, &serviceStatus);
while (serviceStatus.dwCurrentState == SERVICE_RUNNING)
{
char buffer[255] = "GOVNO ZARABOTALO ";
int result = addLogMessage(buffer, i);
i++;
}
return;
}
int addLogMessage(char* str, int code) {
errno_t err;
FILE* log;
if ((err = fopen_s(&log, logfileName, "a+")) != 0) {
return -1;
}
fprintf(log, "[code: %u] %s\n", code, str);
fclose(log);
return 0;
}