LibreOffice Module onlineupdate (master) 1
Macros | Functions | Variables
maintenanceservice.cxx File Reference
#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>
#include <wchar.h>
#include <shlobj.h>
#include "serviceinstall.hxx"
#include "maintenanceservice.hxx"
#include "servicebase.hxx"
#include "workmonitor.hxx"
#include "uachelper.h"
#include "updatehelper.h"
Include dependency graph for maintenanceservice.cxx:

Go to the source code of this file.

Macros

#define LOGS_TO_KEEP   10
 

Functions

BOOL GetLogDirectoryPath (WCHAR *path)
 Obtains the base path where logs should be stored. More...
 
int wmain (int argc, WCHAR **argv)
 
BOOL GetBackupLogPath (LPWSTR path, LPCWSTR basePath, int logNumber)
 Calculated a backup path based on the log number. More...
 
void BackupOldLogs (LPCWSTR basePath, int numLogsToKeep)
 Moves the old log files out of the way before a new one is written. More...
 
DWORD WINAPI EnsureProcessTerminatedThread (LPVOID)
 Ensures the service is shutdown once all work is complete. More...
 
void StartTerminationThread ()
 
void WINAPI SvcMain (DWORD argc, LPWSTR *argv)
 Main entry point when running as a service. More...
 
void ReportSvcStatus (DWORD currentState, DWORD exitCode, DWORD waitHint)
 Sets the current service status and reports it to the SCM. More...
 
DWORD WINAPI StopServiceAndWaitForCommandThread (LPVOID)
 Since the SvcCtrlHandler should only spend at most 30 seconds before returning, this function does the service stop work for the SvcCtrlHandler. More...
 
void WINAPI SvcCtrlHandler (DWORD dwCtrl)
 Called by SCM whenever a control code is sent to the service using the ControlService function. More...
 

Variables

SERVICE_STATUS gSvcStatus = { 0 }
 
SERVICE_STATUS_HANDLE gSvcStatusHandle = nullptr
 
HANDLE gWorkDoneEvent = nullptr
 
HANDLE gThread = nullptr
 
bool gServiceControlStopping = false
 

Macro Definition Documentation

◆ LOGS_TO_KEEP

#define LOGS_TO_KEEP   10

Definition at line 29 of file maintenanceservice.cxx.

Function Documentation

◆ BackupOldLogs()

void BackupOldLogs ( LPCWSTR  basePath,
int  numLogsToKeep 
)

Moves the old log files out of the way before a new one is written.

If you for example keep 3 logs, then this function will do: updater2.log -> updater3.log updater1.log -> updater2.log updater.log -> updater1.log Which clears room for a new updater.log in the basePath directory

Parameters
basePathThe base directory path where log files are stored
numLogsToKeepThe number of logs to keep

Definition at line 208 of file maintenanceservice.cxx.

References GetBackupLogPath(), i, and MAX_PATH.

Referenced by SvcMain().

◆ EnsureProcessTerminatedThread()

DWORD WINAPI EnsureProcessTerminatedThread ( LPVOID  )

Ensures the service is shutdown once all work is complete.

There is an issue on XP SP2 and below where the service can hang in a stop pending state even though the SCM is notified of a stopped state. Control should be returned to StartServiceCtrlDispatcher from the call to SetServiceStatus on a stopped state in the wmain thread. Sometimes this is not the case though. This thread will terminate the process if it has been 5 seconds after all work is done and the process is still not terminated. This thread is only started once a stopped state was sent to the SCM. The stop pending hang can be reproduced intermittently even if you set a stopped state directly and never set a stop pending state. It is safe to forcefully terminate the process ourselves since all work is done once we start this thread.

Definition at line 246 of file maintenanceservice.cxx.

Referenced by StartTerminationThread().

◆ GetBackupLogPath()

BOOL GetBackupLogPath ( LPWSTR  path,
LPCWSTR  basePath,
int  logNumber 
)

Calculated a backup path based on the log number.

Parameters
pathThe out buffer to store the log path of size MAX_PATH + 1
basePathThe base directory where the calculated path should go
logNumberThe log number, 0 == updater.log
Returns
TRUE if successful.

Definition at line 179 of file maintenanceservice.cxx.

References PathAppendSafe().

Referenced by BackupOldLogs().

◆ GetLogDirectoryPath()

BOOL GetLogDirectoryPath ( WCHAR *  path)

Obtains the base path where logs should be stored.

Parameters
pathThe out buffer for the backup log path of size MAX_PATH + 1
Returns
TRUE if successful.

Definition at line 145 of file maintenanceservice.cxx.

References FALSE, hr, PathAppendSafe(), and TRUE.

Referenced by SvcMain(), and wmain().

◆ ReportSvcStatus()

void ReportSvcStatus ( DWORD  currentState,
DWORD  exitCode,
DWORD  waitHint 
)

Sets the current service status and reports it to the SCM.

Parameters
currentStateThe current state (see SERVICE_STATUS)
exitCodeThe system error code
waitHintEstimated time for pending operation in milliseconds

Definition at line 340 of file maintenanceservice.cxx.

References gSvcStatus, and gSvcStatusHandle.

Referenced by StopServiceAndWaitForCommandThread(), SvcCtrlHandler(), and SvcMain().

◆ StartTerminationThread()

void StartTerminationThread ( )

◆ StopServiceAndWaitForCommandThread()

DWORD WINAPI StopServiceAndWaitForCommandThread ( LPVOID  )

Since the SvcCtrlHandler should only spend at most 30 seconds before returning, this function does the service stop work for the SvcCtrlHandler.

Definition at line 380 of file maintenanceservice.cxx.

References gWorkDoneEvent, NO_ERROR, ReportSvcStatus(), and StartTerminationThread().

Referenced by SvcCtrlHandler().

◆ SvcCtrlHandler()

void WINAPI SvcCtrlHandler ( DWORD  dwCtrl)

Called by SCM whenever a control code is sent to the service using the ControlService function.

Definition at line 399 of file maintenanceservice.cxx.

References gServiceControlStopping, NO_ERROR, ReportSvcStatus(), and StopServiceAndWaitForCommandThread().

Referenced by SvcMain().

◆ SvcMain()

void WINAPI SvcMain ( DWORD  argc,
LPWSTR *  argv 
)

◆ wmain()

int wmain ( int  argc,
WCHAR **  argv 
)

Variable Documentation

◆ gServiceControlStopping

bool gServiceControlStopping = false

Definition at line 26 of file maintenanceservice.cxx.

Referenced by SvcCtrlHandler(), and SvcMain().

◆ gSvcStatus

SERVICE_STATUS gSvcStatus = { 0 }

Definition at line 22 of file maintenanceservice.cxx.

Referenced by ReportSvcStatus(), and SvcMain().

◆ gSvcStatusHandle

SERVICE_STATUS_HANDLE gSvcStatusHandle = nullptr

Definition at line 23 of file maintenanceservice.cxx.

Referenced by ReportSvcStatus(), and SvcMain().

◆ gThread

HANDLE gThread = nullptr

Definition at line 25 of file maintenanceservice.cxx.

◆ gWorkDoneEvent

HANDLE gWorkDoneEvent = nullptr

Definition at line 24 of file maintenanceservice.cxx.

Referenced by StopServiceAndWaitForCommandThread(), and SvcMain().