Solved

Service Shutdown - WIN32

Posted on 1998-01-07
6
1,354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

630 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