File: wiservic.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (1027 lines) | stat: -rw-r--r-- 30,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
/*
 *  wiservic.c
 *
 *  $Id$
 *
 *  Windows NT services
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

/*
   New module by Antti Karttunen 29. May 1997
   for installing and starting the Kubl Server executable as
   Windows NT service. With extensive modifications
   to main function in chil.c
   Accompanied with WISERVIC.H, containing prototypes
   and few common macros required here and in chil.c
   Contains also code (e.g. an auxiliary function like
   wisvc_handle_W_option) for Unix builds, so do not miss
   this module from them.

   4.Jun.1997 AK  Added call to main_the_rest() (in chil.c) to the end
   of wisvc_KublServiceStart
   Added the function (dummy macro in Unix platforms)
   wisvc_send_service_running_status() which can be
   called in the initialization when you think is an
   appropriate time to signal the service starter
   that "the Kubl Server has successfully started".
   At least it is called in wisvc_KublServiceStart
   after all the initializations and roll forwards.
   (If called multiple times then the latter invocations
   of it and wisvc_send_wait_hint are silently ignored.)
 */

#include "wi.h"
#include "wiservic.h"		/* Includes also <winsvc.h> if WIN32 */

#ifdef WIN32
#include <direct.h>		/* For getcwd and chdir */
#include <process.h>		/* For getpid */
#endif


int wisvc_Main_G_argc = 0;
char **wisvc_Main_G_argv = NULL;

int
is_started_as_service (void)
{
  return ((NULL != wisvc_Main_G_argv));
}


int
wisvc_Handle_W_option (int argc, char **argv,
		       char *s, int *i_ptr, int called_as_service)
{
  char *work_dir = (s + 2);

  if (!*work_dir)		/* directory in the next arg? */
    {
      if ((++(*i_ptr) >= argc) || !(work_dir = argv[(*i_ptr)]))
	{
	  err_printf ((
			"%s: Directory name missing after command line option \"%s\", exiting.\n",
			argv[0], s));
	  kubl_main_exit (1);
	}
    }

  if (chdir (work_dir))		/* Is not zero, i.e. -1, an error. */
    {
/*   setWindowsError(); */
      err_printf (("%s: Cannot chdir to \"%s\" because: %s",
		   argv[0], work_dir, strerror (errno)));
      kubl_main_exit (1);
    }

  return (0);			/* Return 0 to indicate that everything went all right. */
}


#ifdef WIN32

/* Should we add the stuff also to the Event Log??? */
int
wisvc_err_printf (const char *str, ...)
{
  char temp[2199];
  va_list list;
  va_start (list, str);
  vsnprintf (temp, sizeof (temp), str, list);
  OutputDebugStringA (temp);	/* Some kind of Windows debugging function. */
#ifdef PMN_LOG
  log_error (temp);
#else
  log_error_list (str, list);	/* In log.c, appends stuff to wi.err */
#endif
  return 0;
}



/* Either begins with a slash or backslash, or the second character
   is colon, e.g. D:\somewhere\something\wi
 */
#define is_abs_path(P) (('\\' == *(P)) || ('/' == *(P)) || (':' == *(P+1)))

#define MAX_BINARY_PATH (2*_MAX_PATH)

