windows services. introduction you often need programs that run continuously in the background...

36
WINDOWS SERVICES WINDOWS SERVICES

Upload: lorraine-wright

Post on 26-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

WINDOWS SERVICESWINDOWS SERVICES

IntroductionIntroduction

• You often need programs that run continuously in the background

• Examples:– Email servers– Print spooler

• You often need programs that run continuously in the background

• Examples:– Email servers– Print spooler

Operating Systems ProvisionsOperating Systems Provisions

• UNIX: Daemons

• Windows NT: NT Services

• Windows 2000, Windows XP, Windows Server 2003: Windows services

• UNIX: Daemons

• Windows NT: NT Services

• Windows 2000, Windows XP, Windows Server 2003: Windows services

Facilities For Integrating Products With Windows OS

Facilities For Integrating Products With Windows OS

• The Windows operating system provides a set of services that allow device manufacturers and software vendors to integrate their products with the operating system.

• The Windows operating system provides a set of services that allow device manufacturers and software vendors to integrate their products with the operating system.

Requirements of Windows ServicesRequirements of Windows Services

• Created as normal executable files• Must conform to the interface of the

Services Control Manager (SCM), which manages Windows services

• Must be installed in the Windows services database before use

• Created as normal executable files• Must conform to the interface of the

Services Control Manager (SCM), which manages Windows services

• Must be installed in the Windows services database before use

Skills Needed Skills Needed

• Create a Windows service that conforms to SCM interface

• Create an installer class that is capable of installing a Windows service to the Windows service database

• Create a Windows service that conforms to SCM interface

• Create an installer class that is capable of installing a Windows service to the Windows service database

Skills Needed (cont’d)Skills Needed (cont’d)

• Use the Installer tool (installutil.exe) to install and uninstall a Windows service to the Windows service database

• Connect to a Windows service and issue messages such as start, stop, and continue

• Query a Windows service to retrieve its status

• Use the Installer tool (installutil.exe) to install and uninstall a Windows service to the Windows service database

• Connect to a Windows service and issue messages such as start, stop, and continue

• Query a Windows service to retrieve its status

Facilities (cont’d)Facilities (cont’d)

• Kernel Services– Core part of operating system responsible for

process, thread, and memory management– Enables devices and application to interact with

the kernel and use its functionality– Used by systems programmers

• Kernel Services– Core part of operating system responsible for

process, thread, and memory management– Enables devices and application to interact with

the kernel and use its functionality– Used by systems programmers

Facilities (cont’d)Facilities (cont’d)

• Device Driver Services– A device driver controls and manages a

specific type of hardware device– Device driver services enable

applications to work with devices– Used by systems programmers

• Device Driver Services– A device driver controls and manages a

specific type of hardware device– Device driver services enable

applications to work with devices– Used by systems programmers

Facilities (cont’d)Facilities (cont’d)

• Windows Services– Enables operating system components

and application programs to expose their functionality to other applications

– Applications programmers use this service

• Windows Services– Enables operating system components

and application programs to expose their functionality to other applications

– Applications programmers use this service

Characteristics of Windows ServicesCharacteristics of Windows Services

• Conformance to SCM– Must implement a set of well-known methods,

which enable the service to communicate with the SCM

– E.g.: Stop, Start, Continue, Pause– Not all Windows services handle all messages

• Conformance to SCM– Must implement a set of well-known methods,

which enable the service to communicate with the SCM

– E.g.: Stop, Start, Continue, Pause– Not all Windows services handle all messages

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Examples:– Event Log and Plug and Play do not

accept Pause and Stop messages– Print Spooler accepts Start and Stop, but

not Pause

• Examples:– Event Log and Plug and Play do not

accept Pause and Stop messages– Print Spooler accepts Start and Stop, but

not Pause

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Lack of User Interface– Can have user interface, but most do not– They perform system tasks that usually do not

require user interface– Example: IIS

• Launched when OS is booted up• Is unseen by user, doesn’t require interface

• Lack of User Interface– Can have user interface, but most do not– They perform system tasks that usually do not

require user interface– Example: IIS

• Launched when OS is booted up• Is unseen by user, doesn’t require interface

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Long-lived Process– Typically, is started automatically and

continues to live– Can execute even when no user is

