Solved

Service Shutdown - WIN32

Posted on 1998-01-07
6
1,316 Views
Last Modified: 2013-12-28
If I shutdown NT when my NT Service is running, my service does not receive SERVICE_CONTROL_SHUTDOWN signal even though it is definitely set with SERVICE_ACCEPT_SHUTDOWN. The consequence is that if somebody shuts down NT without stopping my Service, my Service does not clean up properly.
Any ideas.
0
Comment
Question by:ascot
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:biyiadeniran
ID: 1788743
sould you give mor einfo pls
did you use visual basic to write the code.
waht does the line for SERVICE_CONTROL_SHUTDOWN and SERVICE_accept_SHUTDOWN look like
0
 
LVL 32

Expert Comment

by:jhance
ID: 1788744
IS the following a possible cause?

The SERVICE_CONTROL_SHUTDOWN control should only be processed by services that must absolutely clean up during shutdown, because there is an extremely limited time (about 20 seconds) available for service shutdown. After this time expires, system shutdown proceeds regardless of whether service shutdown is complete. If the service needs to take more time to shut down, it should send out STOP_PENDING status messages, along with a waithint, so that the service controller knows how long to wait before reporting to the system that service shutdown is complete. For example, the eventlog service needs to clear a dirty bit in the files that it maintains, and the server service needs to shut down so that network connections aren’t made when the system is in the shutdown state.
0
 

Author Comment

by:ascot
ID: 1788745
No, unfortunately the problem is worse than that because I am not even getting the SERVICE_CONTROL_SHUTDOWN signal.  All that I am trying to do in my shutdown code at the moment is write a message to a log file to indicate that I've had the signal.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 32

Expert Comment

by:jhance
ID: 1788746
In that case, are you getting ANY Service Control Manager messages at all?  If not, then your service is not registering itself with SCM properly.  If it is getting some messages, are you sure that it's not hung or deadlocked for some reason by another service?  
0
 

Author Comment

by:ascot
ID: 1788747
Here are the Service Control handler functions, written in C.  I do get the Service Control Stop message OK.  When I shutdown NT I do not get the "Got SCM Control Code" message output in my log file.

BOOL SendStatusToSCM (      DWORD dwCurrentState,
                  DWORD dwWin32ExitCode,
                  DWORD dwServiceSpecificExitCode,
                  DWORD dwWaitHint)
{
      static DWORD dwCheckPoint = 1;
      BOOL success = TRUE;
      SERVICE_STATUS gserviceStatus;
       
      // Fill in all of the SERVICE_STATUS fields
      gserviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
      gserviceStatus.dwCurrentState = dwCurrentState;

      if (dwCurrentState == SERVICE_START_PENDING)
            gserviceStatus.dwControlsAccepted = 0;
      else
            gserviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;

      gserviceStatus.dwWin32ExitCode =      dwWin32ExitCode;
      
      gserviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;

      if ((dwCurrentState == SERVICE_RUNNING) ||
            (dwCurrentState == SERVICE_STOPPED))
                  gserviceStatus.dwCheckPoint = 0;
        else
                  gserviceStatus.dwCheckPoint = dwCheckPoint++;
      
      gserviceStatus.dwWaitHint = dwWaitHint;

      return (success);
}

VOID ServiceCtrlHandler (DWORD controlCode)
{
      DWORD  currentState = 0;
      BOOL success;

      LogDebug (__FILE__, __LINE__, "Got SCM Control Code %ld", controlCode);
      
      switch(controlCode)
      {
            case SERVICE_CONTROL_STOP:
                  LogDebug (__FILE__, __LINE__, "Got a SERVICE_CONTROL_STOP");
                  gbStopped = TRUE;
                  currentState = SERVICE_STOP_PENDING;
                  success = SendStatusToSCM(      SERVICE_STOP_PENDING,
                                          NO_ERROR,
                                          0,
                                          30000);
                  
                  SetEvent(terminateEvent);
                  return;

            case SERVICE_CONTROL_PAUSE:
                  // Should never get this
                  break;

            case SERVICE_CONTROL_CONTINUE:
                  // Should never get this
                  break;

            case SERVICE_CONTROL_INTERROGATE:
                  LogDebug (__FILE__, __LINE__, "Got a SERVICE_CONTROL_INTERROGATE");
                  // it will fall to bottom and send status
                  break;

            case SERVICE_CONTROL_SHUTDOWN:
                  gbStopped = TRUE;
                  currentState = SERVICE_STOP_PENDING;
                  LogDebug (__FILE__, __LINE__, "Got a SERVICE_CONTROL_SHUTDOWN");
                  SetEvent(terminateEvent);
                  return;

            default:
                   break;
      }
      SendStatusToSCM(      currentState,
                        NO_ERROR,
                        0,
                        0);
}

Here follows extract of my code to install the service :

install ()
{
      scm = OpenSCManager(      0,
                        0,
                        SC_MANAGER_ALL_ACCESS);
      
      if (!scm)
            ErrorHandler ("OpenSCmanager failed");

      // Install the new service
      newService = CreateService(      scm,
                              pszServiceName,
                              pszDisplayName,
                              SERVICE_ALL_ACCESS,
                              SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
                              SERVICE_AUTO_START,
                              SERVICE_ERROR_NORMAL,
                              CommandLine,
                              0,
                              0,
                              "RPCSS\0RPCLOCATOR\0\0",
                               0,
                              0);
}

0
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 1788748
In the examnple below:

>case SERVICE_CONTROL_SHUTDOWN:
>gbStopped = TRUE;
>currentState = SERVICE_STOP_PENDING;
>LogDebug (__FILE__, __LINE__, "Got a SERVICE_CONTROL_SHUTDOWN");
>SetEvent(terminateEvent);
>return;

Is it possible that the service is terminated by NT before the LogDebug operation completes?  LogDebug is your function, how does it ensure that any pending data is flushed before returning?
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question