int
wisvc_Handle_I_and_J_options (int argc, char **argv,
			      char *s, int i, int autostart)
{
  int called_as_service = 0;
  size_t path_len;
  int start_now = 0;
  char *service_name = (*(s + 2) ? (s + 2) : WISVC_DEFAULT_SERVICE_NAME);
  char *progname = argv[0];
  char *last_of_path, *cutpnt;
  char BinaryPathName[(MAX_BINARY_PATH) + 10];

/*
   if(i > 1)
   {
   err_printf((
   "%s: If you give %s option, it MUST be the first argument on command line!",
   progname,s));
   kubl_main_exit(1);
   }
 */

/* Then construct absolute path to this binary executable
   by combining working directory path got with getcwd
   with the program name got from argv[0].
   Note that the program name itself can be relative or absolute path.
   getcwd returns a string that represents the path of
   the current working directory. If the current working
   directory is the root, the string ends with a backslash (\).
   If the current working directory is a directory
   other than the root, the string ends with the directory
   name and not with a backslash.
   Note that absolute paths with upward parts (..)
   like the one below seem to work equally well, so
   we do not need to worry about .. :s and .:s in any
   special way.
   D:\inetpub\wwwroot\..\..\ic\.\diskit\wi\windebug\wi.exe
 */

  if (is_abs_path (progname))
    {
      strncpy (BinaryPathName, progname, (MAX_BINARY_PATH));
    }
  else
    /* We have to combine pwd + relative starting path */
    {
      if (NULL == getcwd (BinaryPathName, _MAX_PATH))
	{
	  err_printf (("%s: Cannot getcwd because: %s",
		       progname, strerror (errno)));
	  exit (1);
	}
      path_len = strlen (BinaryPathName);
      last_of_path = (BinaryPathName + path_len - 1);
      if ((0 == path_len) || !is_abs_path (last_of_path))
	{			/* Add the missing path separator between if needed */
	  strncat_ck (BinaryPathName, "\\", (MAX_BINARY_PATH));
	}
      /* And then the progname itself. */
      strncat_ck (BinaryPathName, progname, (MAX_BINARY_PATH));
    }

  /* Add our own special .eXe extension to the program name, so that
     when service is started, the code in main can see from argv[0]
     that it was started as a service, not as an ordinary command
     line program.
   */
  strncat_ck (BinaryPathName, WISVC_EXE_EXTENSION_FOR_SERVICE, (MAX_BINARY_PATH));

  /* Do chdir to the same directory where the executable is, needed
     because of wi.cfg check soon performed. */
  if ((NULL != (cutpnt = strrchr (BinaryPathName, '\\'))))
    {				/* Search the last backslash. */
      unsigned char
        save_the_following_char = *(((unsigned char *) cutpnt) + 1);
      *(cutpnt + 1) = '\0';

      if (chdir (BinaryPathName))	/* Is not zero, i.e. -1, an error. */
	{			/* However, we do not exit yet. */
	  err_printf (("%s: Cannot chdir to \"%s\" because: %s",
		       argv[0], BinaryPathName, strerror (errno)));
	  exit (1);
	}

      *(((unsigned char *) cutpnt) + 1) = save_the_following_char;
    }


/* Add all command line arguments after the absolute program name
   itself, separated by spaces. The started service will see them
   in the elements of argv vector, in the normal way, that is
   argv[0] will contain just the absolute program name which ends with
   .eXe and arguments are in argv[1], argv[2], etc.

   Check also for options -S (start the service), and -W change working
   directory. The latter would not be actually necessary to do here,
   but, if the directory is invalid, then it is much more friendly
   to give an error message here, than let the service itself fail,
   and hide the same error message to god knows which log file.
   Check that the user does not try to give options -D, -U, -R or -d
   to the service to be installed.

 */

  for (i = 1; i < argc; i++)
    {
      s = argv[i];
      if ('-' == s[0])
	{
	  switch (s[1])
	    {			/* With -S ignore the possibility that a different service
				   name could be specified after it than after -I or -J
				   DON'T ADD OPTIONS -S, -I or -J to BinaryPathName, as
				   they would be ignored anyway in service. */
	    case 'S':
	      {
		start_now = 1;
		continue;
	      }
	    case 'I':
	    case 'J':
	      {
		continue;
	      }
	    case 'W':
	      {
		int stat
		= wisvc_Handle_W_option (argc, argv, s, &i, called_as_service);
		if (stat)
		  {
		    kubl_main_exit (stat);
		  }
		break;
	      }
	    case 'D':
	    case 'U':		/* case 'R': */
	    case 'd':
	      {
		err_printf ((
			      "%s: Sorry, the option %s can be used only from command line, not in service!\n",
			      argv[0], s));
		exit (1);
	      }
	    }
	}

      strncat_ck (BinaryPathName, " ", (MAX_BINARY_PATH));
      strncat_ck (BinaryPathName, argv[i], (MAX_BINARY_PATH));
    }

  {				/* Check already HERE that there is a config file in the final
				   working directory, for the same user-friendly reason as
				   checking the validity of -W option's argument. */
    int fd = open (CFG_FILE, O_RDWR);
    if (fd < 0)
      {
	err_printf ((
		      "There must be a %s file in the server's working directory. Exiting.\n",
		      CFG_FILE));
	exit (-1);
      }
    fd_close (fd, NULL);	/* Defined in widisk.h */
  }

  wisvc_CreateKublService (argc, argv, service_name, BinaryPathName,
			   autostart, start_now);

  return (0);
}