logged on

• Long-lived Process– Typically, is started automatically and

continues to live– Can execute even when no user is

logged on

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Specific User Identity– Most run with System privileges– System is a special account that Windows uses

to perform privileged operations– A service can be launched with a specific user

identity, which can be used to restrict permissions

• Specific User Identity– Most run with System privileges– System is a special account that Windows uses

to perform privileged operations– A service can be launched with a specific user

identity, which can be used to restrict permissions

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Specific User Identity (cont’d)– System administrator can accomplish this easily:

• 1. Set up a user account with the permissions that the administrator wants the service to have

• 2. Configure the Windows service to run with the user identity that was created in Step 1

• Specific User Identity (cont’d)– System administrator can accomplish this easily:

• 1. Set up a user account with the permissions that the administrator wants the service to have

• 2. Configure the Windows service to run with the user identity that was created in Step 1

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Separate Windows Process– A Windows service does not run in the process

of the program that communicates with it– Runs in its own process– Can also share a process with another Windows

service

• Separate Windows Process– A Windows service does not run in the process

of the program that communicates with it– Runs in its own process– Can also share a process with another Windows

service

Characteristics of Windows Services (cont’d)

Characteristics of Windows Services (cont’d)

• Special Installation Procedure– Cannot be started by just executing the

*.exe file– Must be registered with the SCM

• Special Installation Procedure– Cannot be started by just executing the

*.exe file– Must be registered with the SCM

Architecture of Windows ServicesArchitecture of Windows Services

• Windows Service Database

• Service Control Manager

• Windows Service Installer

• Windows Services in execution

• Windows Service Controller

• Windows Service Database

• Service Control Manager

• Windows Service Installer

• Windows Services in execution

• Windows Service Controller

Windows Service DatabaseWindows Service Database

Located in Registry at– HKEY_LOCAL_MACHINE\SYSTEM\

CurrentControlSet\ServicesEach service contains following info:

- Path to the executable file- Security settings- Startup parameters

Located in Registry at– HKEY_LOCAL_MACHINE\SYSTEM\

CurrentControlSet\ServicesEach service contains following info:

- Path to the executable file- Security settings- Startup parameters

Windows Service Database (cont’d)Windows Service Database (cont’d)

• Even though you can directly manipulate the Windows service database by modifying the Windows Registry using regedit, it is recommended that you do not

• To ensure integrity of the Registry, use the interface provided by SCM

• Even though you can directly manipulate the Windows service database by modifying the Windows Registry using regedit, it is recommended that you do not

• To ensure integrity of the Registry, use the interface provided by SCM

Service Control Manager (SCM)Service Control Manager (SCM)

• A Windows component that maintains the Windows service database

• Provides a unified way to control, configure and access these services

• A Windows component that maintains the Windows service database

• Provides a unified way to control, configure and access these services

Service Control Manager (SCM) (cont’d)

Service Control Manager (SCM) (cont’d)

• Tasks performed:– Accepts requests to install and uninstall Windows services

from the database– Starts Windows services either on system startup or on

demand– Enumerates installed Windows services– Maintains status information for running services– Transmits control messages to running services– Locks and unlocks the Windows service database

• Tasks performed:– Accepts requests to install and uninstall Windows services

from the database– Starts Windows services either on system startup or on

demand– Enumerates installed Windows services– Maintains status information for running services– Transmits control messages to running services– Locks and unlocks the Windows service database

Windows Service InstallerWindows Service Installer

• Uses the SCM to install, uninstall and repair a Windows service

• These actions create, delete, and update a Windows service record in the database

• Stores information about how a service will be started

• Uses the SCM to install, uninstall and repair a Windows service

• These actions create, delete, and update a Windows service record in the database

• Stores information about how a service will be started

Windows Service Installer (cont’d)Windows Service Installer (cont’d)

• Information that is stored:– Name that uniquely identifies service– Account name and password under

whose identity the service runs– Independent or shared process– How service is started

• Information that is stored:– Name that uniquely identifies service– Account name and password under

whose identity the service runs– Independent or shared process– How service is started

Windows Services in ExecutionWindows Services in Execution

• SCM creates a process for a service based on information stored in the Windows service database

• SCM sends a Start message to the service• After a Windows service is started, it

