2024.8.23
Loading...
Searching...
No Matches
Win32Service Class Reference

Public Member Functions

 __construct ($name)
 
 create ()
 
 delete ()
 
 getBinPath ()
 
 getDisplayName ()
 
 getError ()
 
 getErrorControl ()
 
 getLatestError ()
 
 getLatestStatus ()
 
 getName ()
 
 getNssm ()
 
 getParams ()
 
 getStartType ()
 
 infos ()
 
 isInstalled ()
 
 isPaused ()
 
 isPending ($status)
 
 isRunning ()
 
 isStopped ()
 
 reset ()
 
 restart ()
 
 setBinPath ($binPath)
 
 setDisplayName ($displayName)
 
 setErrorControl ($errorControl)
 
 setName ($name)
 
 setNssm ($nssm)
 
 setParams ($params)
 
 setStartType ($startType)
 
 start ()
 
 status ($timeout=true)
 
 stop ()
 

Static Public Member Functions

static getVbsKeys ()
 

Data Fields

const PENDING_TIMEOUT = 20
 
const SERVER_ERROR_IGNORE = '0'
 
const SERVER_ERROR_NORMAL = '1'
 
const SERVICE_AUTO_START = '2'
 
const SERVICE_DEMAND_START = '3'
 
const SERVICE_DISABLED = '4'
 
const SLEEP_TIME = 500000
 
const VBS_DESCRIPTION = 'Description'
 
const VBS_DISPLAY_NAME = 'DisplayName'
 
const VBS_NAME = 'Name'
 
const VBS_PATH_NAME = 'PathName'
 
const VBS_STATE = 'State'
 
const WIN32_ERROR_ACCESS_DENIED = '5'
 
const WIN32_ERROR_CIRCULAR_DEPENDENCY = '423'
 
const WIN32_ERROR_DATABASE_DOES_NOT_EXIST = '429'
 
const WIN32_ERROR_DEPENDENT_SERVICES_RUNNING = '41B'
 
const WIN32_ERROR_DUPLICATE_SERVICE_NAME = '436'
 
const WIN32_ERROR_FAILED_SERVICE_CONTROLLER_CONNECT = '427'
 
const WIN32_ERROR_INSUFFICIENT_BUFFER = '7A'
 
const WIN32_ERROR_INVALID_DATA = 'D'
 
const WIN32_ERROR_INVALID_HANDLE = '6'
 
const WIN32_ERROR_INVALID_LEVEL = '7C'
 
const WIN32_ERROR_INVALID_NAME = '7B'
 
const WIN32_ERROR_INVALID_PARAMETER = '57'
 
const WIN32_ERROR_INVALID_SERVICE_ACCOUNT = '421'
 
const WIN32_ERROR_INVALID_SERVICE_CONTROL = '41C'
 
const WIN32_ERROR_PATH_NOT_FOUND = '3'
 
const WIN32_ERROR_SERVICE_ALREADY_RUNNING = '420'
 
const WIN32_ERROR_SERVICE_CANNOT_ACCEPT_CTRL = '425'
 
const WIN32_ERROR_SERVICE_DATABASE_LOCKED = '41F'
 
const WIN32_ERROR_SERVICE_DEPENDENCY_DELETED = '433'
 
const WIN32_ERROR_SERVICE_DEPENDENCY_FAIL = '42C'
 
const WIN32_ERROR_SERVICE_DISABLED = '422'
 
const WIN32_ERROR_SERVICE_DOES_NOT_EXIST = '424'
 
const WIN32_ERROR_SERVICE_EXISTS = '431'
 
const WIN32_ERROR_SERVICE_LOGON_FAILED = '42D'
 
const WIN32_ERROR_SERVICE_MARKED_FOR_DELETE = '430'
 
const WIN32_ERROR_SERVICE_NO_THREAD = '41E'
 
const WIN32_ERROR_SERVICE_NOT_ACTIVE = '426'
 
const WIN32_ERROR_SERVICE_REQUEST_TIMEOUT = '41D'
 
const WIN32_ERROR_SHUTDOWN_IN_PROGRESS = '45B'
 
const WIN32_NO_ERROR = '0'
 
const WIN32_SERVICE_CONTINUE_PENDING = '5'
 
const WIN32_SERVICE_NA = '0'
 
const WIN32_SERVICE_PAUSE_PENDING = '6'
 
const WIN32_SERVICE_PAUSED = '7'
 
const WIN32_SERVICE_RUNNING = '4'
 
const WIN32_SERVICE_START_PENDING = '2'
 
const WIN32_SERVICE_STOP_PENDING = '3'
 
const WIN32_SERVICE_STOPPED = '1'
 

Private Member Functions

 callWin32Service ($function, $param, $checkError=false)
 
 getWin32ErrorCodeDesc ($code)
 
 getWin32ServiceStatusDesc ($status)
 
 writeLog ($log)
 

Private Attributes

 $binPath
 
 $displayName
 
 $errorControl
 
 $latestError
 
 $latestStatus
 
 $name
 
 $nssm
 
 $params
 
 $startType
 

Detailed Description

Class Win32Service

This class provides an interface to manage Windows services. It includes methods to create, delete, start, stop, and query the status of services. It also handles logging and error reporting for service operations.

Definition at line 16 of file class.win32service.php.

Constructor & Destructor Documentation

◆ __construct()

Win32Service::__construct ( $name)

Constructor for the Win32Service class.

