Skip to content

Commit cd2d47b

Browse files
committed
Report service status when needed
1 parent 5750c4e commit cd2d47b

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

svcbatch.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,15 @@ static DWORD reportsvcstatus(DWORD cs, DWORD wh)
854854
static DWORD cpcnt = 1;
855855

856856
EnterCriticalSection(&scmservicelock);
857-
if ((servicestatus.dwCurrentState == SERVICE_STOPPED) || (cs == 0)) {
857+
if (servicestatus.dwCurrentState == SERVICE_STOPPED) {
858+
cs = SERVICE_STOPPED;
859+
goto finished;
860+
}
861+
if (cs == 0) {
858862
cs = servicestatus.dwCurrentState;
859-
LeaveCriticalSection(&scmservicelock);
860-
return cs;
863+
if (wh != 0)
864+
SetServiceStatus(svcstathandle, &servicestatus);
865+
goto finished;
861866
}
862867
servicestatus.dwControlsAccepted = 0;
863868
servicestatus.dwCheckPoint = 0;
@@ -886,6 +891,8 @@ static DWORD reportsvcstatus(DWORD cs, DWORD wh)
886891
}
887892
servicestatus.dwCurrentState = cs;
888893
SetServiceStatus(svcstathandle, &servicestatus);
894+
895+
finished:
889896
LeaveCriticalSection(&scmservicelock);
890897
return cs;
891898
}
@@ -1043,7 +1050,7 @@ static void logprintf(const char *format, ...)
10431050
* Create service log file and rotate any previous
10441051
* files in the Logs directory.
10451052
*/
1046-
static DWORD opensvclog(int sstart)
1053+
static DWORD opensvclog(int ssp)
10471054
{
10481055
HANDLE lh = 0;
10491056
wchar_t *logfn;
@@ -1076,7 +1083,8 @@ static DWORD opensvclog(int sstart)
10761083
sa.lpSecurityDescriptor = 0;
10771084
sa.bInheritHandle = 0;
10781085

1079-
reportsvcstatus(SERVICE_START_PENDING, SVCBATCH_START_HINT);
1086+
if (ssp)
1087+
reportsvcstatus(SERVICE_START_PENDING, SVCBATCH_START_HINT);
10801088
lh = CreateFileW(logfn,
10811089
GENERIC_WRITE,
10821090
FILE_SHARE_READ,
@@ -1105,7 +1113,8 @@ static DWORD opensvclog(int sstart)
11051113
if (MoveFileExW(logpn, lognn, MOVEFILE_REPLACE_EXISTING) == 0)
11061114
return GetLastError();
11071115
xfree(lognn);
1108-
reportsvcstatus(SERVICE_START_PENDING, SVCBATCH_START_HINT);
1116+
if (ssp)
1117+
reportsvcstatus(SERVICE_START_PENDING, SVCBATCH_START_HINT);
11091118
}
11101119
xfree(logpn);
11111120
}
@@ -1336,6 +1345,9 @@ static DWORD WINAPI svcmonitorthread(LPVOID unused)
13361345
break;
13371346
}
13381347
else if (cc == SVCBATCH_CTRL_BREAK) {
1348+
#if defined(_DBGVIEW)
1349+
dbgprintf(__FUNCTION__, "break signaled");
1350+
#endif
13391351
EnterCriticalSection(&logservicelock);
13401352
if (IS_VALID_HANDLE(logfhandle)) {
13411353
logfflush();
@@ -1355,14 +1367,17 @@ static DWORD WINAPI svcmonitorthread(LPVOID unused)
13551367
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
13561368
}
13571369
else if (cc == SVCBATCH_CTRL_ROTATE) {
1370+
#if defined(_DBGVIEW)
1371+
dbgprintf(__FUNCTION__, "rotate signaled");
1372+
#endif
13581373
if (rotatesvclog() != 0) {
1374+
#if defined(_DBGVIEW)
1375+
dbgprintf(__FUNCTION__, "rotate log failed");
1376+
#endif
13591377
/**
13601378
* Logfile rotation failed.
13611379
* Create stop thread which will stop the service.
13621380
*/
1363-
#if defined(_DBGVIEW)
1364-
dbgprintf(__FUNCTION__, "rotate log failed");
1365-
#endif
13661381
xcreatethread(1, &svcstopthread, 0);
13671382
break;
13681383
}
@@ -1528,17 +1543,13 @@ DWORD WINAPI servicehandler(DWORD ctrl, DWORD _xe, LPVOID _xd, LPVOID _xc)
15281543
/**
15291544
* Signal to svcmonitorthread that
15301545
* user send custom service control
1531-
*
1532-
* Those signals can be entered either
1533-
* manually or by some sort of cron job
1534-
* eg. to rotate logfile on regular interval
1535-
* or send CTRL_BREAK to Java applications started
1536-
* from batch file
15371546
*/
15381547
InterlockedExchange(&servicectrlnum, ctrl);
15391548
SetEvent(monitorevent);
1549+
reportsvcstatus(0, 1);
15401550
break;
15411551
case SERVICE_CONTROL_INTERROGATE:
1552+
reportsvcstatus(0, 1);
15421553
break;
15431554
default:
15441555
return ERROR_CALL_NOT_IMPLEMENTED;

0 commit comments

Comments
 (0)