/* Globals */

struct _SERVICE_STATUS wisvc_KublServiceStatus;
SERVICE_STATUS_HANDLE wisvc_KublServiceStatusHandle;


/*
   The lpServiceStartTable parameter contains an entry for each service
   that can run in the calling process. Each entry specifies the
   ServiceMain function for that service. For SERVICE_WIN32_SHARE_PROCESS
   services, each entry must contain the name of a service. This name is
   the service name that was specified by the CreateService function
   when the service was installed. For SERVICE_WIN32_OWN_PROCESS services,
   the service name in the table entry is ignored. (Fortunately!)
 */
SERVICE_TABLE_ENTRY wisvc_ServiceDispatchTable[] =
{
  {TEXT ("Kubl"), ((LPSERVICE_MAIN_FUNCTION) wisvc_KublServiceStart)},
  {NULL, NULL}
};


void
wisvc_start_kubl_service_dispatcher (int argc, char **argv)
{
  if (0 == StartServiceCtrlDispatcher (wisvc_ServiceDispatchTable))
    {
      wisvc_err_printf ("%s: StartServiceCtrlDispatcher error =  %d\n",
			argv[0], GetLastError ());
      exit (1);
    }
}




#define find_service_name_from(AV) ((AV)[0])


/* Note! Unlike the MS documentation claims, argv seems not to be
   NULL-terminated. That is, we have to watch argc when accessing
   elements of argv.
 */

int kubl_main (int argc, char **argv, int called_as_service, DWORD * errptr);

VOID
wisvc_KublServiceStart (DWORD argc, LPTSTR * argv)
{
  DWORD status;
  DWORD specificError = 0;
  char *service_name = find_service_name_from (argv);
  char *cutpnt;

/* First chdir to the same directory where the executable is
   (got from argv[0] of original main arguments), so that further
   error messages printed with wisvc_err_printf (which calls log_error
   in turn) will be appended to wi.err file in more appropriate
   place than \WINNT\SYSTEM32 directory.
 */
  if (wisvc_Main_G_argv && wisvc_Main_G_argv[0] &&
      (NULL != (cutpnt = strrchr (wisvc_Main_G_argv[0], '\\'))))
    {				/* Search the last backslash. */
      ptrlong len;
      char *work_dir;

      len = (cutpnt - wisvc_Main_G_argv[0]);
      if (NULL == (work_dir = ((char *) malloc (len + 1))))
	{
	  exit (7);		/* Failed beyond all reason, best to exit. */
	}
      else
	{
	  strncpy (work_dir, wisvc_Main_G_argv[0], len);	/* Leave backslash there */
	  work_dir[len] = '\0';
	}

      if (chdir (work_dir))	/* Is not zero, i.e. -1, an error. */
	{			/* However, we do not exit yet. */
/*        DWORD erhe = GetLastError(); */

	  wisvc_err_printf ("%s: Cannot chdir to \"%s\" because: %s",
			    argv[0], work_dir, strerror (errno));
	}
      free (work_dir);
    }

  wisvc_KublServiceStatus.dwServiceType = SERVICE_WIN32;
  wisvc_KublServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  wisvc_KublServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  /* | SERVICE_ACCEPT_PAUSE_CONTINUE; (Not for wisvc_Kubl) */
  wisvc_KublServiceStatus.dwWin32ExitCode = 0;
  wisvc_KublServiceStatus.dwServiceSpecificExitCode = 0;
  wisvc_KublServiceStatus.dwCheckPoint = 0;
  wisvc_KublServiceStatus.dwWaitHint = 2;

  wisvc_KublServiceStatusHandle =
    RegisterServiceCtrlHandler (TEXT (service_name),
		       ((LPHANDLER_FUNCTION) wisvc_KublServiceCtrlHandler));

  if (wisvc_KublServiceStatusHandle == (SERVICE_STATUS_HANDLE) 0)
    {
      wisvc_err_printf ("%s: (%s) RegisterServiceCtrlHandler failed %ld\n",
			wisvc_Main_G_argv[0], service_name, GetLastError ());
      return;
    }

  /* Now we are calling kubl_main second time. */
  status = kubl_main (argc, argv, 1, &specificError);

  if (status != NO_ERROR)	/* Handle error condition */
    {
      wisvc_KublServiceStatus.dwCurrentState = SERVICE_STOPPED;
      wisvc_KublServiceStatus.dwCheckPoint = 0;
      wisvc_KublServiceStatus.dwWaitHint = 0;
      wisvc_KublServiceStatus.dwWin32ExitCode = status;
      wisvc_KublServiceStatus.dwServiceSpecificExitCode = specificError;

      SetServiceStatus (wisvc_KublServiceStatusHandle, &wisvc_KublServiceStatus);
      return;
    }

  wisvc_send_service_running_status ();


  /* This is where the service either does few checkpoints now and then
     or does nothing: */
    main_the_rest ();		/* In chil.c */

  return;
}