Parameters
string$nameThe name of the service.

Definition at line 92 of file class.win32service.php.

93 {
94 Util::logInitClass( $this );
95 $this->name = $name;
96 }
static logInitClass($classInstance)

References $name, and Util\logInitClass().

Member Function Documentation

◆ callWin32Service()

Win32Service::callWin32Service ( $function,
$param,
$checkError = false )
private

Calls a Win32 service function.

Parameters
string$functionThe function name.
mixed$paramThe parameter to pass to the function.
bool$checkErrorWhether to check for errors.
Returns
mixed The result of the function call.

Definition at line 134 of file class.win32service.php.

135 {
136 $result = false;
137 if ( function_exists( $function ) ) {
138 $result = call_user_func( $function, $param );
139 if ( $checkError && dechex( $result ) != self::WIN32_NO_ERROR ) {
140 $this->latestError = dechex( $result );
141 }
142 }
143
144 return $result;
145 }
$result

References $result.

Referenced by create(), delete(), start(), status(), and stop().

+ Here is the caller graph for this function:

◆ create()

Win32Service::create ( )

Creates the service.

Returns
bool True if the service was created successfully, false otherwise.

Definition at line 187 of file class.win32service.php.

188 {
189 global $bearsamppBins;
190
191 if ( $this->getName() == BinFilezilla::SERVICE_NAME ) {
192 $bearsamppBins->getFilezilla()->rebuildConf();
193
195 }
196 elseif ( $this->getName() == BinPostgresql::SERVICE_NAME ) {
197 $bearsamppBins->getPostgresql()->rebuildConf();
198 $bearsamppBins->getPostgresql()->initData();
199
201 }
202 if ( $this->getNssm() instanceof Nssm ) {
203 $nssmEnvPath = Util::getAppBinsRegKey( false );
204 $nssmEnvPath .= Util::getNssmEnvPaths();
205 $nssmEnvPath .= '%SystemRoot%/system32;';
206 $nssmEnvPath .= '%SystemRoot%;';
207 $nssmEnvPath .= '%SystemRoot%/system32/Wbem;';
208 $nssmEnvPath .= '%SystemRoot%/system32/WindowsPowerShell/v1.0';
209 $this->getNssm()->setEnvironmentExtra( 'PATH=' . $nssmEnvPath );
210
211 return $this->getNssm()->create();
212 }
213
214 $create = dechex( $this->callWin32Service( 'win32_create_service', array(
215 'service' => $this->getName(),
216 'display' => $this->getDisplayName(),
217 'description' => $this->getDisplayName(),
218 'path' => $this->getBinPath(),
219 'params' => $this->getParams(),
220 'start_type' => $this->getStartType() != null ? $this->getStartType() : self::SERVICE_DEMAND_START,
221 'error_control' => $this->getErrorControl() != null ? $this->getErrorControl() : self::SERVER_ERROR_NORMAL,
222 ), true ) );
223
224 $this->writeLog( 'Create service: ' . $create . ' (status: ' . $this->status() . ')' );
225 $this->writeLog( '-> service: ' . $this->getName() );
226 $this->writeLog( '-> display: ' . $this->getDisplayName() );
227 $this->writeLog( '-> description: ' . $this->getDisplayName() );
228 $this->writeLog( '-> path: ' . $this->getBinPath() );
229 $this->writeLog( '-> params: ' . $this->getParams() );
230 $this->writeLog( '-> start_type: ' . ($this->getStartType() != null ? $this->getStartType() : self::SERVICE_DEMAND_START) );
231 $this->writeLog( '-> service: ' . ($this->getErrorControl() != null ? $this->getErrorControl() : self::SERVER_ERROR_NORMAL) );
232
233 if ( $create != self::WIN32_NO_ERROR ) {
234 return false;
235 }
236 elseif ( !$this->isInstalled() ) {
237 $this->latestError = self::WIN32_NO_ERROR;
238
239 return false;
240 }
241
242 return true;
243 }
global $bearsamppBins
static installPostgresqlService()
static installFilezillaService()
static getNssmEnvPaths()
static getAppBinsRegKey($fromRegistry=true)
callWin32Service($function, $param, $checkError=false)
status($timeout=true)

References $bearsamppBins, callWin32Service(), Util\getAppBinsRegKey(), getBinPath(), getDisplayName(), getErrorControl(), getName(), getNssm(), Util\getNssmEnvPaths(), getParams(), getStartType(), Batch\installFilezillaService(), Batch\installPostgresqlService(), isInstalled(), SERVER_ERROR_NORMAL, SERVICE_DEMAND_START, BinFilezilla\SERVICE_NAME, BinPostgresql\SERVICE_NAME, status(), WIN32_NO_ERROR, and writeLog().

Referenced by reset().

+ Here is the caller graph for this function:

◆ delete()

Win32Service::delete ( )

Deletes the service.

Returns
bool True if the service was deleted successfully, false otherwise.

Definition at line 250 of file class.win32service.php.

