-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.c
431 lines (345 loc) · 9 KB
/
os.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* os.c
*
* Author: <[email protected]>
* Created: Sat Apr 26 1997
*/
static char *rcsid =
"$Id: os.c,v 1.8 2002/11/11 12:50:16 Rommel Exp Rommel $";
static char *rcsrev = "$Revision: 1.8 $";
/*
* $Log: os.c,v $
* Revision 1.8 2002/11/11 12:50:16 Rommel
* fix OS/2 timezone resetting bug
*
* Revision 1.7 2002/05/21 06:50:50 Rommel
* fixes psock_errno problem for OS/2
*
* Revision 1.6 1999/06/13 12:01:44 rommel
* *** empty log message ***
*
* Revision 1.5 1999/03/05 15:47:06 rommel
* added logging
*
* Revision 1.4 1999/02/27 14:16:51 rommel
* changed NT service code
*
* Revision 1.3 1998/07/30 06:46:09 rommel
* added Win32 port
* added SNTP support
* fixed many bugs
* prepared for modem support
*
* Revision 1.2 1997/05/04 13:39:04 rommel
* added NT service code
*
* Revision 1.1 1997/04/26 14:21:54 Rommel
* Initial revision
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "os.h"
extern long difftm (struct tm *a, struct tm *b);
#ifdef OS2
#if defined(__UEL__)
#include <os2/os2.h>
#else
#define INCL_DOS
#include <os2.h>
#endif
int stime(time_t *newtime)
{
struct tm *newtm;
struct tm *pgmtm, gmtm = {0};
DATETIME dt;
pgmtm = gmtime(newtime);
/* copy in case gmtime and localtime use the same buffer */
if (pgmtm) gmtm = *pgmtm;
newtm = localtime(newtime);
DosGetDateTime(&dt);
dt.hours = newtm -> tm_hour;
dt.minutes = newtm -> tm_min;
dt.seconds = newtm -> tm_sec;
dt.hundredths = 0;
dt.day = newtm -> tm_mday;
dt.month = newtm -> tm_mon + 1;
dt.year = newtm -> tm_year + 1900;
dt.weekday = newtm -> tm_wday;
dt.timezone = pgmtm ? difftm(&gmtm, newtm) / 60 : 0;
if(newtm->tm_isdst) dt.timezone += 60;
return DosSetDateTime(&dt) != 0;
}
void print_sock_errno(char *text)
{
#if defined(__EMX__) || defined(__UEL__)
int rc = errno;
lprintf("%s: %s", text, strerror(rc));
#else
int rc = sock_errno();
lprintf("%s: error code %d", text, rc);
#endif
}
void print_h_errno(char *text)
{
const char* msg;
switch (h_errno)
{
case NETDB_INTERNAL:
msg = "Internal error";
break;
case NETDB_SUCCESS:
msg = "No error";
break;
case HOST_NOT_FOUND:
msg = "Host not found";
break;
case TRY_AGAIN:
msg = "Try again";
break;
case NO_RECOVERY:
msg = "Non recoverable error";
break;
case NO_DATA:
msg = "No such record";
break;
default:
msg = "Unknown error";
}
lprintf("%s: %s", text, msg);
}
#endif
#ifdef WIN32
#include <windows.h>
#include <stdio.h>
int stime(time_t *newtime)
{
struct tm *newtm = localtime(newtime);
SYSTEMTIME dt;
dt.wHour = newtm -> tm_hour;
dt.wMinute = newtm -> tm_min;
dt.wSecond = newtm -> tm_sec;
dt.wMilliseconds = 0;
dt.wDay = newtm -> tm_mday;
dt.wMonth = newtm -> tm_mon + 1;
dt.wYear = newtm -> tm_year + 1900;
dt.wDayOfWeek = 0;
return !SetLocalTime(&dt);
}
int sock_init(void)
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(1, 1), &wsaData);
}
void print_sock_errno(char *text)
{
int rc = WSAGetLastError();
lprintf("%s: error code %d", text, rc);
}
static char *szServiceName;
static int (*fnServiceFunction)(void);
static int nServiceRC;
static SC_HANDLE schService;
static SC_HANDLE schSCManager;
static SERVICE_STATUS ssStatus;
static SERVICE_STATUS_HANDLE sshStatus;
static HANDLE hEventTerminate;
static HANDLE hThread;
static void StopService(LPTSTR lpszMsg)
{
HANDLE hEventSource;
CHAR chMsg[256];
LPCTSTR lpszStrings[2];
DWORD dwError = GetLastError();
sprintf(chMsg, "%s error: %d", szServiceName, dwError);
lpszStrings[0] = chMsg;
lpszStrings[1] = lpszMsg;
if ((hEventSource = RegisterEventSource(NULL, szServiceName)) != NULL)
{
ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0, 0, NULL, 2, 0,
lpszStrings, NULL);
DeregisterEventSource(hEventSource);
}
SetEvent(hEventTerminate);
}
static BOOL ReportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwCheckPoint, DWORD dwWaitHint)
{
BOOL fResult;
if (dwCurrentState == SERVICE_START_PENDING)
ssStatus.dwControlsAccepted = 0;
else
ssStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
ssStatus.dwCurrentState = dwCurrentState;
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
ssStatus.dwCheckPoint = dwCheckPoint;
ssStatus.dwWaitHint = dwWaitHint;
if (!(fResult = SetServiceStatus(sshStatus, &ssStatus)))
StopService("SetServiceStatus");
return fResult;
}
static VOID CALLBACK ServiceControl(DWORD dwCtrlCode)
{
DWORD dwState = SERVICE_RUNNING;
switch(dwCtrlCode)
{
case SERVICE_CONTROL_PAUSE:
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
SuspendThread(hThread);
dwState = SERVICE_PAUSED;
}
break;
case SERVICE_CONTROL_CONTINUE:
if (ssStatus.dwCurrentState == SERVICE_PAUSED)
{
ResumeThread(hThread);
dwState = SERVICE_RUNNING;
}
break;
case SERVICE_CONTROL_STOP:
dwState = SERVICE_STOP_PENDING;
ReportStatus(SERVICE_STOP_PENDING, NO_ERROR, 1, 3000);
SetEvent(hEventTerminate);
return;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
ReportStatus(dwState, NO_ERROR, 0, 0);
}
static DWORD CALLBACK WorkerThread(LPVOID pArgs)
{
nServiceRC = fnServiceFunction();
return 0;
}
static void ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
DWORD nTID;
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode = 0;
sshStatus = RegisterServiceCtrlHandler(TEXT("SimpleService"), ServiceControl);
if (sshStatus)
{
if (ReportStatus(SERVICE_START_PENDING, NO_ERROR, 0, 0))
{
hEventTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hEventTerminate)
{
hThread = CreateThread(NULL, 0, WorkerThread, NULL, 0, &nTID);
if (hThread)
{
if (ReportStatus(SERVICE_RUNNING, NO_ERROR, 0, 0))
WaitForSingleObject(hEventTerminate, INFINITE);
}
CloseHandle(hEventTerminate);
}
}
}
ReportStatus(SERVICE_STOPPED, nServiceRC, 0, 0);
return;
}
void run_as_service(char *name, int (*function)(void))
{
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{TEXT(name), (LPSERVICE_MAIN_FUNCTION) ServiceMain},
{NULL, NULL}
};
szServiceName = name;
fnServiceFunction = function;
if (!StartServiceCtrlDispatcher(dispatchTable))
StopService("StartServiceCtrlDispatcher failed.");
}
int started_as_service(void)
{
STARTUPINFO si;
GetStartupInfo(&si);
return (si.wShowWindow == 0);
}
int install_as_service(char *name, char *title, int install)
{
char szPath[512];
int rc = 0;
GetModuleFileName(NULL, szPath, sizeof(szPath));
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (install)
{
schService = CreateService(schSCManager, name, title, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szPath,
NULL, NULL, NULL, NULL, NULL);
if (!schService)
rc = GetLastError();
else
CloseServiceHandle(schService);
}
else
{
schService = OpenService(schSCManager, name, SERVICE_ALL_ACCESS);
if (schService)
if (!DeleteService(schService))
rc = GetLastError();
}
CloseServiceHandle(schSCManager);
return rc;
}
int save_options(char *name, void *data, int size)
{
char keyname[512];
HKEY key;
DWORD rc;
strcpy(keyname, "System\\CurrentControlSet\\Services\\");
strcat(keyname, name);
if ((rc = RegOpenKey(HKEY_LOCAL_MACHINE, keyname, &key)) != 0)
return rc;
rc = RegSetValueEx(key, "Parameters", 0, REG_BINARY, data, size);
RegCloseKey(key);
return rc;
}
int restore_options(char *name, void *data, int size)
{
char keyname[512];
HKEY key;
DWORD rc, type, bytes;
strcpy(keyname, "System\\CurrentControlSet\\Services\\");
strcat(keyname, name);
if ((rc = RegOpenKey(HKEY_LOCAL_MACHINE, keyname, &key)) != 0)
return rc;
bytes = size;
rc = RegQueryValueEx(key, "Parameters", 0, &type, data, &bytes);
RegCloseKey(key);
return rc;
}
#endif
char *logfile;
int lprintf(char *format, ...)
{
va_list argptr;
char buffer[1024];
int count;
char filename[256], datetime[32];
FILE *log;
time_t now;
struct tm *tm;
time(&now);
tm = localtime(&now);
count = strftime(buffer, sizeof(buffer), "%m/%d/%y-%H:%M:%S ", tm);
va_start(argptr, format);
count += vsprintf(buffer + count, format, argptr);
va_end(argptr);
if (logfile != NULL && (log = fopen(logfile, "a")) != NULL)
{
fputs(buffer, log);
putc('\n', log);
fclose(log);
}
fputs(buffer, stdout);
putc('\n', stdout);
fflush(stdout);
return count;
}
/* end of os.c */