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

create.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/create.c
00005  * PURPOSE:     Create a service
00006  * COPYRIGHT:   Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
00007  *              Roel Messiant <roelmessiant@gmail.com>
00008  *
00009  */
00010 
00011 #include "sc.h"
00012 
00013 
00014 typedef struct
00015 {
00016     LPCTSTR lpOption;
00017     DWORD dwValue;
00018 } OPTION_INFO;
00019 
00020 typedef struct
00021 {
00022     LPCTSTR lpServiceName;
00023     LPCTSTR lpDisplayName;
00024     DWORD dwServiceType;
00025     DWORD dwStartType;
00026     DWORD dwErrorControl;
00027     LPCTSTR lpBinaryPathName;
00028     LPCTSTR lpLoadOrderGroup;
00029     DWORD dwTagId;
00030     LPCTSTR lpDependencies;
00031     LPCTSTR lpServiceStartName;
00032     LPCTSTR lpPassword;
00033 
00034     BOOL bTagId;
00035 } SERVICE_CREATE_INFO, *LPSERVICE_CREATE_INFO;
00036 
00037 
00038 static const OPTION_INFO TypeOpts[] =
00039 {
00040     { _T("own"),      SERVICE_WIN32_OWN_PROCESS   },
00041     { _T("share"),    SERVICE_WIN32_SHARE_PROCESS },
00042     { _T("interact"), SERVICE_INTERACTIVE_PROCESS },
00043     { _T("kernel"),   SERVICE_KERNEL_DRIVER       },
00044     { _T("filesys"),  SERVICE_FILE_SYSTEM_DRIVER  },
00045     { _T("rec"),      SERVICE_RECOGNIZER_DRIVER   }
00046 };
00047 
00048 static const OPTION_INFO StartOpts[] =
00049 {
00050     { _T("boot"),     SERVICE_BOOT_START   },
00051     { _T("system"),   SERVICE_SYSTEM_START },
00052     { _T("auto"),     SERVICE_AUTO_START   },
00053     { _T("demand"),   SERVICE_DEMAND_START },
00054     { _T("disabled"), SERVICE_DISABLED     }
00055 };
00056 
00057 static const OPTION_INFO ErrorOpts[] =
00058 {
00059     { _T("normal"),   SERVICE_ERROR_NORMAL   },
00060     { _T("severe"),   SERVICE_ERROR_SEVERE   },
00061     { _T("critical"), SERVICE_ERROR_CRITICAL },
00062     { _T("ignore"),   SERVICE_ERROR_IGNORE   }
00063 };
00064 
00065 static const OPTION_INFO TagOpts[] =
00066 {
00067     { _T("yes"), TRUE  },
00068     { _T("no"),  FALSE }
00069 };
00070 
00071 
00072 static BOOL ParseCreateArguments(
00073     LPCTSTR *ServiceArgs,
00074     INT ArgCount,
00075     OUT LPSERVICE_CREATE_INFO lpServiceInfo
00076 )
00077 {
00078     INT i, ArgIndex = 1;
00079 
00080     if (ArgCount < 1)
00081         return FALSE;
00082 
00083     ZeroMemory(lpServiceInfo, sizeof(SERVICE_CREATE_INFO));
00084 
00085     lpServiceInfo->lpServiceName = ServiceArgs[0];
00086 
00087     ArgCount--;
00088 
00089     while (ArgCount > 1)
00090     {
00091         if (!lstrcmpi(ServiceArgs[ArgIndex], _T("type=")))
00092         {
00093             for (i = 0; i < sizeof(TypeOpts) / sizeof(TypeOpts[0]); i++)
00094                 if (!lstrcmpi(ServiceArgs[ArgIndex + 1], TypeOpts[i].lpOption))
00095                 {
00096                     lpServiceInfo->dwServiceType |= TypeOpts[i].dwValue;
00097                     break;
00098                 }
00099 
00100             if (i == sizeof(TypeOpts) / sizeof(TypeOpts[0]))
00101                 break;
00102         }
00103         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("start=")))
00104         {
00105             for (i = 0; i < sizeof(StartOpts) / sizeof(StartOpts[0]); i++)
00106                 if (!lstrcmpi(ServiceArgs[ArgIndex + 1], StartOpts[i].lpOption))
00107                 {
00108                     lpServiceInfo->dwStartType = StartOpts[i].dwValue;
00109                     break;
00110                 }
00111 
00112             if (i == sizeof(StartOpts) / sizeof(StartOpts[0]))
00113                 break;
00114         }
00115         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("error=")))
00116         {
00117             for (i = 0; i < sizeof(ErrorOpts) / sizeof(ErrorOpts[0]); i++)
00118                 if (!lstrcmpi(ServiceArgs[ArgIndex + 1], ErrorOpts[i].lpOption))
00119                 {
00120                     lpServiceInfo->dwErrorControl = ErrorOpts[i].dwValue;
00121                     break;
00122                 }
00123 
00124             if (i == sizeof(ErrorOpts) / sizeof(ErrorOpts[0]))
00125                 break;
00126         }
00127         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("tag=")))
00128         {
00129             for (i = 0; i < sizeof(TagOpts) / sizeof(TagOpts[0]); i++)
00130                 if (!lstrcmpi(ServiceArgs[ArgIndex + 1], TagOpts[i].lpOption))
00131                 {
00132                     lpServiceInfo->bTagId = TagOpts[i].dwValue;
00133                     break;
00134                 }
00135 
00136             if (i == sizeof(TagOpts) / sizeof(TagOpts[0]))
00137                 break;
00138         }
00139         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("binpath=")))
00140         {
00141             lpServiceInfo->lpBinaryPathName = ServiceArgs[ArgIndex + 1];
00142         }
00143         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("group=")))
00144         {
00145             lpServiceInfo->lpLoadOrderGroup = ServiceArgs[ArgIndex + 1];
00146         }
00147         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("depend=")))
00148         {
00149             lpServiceInfo->lpDependencies = ServiceArgs[ArgIndex + 1];
00150         }
00151         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("obj=")))
00152         {
00153             lpServiceInfo->lpServiceStartName = ServiceArgs[ArgIndex + 1];
00154         }
00155         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("displayname=")))
00156         {
00157             lpServiceInfo->lpDisplayName = ServiceArgs[ArgIndex + 1];
00158         }
00159         else if (!lstrcmpi(ServiceArgs[ArgIndex], _T("password=")))
00160         {
00161             lpServiceInfo->lpPassword = ServiceArgs[ArgIndex + 1];
00162         }
00163 
00164         ArgIndex += 2;
00165         ArgCount -= 2;
00166     }
00167 
00168     return (ArgCount == 0);
00169 }
00170 
00171 BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount)
00172 {
00173     SC_HANDLE hSCManager;
00174     SC_HANDLE hSc;
00175     BOOL bRet = FALSE;
00176 
00177     INT i;
00178     INT Length;
00179     LPTSTR lpBuffer = NULL;
00180     SERVICE_CREATE_INFO ServiceInfo;
00181 
00182     if (!ParseCreateArguments(ServiceArgs, ArgCount, &ServiceInfo))
00183     {
00184         CreateUsage();
00185         return FALSE;
00186     }
00187 
00188     if (!ServiceInfo.dwServiceType)
00189         ServiceInfo.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
00190 
00191     if (!ServiceInfo.dwStartType)
00192         ServiceInfo.dwStartType = SERVICE_DEMAND_START;
00193 
00194     if (!ServiceInfo.dwErrorControl)
00195         ServiceInfo.dwErrorControl = SERVICE_ERROR_NORMAL;
00196 
00197     if (ServiceInfo.lpDependencies)
00198     {
00199         Length = lstrlen(ServiceInfo.lpDependencies);
00200 
00201         lpBuffer = HeapAlloc(GetProcessHeap(),
00202                              0,
00203                             (Length + 2) * sizeof(TCHAR));
00204 
00205         for (i = 0; i < Length; i++)
00206             if (ServiceInfo.lpDependencies[i] == _T('/'))
00207                 lpBuffer[i] = 0;
00208             else
00209                 lpBuffer[i] = ServiceInfo.lpDependencies[i];
00210 
00211         lpBuffer[Length] = 0;
00212         lpBuffer[Length + 1] = 0;
00213 
00214         ServiceInfo.lpDependencies = lpBuffer;
00215     }
00216 
00217 #ifdef SCDBG
00218     _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
00219     _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
00220     _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
00221     _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
00222     _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
00223     _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
00224     _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
00225     _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
00226     _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
00227     _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
00228     _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
00229 #endif
00230 
00231     hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
00232 
00233     if (hSCManager != NULL)
00234     {
00235         hSc = CreateService(hSCManager,
00236                             ServiceInfo.lpServiceName,
00237                             ServiceInfo.lpDisplayName,
00238                             SERVICE_ALL_ACCESS,
00239                             ServiceInfo.dwServiceType,
00240                             ServiceInfo.dwStartType,
00241                             ServiceInfo.dwErrorControl,
00242                             ServiceInfo.lpBinaryPathName,
00243                             ServiceInfo.lpLoadOrderGroup,
00244                             ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
00245                             ServiceInfo.lpDependencies,
00246                             ServiceInfo.lpServiceStartName,
00247                             ServiceInfo.lpPassword);
00248 
00249         if (hSc != NULL)
00250         {
00251             _tprintf(_T("[SC] CreateService SUCCESS\n"));
00252 
00253             CloseServiceHandle(hSc);
00254             bRet = TRUE;
00255         }
00256         else
00257             ReportLastError();
00258 
00259         CloseServiceHandle(hSCManager);
00260     }
00261     else
00262         ReportLastError();
00263 
00264     if (lpBuffer != NULL)
00265         HeapFree(GetProcessHeap(), 0, lpBuffer);
00266 
00267     return bRet;
00268 }

Generated on Sun May 27 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.