continues to run until it receives a Pause or Stop message

• SCM creates a process for a service based on information stored in the Windows service database

• SCM sends a Start message to the service• After a Windows service is started, it

continues to run until it receives a Pause or Stop message

Windows Service Controller ProgramWindows Service Controller Program

• Starts ,Stops, and Pauses a Windows service

• Resumes execution of a paused Windows service

• Queries the database to retrieve the status for a Windows service

• Starts ,Stops, and Pauses a Windows service

• Resumes execution of a paused Windows service

• Queries the database to retrieve the status for a Windows service

Windows Service Controller Program (cont’d)

Windows Service Controller Program (cont’d)

• Installs a new Windows service in the database

• Uninstalls an installed Windows service from the database

• Modifies the configuration information for a Windows service

• Installs a new Windows service in the database

• Uninstalls an installed Windows service from the database

• Modifies the configuration information for a Windows service

.NET Framework Support for Windows Services

.NET Framework Support for Windows Services

• System.ServiceProcess namespace– Contains classes that enable ou to implement,

install and control Windows services

• ServiceBase class– Provides the base-level functionality for a

Windows services application

• System.ServiceProcess namespace– Contains classes that enable ou to implement,

install and control Windows services

• ServiceBase class– Provides the base-level functionality for a

Windows services application

.NET Framework Support for Windows Services (cont’d)

.NET Framework Support for Windows Services (cont’d)

• ServiceProcessInstaller and ServiceInstaller classes

– Enable you to use an installation utility such as installutil.exe to install a Windows service application

• ServiceController class– Enables a program to connect to a Windows service and

perform various operations, such as Start, Stop and Query

• ServiceProcessInstaller and ServiceInstaller classes

– Enable you to use an installation utility such as installutil.exe to install a Windows service application

• ServiceController class– Enables a program to connect to a Windows service and

perform various operations, such as Start, Stop and Query

ServiceBase ClassServiceBase Class

• Provides basic functionality to a Windows service class

• Provides its derived classes with some well-knows methods and properties.

• SCM uses these methods and properties to communicate with Windows services

• Provides basic functionality to a Windows service class

• Provides its derived classes with some well-knows methods and properties.

• SCM uses these methods and properties to communicate with Windows services

Important Members of the ServiceBase Class

Important Members of the ServiceBase Class

• Properties:– AutoLog– CanPauseAndContinue– CanShutDown– CanStop– EventLog– ServiceName

• Properties:– AutoLog– CanPauseAndContinue– CanShutDown– CanStop– EventLog– ServiceName

Important Members of the ServiceBase Class (cont’d)Important Members of the ServiceBase Class (cont’d)

• Methods:– OnContinue()– OnPause()– OnStart()– OnStop()– Run()

• Methods:– OnContinue()– OnPause()– OnStart()– OnStop()– Run()

Understanding How SCM Interacts With A Windows Service

Understanding How SCM Interacts With A Windows Service

• A Windows service contains one or more classes derived from ServiceBase

• Only one of these classes can have a Main() method

• Main() passes instances of the service to Run()• Run() passes references to Windows service

objects to the SCM• SCM uses these objects to send messages to the

Windows service

• A Windows service contains one or more classes derived from ServiceBase

• Only one of these classes can have a Main() method

• Main() passes instances of the service to Run()• Run() passes references to Windows service

objects to the SCM• SCM uses these objects to send messages to the

Windows service

Example: Start MessageExample: Start Message

• SCM finds the path of the Windows service executable file from the Windows service database

• SCM creates a Windows service process by executing Main() in the Windows service executable

• SCM finds the path of the Windows service executable file from the Windows service database

• SCM creates a Windows service process by executing Main() in the Windows service executable

Example: Start Message (cont’d)Example: Start Message (cont’d)

• Main() creates one or more instances of the service class and passes their references to the SCM through the static Run() method

• The handler associated with the start message – OnStart() is executed

– May listen to a port for incoming messages, log events to the event log, spool print jobs, etc.

• Main() creates one or more instances of the service class and passes their references to the SCM through the static Run() method

• The handler associated with the start message – OnStart() is executed

– May listen to a port for incoming messages, log events to the event log, spool print jobs, etc.