Main.c 3.28 KB
Newer Older
1 2
/**************************************************************************/
/*                                                                        */
3
/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
4 5 6 7 8 9 10 11 12 13
/*                                                                        */
/* NXPROXY, NX protocol compression and NX extensions to this software    */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
/*                                                                        */
14
/* All rights reserved.                                                   */
15 16 17 18 19 20 21
/*                                                                        */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
22 23
#include <errno.h>
#include <limits.h>
24 25 26 27 28 29 30 31

#include "NX.h"

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

32 33
extern const char *__progname;

34 35 36 37 38 39 40 41 42 43
/*
 * Entry point when running nxproxy stand-alone.
 */

int main(int argc, const char **argv)
{
  int result = -1;

  char *options = NULL;

44
  unsigned long int nx_commfd;
45 46
  char *nx_commfd_str = NULL;

47 48
  options = getenv("NX_DISPLAY");

49
  if ((nx_commfd_str = getenv("NX_COMMFD")) != NULL)
50
  {
51
    errno = 0;
52
    nx_commfd = strtoul(nx_commfd_str, NULL, 10);
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
    if ((errno) && (0 == nx_commfd))
    {
      fprintf(stderr, "%s: NX_COMMFD environment variable's value [%s] is not a file descriptor number. "
                      "Aborting...\n",
                      __progname, nx_commfd_str
                      );
      NXTransCleanup();
    }
    else if ((unsigned long int) INT_MAX < nx_commfd)
    {
      fprintf(stderr, "%s: NX_COMMFD environment variable's value [%lu] is out of range for a file descriptor number. "
                      "Aborting...\n",
                      __progname, nx_commfd);
      NXTransCleanup();
    }
    else {
      result = NXTransCreate(nx_commfd, NX_MODE_SERVER, options);

      if (result != 1)
      {
        fprintf(stderr, "%s: NXTransCreate failed for FD#%lu\n"
                        "Aborting...",
                        __progname, nx_commfd);
        NXTransCleanup();
      }

    }

    // go into endless loop

    if (result == 1)
    {
      while (NXTransRunning(NX_FD_ANY))
        result = NXTransContinue(NULL);
    }
89
  }
90 91
  else
  {
92

93 94 95 96
    if (NXTransParseCommandLine(argc, argv) < 0)
    {
      NXTransCleanup();
    }
97

98 99 100 101
    if (NXTransParseEnvironment(options, 0) < 0)
    {
      NXTransCleanup();
    }
102

103 104 105 106 107 108 109 110 111 112
    /*
     * This should not return...
     */

    #ifdef TEST
    fprintf(stderr, "%s: Yielding control to NX entry point.\n", __progname);
    #endif

    result = NXTransProxy(NX_FD_ANY, NX_MODE_ANY, NX_DISPLAY_ANY);
  }
113 114 115 116 117 118 119 120 121

  /*
   * ...So these should not be called.
   */

  NXTransExit(result);

  return 0;
}