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

query.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/system/sc/query.c
00005  * PURPOSE:     queries service info
00006  * COPYRIGHT:   Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
00007  *
00008  */
00009 /*
00010  * TODO:
00011  * Allow calling of 2 options e.g.:
00012  *    type= driver state= inactive
00013  */
00014 
00015 #include "sc.h"
00016 
00017 LPTSTR QueryOpts[] = {
00018     _T("type="),
00019     _T("state="),
00020     _T("bufsize="),
00021     _T("ri="),
00022     _T("group="),
00023 };
00024 
00025 
00026 LPSERVICE_STATUS_PROCESS
00027 QueryService(LPCTSTR ServiceName)
00028 {
00029     SC_HANDLE hSCManager = NULL;
00030     LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;
00031     SC_HANDLE hSc = NULL;
00032     DWORD BufSiz = 0;
00033     DWORD BytesNeeded = 0;
00034     DWORD Ret;
00035 
00036     hSCManager = OpenSCManager(NULL,
00037                                NULL,
00038                                SC_MANAGER_CONNECT);
00039     if (hSCManager == NULL)
00040     {
00041         ReportLastError();
00042         return NULL;
00043     }
00044 
00045     hSc = OpenService(hSCManager,
00046                       ServiceName,
00047                       SERVICE_QUERY_STATUS);
00048     if (hSc == NULL)
00049         goto fail;
00050 
00051     Ret = QueryServiceStatusEx(hSc,
00052                                SC_STATUS_PROCESS_INFO,
00053                                NULL,
00054                                BufSiz,
00055                                &BytesNeeded);
00056     if ((Ret != 0) || (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
00057         goto fail;
00058 
00059     pServiceInfo = (LPSERVICE_STATUS_PROCESS)HeapAlloc(GetProcessHeap(),
00060                                                        0,
00061                                                        BytesNeeded);
00062     if (pServiceInfo == NULL)
00063         goto fail;
00064 
00065     if (!QueryServiceStatusEx(hSc,
00066                               SC_STATUS_PROCESS_INFO,
00067                               (LPBYTE)pServiceInfo,
00068                               BytesNeeded,
00069                               &BytesNeeded))
00070     {
00071         goto fail;
00072     }
00073 
00074     CloseServiceHandle(hSc);
00075     CloseServiceHandle(hSCManager);
00076     return pServiceInfo;
00077 
00078 fail:
00079     ReportLastError();
00080     if (pServiceInfo) HeapFree(GetProcessHeap(), 0, pServiceInfo);
00081     if (hSc) CloseServiceHandle(hSc);
00082     if (hSCManager) CloseServiceHandle(hSCManager);
00083     return NULL;
00084 }
00085 
00086 
00087 static BOOL
00088 EnumServices(ENUM_SERVICE_STATUS_PROCESS **pServiceStatus,
00089              DWORD ServiceType,
00090              DWORD ServiceState)
00091 {
00092     SC_HANDLE hSCManager;
00093     DWORD BufSize = 0;
00094     DWORD BytesNeeded = 0;
00095     DWORD ResumeHandle = 0;
00096     DWORD NumServices = 0;
00097     DWORD Ret;
00098 
00099     hSCManager = OpenSCManager(NULL,
00100                                NULL,
00101                                SC_MANAGER_ENUMERATE_SERVICE);
00102     if (hSCManager == NULL)
00103     {
00104         ReportLastError();
00105         return FALSE;
00106     }
00107 
00108     Ret = EnumServicesStatusEx(hSCManager,
00109                                SC_ENUM_PROCESS_INFO,
00110                                ServiceType,
00111                                ServiceState,
00112                                (LPBYTE)*pServiceStatus,
00113                                BufSize,
00114                                &BytesNeeded,
00115                                &NumServices,
00116                                &ResumeHandle,
00117                                0);
00118 
00119     if ((Ret == 0) && (GetLastError() == ERROR_MORE_DATA))
00120     {
00121         *pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *)
00122                           HeapAlloc(GetProcessHeap(),
00123                                     0,
00124                                     BytesNeeded);
00125         if (*pServiceStatus != NULL)
00126         {
00127             if (EnumServicesStatusEx(hSCManager,
00128                                      SC_ENUM_PROCESS_INFO,
00129                                      ServiceType,
00130                                      ServiceState,
00131                                      (LPBYTE)*pServiceStatus,
00132                                      BytesNeeded,
00133                                      &BytesNeeded,
00134                                      &NumServices,
00135                                      &ResumeHandle,
00136                                      0))
00137             {
00138                 return NumServices;
00139             }
00140         }
00141     }
00142 
00143     ReportLastError();
00144     if (pServiceStatus)
00145         HeapFree(GetProcessHeap(), 0, *pServiceStatus);
00146 
00147     return NumServices;
00148 }
00149 
00150 
00151 BOOL
00152 Query(LPCTSTR *ServiceArgs,
00153       DWORD ArgCount,
00154       BOOL bExtended)
00155 {
00156     LPENUM_SERVICE_STATUS_PROCESS pServiceStatus = NULL;
00157     DWORD NumServices = 0;
00158     //DWORD ServiceType;
00159     //DWORD ServiceState;
00160     BOOL bServiceName = TRUE;
00161     DWORD OptSize, i;
00162 
00163     LPCTSTR *TmpArgs;
00164     INT TmpCnt;
00165 
00166 #ifdef SCDBG
00167     TmpArgs = ServiceArgs;
00168     TmpCnt = ArgCount;
00169     _tprintf(_T("Arguments:\n"));
00170     while (TmpCnt)
00171     {
00172         _tprintf(_T("  %s\n"), *TmpArgs);
00173         TmpArgs++;
00174         TmpCnt--;
00175     }
00176     _tprintf(_T("\n"));
00177 #endif /* SCDBG */
00178 
00179     /* display all running services and drivers */
00180     if (ArgCount == 0)
00181     {
00182         NumServices = EnumServices(&pServiceStatus,
00183                                    SERVICE_WIN32,
00184                                    SERVICE_ACTIVE);
00185 
00186         if (NumServices != 0)
00187         {
00188             for (i=0; i < NumServices; i++)
00189             {
00190                 PrintService(pServiceStatus[i].lpServiceName,
00191                              &pServiceStatus[i].ServiceStatusProcess,
00192                              bExtended);
00193             }
00194 
00195             _tprintf(_T("number : %lu\n"), NumServices);
00196 
00197             if (pServiceStatus)
00198                 HeapFree(GetProcessHeap(), 0, pServiceStatus);
00199 
00200             return TRUE;
00201         }
00202 
00203         return FALSE;
00204     }
00205 
00206     TmpArgs = ServiceArgs;
00207     TmpCnt = ArgCount;
00208     OptSize = sizeof(QueryOpts) / sizeof(QueryOpts[0]);
00209     while (TmpCnt--)
00210     {
00211         for (i=0; i < OptSize; i++)
00212         {
00213             if (!lstrcmpi(*TmpArgs, QueryOpts[i]))
00214             {
00215                 bServiceName = FALSE;
00216             }
00217         }
00218         TmpArgs++;
00219     }
00220 
00221 
00222     /* FIXME: parse options */
00223 
00224 
00225     /* print only the service requested */
00226     if (bServiceName)
00227     {
00228         LPSERVICE_STATUS_PROCESS pStatus;
00229         LPCTSTR ServiceName = *ServiceArgs;
00230 
00231         pStatus = QueryService(ServiceName);
00232         if (pStatus)
00233         {
00234             PrintService(ServiceName,
00235                          pStatus,
00236                          bExtended);
00237         }
00238     }
00239 
00240     if (pServiceStatus)
00241         HeapFree(GetProcessHeap(), 0, pServiceStatus);
00242 
00243     return TRUE;
00244 }

Generated on Mon May 28 2012 04:16: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.