VOID wisvc_KublServiceCtrlHandler (IN DWORD Opcode)
{
  DWORD status;
  int my_pid = getpid ();	/* To ease the debugging. */

  switch (Opcode)
    {
    case SERVICE_CONTROL_STOP:
      {
	/* Do whatever it takes to stop here.
	   What it might be? Killing a thread or two?
	   Setting a global flag, stop now boys?
	 */
	wisvc_KublServiceStatus.dwWin32ExitCode = 0;
	wisvc_KublServiceStatus.dwCurrentState = SERVICE_STOPPED;
	wisvc_KublServiceStatus.dwCheckPoint = 0;
	wisvc_KublServiceStatus.dwWaitHint = 0;

	if (!SetServiceStatus (wisvc_KublServiceStatusHandle,
			       &wisvc_KublServiceStatus))
	  {
	    status = GetLastError ();
	    wisvc_err_printf (
			" [KUBL_SERVICE (%d)] SetServiceStatus error %ld\n",
			       my_pid, status);
	  }

	wisvc_err_printf (" [KUBL_SERVICE (%d)] Stopped\n",
			  my_pid);
	return;
      }

    case SERVICE_CONTROL_INTERROGATE:
      {
	/* Just send the current status in the end of this function. */
	break;
      }

#ifdef NOT_IMPLEMENTED_FOR_KUBL_SERVICE
    case SERVICE_CONTROL_PAUSE:
      /* Do whatever it takes to pause here. */
      wisvc_KublServiceStatus.dwCurrentState = SERVICE_PAUSED;
      break;

    case SERVICE_CONTROL_CONTINUE:
      /* Do whatever it takes to continue here. */
      wisvc_KublServiceStatus.dwCurrentState = SERVICE_RUNNING;
      break;
#endif

    default:
      {
	wisvc_err_printf (
	       " [KUBL_SERVICE (%d)] Unimplemented/recognized opcode %ld\n",
			   my_pid, Opcode);
      }
    }				/* switch */

  /* Send current status. */
  if (!SetServiceStatus (wisvc_KublServiceStatusHandle, &wisvc_KublServiceStatus))
    {
      status = GetLastError ();
      wisvc_err_printf (
			 " [KUBL_SERVICE (%d)] SetServiceStatus error %ld\n",
			 my_pid, status);
    }
  return;
}