251 {
252 if ( !$this->isInstalled() ) {
253 return true;
254 }
255
256 $this->stop();
257
258 if ( $this->getName() == BinFilezilla::SERVICE_NAME ) {
260 }
261 elseif ( $this->getName() == BinPostgresql::SERVICE_NAME ) {
263 }
264
265 $delete = dechex( $this->callWin32Service( 'win32_delete_service', $this->getName(), true ) );
266 $this->writeLog( 'Delete service ' . $this->getName() . ': ' . $delete . ' (status: ' . $this->status() . ')' );
267
268 if ( $delete != self::WIN32_NO_ERROR && $delete != self::WIN32_ERROR_SERVICE_DOES_NOT_EXIST ) {
269 return false;
270 }
271 elseif ( $this->isInstalled() ) {
272 $this->latestError = self::WIN32_NO_ERROR;
273
274 return false;
275 }
276
277 return true;
278 }
static uninstallFilezillaService()
static uninstallPostgresqlService()

References callWin32Service(), getName(), isInstalled(), BinFilezilla\SERVICE_NAME, BinPostgresql\SERVICE_NAME, status(), stop(), Batch\uninstallFilezillaService(), Batch\uninstallPostgresqlService(), WIN32_NO_ERROR, and writeLog().

◆ getBinPath()

Win32Service::getBinPath ( )

Gets the binary path of the service.

Returns
string The binary path of the service.

Definition at line 609 of file class.win32service.php.

610 {
611 return $this->binPath;
612 }

References $binPath.

Referenced by create().

+ Here is the caller graph for this function:

◆ getDisplayName()

Win32Service::getDisplayName ( )

Gets the display name of the service.

Returns
string The display name of the service.

Definition at line 589 of file class.win32service.php.

590 {
591 return $this->displayName;
592 }

References $displayName.

Referenced by create().

+ Here is the caller graph for this function:

◆ getError()

Win32Service::getError ( )

Gets a detailed error message for the latest error encountered by the service.

Returns
string|null The detailed error message, or null if no error.

Definition at line 735 of file class.win32service.php.

736 {
737 global $bearsamppLang;
738 if ( $this->latestError != self::WIN32_NO_ERROR ) {
739 return $bearsamppLang->getValue( Lang::ERROR ) . ' ' .
740 $this->latestError . ' (' . hexdec( $this->latestError ) . ' : ' . $this->getWin32ErrorCodeDesc( $this->latestError ) . ')';
741 }
742 elseif ( $this->latestStatus != self::WIN32_SERVICE_NA ) {
743 return $bearsamppLang->getValue( Lang::STATUS ) . ' ' .
744 $this->latestStatus . ' (' . hexdec( $this->latestStatus ) . ' : ' . $this->getWin32ServiceStatusDesc( $this->latestStatus ) . ')';
745 }
746
747 return null;
748 }
global $bearsamppLang
const ERROR
const STATUS
getWin32ServiceStatusDesc($status)

References $bearsamppLang, Lang\ERROR, getWin32ErrorCodeDesc(), getWin32ServiceStatusDesc(), and Lang\STATUS.

◆ getErrorControl()

Win32Service::getErrorControl ( )

Gets the error control setting of the service.

Returns
string The error control setting of the service.

Definition at line 669 of file class.win32service.php.

670 {
671 return $this->errorControl;
672 }

References $errorControl.

Referenced by create().

+ Here is the caller graph for this function:

◆ getLatestError()

Win32Service::getLatestError ( )

Gets the latest error encountered by the service.

Returns
string The latest error encountered by the service.

Definition at line 725 of file class.win32service.php.

726 {
727 return $this->latestError;
728 }

References $latestError.

◆ getLatestStatus()

Win32Service::getLatestStatus ( )

Gets the latest status of the service.

Returns
string The latest status of the service.

Definition at line 715 of file class.win32service.php.

716 {
717 return $this->latestStatus;
718 }

References $latestStatus.

◆ getName()

Win32Service::getName ( )

Gets the name of the service.

Returns
string The name of the service.

Definition at line 569 of file class.win32service.php.

570 {
571 return $this->name;
572 }

References $name.

Referenced by create(), delete(), infos(), isInstalled(), isPaused(), isRunning(), isStopped(), start(), status(), and stop().

+ Here is the caller graph for this function:

◆ getNssm()

Win32Service::getNssm ( )

Gets the NSSM instance associated with the service.

Returns
Nssm The NSSM instance.

Definition at line 689 of file class.win32service.php.

690 {
691 return $this->nssm;
692 }

References $nssm.

Referenced by create(), and infos().

+ Here is the caller graph for this function:

◆ getParams()

Win32Service::getParams ( )

Gets the parameters for the service.

Returns
string The parameters for the service.

Definition at line 629 of file class.win32service.php.

630 {
631 return $this->params;
632 }

References $params.

Referenced by create().

+ Here is the caller graph for this function:

◆ getStartType()

Win32Service::getStartType ( )

Gets the start type of the service.

Returns
string The start type of the service.

Definition at line 649 of file class.win32service.php.

650 {
651 return $this->startType;
652 }

References $startType.

Referenced by create().

+ Here is the caller graph for this function:

◆ getVbsKeys()

static Win32Service::getVbsKeys ( )
static

Returns an array of VBS keys used for service information.

Returns
array The array of VBS keys.

Definition at line 114 of file class.win32service.php.

115 {
116 return array(
117 self::VBS_NAME,
118 self::VBS_DISPLAY_NAME,
119 self::VBS_DESCRIPTION,
120 self::VBS_PATH_NAME,
121 self::VBS_STATE
122 );
123 }

Referenced by Vbs\getServiceInfos().

+ Here is the caller graph for this function:

◆ getWin32ErrorCodeDesc()

Win32Service::getWin32ErrorCodeDesc ( $code)
private

Returns a description of the Win32 error code.

