Solved

Service Shutdown - WIN32

Posted on 1998-01-07
6
1,329 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wmiprvse.exe (How to prevent wmiprvse to check COM port?) 1 29
OS Selection Menu 1 47
FTP server windows 2008 5 40
enabling cortana on Windows 10 6 22
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

831 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