aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/src/Children.cpp
blob: ab7e854b931d39cbdfe29d7862d159a594c9e9fb (plain)
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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com)          */
/* Copyright (c) 2008-2014 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>  */
/* Copyright (c) 2014-2016 Ulrich Sibiller <uli42@gmx.de>                 */
/* Copyright (c) 2014-2016 Mihai Moldovan <ionic@ionic.de>                */
/* Copyright (c) 2011-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com)           */
/*                                                                        */
/* NXCOMP, NX protocol compression and NX extensions to this software     */
/* are copyright of the aforementioned persons and companies.             */
/*                                                                        */
/* Redistribution and use of the present software is allowed according    */
/* to terms specified in the file LICENSE.nxcomp which comes in the       */
/* source distribution.                                                   */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/* NOTE: This software has received contributions from various other      */
/* contributors, only the core maintainers and supporters are listed as   */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well.                                          */
/*                                                                        */
/**************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>

#include "NX.h"

#include "Misc.h"

#include "Types.h"
#include "Timestamp.h"

#include "Control.h"
#include "Statistics.h"
#include "Proxy.h"

#include "Keeper.h"
#include "Fork.h"

//
// Set the verbosity level.
//

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

#define DISPLAY_LENGTH_LIMIT  256
#define DEFAULT_STRING_LIMIT  512

//
// These are from the main loop.
//

extern Keeper *keeper;

extern int (*signalHandler)(int);

extern int useUnixSocket;

extern int lastDialog;
extern int lastWatchdog;
extern int lastKeeper;

extern void CleanupListeners();
extern void CleanupSockets();
extern void CleanupAgent();
extern void CleanupGlobal();

extern void InstallSignals();

extern char *GetClientPath();

extern int CheckParent(const char *name, const char *type,
                           int parent);

#ifdef __sun
extern char **environ;
#endif

//
// Close all the unused descriptors and
// install any signal handler that might
// have been disabled in the main process.
//

static void SystemCleanup(const char *name);

//
// Release all objects allocated in the
// heap.

static void MemoryCleanup(const char *name);

//
// Remove 'name' from the environment.
//

static int UnsetEnv(const char *name);

static int NXTransKeeperHandler(int signal);
static void NXTransKeeperCheck();


//
// Start a nxclient process in dialog mode.
//

int NXTransDialog(const char *caption, const char *message,
                      const char *window, const char *type, int local,
                         const char* display)
{
  //
  // Be sure log file is valid.
  //

  if (logofs == NULL)
  {
    logofs = &cerr;
  }

  int pid;

  #ifdef TEST
  *logofs << "NXTransDialog: Going to fork with NX pid '"
          << getpid() << "'.\n" << logofs_flush;
  #endif

  pid = Fork();

  if (pid != 0)
  {
    if (pid < 0)
    {
      #ifdef TEST
      *logofs << "NXTransDialog: WARNING! Function fork failed. "
              << "Error is " << EGET() << " '" << ESTR()
              << "'.\n" << logofs_flush;
      #endif

      cerr << "Warning" << ": Function fork failed. "
           << "Error is " << EGET() << " '" << ESTR()
           << "'.\n";
    }
    #ifdef TEST
    else
    {
      *logofs << "NXTransDialog: Created NX dialog process "
              << "with pid '" << pid << "'.\n"
              << logofs_flush;
    }
    #endif

    return pid;
  }

  #ifdef TEST
  *logofs << "NXTransDialog: Executing child with pid '"
          << getpid() << "' and parent '" << getppid()
          << "'.\n" << logofs_flush;
  #endif

  SystemCleanup("NXTransDialog");

  //
  // Copy the client command before
  // freeing up the control class.
  //

  char command[DEFAULT_STRING_LIMIT];

  if (control != NULL)
  {
    strcpy(command, control -> ClientPath);
  }
  else
  {
    char *path = GetClientPath();

    strcpy(command, path);

    delete [] path;
  }

  //
  // Get rid of the unused resources.
  //

  MemoryCleanup("NXTransDialog");

  #ifdef TEST
  *logofs << "NXTransDialog: Running external NX dialog with caption '"
          << caption << "' message '" << message << "' type '"
          << type << "' local '" << local << "' display '"
          << display << "'.\n"
          << logofs_flush;
  #endif

  int pulldown = (strcmp(type, "pulldown") == 0);

  char parent[DEFAULT_STRING_LIMIT];

  snprintf(parent, DEFAULT_STRING_LIMIT, "%d", getppid());

  parent[DEFAULT_STRING_LIMIT - 1] = '\0';

  UnsetEnv("LD_LIBRARY_PATH");

  if (!pulldown)
  {
    cerr << "Info: Upcoming dialog:\n"
         << "/-----------------------------------------------------------\n"
         << caption << ":\n"
         << message << "\n"
         << "\\-----------------------------------------------------------\n";
  }

  for (int i = 0; i < 4; i++)
  {
    if (local != 0)
    {
      if (pulldown)
      {
        execlp(command, command, "--dialog", type, "--caption", caption,
                   "--window", window, "--local", "--parent", parent,
                       "--display", display, NULL);
      }
      else
      {
        execlp(command, command, "--dialog", type, "--caption", caption,
                   "--message", message, "--local", "--parent", parent,
                       "--display", display, NULL);
      }
    }
    else
    {
      if (pulldown)
      {
        execlp(command, command, "--dialog", type, "--caption", caption,
                   "--window", window, "--parent", parent,
                       "--display", display, NULL);
      }
      else
      {
        execlp(command, command, "--dialog", type, "--caption", caption,
                   "--message", message, "--parent", parent,
                       "--display", display, NULL);
      }
    }

    #ifdef TEST
    *logofs << "NXTransDialog: WARNING! Couldn't start '"
            << command << "'. " << "Error is " << EGET()
            << " '" << ESTR() << "'.\n" << logofs_flush;
    #endif

    #ifdef WARNING
    cerr << "Warning" << ": Couldn't start '" << command
         << "'. Error is " << EGET() << " '" << ESTR()
         << "'.\n";
    #endif

    //
    // Retry by looking for the default name
    // in the default NX path.
    //

    if (i == 0 || i == 2)
    {
      strcpy(command, "nxclient");
    }
    else if (i == 1 || i == 3)
    {
      strcpy(command, "nxdialog");
    }

    if (i == 2)
    {
      char newPath[DEFAULT_STRING_LIMIT];

      strcpy(newPath, "/usr/NX/bin:/opt/NX/bin:/usr/local/NX/bin:");

      #ifdef __APPLE__
      // FIXME: missing length limitation!
      strcat(newPath, "/Applications/NX Client for OSX.app/Contents/MacOS:");
      #endif

      #if defined(__CYGWIN__) || defined(__CYGWIN32__)
      // FIXME: missing length limitation!
      strcat(newPath, ".:");
      #endif

      int newLength = strlen(newPath);
      char *oldPath = getenv("PATH");

      // FIXME: check if strncat would be better here
      snprintf(newPath + newLength, DEFAULT_STRING_LIMIT - newLength, "%s", oldPath);

      #ifdef TEST
      *logofs << "NXTransDialog: WARNING! Trying with path '"
              << newPath << "'.\n" << logofs_flush;
      #endif

      #ifdef WARNING
      cerr << "Warning" << ": Trying with path '" << newPath
           << "'.\n";
      #endif

      //
      // Solaris doesn't seem to have
      // function setenv().
      //

      #ifdef __sun
      char newEnv[DEFAULT_STRING_LIMIT + 5];
      sprintf(newEnv,"PATH=%s", newPath);
      putenv(newEnv);
      #else
      setenv("PATH", newPath, 1);
      #endif
    }
    else
    {
      #ifdef TEST
      *logofs << "NXTransDialog: WARNING! Trying with command '"
              << command << "'.\n" << logofs_flush;
      #endif

      #ifdef WARNING
      cerr << "Warning" << ": Trying with command '" << command
           << "'.\n";
      #endif
    }
  }

  #ifdef TEST
  *logofs << "NXTransDialog: WARNING! Could not display dialog.\n"
          << logofs_flush;
  #endif

  #ifdef WARNING
  cerr << "Warning" << ": Could not display dialog.\n";
  #endif

  //
  // Hopefully useless.
  //

  exit(0);
}

//
// Start a nxclient process in dialog mode.
//

int NXTransClient(const char* display)
{
  //
  // Be sure log file is valid.
  //

  if (logofs == NULL)
  {
    logofs = &cerr;
  }

  int pid;

  #ifdef TEST
  *logofs << "NXTransClient: Going to fork with NX pid '"
          << getpid() << "'.\n" << logofs_flush;
  #endif

  pid = Fork();

  if (pid != 0)
  {
    if (pid < 0)
    {
      #ifdef TEST
      *logofs << "NXTransClient: WARNING! Function fork failed. "
              << "Error is " << EGET() << " '" << ESTR()
              << "'.\n" << logofs_flush;
      #endif

      cerr << "Warning" << ": Function fork failed. "
           << "Error is " << EGET() << " '" << ESTR()
           << "'.\n";
    }
    #ifdef TEST
    else
    {
      *logofs << "NXTransClient: Created NX client process "
              << "with pid '" << pid << "'.\n"
              << logofs_flush;
    }
    #endif

    return pid;
  }

  #ifdef TEST
  *logofs << "NXTransClient: Executing child with pid '"
          << getpid() << "' and parent '" << getppid()
          << "'.\n" << logofs_flush;
  #endif

  SystemCleanup("NXTransClient");

  //
  // Copy the client command before
  // freeing up the control class.
  //

  char command[DEFAULT_STRING_LIMIT];

  if (control != NULL)
  {
    strcpy(command, control -> ClientPath);
  }
  else
  {
    char *path = GetClientPath();

    strcpy(command, path);

    delete [] path;
  }

  //
  // Get rid of unused resources.
  //

  MemoryCleanup("NXTransClient");

  #ifdef TEST
  *logofs << "NXTransClient: Running external NX client with display '"
          << display << "'.\n" << logofs_flush;
  #endif

  //
  // Provide the display in the environment.
  //

  char newDisplay[DISPLAY_LENGTH_LIMIT];

  #ifdef __sun

  snprintf(newDisplay, DISPLAY_LENGTH_LIMIT, "DISPLAY=%s", display);

  putenv(newDisplay);

  #else

  snprintf(newDisplay, DISPLAY_LENGTH_LIMIT, "%s", display);

  setenv("DISPLAY", newDisplay, 1);

  #endif

  UnsetEnv("LD_LIBRARY_PATH");

  for (int i = 0; i < 2; i++)
  {
    execlp(command, command, NULL);

    #ifdef WARNING
    *logofs << "NXTransClient: WARNING! Couldn't start '"
            << command << "'. Error is " << EGET() << " '"
            << ESTR() << "'.\n" << logofs_flush;
    #endif

    cerr << "Warning" << ": Couldn't start '" << command
         << "'. Error is " << EGET() << " '" << ESTR()
         << "'.\n";

    //
    // Retry by looking for the default name
    // in the default NX path.
    //

    if (i == 0)
    {

      // FIXME: code dpulication: this whole block is duplicated in NXTransDialog
      strcpy(command, "nxclient");

      char newPath[DEFAULT_STRING_LIMIT];

      strcpy(newPath, "/usr/NX/bin:/opt/NX/bin:/usr/local/NX/bin:");

      #ifdef __APPLE__

      strcat(newPath, "/Applications/NX Client for OSX.app/Contents/MacOS:");

      #endif

      #if defined(__CYGWIN__) || defined(__CYGWIN32__)

      strcat(newPath, ".:");

      #endif

      int newLength = strlen(newPath);

      char *oldPath = getenv("PATH");

      // FIXME: check if strncat would be better here
      snprintf(newPath + newLength, DEFAULT_STRING_LIMIT - newLength, "%s", oldPath);

      newPath[DEFAULT_STRING_LIMIT - 1] = '\0';

      #ifdef WARNING
      *logofs << "NXTransClient: WARNING! Trying with path '"
              << newPath << "'.\n" << logofs_flush;
      #endif

      cerr << "Warning" << ": Trying with path '" << newPath
           << "'.\n";

      //
      // Solaris doesn't seem to have
      // function setenv().
      //

      #ifdef __sun

      char newEnv[DEFAULT_STRING_LIMIT + 5];

      sprintf(newEnv,"PATH=%s", newPath);

      putenv(newEnv);

      #else

      setenv("PATH", newPath, 1);

      #endif
    }

  }
  //
  // Hopefully useless.
  //

  exit(0);
}

//
// Wait until the timeout is expired.
// The timeout is expressed in milli-
// seconds.
//

int NXTransWatchdog(int timeout)
{
  //
  // Be sure log file is valid.
  //

  if (logofs == NULL)
  {
    logofs = &cerr;
  }

  int pid;

  #ifdef TEST
  *logofs << "NXTransWatchdog: Going to fork with NX pid '"
          << getpid() << "'.\n" << logofs_flush;
  #endif

  pid = Fork();

  if (pid != 0)
  {
    if (pid < 0)
    {
      #ifdef TEST
      *logofs << "NXTransWatchdog: WARNING! Function fork failed. "
              << "Error is " << EGET() << " '" << ESTR()
              << "'.\n" << logofs_flush;
      #endif

      cerr << "Warning" << ": Function fork failed. "
           << "Error is " << EGET() << " '" << ESTR()
           << "'.\n";
    }
    #ifdef TEST
    else
    {
      *logofs << "NXTransWatchdog: Created NX watchdog process "
              << "with pid '" << pid << "'.\n" << logofs_flush;
    }
    #endif

    return pid;
  }

  int parent = getppid();

  #ifdef TEST
  *logofs << "NXTransWatchdog: Executing child with pid '"
          << getpid() << "' and parent '" << parent
          << "'.\n" << logofs_flush;
  #endif

  SystemCleanup("NXTransWatchdog");

  //
  // Get rid of unused resources.
  //

  MemoryCleanup("NXTransWatchdog");

  //
  // Run until the timeout is expired
  // or forever, if no timeout is
  // provided.
  //

  T_timestamp startTs = getTimestamp();

  int diffTs = 0;

  for (;;)
  {
    //
    // Complain if the parent is dead.
    //

    if (CheckParent("NXTransWatchdog", "watchdog", parent) == 0)
    {
      #ifdef TEST
      *logofs << "NXTransWatchdog: Exiting with no parent "
              << "running.\n" << logofs_flush;
      #endif

      HandleCleanup();
    }

    if (timeout > 0)
    {
      if (diffTs >= timeout)
      {
        #ifdef TEST
        *logofs << "NXTransWatchdog: Timeout of " << timeout
                << " ms raised in watchdog.\n" << logofs_flush;
        #endif

        //
        // We will just exit. Our parent should be
        // monitoring us and detect that the process
        // is gone.
        //

        HandleCleanup();
      }
    }

    if (timeout > 0)
    {
      #ifdef TEST
      *logofs << "NXTransWatchdog: Waiting for the timeout "
              << "with " << timeout - diffTs << " ms to run.\n"
              << logofs_flush;
      #endif

      usleep((timeout - diffTs) * 1000);

      diffTs = diffTimestamp(startTs, getNewTimestamp());
    }
    else
    {
      #ifdef TEST
      *logofs << "NXTransWatchdog: Waiting for a signal.\n"
              << logofs_flush;
      #endif

      sleep(10);
    }
  }

  //
  // Hopefully useless.
  //

  exit(0);
}

int NXTransKeeperHandler(int signal)
{
  if (keeper != NULL)
  {
    switch (signal)
    {
      case SIGTERM:
      case SIGINT:
      case SIGHUP:
      {
        #ifdef TEST
        *logofs << "NXTransKeeperHandler: Requesting giveup "
                << "because of signal " << signal << " ,'"
                << DumpSignal(signal) << "'.\n"
                << logofs_flush;
        #endif

        keeper -> setSignal(signal);

        return 0;
      }
    }
  }

  return 1;
}

void NXTransKeeperCheck()
{
  if (CheckParent("NXTransKeeper", "keeper",
          keeper -> getParent()) == 0 || keeper -> getSignal() != 0)
  {
    #ifdef TEST
    *logofs << "NXTransKeeperCheck: Exiting because of signal "
            << "or no parent running.\n" << logofs_flush;
    #endif

    HandleCleanup();
  }
}

int NXTransKeeper(int caches, int images, const char *root)
{
  //
  // Be sure log file is valid.
  //

  if (logofs == NULL)
  {
    logofs = &cerr;
  }

  if (caches == 0 && images == 0)
  {
    #ifdef TEST
    *logofs << "NXTransKeeper: No NX cache house-keeping needed.\n"
            << logofs_flush;
    #endif

    return 0;
  }

  int pid;

  #ifdef TEST
  *logofs << "NXTransKeeper: Going to fork with NX pid '"
          << getpid() << "'.\n" << logofs_flush;
  #endif

  pid = Fork();

  if (pid != 0)
  {
    if (pid < 0)
    {
      #ifdef TEST
      *logofs << "NXTransKeeper: WARNING! Function fork failed. "
              << "Error is " << EGET() << " '" << ESTR()
              << "'.\n" << logofs_flush;
      #endif

      cerr << "Warning" << ": Function fork failed. "
           << "Error is " << EGET() << " '" << ESTR()
           << "'.\n";
    }
    #ifdef TEST
    else
    {
      *logofs << "NXTransKeeper: Created NX keeper process "
              << "with pid '" << pid << "'.\n"
              << logofs_flush;
    }
    #endif

    return pid;
  }

  int parent = getppid();

  #ifdef TEST
  *logofs << "NXTransKeeper: Executing child with pid '"
          << getpid() << "' and parent '" << parent
          << "'.\n" << logofs_flush;
  #endif

  SystemCleanup("NXTransKeeper");

  #ifdef TEST
  *logofs << "NXTransKeeper: Going to run with caches " << caches
          << " images " << images << " and root " << root
          << " at " << strMsTimestamp() << ".\n" << logofs_flush;
  #endif

  //
  // Create the house-keeper class.
  //

  int timeout = control -> KeeperTimeout;

  keeper = new Keeper(caches, images, root, 100, parent);

  signalHandler = NXTransKeeperHandler;

  if (keeper == NULL)
  {
    #ifdef PANIC
    *logofs << "NXTransKeeper: PANIC! Failed to create the keeper object.\n"
            << logofs_flush;
     #endif

     cerr << "Error" << ": Failed to create the keeper object.\n";

     HandleCleanup();
  }

  //
  // Get rid of unused resources. Root path
  // must be copied in keeper's constructor
  // before control is deleted.
  //

  MemoryCleanup("NXTransKeeper");

  //
  // Decrease the priority of this process.
  //
  // The following applies to Cygwin: "Cygwin processes can be
  // set to IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, HIGH_-
  // PRIORITY_CLASS, or REALTIME_PRIORITY_CLASS with the nice
  // call. If you pass a positive number to nice(), then the
  // priority level will decrease by one (within the above list
  // of priorities). A negative number would make it increase
  // by one. It is not possible to change it by more than one
  // at a time without making repeated calls".
  //

  if (nice(5) < 0 && errno != 0)
  {
    #ifdef WARNING
    *logofs << "NXTransKeeper: WARNING! Failed to renice process to +5. "
            << "Error is " << EGET() << " '" << ESTR()
            << "'.\n" << logofs_flush;
     #endif

     cerr << "Warning" << ": Failed to renice process to +5. "
          << "Error is " << EGET() << " '" << ESTR()
          << "'.\n";
  }

  //
  // Delay a bit the first run to give
  // a boost to the session startup.
  //

  #ifdef TEST
  *logofs << "NXTransKeeper: Going to sleep for "
          << timeout / 20 << " ms.\n" << logofs_flush;
  #endif

  usleep(timeout / 20 * 1000);

  NXTransKeeperCheck();

  //
  // The house keeping of the persistent
  // caches is performed only once.
  //

  if (caches != 0)
  {
    #ifdef TEST
    *logofs << "NXTransKeeper: Going to cleanup the NX cache "
            << "directories at " << strMsTimestamp() << ".\n"
            << logofs_flush;
    #endif

    keeper -> cleanupCaches();

    #ifdef TEST
    *logofs << "NXTransKeeper: Completed cleanup of NX cache "
            << "directories at " << strMsTimestamp() << ".\n"
            << logofs_flush;
    #endif
  }
  #ifdef TEST
  else
  {
    *logofs << "NXTransKeeper: Nothing to do for the "
            << "persistent caches.\n" << logofs_flush;
  }
  #endif

  if (images == 0)
  {
    #ifdef TEST
    *logofs << "NXTransKeeper: Nothing to do for the "
            << "persistent images.\n" << logofs_flush;
    #endif

    HandleCleanup();
  }

  //
  // Take care of the persistent image cache.
  // Run a number of iterations and then exit,
  // so we can keep the memory consumption
  // low. The parent will check our exit code
  // and will eventually restart us.
  //

  for (int iterations = 0; iterations < 100; iterations++)
  {
    #ifdef TEST
    *logofs << "NXTransKeeper: Running iteration " << iterations
            << " at " << strMsTimestamp() << ".\n"
            << logofs_flush;
    #endif

    NXTransKeeperCheck();

    #ifdef TEST
    *logofs << "NXTransKeeper: Going to cleanup the NX images "
            << "directories at " << strMsTimestamp() << ".\n"
            << logofs_flush;
    #endif

    if (keeper -> cleanupImages() < 0)
    {
      #ifdef TEST
      *logofs << "NXTransKeeper: Exiting because of error "
              << "handling the image cache.\n" << logofs_flush;
      #endif

      HandleCleanup();
    }

    #ifdef TEST
    *logofs << "NXTransKeeper: Completed cleanup of NX images "
            << "directories at " << strMsTimestamp() << ".\n"
            << logofs_flush;
    #endif

    NXTransKeeperCheck();

    #ifdef TEST
    *logofs << "NXTransKeeper: Going to sleep for " << timeout
            << " ms.\n" << logofs_flush;
    #endif

    usleep(timeout * 1000);
  }

  HandleCleanup(2);

  //
  // Hopefully useless.
  //

  exit(0);
}

void SystemCleanup(const char *name)
{
  #ifdef TEST
  *logofs << name << ": Performing system cleanup in process "
          << "with pid '" << getpid() << "'.\n"
          << logofs_flush;
  #endif

  //
  // Reinstall signals that might
  // have been restored by agents.
  //

  InstallSignals();
}

void MemoryCleanup(const char *name)
{
  #ifdef TEST
  *logofs << name << ": Performing memory cleanup in process "
          << "with pid '" << getpid() << "'.\n"
          << logofs_flush;
  #endif

  DisableSignals();

  //
  // Prevent deletion of unix socket
  // and lock file.
  //

  useUnixSocket = 0;

  //
  // Don't let cleanup kill other
  // children.
  //

  lastDialog   = 0;
  lastWatchdog = 0;
  lastKeeper   = 0;

  CleanupListeners();

  CleanupSockets();

  CleanupGlobal();

  EnableSignals();
}

int UnsetEnv(const char *name)
{
  int result;

  #ifdef __sun

  char **pEnv = environ;

  int nameLen = strlen(name) + 1;

  char *varName = new char[nameLen + 1];

  strcpy(varName, name);

  strcat(varName, "=");

  pEnv = environ;

  while (*pEnv != NULL)
  {
    if (!strncmp(varName, *pEnv, nameLen))
    {
      break;
    }

    *pEnv++;
  }

  while (*pEnv != NULL)
  {
    *pEnv = *(pEnv + 1);

    pEnv++;
  }

  result = 0;
  delete[] varName;

  #else

  #ifdef __APPLE__

  unsetenv(name);
  result = 0;

  #else

  result = unsetenv(name);

  #endif

  #endif

  return result;
}