Parameters
string$codeThe error code.
Returns
string|null The description of the error code, or null if the code is not recognized.

Definition at line 553 of file class.win32service.php.

554 {
555 switch ( $code ) {
557 return 'The handle to the SCM database does not have the appropriate access rights.';
558 // ... other cases ...
559 default:
560 return null;
561 }
562 }
const WIN32_ERROR_ACCESS_DENIED

References WIN32_ERROR_ACCESS_DENIED.

Referenced by getError().

+ Here is the caller graph for this function:

◆ getWin32ServiceStatusDesc()

Win32Service::getWin32ServiceStatusDesc ( $status)
private

Returns a description of the Win32 service status.

Parameters
string$statusThe status code.
Returns
string|null The status description.

Definition at line 505 of file class.win32service.php.

506 {
507 switch ( $status ) {
509 return 'The service continue is pending.';
510 break;
511
513 return 'The service pause is pending.';
514 break;
515
517 return 'The service is paused.';
518 break;
519
521 return 'The service is running.';
522 break;
523
525 return 'The service is starting.';
526 break;
527
529 return 'The service is stopping.';
530 break;
531
533 return 'The service is not running.';
534 break;
535
537 return 'Cannot retrieve service status.';
538 break;
539
540 default:
541 return null;
542 break;
543 }
544 }
const WIN32_SERVICE_STOP_PENDING
const WIN32_SERVICE_START_PENDING
const WIN32_SERVICE_CONTINUE_PENDING
const WIN32_SERVICE_PAUSE_PENDING

References WIN32_SERVICE_CONTINUE_PENDING, WIN32_SERVICE_NA, WIN32_SERVICE_PAUSE_PENDING, WIN32_SERVICE_PAUSED, WIN32_SERVICE_RUNNING, WIN32_SERVICE_START_PENDING, WIN32_SERVICE_STOP_PENDING, and WIN32_SERVICE_STOPPED.

Referenced by getError().

+ Here is the caller graph for this function:

◆ infos()

Win32Service::infos ( )

Retrieves information about the service.

Returns
array The service information.

Definition at line 424 of file class.win32service.php.

425 {
426 if ( $this->getNssm() instanceof Nssm ) {
427 return $this->getNssm()->infos();
428 }
429
430 return Vbs::getServiceInfos( $this->getName() );
431 }
static getServiceInfos($serviceName)

References getName(), getNssm(), and Vbs\getServiceInfos().

◆ isInstalled()

Win32Service::isInstalled ( )

Checks if the service is installed.

Returns
bool True if the service is installed, false otherwise.

Definition at line 438 of file class.win32service.php.

439 {
440 $status = $this->status();
441 $this->writeLog( 'isInstalled ' . $this->getName() . ': ' . ($status != self::WIN32_SERVICE_NA ? 'YES' : 'NO') . ' (status: ' . $status . ')' );
442
443 return $status != self::WIN32_SERVICE_NA;
444 }

References getName(), status(), WIN32_SERVICE_NA, and writeLog().

Referenced by create(), and delete().

+ Here is the caller graph for this function:

◆ isPaused()

Win32Service::isPaused ( )

Checks if the service is paused.

Returns
bool True if the service is paused, false otherwise.

Definition at line 477 of file class.win32service.php.

478 {
479 $status = $this->status();
480 $this->writeLog( 'isPaused ' . $this->getName() . ': ' . ($status == self::WIN32_SERVICE_PAUSED ? 'YES' : 'NO') . ' (status: ' . $status . ')' );
481
482 return $status == self::WIN32_SERVICE_PAUSED;
483 }

References getName(), status(), WIN32_SERVICE_PAUSED, and writeLog().

◆ isPending()

Win32Service::isPending ( $status)

Checks if the service is in a pending state.

Parameters
string$statusThe status to check.
Returns
bool True if the service is in a pending state, false otherwise.

Definition at line 492 of file class.win32service.php.

493 {
494 return $status == self::WIN32_SERVICE_START_PENDING || $status == self::WIN32_SERVICE_STOP_PENDING
495 || $status == self::WIN32_SERVICE_CONTINUE_PENDING || $status == self::WIN32_SERVICE_PAUSE_PENDING;
496 }

References WIN32_SERVICE_PAUSE_PENDING.

Referenced by status().

+ Here is the caller graph for this function:

◆ isRunning()

Win32Service::isRunning ( )

Checks if the service is running.

Returns
bool True if the service is running, false otherwise.

Definition at line 451 of file class.win32service.php.

452 {
453 $status = $this->status();
454 $this->writeLog( 'isRunning ' . $this->getName() . ': ' . ($status == self::WIN32_SERVICE_RUNNING ? 'YES' : 'NO') . ' (status: ' . $status . ')' );
455
456 return $status == self::WIN32_SERVICE_RUNNING;
457 }

References getName(), status(), WIN32_SERVICE_RUNNING, and writeLog().

Referenced by start().

+ Here is the caller graph for this function:

◆ isStopped()

Win32Service::isStopped ( )

Checks if the service is stopped.

Returns
bool True if the service is stopped, false otherwise.

Definition at line 464 of file class.win32service.php.

465 {
466 $status = $this->status();
467 $this->writeLog( 'isStopped ' . $this->getName() . ': ' . ($status == self::WIN32_SERVICE_STOPPED ? 'YES' : 'NO') . ' (status: ' . $status . ')' );
468
469 return $status == self::WIN32_SERVICE_STOPPED;
470 }