/*

   A service configuration program uses the CreateService function to
   install a service in a service control manager database.
   The application-defined schSCManager handle must have
   SC_MANAGER_CREATE_SERVICE access to the SCManager object.
   The following example shows how to install a service.

   Start type is one of the following:
   SERVICE_AUTO_START   Specifies a device driver or Win32 service started
   by the service control manager automatically during
   system startup.
   SERVICE_DEMAND_START Specifies a device driver or Win32 service started
   by the service control manager when a process calls
   the StartService function.
   SERVICE_DISABLED     Specifies a device driver or Win32 service that can
   no longer be started.

   Error control type is one of the following:

   SERVICE_ERROR_IGNORE   The startup (boot) program logs the error but
   continues the startup operation.
   SERVICE_ERROR_NORMAL   The startup program logs the error and displays
   a message but continues the startup operation.
   SERVICE_ERROR_SEVERE   The startup program logs the error. If the
   last-known-good configuration is being started,
   the startup operation continues. Otherwise,
   the system is restarted with the last-known-good
   configuration.
   SERVICE_ERROR_CRITICAL The startup program logs the error, if possible.
   If the last-known-good configuration is being
   started, the startup operation fails. Otherwise,
   the system is restarted with the last-known-good
   configuration.

 */


void
wisvc_CreateKublService (int argc, char **argv,
			 char *service_name, char *BinaryPathName,
			 int autostart, int start_now)
{
  int called_as_service = 0;	/* Needed by macro err_printf */
  SC_HANDLE schSCManager, schService;
  int stat;

  schSCManager = OpenSCManager (
				 NULL,	/* LPCTSTR  lpMachineName, address of machine name string */
				 NULL,	/* LPCTSTR  lpDatabaseName, address of database name string */
				 SC_MANAGER_ALL_ACCESS	/* DWORD dwDesiredAccess, type of access */
    );

  if (NULL == schSCManager)
    {
      DWORD erhe = GetLastError ();

      err_printf ((
       "%s: Installing \"%s\" (path: \"%s\") as Windows NT service failed. "
	"Could not open Services Database with OpenSCManager, errno=%ld.\n",
		    argv[0], service_name, BinaryPathName, erhe));
      exit (1);
    }

  schService = CreateService (
			       schSCManager,	/* SCManager database      */
			       TEXT (service_name),	/* name of service         */
			       service_name,	/* service name to display */
			       SERVICE_ALL_ACCESS,	/* desired access          */
			       SERVICE_WIN32_OWN_PROCESS,	/* service type            */
		    (autostart ? SERVICE_AUTO_START : SERVICE_DEMAND_START),	/* start type */
			       SERVICE_ERROR_NORMAL,	/* error control type      */
			       ((LPCSTR) BinaryPathName),	/* service's binary        */
			       NULL,	/* no load ordering group  */
			       NULL,	/* no tag identifier       */
			       NULL,	/* no dependencies         */
			       NULL,	/* LocalSystem account     */
			       NULL);	/* no password             */

  if (NULL == schService)
    {
      DWORD erhe = GetLastError ();

      if (ERROR_SERVICE_EXISTS == erhe)
	{
	  err_printf ((
			"%s: Cannot install service \"%s\" because a service with the same "
			"name already exists! (errno=%ld, path=\"%s\").\n",
			argv[0], service_name, erhe, BinaryPathName));
	}
      else if (ERROR_SERVICE_MARKED_FOR_DELETE == erhe)
	{
	  err_printf ((
			"%s: Cannot install service \"%s\" because a service with the same "
			"name still exists, although it has been marked for delete. Use ISQL to "
			"stop the old service with shutdown or raw_exit() before continuing "
			" (errno=%ld, path=\"%s\").\n",
			argv[0], service_name, erhe, BinaryPathName));
	}
      else
	{
	  err_printf ((
			"%s: Installing \"%s\" (path: \"%s\") as Windows NT service failed. "
			"CreateService returned NULL, errno=%ld.\n",
			argv[0], service_name, BinaryPathName, erhe));
	}
      exit (1);
    }

  err_printf (("%s: Service \"%s\" installed successfully.\n",
	       argv[0], service_name));

  if (start_now)
    {
      stat = wisvc_StartKublService (argc, argv, schService,
				     service_name, BinaryPathName, 1);

    }

  CloseServiceHandle (schService);

  exit (!stat);
}


