~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to snmplib/winservice.c

  • Committer: Bazaar Package Importer
  • Author(s): Jochen Friedrich
  • Date: 2010-06-10 18:02:54 UTC
  • mto: (1.4.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100610180254-6ezvupl2clicwdqf
ImportĀ upstreamĀ versionĀ 5.4.3~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
#endif /* mingw32 */
33
33
 
 
34
 
 
35
#define CountOf(arr) ( sizeof(arr) / sizeof(arr[0]) )
 
36
 
 
37
 
34
38
    /*
35
39
     * External global variables used here
36
40
     */
105
109
  SC_HANDLE hSCManager = NULL;
106
110
  SC_HANDLE hService = NULL;
107
111
  TCHAR szRegAppLogKey[] =
108
 
    "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
 
112
    _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
109
113
  TCHAR szRegKey[512];
110
114
  HKEY hKey = NULL;             /* Key to registry entry */
111
115
  HKEY hParamKey = NULL;        /* To store startup parameters */
130
134
    /*
131
135
     * Generate the Command to be executed by SCM 
132
136
     */
133
 
    _snprintf (szServiceCommand, sizeof(szServiceCommand), "%s %s", szServicePath, _T ("-service"));
 
137
    _sntprintf (szServiceCommand, CountOf(szServiceCommand), _T("%s %s"), szServicePath, _T ("-service"));
134
138
 
135
139
    /*
136
140
     * Create the Desired service 
145
149
                              NULL);    /* password */
146
150
    if (hService == NULL)
147
151
      {
148
 
        _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
152
        _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
149
153
                   _T ("Can't create service"), lpszServiceDisplayName);
150
154
        ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
151
155
 
167
171
     */
168
172
    if (RegCreateKey (HKEY_LOCAL_MACHINE, szRegKey, &hKey) != ERROR_SUCCESS)
169
173
      {
170
 
        _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
174
        _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
171
175
                   _T ("is unable to create registry entries"), lpszServiceDisplayName);
172
176
        ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
173
177
        exitStatus = SERVICE_ERROR_CREATE_REGISTRY_ENTRIES;
177
181
    /*
178
182
     * Add Event ID message file name to the 'EventMessageFile' subkey 
179
183
     */
180
 
    RegSetValueEx (hKey, "EventMessageFile", 0, REG_EXPAND_SZ,
 
184
    RegSetValueEx (hKey, _T("EventMessageFile"), 0, REG_EXPAND_SZ,
181
185
                   (CONST BYTE *) szServicePath,
182
186
                   _tcslen (szServicePath) + sizeof (TCHAR));
183
187
 
185
189
     * Set the supported types flags. 
186
190
     */
187
191
    dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
188
 
    RegSetValueEx (hKey, "TypesSupported", 0, REG_DWORD,
 
192
    RegSetValueEx (hKey, _T("TypesSupported"), 0, REG_DWORD,
189
193
                   (CONST BYTE *) & dwData, sizeof (DWORD));
190
194
 
191
195
    /*
211
215
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_WRITE,
212
216
                          &hKey) != ERROR_SUCCESS)
213
217
          {
214
 
            _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
218
            _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
215
219
                       _T ("is unable to create registry entries"),
216
220
                       lpszServiceDisplayName);
217
221
            ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
224
228
         */
225
229
        if (lpszServiceDescription != NULL)
226
230
          {
227
 
            if (RegSetValueEx (hKey, "Description", 0, REG_SZ,
 
231
            if (RegSetValueEx (hKey, _T("Description"), 0, REG_SZ,
228
232
                               (CONST BYTE *) lpszServiceDescription,
229
233
                               _tcslen (lpszServiceDescription) +
230
234
                               sizeof (TCHAR)) != ERROR_SUCCESS)
231
235
              {
232
 
                _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
236
                _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
233
237
                           _T ("is unable to create registry entries"),
234
238
                           lpszServiceDisplayName);
235
239
                ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
247
251
             * Create Subkey parameters 
248
252
             */
249
253
            if (RegCreateKeyEx
250
 
                (hKey, "Parameters", 0, NULL,
 
254
                (hKey, _T("Parameters"), 0, NULL,
251
255
                 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
252
256
                 &hParamKey, NULL) != ERROR_SUCCESS)
253
257
              {
254
 
                _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
258
                _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
255
259
                           _T ("is unable to create registry entries"),
256
260
                           lpszServiceDisplayName);
257
261
                ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
273
277
 
274
278
            for (j = 1; i < StartUpArg->Argc; i++, j++)
275
279
              {
276
 
                _snprintf (szRegKey, sizeof(szRegKey), "%s%d", _T ("Param"), j);
 
280
                _sntprintf (szRegKey, CountOf(szRegKey), _T("%s%d"), _T ("Param"), j);
277
281
 
278
282
                /*
279
283
                 * Create registry key 
284
288
                     _tcslen (StartUpArg->Argv[i]) +
285
289
                     sizeof (TCHAR)) != ERROR_SUCCESS)
286
290
                  {
287
 
                    _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s",
 
291
                    _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"),
288
292
                               _T ("is unable to create registry entries"),
289
293
                               lpszServiceDisplayName);
290
294
                    ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
308
312
    /*
309
313
     * Successfully registered as service 
310
314
     */
311
 
    _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s", lpszServiceName,
 
315
    _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"), lpszServiceName,
312
316
               _T ("successfully registered as a service"));
313
317
 
314
318
    /*
336
340
     * Input - ServiceName
337
341
     */
338
342
int
339
 
UnregisterService (LPCSTR lpszServiceName, int quiet)
 
343
UnregisterService (LPCTSTR lpszServiceName, int quiet)
340
344
{
341
345
  TCHAR MsgErrorString[MAX_STR_SIZE];   /* Message or Error string */
342
346
  SC_HANDLE hSCManager = NULL;  /* SCM handle */
343
347
  SC_HANDLE hService = NULL;    /* Service Handle */
344
348
  SERVICE_STATUS sStatus;
345
349
  TCHAR szRegAppLogKey[] =
346
 
    "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
 
350
    _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
347
351
  TCHAR szRegKey[512];
348
352
  int exitStatus = 0;
349
353
/*  HKEY hKey = NULL;           ?* Key to registry entry */
366
370
    hService = OpenService (hSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
367
371
    if (hService == NULL)
368
372
      {
369
 
        _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s", _T ("Can't open service"),
 
373
        _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"), _T ("Can't open service"),
370
374
                   lpszServiceName);
371
375
        ProcessError (EVENTLOG_ERROR_TYPE, MsgErrorString, 1, quiet);
372
376
        exitStatus = SERVICE_ERROR_OPEN_SERVICE;       
391
395
     */
392
396
    if (DeleteService (hService) == FALSE)
393
397
      {
394
 
        _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s", _T ("Can't delete service"),
 
398
        _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"), _T ("Can't delete service"),
395
399
                   lpszServiceName);
396
400
 
397
401
        /*
404
408
    /*
405
409
     * Log "Service deleted successfully " message to eventlog
406
410
     */
407
 
    _snprintf (MsgErrorString, sizeof(MsgErrorString), "%s %s", lpszServiceName, _T ("service deleted"));
 
411
    _sntprintf (MsgErrorString, CountOf(MsgErrorString), _T("%s %s"), lpszServiceName, _T ("service deleted"));
408
412
    ProcessError (EVENTLOG_INFORMATION_TYPE, MsgErrorString, 0, quiet);
409
413
 
410
414
    /*
440
444
  va_list ArgList;
441
445
  HANDLE hEventSource = NULL;
442
446
  va_start (ArgList, pszFormat);
443
 
  _vsnprintf (szMessage, sizeof(szMessage), pszFormat, ArgList);
 
447
  _vsntprintf (szMessage, CountOf(szMessage), pszFormat, ArgList);
444
448
  va_end (ArgList);
445
449
  LogStr[0] = szMessage;
446
450
  hEventSource = RegisterEventSource (NULL, app_name_long);
533
537
                 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
534
538
        (LPTSTR) & pErrorMsgTemp, 0, NULL);
535
539
 
536
 
    _snprintf (pszMessageFull, sizeof(pszMessageFull), "%s: %s", pszMessage, pErrorMsgTemp);
 
540
    _sntprintf (pszMessageFull, CountOf(pszMessageFull), _T("%s: %s"), pszMessage, pErrorMsgTemp);
537
541
    if (pErrorMsgTemp) {
538
542
      LocalFree (pErrorMsgTemp);
539
543
      pErrorMsgTemp = NULL;
540
544
    }
541
545
  }
542
546
  else {
543
 
    _snprintf (pszMessageFull, sizeof(pszMessageFull), "%s", pszMessage);
 
547
    _sntprintf (pszMessageFull, CountOf(pszMessageFull), _T("%s"), pszMessage);
544
548
  }
545
549
  
546
550
  hEventSource = RegisterEventSource (NULL, app_name_long);
562
566
          FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
563
567
          MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
564
568
          (LPTSTR) & pErrorMsgTemp, 0, NULL);
565
 
      
566
 
      fprintf(stderr,"Could NOT lot to Event Log.  Error returned from ReportEvent(): %s\n",pErrorMsgTemp);
 
569
      _ftprintf(stderr,_T("Could NOT lot to Event Log.  Error returned from ReportEvent(): %s\n"),pErrorMsgTemp);
567
570
      if (pErrorMsgTemp) {
568
571
        LocalFree (pErrorMsgTemp);
569
572
        pErrorMsgTemp = NULL;
573
576
    }
574
577
 
575
578
      if (quiet) {
576
 
    fprintf(stderr,"%s\n",pszMessageFull);
 
579
    _ftprintf(stderr,_T("%s\n"),pszMessageFull);
577
580
      }
578
581
      else {
579
582
    switch (eventLogType) {
651
654
ServiceMain (DWORD argc, LPTSTR argv[])
652
655
{
653
656
  SECURITY_ATTRIBUTES SecurityAttributes;
654
 
  DWORD dwThreadId;
 
657
  unsigned threadId;
655
658
 
656
659
  /*
657
660
   * Input Arguments to function startup 
683
686
  /*
684
687
   * Create Registry Key path 
685
688
   */
686
 
  _snprintf (szRegKey, sizeof(szRegKey), "%s%s\\%s",
 
689
  _sntprintf (szRegKey, CountOf(szRegKey), _T("%s%s\\%s"),
687
690
             _T ("SYSTEM\\CurrentControlSet\\Services\\"), app_name_long,
688
 
             "Parameters");
 
691
             _T("Parameters"));
689
692
  if (RegOpenKeyEx
690
693
      (HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_ALL_ACCESS, &hParamKey) == ERROR_SUCCESS)
691
694
    {
725
728
                  /*
726
729
                   * Create Subkey value name 
727
730
                   */
728
 
                  _snprintf (szRegKey, sizeof(szRegKey), "%s%d", "Param", i);
 
731
                  _sntprintf (szRegKey, CountOf(szRegKey), _T("%s%d"), _T("Param"), i);
729
732
 
730
733
                  /*
731
734
                   * Set size 
785
788
    hServiceThread =
786
789
      (void *) _beginthreadex (&SecurityAttributes, 0,
787
790
                               ThreadFunction,
788
 
                               (void *) &ThreadInputParams, 0, &dwThreadId);
 
791
                               (void *) &ThreadInputParams, 0, &threadId);
789
792
    if (hServiceThread == NULL)
790
793
      {
791
794
        WriteToEventLog (EVENTLOG_ERROR_TYPE, _T ("Couldn't start worker thread"));
998
1001
     * Input:
999
1002
     *   lpParam contains argc and argv, pass to service main function 
1000
1003
     */
1001
 
DWORD WINAPI
 
1004
unsigned WINAPI
1002
1005
ThreadFunction (LPVOID lpParam)
1003
1006
{
1004
1007
  InputParams * pInputArg = (InputParams *) lpParam;