References getName(), status(), WIN32_SERVICE_STOPPED, and writeLog().

Referenced by stop().

+ Here is the caller graph for this function:

◆ reset()

Win32Service::reset ( )

Resets the service by deleting and recreating it.

Returns
bool True if the service was reset successfully, false otherwise.

Definition at line 285 of file class.win32service.php.

286 {
287 if ( $this->delete() ) {
288 usleep( self::SLEEP_TIME );
289
290 return $this->create();
291 }
292
293 return false;
294 }

References create().

◆ restart()

Win32Service::restart ( )

Restarts the service by stopping and then starting it.

Returns
bool True if the service was restarted successfully, false otherwise.

Definition at line 410 of file class.win32service.php.

411 {
412 if ( $this->stop() ) {
413 return $this->start();
414 }
415
416 return false;
417 }

References start(), and stop().

◆ setBinPath()

Win32Service::setBinPath ( $binPath)

Sets the binary path of the service.

Parameters
string$binPathThe binary path to set.

Definition at line 619 of file class.win32service.php.

620 {
621 $this->binPath = str_replace( '"', '', Util::formatWindowsPath( $binPath ) );
622 }
static formatWindowsPath($path)

References $binPath, and Util\formatWindowsPath().

Referenced by setNssm().

+ Here is the caller graph for this function:

◆ setDisplayName()

Win32Service::setDisplayName ( $displayName)

Sets the display name of the service.

Parameters
string$displayNameThe display name to set.

Definition at line 599 of file class.win32service.php.

600 {
601 $this->displayName = $displayName;
602 }

References $displayName.

Referenced by setNssm().

+ Here is the caller graph for this function:

◆ setErrorControl()

Win32Service::setErrorControl ( $errorControl)

Sets the error control setting of the service.

Parameters
string$errorControlThe error control setting to set.

Definition at line 679 of file class.win32service.php.

680 {
681 $this->errorControl = $errorControl;
682 }

References $errorControl.

◆ setName()

Win32Service::setName ( $name)

Sets the name of the service.

Parameters
string$nameThe name to set.

Definition at line 579 of file class.win32service.php.

580 {
581 $this->name = $name;
582 }

References $name.

◆ setNssm()

Win32Service::setNssm ( $nssm)

Sets the NSSM instance associated with the service.

Parameters
Nssm$nssmThe NSSM instance to set.

Definition at line 699 of file class.win32service.php.

700 {
701 if ( $nssm instanceof Nssm ) {
702 $this->setDisplayName( $nssm->getDisplayName() );
703 $this->setBinPath( $nssm->getBinPath() );
704 $this->setParams( $nssm->getParams() );
705 $this->setStartType( $nssm->getStart() );
706 $this->nssm = $nssm;
707 }
708 }
setDisplayName($displayName)
setStartType($startType)

References $nssm, setBinPath(), setDisplayName(), setParams(), and setStartType().

◆ setParams()

Win32Service::setParams ( $params)

Sets the parameters for the service.

Parameters
string$paramsThe parameters to set.

Definition at line 639 of file class.win32service.php.

640 {
641 $this->params = $params;
642 }

References $params.

Referenced by setNssm().

+ Here is the caller graph for this function:

◆ setStartType()

Win32Service::setStartType ( $startType)

Sets the start type of the service.

Parameters
string$startTypeThe start type to set.

Definition at line 659 of file class.win32service.php.

660 {
661 $this->startType = $startType;
662 }

References $startType.

Referenced by setNssm().

+ Here is the caller graph for this function:

◆ start()

Win32Service::start ( )

Starts the service.

Returns
bool True if the service was started successfully, false otherwise.

Definition at line 301 of file class.win32service.php.

