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

config.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Service Control Manager
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        base/system/services/config.c
00005  * PURPOSE:     Service configuration interface
00006  * COPYRIGHT:   Copyright 2005 Eric Kohl
00007  *
00008  */
00009 
00010 /* INCLUDES *****************************************************************/
00011 
00012 #include "services.h"
00013 
00014 #define NDEBUG
00015 #include <debug.h>
00016 
00017 /* FUNCTIONS *****************************************************************/
00018 
00019 
00020 DWORD
00021 ScmOpenServiceKey(LPWSTR lpServiceName,
00022                   REGSAM samDesired,
00023                   PHKEY phKey)
00024 {
00025     HKEY hServicesKey = NULL;
00026     DWORD dwError;
00027 
00028     *phKey = NULL;
00029 
00030     dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
00031                             L"System\\CurrentControlSet\\Services",
00032                             0,
00033                             KEY_READ,
00034                             &hServicesKey);
00035     if (dwError != ERROR_SUCCESS)
00036         return dwError;
00037 
00038     dwError = RegOpenKeyExW(hServicesKey,
00039                             lpServiceName,
00040                             0,
00041                             samDesired,
00042                             phKey);
00043 
00044     RegCloseKey(hServicesKey);
00045 
00046     return dwError;
00047 }
00048 
00049 
00050 DWORD
00051 ScmCreateServiceKey(LPCWSTR lpServiceName,
00052                     REGSAM samDesired,
00053                     PHKEY phKey)
00054 {
00055     HKEY hServicesKey = NULL;
00056     DWORD dwDisposition;
00057     DWORD dwError;
00058 
00059     *phKey = NULL;
00060 
00061     dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
00062                             L"System\\CurrentControlSet\\Services",
00063                             0,
00064                             KEY_READ | KEY_CREATE_SUB_KEY,
00065                             &hServicesKey);
00066     if (dwError != ERROR_SUCCESS)
00067         return dwError;
00068 
00069     dwError = RegCreateKeyExW(hServicesKey,
00070                               lpServiceName,
00071                               0,
00072                               NULL,
00073                               REG_OPTION_NON_VOLATILE,
00074                               samDesired,
00075                               NULL,
00076                               phKey,
00077                               &dwDisposition);
00078 #if 0
00079     if ((dwError == ERROR_SUCCESS) &&
00080         (dwDisposition == REG_OPENED_EXISTING_KEY))
00081     {
00082         RegCloseKey(*phKey);
00083         *phKey = NULL;
00084         dwError = ERROR_SERVICE_EXISTS;
00085     }
00086 #endif
00087 
00088     RegCloseKey(hServicesKey);
00089 
00090     return dwError;
00091 }
00092 
00093 
00094 
00095 DWORD
00096 ScmWriteDependencies(HKEY hServiceKey,
00097                      LPCWSTR lpDependencies,
00098                      DWORD dwDependenciesLength)
00099 {
00100     DWORD dwError = ERROR_SUCCESS;
00101     DWORD dwGroupLength = 0;
00102     DWORD dwServiceLength = 0;
00103     DWORD dwLength;
00104     LPWSTR lpGroupDeps;
00105     LPWSTR lpServiceDeps;
00106     LPCWSTR lpSrc;
00107     LPWSTR lpDst;
00108 
00109     if (*lpDependencies == 0)
00110     {
00111         RegDeleteValueW(hServiceKey,
00112                        L"DependOnService");
00113         RegDeleteValueW(hServiceKey,
00114                        L"DependOnGroup");
00115     }
00116     else
00117     {
00118         lpGroupDeps = HeapAlloc(GetProcessHeap(),
00119                                 HEAP_ZERO_MEMORY,
00120                                 (dwDependenciesLength + 2) * sizeof(WCHAR));
00121         if (lpGroupDeps == NULL)
00122             return ERROR_NOT_ENOUGH_MEMORY;
00123 
00124         lpSrc = lpDependencies;
00125         lpDst = lpGroupDeps;
00126         while (*lpSrc != 0)
00127         {
00128             dwLength = wcslen(lpSrc) + 1;
00129             if (*lpSrc == SC_GROUP_IDENTIFIERW)
00130             {
00131                 lpSrc++;
00132                 dwGroupLength += dwLength;
00133                 wcscpy(lpDst, lpSrc);
00134                 lpDst = lpDst + dwLength;
00135             }
00136 
00137             lpSrc = lpSrc + dwLength;
00138         }
00139         *lpDst = 0;
00140         lpDst++;
00141         dwGroupLength++;
00142 
00143         lpSrc = lpDependencies;
00144         lpServiceDeps = lpDst;
00145         while (*lpSrc != 0)
00146         {
00147             dwLength = wcslen(lpSrc) + 1;
00148             if (*lpSrc != SC_GROUP_IDENTIFIERW)
00149             {
00150                 dwServiceLength += dwLength;
00151                 wcscpy(lpDst, lpSrc);
00152                 lpDst = lpDst + dwLength;
00153             }
00154 
00155             lpSrc = lpSrc + dwLength;
00156         }
00157         *lpDst = 0;
00158         dwServiceLength++;
00159 
00160         if (dwGroupLength > 1)
00161         {
00162             dwError = RegSetValueExW(hServiceKey,
00163                                      L"DependOnGroup",
00164                                      0,
00165                                      REG_MULTI_SZ,
00166                                      (LPBYTE)lpGroupDeps,
00167                                      dwGroupLength * sizeof(WCHAR));
00168         }
00169         else
00170         {
00171             RegDeleteValueW(hServiceKey,
00172                             L"DependOnGroup");
00173         }
00174 
00175         if (dwError == ERROR_SUCCESS)
00176         {
00177             if (dwServiceLength > 1)
00178             {
00179                 dwError = RegSetValueExW(hServiceKey,
00180                                          L"DependOnService",
00181                                          0,
00182                                          REG_MULTI_SZ,
00183                                          (LPBYTE)lpServiceDeps,
00184                                          dwServiceLength * sizeof(WCHAR));
00185             }
00186             else
00187             {
00188                 RegDeleteValueW(hServiceKey,
00189                                 L"DependOnService");
00190             }
00191         }
00192 
00193         HeapFree(GetProcessHeap(), 0, lpGroupDeps);
00194     }
00195 
00196     return dwError;
00197 }
00198 
00199 
00200 DWORD
00201 ScmMarkServiceForDelete(PSERVICE pService)
00202 {
00203     HKEY hServiceKey = NULL;
00204     DWORD dwValue = 1;
00205     DWORD dwError;
00206 
00207     DPRINT("ScmMarkServiceForDelete() called\n");
00208 
00209     dwError = ScmOpenServiceKey(pService->lpServiceName,
00210                                 KEY_WRITE,
00211                                 &hServiceKey);
00212     if (dwError != ERROR_SUCCESS)
00213         return dwError;
00214 
00215     dwError = RegSetValueExW(hServiceKey,
00216                              L"DeleteFlag",
00217                              0,
00218                              REG_DWORD,
00219                              (LPBYTE)&dwValue,
00220                              sizeof(DWORD));
00221 
00222     RegCloseKey(hServiceKey);
00223 
00224     return dwError;
00225 }
00226 
00227 
00228 BOOL
00229 ScmIsDeleteFlagSet(HKEY hServiceKey)
00230 {
00231     DWORD dwError;
00232     DWORD dwType;
00233     DWORD dwFlag;
00234     DWORD dwSize = sizeof(DWORD);
00235 
00236     dwError = RegQueryValueExW(hServiceKey,
00237                                L"DeleteFlag",
00238                                0,
00239                                &dwType,
00240                                (LPBYTE)&dwFlag,
00241                                &dwSize);
00242 
00243     return (dwError == ERROR_SUCCESS);
00244 }
00245 
00246 
00247 DWORD
00248 ScmReadString(HKEY hServiceKey,
00249               LPWSTR lpValueName,
00250               LPWSTR *lpValue)
00251 {
00252     DWORD dwError;
00253     DWORD dwSize;
00254     DWORD dwType;
00255     DWORD dwSizeNeeded;
00256     LPWSTR expanded = NULL;
00257     LPWSTR ptr = NULL;
00258 
00259     *lpValue = NULL;
00260 
00261     dwSize = 0;
00262     dwError = RegQueryValueExW(hServiceKey,
00263                                lpValueName,
00264                                0,
00265                                &dwType,
00266                                NULL,
00267                                &dwSize);
00268     if (dwError != ERROR_SUCCESS)
00269         return dwError;
00270 
00271     ptr = HeapAlloc(GetProcessHeap(), 0, dwSize);
00272     if (ptr == NULL)
00273         return ERROR_NOT_ENOUGH_MEMORY;
00274 
00275     dwError = RegQueryValueExW(hServiceKey,
00276                                lpValueName,
00277                                0,
00278                                &dwType,
00279                                (LPBYTE)ptr,
00280                                &dwSize);
00281     if (dwError != ERROR_SUCCESS)
00282         goto done;
00283 
00284     if (dwType == REG_EXPAND_SZ)
00285     {
00286         /* Expand the value... */
00287         dwSizeNeeded = ExpandEnvironmentStringsW((LPCWSTR)ptr, NULL, 0);
00288         if (dwSizeNeeded == 0)
00289         {
00290             dwError = GetLastError();
00291             goto done;
00292         }
00293         expanded = HeapAlloc(GetProcessHeap(), 0, dwSizeNeeded * sizeof(WCHAR));
00294         if (dwSizeNeeded < ExpandEnvironmentStringsW((LPCWSTR)ptr, expanded, dwSizeNeeded))
00295         {
00296             dwError = GetLastError();
00297             goto done;
00298         }
00299         *lpValue = expanded;
00300         HeapFree(GetProcessHeap(), 0, ptr);
00301         dwError = ERROR_SUCCESS;
00302     }
00303     else
00304     {
00305         *lpValue = ptr;
00306     }
00307 
00308 done:
00309     if (dwError != ERROR_SUCCESS)
00310     {
00311         HeapFree(GetProcessHeap(), 0, ptr);
00312         if (expanded)
00313             HeapFree(GetProcessHeap(), 0, expanded);
00314     }
00315 
00316     return dwError;
00317 }
00318 
00319 
00320 DWORD
00321 ScmReadDependencies(HKEY hServiceKey,
00322                     LPWSTR *lpDependencies,
00323                     DWORD *lpdwDependenciesLength)
00324 {
00325     LPWSTR lpGroups = NULL;
00326     LPWSTR lpServices = NULL;
00327     DWORD dwGroupsLength = 0;
00328     DWORD dwServicesLength = 0;
00329     LPWSTR lpSrc;
00330     LPWSTR lpDest;
00331     DWORD len;
00332     DWORD dwTotalLength;
00333 
00334     *lpDependencies = NULL;
00335     *lpdwDependenciesLength = 0;
00336 
00337     /* Read the dependency values */
00338     ScmReadString(hServiceKey,
00339                   L"DependOnGroup",
00340                   &lpGroups);
00341 
00342     ScmReadString(hServiceKey,
00343                   L"DependOnService",
00344                   &lpServices);
00345 
00346     /* Leave, if there are no dependencies */
00347     if (lpGroups == NULL && lpServices == NULL)
00348         return ERROR_SUCCESS;
00349 
00350     /* Determine the total buffer size for the dependencies */
00351     if (lpGroups)
00352     {
00353         DPRINT("Groups:\n");
00354         lpSrc = lpGroups;
00355         while (*lpSrc != 0)
00356         {
00357             DPRINT("  %S\n", lpSrc);
00358 
00359             len = wcslen(lpSrc) + 1;
00360             dwGroupsLength += len + 1;
00361 
00362             lpSrc = lpSrc + len;
00363         }
00364     }
00365 
00366     if (lpServices)
00367     {
00368         DPRINT("Services:\n");
00369         lpSrc = lpServices;
00370         while (*lpSrc != 0)
00371         {
00372             DPRINT("  %S\n", lpSrc);
00373 
00374             len = wcslen(lpSrc) + 1;
00375             dwServicesLength += len;
00376 
00377             lpSrc = lpSrc + len;
00378         }
00379     }
00380 
00381     dwTotalLength = dwGroupsLength + dwServicesLength + 1;
00382     DPRINT("dwTotalLength: %lu\n", dwTotalLength);
00383 
00384     /* Allocate the common buffer for the dependencies */
00385     *lpDependencies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwTotalLength * sizeof(WCHAR));
00386     if (*lpDependencies == NULL)
00387     {
00388         if (lpGroups)
00389             HeapFree(GetProcessHeap(), 0, lpGroups);
00390 
00391         if (lpServices)
00392             HeapFree(GetProcessHeap(), 0, lpServices);
00393 
00394         return ERROR_NOT_ENOUGH_MEMORY;
00395     }
00396 
00397     /* Return the allocated buffer length in characters */
00398     *lpdwDependenciesLength = dwTotalLength;
00399 
00400     /* Copy the service dependencies into the common buffer */
00401     lpDest = *lpDependencies;
00402     if (lpServices)
00403     {
00404         memcpy(lpDest,
00405                lpServices,
00406                dwServicesLength * sizeof(WCHAR));
00407 
00408         lpDest = lpDest + dwServicesLength;
00409     }
00410 
00411     /* Copy the group dependencies into the common buffer */
00412     if (lpGroups)
00413     {
00414         lpSrc = lpGroups;
00415         while (*lpSrc != 0)
00416         {
00417             len = wcslen(lpSrc) + 1;
00418 
00419             *lpDest = SC_GROUP_IDENTIFIERW;
00420             lpDest++;
00421 
00422             wcscpy(lpDest, lpSrc);
00423 
00424             lpDest = lpDest + len;
00425             lpSrc = lpSrc + len;
00426         }
00427     }
00428 
00429     /* Free the temporary buffers */
00430     if (lpGroups)
00431         HeapFree(GetProcessHeap(), 0, lpGroups);
00432 
00433     if (lpServices)
00434         HeapFree(GetProcessHeap(), 0, lpServices);
00435 
00436     return ERROR_SUCCESS;
00437 }
00438 
00439 /* EOF */

Generated on Fri May 25 2012 04:16:57 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.