/*
   To start a service, the following function uses a handle created
   with CreateService given from the function CreateKublService above,
   and gives that handle to StartService function. StartService can be
   used to start either a Win32 service or a driver service, but this
   function assumes that a Win32 service is being started. After
   starting the service, the function uses the members of the
   SERVICE_STATUS structure returned by the QueryServiceStatus
   function to track the progress of the Win32 service.

   When calling from function above (wisvc_CreateKublService)
   with discard_argv as non-zero, we pass zero and NULL as argc and argv
   to the service being started, so that it will use the arguments
   specified permanently in BinaryPathName, so that service's
   startup will be identical each time, also when it is started
   automatically in the next boot-up. This is because arguments given
   in StartService are already lost at that time, but ones specified
   in BinaryPathName will remain, being available in original argv
   of main function (and stored immediately into wisvc_Main_G_argv).
   It would work also with discard_argv being zero, but this way we
   may spot few insipid bugs faster, because arguments specified by
   a "temporary" and "permanent" means might differ a little.

 */

int
wisvc_StartKublService (int argc, char **argv, SC_HANDLE schService,
			char *service_name, char *BinaryPathName,
			int discard_argv)
{				/* The last two arguments not really needed except for error messages */
  int called_as_service = 0;	/* Needed by macro err_printf */
  int checkpoint_has_stayed_stagnant_n_iterations = 0;
  SERVICE_STATUS ssStatus;
  DWORD dwOldCheckPoint = 0, dwOlderCheckPoint = 0;

/*
   err_printf(("StartKublService: argc=%d, argv[0]=%s, argv[1]=%s\n",
   argc,argv[0],argv[1]));
 */

  if (!StartService (schService,	/* handle of service    */
		     (discard_argv ? 0 : (argc - 1)),	/* number of arguments  */
		     (discard_argv ? NULL : (argv + 1))))	/* Arg vector from main */
    {				/* without argv[0] */
      DWORD erhe = GetLastError ();

      err_printf ((
		    "%s: Starting service \"%s\" (path: \"%s\") failed. "
		    "StartService returned zero, errno=%ld%s\n",
		    argv[0], service_name, BinaryPathName, erhe,
		    ((ERROR_SERVICE_ALREADY_RUNNING == erhe) ?
		     " because service has been already started!" : ".")
		  ));
      return (0);
    }
  else
    {
      err_printf (("Service %s start in progress, BinaryPathName=%s\n",
		   service_name, BinaryPathName));
    }

  /* Check the status until the service is running. */

  if (!QueryServiceStatus (schService,	/* handle of service       */
			   &ssStatus))	/* address of status info  */
    {
      DWORD erhe = GetLastError ();

      err_printf ((
	     "%s: Querying status of service \"%s\" (path: \"%s\") failed. "
		    "QueryServiceStatus returned zero, errno=%ld.\n",
		    argv[0], service_name, BinaryPathName, erhe));
      return (0);
    }

  Sleep (10);			/* First sleep ten seconds. */
  while (ssStatus.dwCurrentState != SERVICE_RUNNING)
    {
      if (SERVICE_STOPPED == ssStatus.dwCurrentState)
	{
	  break;
	}

      dwOlderCheckPoint = dwOldCheckPoint;
      dwOldCheckPoint = ssStatus.dwCheckPoint;	/* Save current checkpoint */

      if (ssStatus.dwWaitHint > 300)
	{
	  ssStatus.dwWaitHint = 300;
	}
      Sleep (ssStatus.dwWaitHint);	/* Wait for the specified interval. */

      /* Check the status again. */
      if (!QueryServiceStatus (schService, &ssStatus))
	{
	  break;
	}
/*
   err_printf((
   "dwOlderCheckPoint=%ld, dwOldCheckPoint=%ld, ssStatus.dwCheckPoint=%ld, ssStatus.dwWaitHint=%ld\n",
   dwOlderCheckPoint, dwOldCheckPoint,
   ssStatus.dwCheckPoint, ssStatus.dwWaitHint));
 */

/* Break if the checkpoint has not been incremented for three times. */
      if ((dwOldCheckPoint >= ssStatus.dwCheckPoint)
	  && (dwOlderCheckPoint >= ssStatus.dwCheckPoint))
	{
	  if (++checkpoint_has_stayed_stagnant_n_iterations > 3)
	    {
	      break;
	    }
	}
      checkpoint_has_stayed_stagnant_n_iterations = 0;
    }


  if (ssStatus.dwCurrentState == SERVICE_RUNNING)
    {
      {
	err_printf ((
		 "%s: Service \"%s\" started successfully (path: \"%s\").\n",
		      argv[0], service_name, BinaryPathName));
      }
      return (1);
    }
  else
    {
      err_printf ((
		"%s: Service \"%s\" (path: \"%s\") %s started correctly.\n",
		    argv[0], service_name, BinaryPathName,
	     ((SERVICE_START_PENDING != ssStatus.dwCurrentState) ? "has not"
	      : "may or may not have")));
      err_printf (("  Current State: %d\n",
		   ssStatus.dwCurrentState));
      err_printf (("  Exit Code: %d\n", ssStatus.dwWin32ExitCode));
      err_printf (("  Service Specific Exit Code: %d\n",
		   ssStatus.dwServiceSpecificExitCode));
      err_printf (("  Check Point: %d\n", ssStatus.dwCheckPoint));
      err_printf (("  Wait Hint: %d\n", ssStatus.dwWaitHint));
      err_printf ((
		    "Please use services icon in Control Panel to see whether service \"%s\" was really started."
	 " Check also the file wi.err in the server's working directory.\n",
		    service_name));
      return (0);
    }
}


