-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflashback.cpp
More file actions
157 lines (134 loc) · 3.74 KB
/
flashback.cpp
File metadata and controls
157 lines (134 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* $Id: flashback.cpp,v 1.20 2009/05/17 00:05:57 laffer1 Exp $ */
#include "global.h" /// < Holds Common Global Header Files
void core(); /// < flashback core running function
#ifdef Win32
#define SVCNAME TEXT("FlashBack")
VOID SvcInstall(void);
VOID WINAPI SvcMain( DWORD, LPTSTR * );
VOID SvcReportEvent( LPTSTR );
//
// Purpose:
// Entry point for the process
//
// Parameters:
// None
//
// Return value:
// None
//
void __cdecl _tmain(int argc, TCHAR *argv[])
{
// If command-line parameter is "install", install the service.
// Otherwise, the service is probably being started by the SCM.
if( lstrcmpi( argv[1], TEXT("install")) == 0 )
{
SvcInstall();
return;
}
// TO_DO: Add any additional services for the process to this table.
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain },
{ NULL, NULL }
};
// This call returns when the service has stopped.
// The process should simply terminate when the call returns.
if (!StartServiceCtrlDispatcher( DispatchTable ))
{
SvcReportEvent(TEXT("StartServiceCtrlDispatcher"));
}
}
//
// Purpose:
// Logs messages to the event log
//
// Parameters:
// szFunction - name of function that failed
//
// Return value:
// None
//
// Remarks:
// The service must have an entry in the Application event log.
//
VOID SvcReportEvent(LPTSTR szFunction)
{
HANDLE hEventSource;
LPCTSTR lpszStrings[2];
TCHAR Buffer[80];
hEventSource = RegisterEventSource(NULL, SVCNAME);
if( NULL != hEventSource )
{
StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());
lpszStrings[0] = SVCNAME;
lpszStrings[1] = Buffer;
ReportEvent(hEventSource, // event log handle
EVENTLOG_ERROR_TYPE, // event type
0, // event category
SVC_ERROR, // event identifier
NULL, // no security identifier
2, // size of lpszStrings array
0, // no binary data
lpszStrings, // array of strings
NULL); // no binary data
DeregisterEventSource(hEventSource);
}
}
#else
/**
* main
* Main program Entry Point
* @param argc number of input params
* @param args pointer to string array of input param
* @return returns program success
*/
int main(int argc, char** args)
{
#ifdef Debug
cout << "Running in Debug Mode!" << endl;
#endif
/* In UNIX programming, a daemon MUST let go of the terminal it is
running on. Otherwise, when you log out, the process dies.
The solution is to fork a copy of yourself and then disconnect
ties to the original terminal like stdin, stdout, stderr
The windows alternative would be whatever one must do to
get a "service" in a microsoft product.
*/
int pid = fork();
switch(pid)
{
// DIDNT WORK
case -1:
#ifdef Debug
std::cout << "Failed to fork" << endl;
#endif
perror("fork");
return -1;
break;
case 0:
#ifdef Debug
std::cout << "Starting Deamon.." << endl;
#endif
// close unneeded file descriptors
fclose(stdout);
fclose(stdin);
fclose(stderr);
// safety features *
setsid(); // become session leader
if (chdir("/") == -1) { // change wrkdir to root
return 1;
}
umask(0); // clear file mode creation mask
//Call Flashback Start
core();
break;
// parent process returns value > 0 upon success (child id)
default:
#ifdef Debug
std::cout << "Deamon running.." << pid << endl;
#endif
break;
}
return 0;
}
#endif