302 {
303 global $bearsamppBins;
304
305 Util::logInfo('Attempting to start service: ' . $this->getName());
306
307 if ( $this->getName() == BinFilezilla::SERVICE_NAME ) {
308 $bearsamppBins->getFilezilla()->rebuildConf();
309 }
310 elseif ( $this->getName() == BinMysql::SERVICE_NAME ) {
311 $bearsamppBins->getMysql()->initData();
312 }
313 elseif ( $this->getName() == BinMailhog::SERVICE_NAME ) {
314 $bearsamppBins->getMailhog()->rebuildConf();
315 }
316 elseif ( $this->getName() == BinMailpit::SERVICE_NAME ) {
317 $bearsamppBins->getMailpit()->rebuildConf();
318 }
319 elseif ( $this->getName() == BinMemcached::SERVICE_NAME ) {
320 $bearsamppBins->getMemcached()->rebuildConf();
321 }
322 elseif ( $this->getName() == BinPostgresql::SERVICE_NAME ) {
323 $bearsamppBins->getPostgresql()->rebuildConf();
324 $bearsamppBins->getPostgresql()->initData();
325 }
326 elseif ( $this->getName() == BinXlight::SERVICE_NAME ) {
327 $bearsamppBins->getXlight()->rebuildConf();
328 }
329
330
331 $start = dechex( $this->callWin32Service( 'win32_start_service', $this->getName(), true ) );
332 Util::logDebug( 'Start service ' . $this->getName() . ': ' . $start . ' (status: ' . $this->status() . ')' );
333
334 if ( $start != self::WIN32_NO_ERROR && $start != self::WIN32_ERROR_SERVICE_ALREADY_RUNNING ) {
335
336 // Write error to log
337 Util::logError('Failed to start service: ' . $this->getName() . ' with error code: ' . $start);
338
339 if ( $this->getName() == BinApache::SERVICE_NAME ) {
340 $cmdOutput = $bearsamppBins->getApache()->getCmdLineOutput( BinApache::CMD_SYNTAX_CHECK );
341 if ( !$cmdOutput['syntaxOk'] ) {
342 file_put_contents(
343 $bearsamppBins->getApache()->getErrorLog(),
344 '[' . date( 'Y-m-d H:i:s', time() ) . '] [error] ' . $cmdOutput['content'] . PHP_EOL,
345 FILE_APPEND
346 );
347 }
348 }
349 elseif ( $this->getName() == BinMysql::SERVICE_NAME ) {
350 $cmdOutput = $bearsamppBins->getMysql()->getCmdLineOutput( BinMysql::CMD_SYNTAX_CHECK );
351 if ( !$cmdOutput['syntaxOk'] ) {
352 file_put_contents(
353 $bearsamppBins->getMysql()->getErrorLog(),
354 '[' . date( 'Y-m-d H:i:s', time() ) . '] [error] ' . $cmdOutput['content'] . PHP_EOL,
355 FILE_APPEND
356 );
357 }
358 }
359 elseif ( $this->getName() == BinMariadb::SERVICE_NAME ) {
360 $cmdOutput = $bearsamppBins->getMariadb()->getCmdLineOutput( BinMariadb::CMD_SYNTAX_CHECK );
361 if ( !$cmdOutput['syntaxOk'] ) {
362 file_put_contents(
363 $bearsamppBins->getMariadb()->getErrorLog(),
364 '[' . date( 'Y-m-d H:i:s', time() ) . '] [error] ' . $cmdOutput['content'] . PHP_EOL,
365 FILE_APPEND
366 );
367 }
368 }
369
370 return false;
371 }
372 elseif ( !$this->isRunning() ) {
373 $this->latestError = self::WIN32_NO_ERROR;
374 Util::logError('Service ' . $this->getName() . ' is not running after start attempt.');
375 $this->latestError = null;
376 return false;
377 }
378
379 Util::logInfo('Service ' . $this->getName() . ' started successfully.');
380 return true;
381 }
const CMD_SYNTAX_CHECK
const CMD_SYNTAX_CHECK
const SERVICE_NAME
static logError($data, $file=null)
static logDebug($data, $file=null)
static logInfo($data, $file=null)

References $bearsamppBins, callWin32Service(), BinApache\CMD_SYNTAX_CHECK, BinMariadb\CMD_SYNTAX_CHECK, BinMysql\CMD_SYNTAX_CHECK, getName(), isRunning(), Util\logDebug(), Util\logError(), Util\logInfo(), BinApache\SERVICE_NAME, BinFilezilla\SERVICE_NAME, BinMailhog\SERVICE_NAME, BinMailpit\SERVICE_NAME, BinMariadb\SERVICE_NAME, BinMemcached\SERVICE_NAME, BinMysql\SERVICE_NAME, BinPostgresql\SERVICE_NAME, BinXlight\SERVICE_NAME, status(), and WIN32_NO_ERROR.

Referenced by restart().

+ Here is the caller graph for this function:

◆ status()

Win32Service::status ( $timeout = true)

Queries the status of the service.

Parameters
bool$timeoutWhether to use a timeout.
Returns
string The status of the service.

Definition at line 154 of file class.win32service.php.

155 {
156 usleep( self::SLEEP_TIME );
157
158 $this->latestStatus = self::WIN32_SERVICE_NA;
159 $maxtime = time() + self::PENDING_TIMEOUT;
160
161 while ( $this->latestStatus == self::WIN32_SERVICE_NA || $this->isPending( $this->latestStatus ) ) {
162 $this->latestStatus = $this->callWin32Service( 'win32_query_service_status', $this->getName() );
163 if ( is_array( $this->latestStatus ) && isset( $this->latestStatus['CurrentState'] ) ) {
164 $this->latestStatus = dechex( $this->latestStatus['CurrentState'] );
165 }
166 elseif ( dechex( $this->latestStatus ) == self::WIN32_ERROR_SERVICE_DOES_NOT_EXIST ) {
167 $this->latestStatus = dechex( $this->latestStatus );
168 }
169 if ( $timeout && $maxtime < time() ) {
170 break;
171 }
172 }
173
174 if ( $this->latestStatus == self::WIN32_ERROR_SERVICE_DOES_NOT_EXIST ) {
175 $this->latestError = $this->latestStatus;
176 $this->latestStatus = self::WIN32_SERVICE_NA;
177 }
178
179 return $this->latestStatus;
180 }

References $latestStatus, callWin32Service(), getName(), isPending(), PENDING_TIMEOUT, and WIN32_SERVICE_NA.

Referenced by create(), delete(), isInstalled(), isPaused(), isRunning(), isStopped(), start(), and stop().

+ Here is the caller graph for this function:

◆ stop()

Win32Service::stop ( )

Stops the service.

Returns
bool True if the service was stopped successfully, false otherwise.

Definition at line 388 of file class.win32service.php.

