ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

stop.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Services
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        base/applications/mscutils/servman/stop.c
00005  * PURPOSE:     Stops running a service
00006  * COPYRIGHT:   Copyright 2006-2010 Ged Murphy <gedmurphy@reactos.org>
00007  *
00008  */
00009 
00010 #include "precomp.h"
00011 
00012 
00013 static BOOL
00014 StopService(PMAIN_WND_INFO pInfo,
00015             LPWSTR lpServiceName,
00016             HWND hProgress OPTIONAL)
00017 {
00018     SC_HANDLE hSCManager;
00019     SC_HANDLE hService;
00020     SERVICE_STATUS_PROCESS ServiceStatus;
00021     DWORD dwBytesNeeded;
00022     DWORD dwStartTime;
00023     DWORD dwTimeout;
00024     BOOL bRet = FALSE;
00025 
00026     if (hProgress)
00027     {
00028         /* Set the service name and reset the progress bag */
00029         InitializeProgressDialog(hProgress, lpServiceName);
00030     }
00031 
00032     hSCManager = OpenSCManager(NULL,
00033                                NULL,
00034                                SC_MANAGER_CONNECT);
00035     if (hSCManager)
00036     {
00037         hService = OpenService(hSCManager,
00038                                lpServiceName,
00039                                SERVICE_STOP | SERVICE_QUERY_STATUS);
00040         if (hService)
00041         {
00042             if (hProgress)
00043             {
00044                 /* Increment the progress bar */
00045                 IncrementProgressBar(hProgress, DEFAULT_STEP);
00046             }
00047 
00048             /* Set the wait time to 30 secs */
00049             dwStartTime = GetTickCount();
00050             dwTimeout = 30000;
00051 
00052             /* Send the service the stop code */
00053             if (ControlService(hService,
00054                                SERVICE_CONTROL_STOP,
00055                                (LPSERVICE_STATUS)&ServiceStatus))
00056             {
00057                 if (hProgress)
00058                 {
00059                     /* Increment the progress bar */
00060                     IncrementProgressBar(hProgress, DEFAULT_STEP);
00061                 }
00062 
00063                 while (ServiceStatus.dwCurrentState != SERVICE_STOPPED)
00064                 {
00065                     /* Don't sleep for more than 3 seconds */
00066                     if (ServiceStatus.dwWaitHint > 3000)
00067                         ServiceStatus.dwWaitHint = 3000;
00068 
00069                     Sleep(ServiceStatus.dwWaitHint);
00070 
00071                     if (hProgress)
00072                     {
00073                         /* Increment the progress bar */
00074                         IncrementProgressBar(hProgress, DEFAULT_STEP);
00075                     }
00076 
00077                     if (QueryServiceStatusEx(hService,
00078                                              SC_STATUS_PROCESS_INFO,
00079                                              (LPBYTE)&ServiceStatus,
00080                                              sizeof(SERVICE_STATUS_PROCESS),
00081                                              &dwBytesNeeded))
00082                     {
00083                         /* Have we exceeded our wait time? */
00084                         if (GetTickCount() - dwStartTime > dwTimeout)
00085                         {
00086                             /* Yep, give up */
00087                             break;
00088                         }
00089                     }
00090                 }
00091 
00092                 /* If the service is stopped, return TRUE */
00093                 if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
00094                 {
00095                     bRet = TRUE;
00096                 }
00097             }
00098 
00099             CloseServiceHandle(hService);
00100         }
00101 
00102         CloseServiceHandle(hSCManager);
00103     }
00104 
00105     return bRet;
00106 }
00107 
00108 static BOOL
00109 StopDependantServices(PMAIN_WND_INFO pInfo,
00110                       LPWSTR lpServiceList,
00111                       HWND hProgress OPTIONAL)
00112 {
00113     LPWSTR lpStr;
00114     BOOL bRet = FALSE;
00115 
00116     lpStr = lpServiceList;
00117 
00118     /* Loop through all the services in the list */
00119     while (TRUE)
00120     {
00121         /* Break when we hit the double null */
00122         if (*lpStr == L'\0' && *(lpStr + 1) == L'\0')
00123             break;
00124 
00125         /* If this isn't our first time in the loop we'll
00126            have been left on a null char */
00127         if (*lpStr == L'\0')
00128             lpStr++;
00129 
00130         /* Stop the requested service */
00131         bRet = StopService(pInfo,
00132                            lpStr,
00133                            hProgress);
00134 
00135         /* Complete the progress bar if we succeeded */
00136         if (bRet)
00137         {
00138             CompleteProgressBar(hProgress);
00139         }
00140 
00141         /* Move onto the next string */
00142         while (*lpStr != L'\0')
00143             lpStr++;
00144     }
00145 
00146     return bRet;
00147 }
00148 
00149 
00150 BOOL
00151 DoStop(PMAIN_WND_INFO pInfo)
00152 {
00153     HWND hProgress;
00154     LPWSTR lpServiceList;
00155     BOOL bRet = FALSE;
00156     BOOL bStopMainService = TRUE;
00157 
00158     if (pInfo)
00159     {
00160         /* Does the service have any dependent services which need stopping first */
00161         lpServiceList = GetListOfServicesToStop(pInfo->pCurrentService->lpServiceName);
00162         if (lpServiceList)
00163         {
00164             /* Tag the service list to the main wnd info */
00165             pInfo->pTag = (PVOID)lpServiceList;
00166 
00167             /* List them and ask the user if they want to stop them */
00168             if (DialogBoxParamW(hInstance,
00169                                      MAKEINTRESOURCEW(IDD_DLG_DEPEND_STOP),
00170                                      pInfo->hMainWnd,
00171                                      StopDependsDialogProc,
00172                                      (LPARAM)pInfo) == IDOK)
00173             {
00174                 /* Create a progress window to track the progress of the stopping services */
00175                 hProgress = CreateProgressDialog(pInfo->hMainWnd,
00176                                                  IDS_PROGRESS_INFO_STOP);
00177 
00178                 /* Stop all the dependant services */
00179                 StopDependantServices(pInfo, lpServiceList, hProgress);
00180 
00181                 /* Now stop the requested one */
00182                 bRet = StopService(pInfo,
00183                                    pInfo->pCurrentService->lpServiceName,
00184                                    hProgress);
00185 
00186                 /* We've already stopped the main service, don't try to stop it again */
00187                 bStopMainService = FALSE;
00188 
00189                 if (hProgress)
00190                 {
00191                     /* Complete and destroy the progress bar */
00192                     DestroyProgressDialog(hProgress, TRUE);
00193                 }
00194             }
00195             else
00196             {
00197                 /* Don't stop the main service if the user selected not to */
00198                 bStopMainService = FALSE;
00199             }
00200 
00201             HeapFree(GetProcessHeap(),
00202                      0,
00203                      lpServiceList);
00204         }
00205 
00206         /* If the service has no running dependents, then we stop it here */
00207         if (bStopMainService)
00208         {
00209             /* Create a progress window to track the progress of the stopping service */
00210             hProgress = CreateProgressDialog(pInfo->hMainWnd,
00211                                              IDS_PROGRESS_INFO_STOP);
00212 
00213             /* Stop the requested service */
00214             bRet = StopService(pInfo,
00215                                pInfo->pCurrentService->lpServiceName,
00216                                hProgress);
00217 
00218             if (hProgress)
00219             {
00220                 /* Complete and destroy the progress bar */
00221                 DestroyProgressDialog(hProgress, TRUE);
00222             }
00223         }
00224     }
00225 
00226     return bRet;
00227 }

Generated on Sat May 26 2012 04:15:58 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.