/*
   In the following function, a service configuration program uses the
   OpenService function to get a handle with DELETE access to an
   installed service object. The function then uses the service object
   handle in the DeleteService function to remove the service from the
   service control manager database.
 */

SC_HANDLE
wisvc_OpenKublService (char **argv, char *service_name,
		       char *what_for, DWORD access_code)
{
  int called_as_service = 0;	/* Needed by macro err_printf */
  SC_HANDLE schSCManager, schService;

  schSCManager = OpenSCManager (
				 NULL,	/* LPCTSTR  lpMachineName, address of machine name string */
				 NULL,	/* LPCTSTR  lpDatabaseName, address of database name string */
				 SC_MANAGER_ALL_ACCESS	/* DWORD dwDesiredAccess, type of access */
    );

  if (NULL == schSCManager)
    {
      DWORD erhe = GetLastError ();

      err_printf ((
		    "%s: %sing service \"%s\" failed. "
	"Could not open Services Database with OpenSCManager, errno=%ld.\n",
		    argv[0], what_for, service_name, erhe));
      exit (1);
    }


  schService = OpenService (
			     schSCManager,	/* SCManager database         */
			     TEXT (service_name),	/* name of service            */
			     access_code);	/* only need access specified */

  if (schService == NULL)
    {
      DWORD erhe = GetLastError ();
      if (ERROR_SERVICE_DOES_NOT_EXIST == erhe)
	{
	  err_printf ((
			"%s: Cannot %s non-existent service \"%s\" OpenService failed, errno=%ld.\n",
			argv[0], what_for, service_name, erhe));
	}
      else if (ERROR_SERVICE_MARKED_FOR_DELETE == erhe)
	{
	  err_printf ((
		 "%s: Cannot %s service \"%s\" because a service with that "
			"name still exists, although it has been marked for delete. Use ISQL to "
			"stop the old service with shutdown or raw_exit() before continuing "
			" (errno=%ld).\n",
			argv[0], what_for, service_name, erhe));
	}
      else
	{
	  err_printf ((
			"%s: %sing service \"%s\" failed. "
		    "Could not Open Service with OpenService, errno=%ld.\n",
			argv[0], what_for, service_name, erhe));
	}
      exit (1);
    }

  return (schService);
}



void
wisvc_UninstallKublService (char **argv, char *service_name)
{
  int called_as_service = 0;	/* Needed by macro err_printf */
  SC_HANDLE schService = wisvc_OpenKublService (argv, service_name,
						"uninstall", DELETE);

  if (!DeleteService (schService))
    {
      DWORD erhe = GetLastError ();

      if (ERROR_SERVICE_MARKED_FOR_DELETE == erhe)
	{
	  err_printf ((
		 "%s: Cannot %s service \"%s\" because a service with that "
			"name still exists, although it has been marked for delete. Use ISQL to "
			"stop the old service with shutdown or raw_exit() before continuing "
			" (errno=%ld).\n",
			argv[0], "uninstall", service_name, erhe));
	}
      else
	{
	  err_printf ((
			"%s: Uninstalling service \"%s\" failed. "
			"DeleteService returned zero, errno=%ld.\n",
			argv[0], service_name, erhe));
	}
      exit (1);
    }
  else
    {
      err_printf (("%s: Service \"%s\" uninstalled successfully.\n",
		   argv[0], service_name));
    }

  CloseServiceHandle (schService);
}