389 {
390 $stop = dechex( $this->callWin32Service( 'win32_stop_service', $this->getName(), true ) );
391 $this->writeLog( 'Stop service ' . $this->getName() . ': ' . $stop . ' (status: ' . $this->status() . ')' );
392
393 if ( $stop != self::WIN32_NO_ERROR ) {
394 return false;
395 }
396 elseif ( !$this->isStopped() ) {
397 $this->latestError = self::WIN32_NO_ERROR;
398
399 return false;
400 }
401
402 return true;
403 }

References callWin32Service(), getName(), isStopped(), status(), WIN32_NO_ERROR, and writeLog().

Referenced by delete(), and restart().

+ Here is the caller graph for this function:

◆ writeLog()

Win32Service::writeLog ( $log)
private

Writes a log entry.

Parameters
string$logThe log message.

Definition at line 103 of file class.win32service.php.

104 {
105 global $bearsamppRoot;
106 Util::logDebug( $log, $bearsamppRoot->getServicesLogFilePath() );
107 }
global $bearsamppRoot

References $bearsamppRoot, and Util\logDebug().

Referenced by create(), delete(), isInstalled(), isPaused(), isRunning(), isStopped(), and stop().

+ Here is the caller graph for this function:

Field Documentation

◆ $binPath

Win32Service::$binPath
private

Definition at line 78 of file class.win32service.php.

Referenced by getBinPath(), and setBinPath().

◆ $displayName

Win32Service::$displayName
private

Definition at line 77 of file class.win32service.php.

Referenced by getDisplayName(), and setDisplayName().

◆ $errorControl

Win32Service::$errorControl
private

Definition at line 81 of file class.win32service.php.

Referenced by getErrorControl(), and setErrorControl().

◆ $latestError

Win32Service::$latestError
private

Definition at line 85 of file class.win32service.php.

Referenced by getLatestError().

◆ $latestStatus

Win32Service::$latestStatus
private

Definition at line 84 of file class.win32service.php.

Referenced by getLatestStatus(), and status().

◆ $name

Win32Service::$name
private

Definition at line 76 of file class.win32service.php.

Referenced by __construct(), getName(), and setName().

◆ $nssm

Win32Service::$nssm
private

Definition at line 82 of file class.win32service.php.

Referenced by getNssm(), and setNssm().

◆ $params

Win32Service::$params
private

Definition at line 79 of file class.win32service.php.

Referenced by getParams(), and setParams().

◆ $startType

Win32Service::$startType
private

Definition at line 80 of file class.win32service.php.

Referenced by getStartType(), and setStartType().

◆ PENDING_TIMEOUT

const Win32Service::PENDING_TIMEOUT = 20

Definition at line 67 of file class.win32service.php.

Referenced by status().

◆ SERVER_ERROR_IGNORE

const Win32Service::SERVER_ERROR_IGNORE = '0'

Definition at line 60 of file class.win32service.php.

◆ SERVER_ERROR_NORMAL

const Win32Service::SERVER_ERROR_NORMAL = '1'

◆ SERVICE_AUTO_START

const Win32Service::SERVICE_AUTO_START = '2'

Definition at line 63 of file class.win32service.php.

◆ SERVICE_DEMAND_START

const Win32Service::SERVICE_DEMAND_START = '3'

◆ SERVICE_DISABLED

const Win32Service::SERVICE_DISABLED = '4'

Definition at line 65 of file class.win32service.php.

◆ SLEEP_TIME

const Win32Service::SLEEP_TIME = 500000

Definition at line 68 of file class.win32service.php.

◆ VBS_DESCRIPTION

const Win32Service::VBS_DESCRIPTION = 'Description'

Definition at line 72 of file class.win32service.php.

◆ VBS_DISPLAY_NAME

const Win32Service::VBS_DISPLAY_NAME = 'DisplayName'

Definition at line 71 of file class.win32service.php.

◆ VBS_NAME

const Win32Service::VBS_NAME = 'Name'

Definition at line 70 of file class.win32service.php.

◆ VBS_PATH_NAME

const Win32Service::VBS_PATH_NAME = 'PathName'

Definition at line 73 of file class.win32service.php.

Referenced by Nssm\infos(), and ActionStartup\installServices().

◆ VBS_STATE

const Win32Service::VBS_STATE = 'State'

Definition at line 74 of file class.win32service.php.

◆ WIN32_ERROR_ACCESS_DENIED

const Win32Service::WIN32_ERROR_ACCESS_DENIED = '5'

Definition at line 29 of file class.win32service.php.

Referenced by getWin32ErrorCodeDesc().

◆ WIN32_ERROR_CIRCULAR_DEPENDENCY

const Win32Service::WIN32_ERROR_CIRCULAR_DEPENDENCY = '423'

Definition at line 30 of file class.win32service.php.

◆ WIN32_ERROR_DATABASE_DOES_NOT_EXIST

const Win32Service::WIN32_ERROR_DATABASE_DOES_NOT_EXIST = '429'

Definition at line 31 of file class.win32service.php.

◆ WIN32_ERROR_DEPENDENT_SERVICES_RUNNING

const Win32Service::WIN32_ERROR_DEPENDENT_SERVICES_RUNNING = '41B'

Definition at line 32 of file class.win32service.php.

◆ WIN32_ERROR_DUPLICATE_SERVICE_NAME

const Win32Service::WIN32_ERROR_DUPLICATE_SERVICE_NAME = '436'

Definition at line 33 of file class.win32service.php.

◆ WIN32_ERROR_FAILED_SERVICE_CONTROLLER_CONNECT

const Win32Service::WIN32_ERROR_FAILED_SERVICE_CONTROLLER_CONNECT = '427'

