summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp.c
blob: 166ecf55d3af2754c97e21d54be8d6220efc8050 (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
/*
   This program 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; either version 2 of the License, or
   (at your option) any later version.

   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., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2004-2005

   main program

*/

#if defined(_WIN32)
#include <windows.h>
#else
#include <pthread.h>
#endif
#include "xrdp.h"

static struct xrdp_listen* g_listen = 0;
static int g_threadid = 0; /* main threadid */

#if defined(_WIN32)
static CRITICAL_SECTION g_term_mutex;
#else
static pthread_mutex_t g_term_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static int g_term = 0;

/*****************************************************************************/
void
xrdp_shutdown(int sig)
{
  struct xrdp_listen* listen;

  if (g_get_threadid() != g_threadid)
  {
    return;
  }
  g_printf("shutting down\n\r");
  g_printf("signal %d threadid %d\n\r", sig, g_get_threadid());
  listen = g_listen;
  g_listen = 0;
  if (listen != 0)
  {
    g_set_term(1);
    g_sleep(1000);
    xrdp_listen_delete(listen);
#if defined(_WIN32)
    WSACleanup();
    DeleteCriticalSection(&g_term_mutex);
#endif
  }
}

/*****************************************************************************/
int
g_is_term(void)
{
  int rv;

#if defined(_WIN32)
  EnterCriticalSection(&g_term_mutex);
  rv = g_term;
  LeaveCriticalSection(&g_term_mutex);
#else
  pthread_mutex_lock(&g_term_mutex);
  rv = g_term;
  pthread_mutex_unlock(&g_term_mutex);
#endif
  return rv;
}

/*****************************************************************************/
void
g_set_term(int in_val)
{
#if defined(_WIN32)
  EnterCriticalSection(&g_term_mutex);
  g_term = in_val;
  LeaveCriticalSection(&g_term_mutex);
#else
  pthread_mutex_lock(&g_term_mutex);
  g_term = in_val;
  pthread_mutex_unlock(&g_term_mutex);
#endif
}

/*****************************************************************************/
void
pipe_sig(int sig_num)
{
  /* do nothing */
  g_printf("got SIGPIPE(%d)\n\r", sig_num);
}

/*****************************************************************************/
int
main(int argc, char** argv)
{
#if defined(_WIN32)
  WSADATA w;

  WSAStartup(2, &w);
  InitializeCriticalSection(&g_term_mutex);
#endif
  g_threadid = g_get_threadid();
  g_listen = xrdp_listen_create();
  g_signal(2, xrdp_shutdown);
  g_signal(9, xrdp_shutdown);
  g_signal(13, pipe_sig); /* sig pipe */
  g_signal(15, xrdp_shutdown);
  xrdp_listen_main_loop(g_listen);
  return 0;
}