/*
   If your service's initialization performs tasks that are expected
   to take longer than one second, your code must call SetServiceStatus
   periodically to send out wait hints and check points indicating that
   progress is being made.
   This is to be called from log_replay_file loop
   (maybe log_replay_entry?)
   We use wisvc_Main_G_argv to check whether this has been called
   as a service or from command line (in the latter case, do nothing.)

   Changed 4.June.1997 by transferring the first argument last_time_sent
   inside function itself, as a static variable.
   Note also that if the service has been already signaled as
   running (with the next function wisvc_send_service_running_status),
   then this function is NO-OP again.
 */

unsigned long
wisvc_send_wait_hint (unsigned long every_n_msec,
		      unsigned long wait_n_secs)
{

    static unsigned long last_time_sent = 0;
  unsigned long now;

  if (!wisvc_Main_G_argv)
    {
      return (last_time_sent);
    }				/* Not a service? */


    if (SERVICE_RUNNING == wisvc_KublServiceStatus.dwCurrentState)

    {
      return (last_time_sent);
    }


    now = GetTickCount ();	/* May wrap over to zero if Windows
				   runs continuously (hah!) over approximately 49.7 days. */

/*
   wisvc_err_printf(
   "wisvc_send_wait_hint: last_time_sent=%ld, now=%ld, every_n_msec=%ld\n",
   last_time_sent, now, every_n_msec);
 */

  if ((0 == last_time_sent) || ((now - last_time_sent) >= every_n_msec))
    {
      wisvc_KublServiceStatus.dwCheckPoint++;
      wisvc_KublServiceStatus.dwWaitHint = wait_n_secs;

/*
   wisvc_err_printf(
   "wisvc_send_wait_hint: wisvc_KublServiceStatus.dwCheckPoint=%ld\n",
   wisvc_KublServiceStatus.dwCheckPoint);
 */

      if (0 == SetServiceStatus (wisvc_KublServiceStatusHandle,
				 &wisvc_KublServiceStatus))
	{
	  DWORD status = GetLastError ();

	  wisvc_err_printf (
	  "%s: SetServiceStatus failed (error=%ld) in wisvc_send_wait_hint "
			     " wisvc_KublServiceStatus.dwCheckPoint=%ld\n",
			     wisvc_Main_G_argv[0], status,
			     wisvc_KublServiceStatus.dwCheckPoint);

	}

	last_time_sent = now;
      return (now);
    }
  else
    {
      return (last_time_sent);
    }
}



/* Can be called twice or more. The latter times are just ignored.
   Once called, the previous function, wisvc_send_wait_hint is
   also ignored.
 */

void
wisvc_send_service_running_status (void)
{

    if (wisvc_Main_G_argv &&
	(SERVICE_RUNNING != wisvc_KublServiceStatus.dwCurrentState))

    {				/* Initialization complete - report running status */

	wisvc_KublServiceStatus.dwCurrentState = SERVICE_RUNNING;

	wisvc_KublServiceStatus.dwCheckPoint = 0;

	wisvc_KublServiceStatus.dwWaitHint = 0;


	if (0 == SetServiceStatus (wisvc_KublServiceStatusHandle,
				   &wisvc_KublServiceStatus))

	{

	    DWORD status = GetLastError ();

	    wisvc_err_printf (
			      "%s: (wisvc_send_service_running_status) SetServiceStatus failed %ld\n",
			      wisvc_Main_G_argv[0], status);

	}

    }

}




#else /* WIN32 */

/* On Unix-platforms this is currently a NO-OP
   (defined as macro in wiservic.h) */

/*
   unsigned long wisvc_send_wait_hint(unsigned long every_n_msec,
   unsigned long wait_n_secs)
   {
   return(0);
   }
 */

#endif