Definition at line 34 of file class.win32service.php.

◆ WIN32_ERROR_INSUFFICIENT_BUFFER

const Win32Service::WIN32_ERROR_INSUFFICIENT_BUFFER = '7A'

Definition at line 35 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_DATA

const Win32Service::WIN32_ERROR_INVALID_DATA = 'D'

Definition at line 36 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_HANDLE

const Win32Service::WIN32_ERROR_INVALID_HANDLE = '6'

Definition at line 37 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_LEVEL

const Win32Service::WIN32_ERROR_INVALID_LEVEL = '7C'

Definition at line 38 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_NAME

const Win32Service::WIN32_ERROR_INVALID_NAME = '7B'

Definition at line 39 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_PARAMETER

const Win32Service::WIN32_ERROR_INVALID_PARAMETER = '57'

Definition at line 40 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_SERVICE_ACCOUNT

const Win32Service::WIN32_ERROR_INVALID_SERVICE_ACCOUNT = '421'

Definition at line 41 of file class.win32service.php.

◆ WIN32_ERROR_INVALID_SERVICE_CONTROL

const Win32Service::WIN32_ERROR_INVALID_SERVICE_CONTROL = '41C'

Definition at line 42 of file class.win32service.php.

◆ WIN32_ERROR_PATH_NOT_FOUND

const Win32Service::WIN32_ERROR_PATH_NOT_FOUND = '3'

Definition at line 43 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_ALREADY_RUNNING

const Win32Service::WIN32_ERROR_SERVICE_ALREADY_RUNNING = '420'

Definition at line 44 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_CANNOT_ACCEPT_CTRL

const Win32Service::WIN32_ERROR_SERVICE_CANNOT_ACCEPT_CTRL = '425'

Definition at line 45 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_DATABASE_LOCKED

const Win32Service::WIN32_ERROR_SERVICE_DATABASE_LOCKED = '41F'

Definition at line 46 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_DEPENDENCY_DELETED

const Win32Service::WIN32_ERROR_SERVICE_DEPENDENCY_DELETED = '433'

Definition at line 47 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_DEPENDENCY_FAIL

const Win32Service::WIN32_ERROR_SERVICE_DEPENDENCY_FAIL = '42C'

Definition at line 48 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_DISABLED

const Win32Service::WIN32_ERROR_SERVICE_DISABLED = '422'

Definition at line 49 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_DOES_NOT_EXIST

const Win32Service::WIN32_ERROR_SERVICE_DOES_NOT_EXIST = '424'

Definition at line 50 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_EXISTS

const Win32Service::WIN32_ERROR_SERVICE_EXISTS = '431'

Definition at line 51 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_LOGON_FAILED

const Win32Service::WIN32_ERROR_SERVICE_LOGON_FAILED = '42D'

Definition at line 52 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_MARKED_FOR_DELETE

const Win32Service::WIN32_ERROR_SERVICE_MARKED_FOR_DELETE = '430'

Definition at line 53 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_NO_THREAD

const Win32Service::WIN32_ERROR_SERVICE_NO_THREAD = '41E'

Definition at line 54 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_NOT_ACTIVE

const Win32Service::WIN32_ERROR_SERVICE_NOT_ACTIVE = '426'

Definition at line 55 of file class.win32service.php.

◆ WIN32_ERROR_SERVICE_REQUEST_TIMEOUT

const Win32Service::WIN32_ERROR_SERVICE_REQUEST_TIMEOUT = '41D'

Definition at line 56 of file class.win32service.php.

◆ WIN32_ERROR_SHUTDOWN_IN_PROGRESS

const Win32Service::WIN32_ERROR_SHUTDOWN_IN_PROGRESS = '45B'

Definition at line 57 of file class.win32service.php.

◆ WIN32_NO_ERROR

const Win32Service::WIN32_NO_ERROR = '0'

Definition at line 58 of file class.win32service.php.

Referenced by create(), delete(), start(), and stop().

◆ WIN32_SERVICE_CONTINUE_PENDING

const Win32Service::WIN32_SERVICE_CONTINUE_PENDING = '5'

Definition at line 19 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc().

◆ WIN32_SERVICE_NA

const Win32Service::WIN32_SERVICE_NA = '0'

Definition at line 26 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc(), isInstalled(), and status().

◆ WIN32_SERVICE_PAUSE_PENDING

const Win32Service::WIN32_SERVICE_PAUSE_PENDING = '6'

Definition at line 20 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc(), and isPending().

◆ WIN32_SERVICE_PAUSED

const Win32Service::WIN32_SERVICE_PAUSED = '7'

Definition at line 21 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc(), and isPaused().

◆ WIN32_SERVICE_RUNNING

const Win32Service::WIN32_SERVICE_RUNNING = '4'

Definition at line 22 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc(), and isRunning().

◆ WIN32_SERVICE_START_PENDING

const Win32Service::WIN32_SERVICE_START_PENDING = '2'

Definition at line 23 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc().

◆ WIN32_SERVICE_STOP_PENDING

const Win32Service::WIN32_SERVICE_STOP_PENDING = '3'

Definition at line 24 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc().

◆ WIN32_SERVICE_STOPPED

const Win32Service::WIN32_SERVICE_STOPPED = '1'

Definition at line 25 of file class.win32service.php.

Referenced by getWin32ServiceStatusDesc(), and isStopped().


The documentation for this class was generated from the following file: