summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp.c
blob: e10193a98fff91be15aef043d9036bb7156c341f (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
/*
   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-2012

   main program

*/

#include "xrdp.h"

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

static long g_sync_mutex = 0;
static long g_sync1_mutex = 0;
static tbus g_term_event = 0;
static tbus g_sync_event = 0;
/* syncronize stuff */
static int g_sync_command = 0;
static long g_sync_result = 0;
static long g_sync_param1 = 0;
static long g_sync_param2 = 0;
static long (*g_sync_func)(long param1, long param2);

/*****************************************************************************/
long APP_CC
g_xrdp_sync(long (*sync_func)(long param1, long param2), long sync_param1,
            long sync_param2)
{
  long sync_result;
  int sync_command;

  if (tc_threadid_equal(tc_get_threadid(), g_threadid))
  {
    /* this is the main thread, call the function directly */
    sync_result = sync_func(sync_param1, sync_param2);
  }
  else
  {
    tc_mutex_lock(g_sync1_mutex);
    tc_mutex_lock(g_sync_mutex);
    g_sync_param1 = sync_param1;
    g_sync_param2 = sync_param2;
    g_sync_func = sync_func;
    g_sync_command = 100;
    tc_mutex_unlock(g_sync_mutex);
    g_set_wait_obj(g_sync_event);
    do
    {
      g_sleep(100);
      tc_mutex_lock(g_sync_mutex);
      sync_command = g_sync_command;
      sync_result = g_sync_result;
      tc_mutex_unlock(g_sync_mutex);
    }
    while (sync_command != 0);
    tc_mutex_unlock(g_sync1_mutex);
  }
  return sync_result;
}

/*****************************************************************************/
void DEFAULT_CC
xrdp_shutdown(int sig)
{
  tbus threadid;

  threadid = tc_get_threadid();
  g_writeln("shutting down");
  g_writeln("signal %d threadid %p", sig, threadid);
  if (!g_is_wait_obj_set(g_term_event))
  {
    g_set_wait_obj(g_term_event);
  }
}

/*****************************************************************************/
int APP_CC
g_is_term(void)
{
  return g_is_wait_obj_set(g_term_event);
}

/*****************************************************************************/
void APP_CC
g_set_term(int in_val)
{
  if (in_val)
  {
    g_set_wait_obj(g_term_event);
  }
  else
  {
    g_reset_wait_obj(g_term_event);
  }
}

/*****************************************************************************/
tbus APP_CC
g_get_term_event(void)
{
  return g_term_event;
}

/*****************************************************************************/
tbus APP_CC
g_get_sync_event(void)
{
  return g_sync_event;
}

/*****************************************************************************/
void DEFAULT_CC
pipe_sig(int sig_num)
{
  /* do nothing */
  g_writeln("got SIGPIPE(%d)", sig_num);
}

/*****************************************************************************/
void APP_CC
g_loop(void)
{
  tc_mutex_lock(g_sync_mutex);
  if (g_sync_command != 0)
  {
    if (g_sync_func != 0)
    {
      if (g_sync_command == 100)
      {
        g_sync_result = g_sync_func(g_sync_param1, g_sync_param2);
      }
    }
    g_sync_command = 0;
  }
  tc_mutex_unlock(g_sync_mutex);
}

/*****************************************************************************/
int DEFAULT_CC
main(int argc, char** argv)
{
  int test;
  int host_be;
  int pid;
  int fd;
  int no_daemon;
  char text[256];
  char pid_file[256];

  g_init();
  ssl_init();
  /* check compiled endian with actual endian */
  test = 1;
  host_be = !((int)(*(unsigned char*)(&test)));
#if defined(B_ENDIAN)
  if (!host_be)
#endif
#if defined(L_ENDIAN)
  if (host_be)
#endif
  {
    g_writeln("endian wrong, edit arch.h");
    return 0;
  }
  /* check long, int and void* sizes */
  if (sizeof(int) != 4)
  {
    g_writeln("unusable int size, must be 4");
    return 0;
  }
  if (sizeof(long) != sizeof(void*))
  {
    g_writeln("long size must match void* size");
    return 0;
  }
  if (sizeof(long) != 4 && sizeof(long) != 8)
  {
    g_writeln("unusable long size, must be 4 or 8");
    return 0;
  }
  if (sizeof(tui64) != 8)
  {
    g_writeln("unusable tui64 size, must be 8");
    return 0;
  }
  g_snprintf(pid_file, 255, "%s/xrdp.pid", XRDP_PID_PATH);
  no_daemon = 0;
  if (argc == 2)
  {
    if ((g_strncasecmp(argv[1], "-kill", 255) == 0) ||
        (g_strncasecmp(argv[1], "--kill", 255) == 0) ||
        (g_strncasecmp(argv[1], "-k", 255) == 0))
    {
      g_writeln("stopping xrdp");
      /* read the xrdp.pid file */
      fd = -1;
      if (g_file_exist(pid_file)) /* xrdp.pid */
      {
        fd = g_file_open(pid_file); /* xrdp.pid */
      }
      if (fd == -1)
      {
        g_writeln("problem opening to xrdp.pid");
        g_writeln("maybe its not running");
      }
      else
      {
        g_memset(text, 0, 32);
        g_file_read(fd, text, 31);
        pid = g_atoi(text);
        g_writeln("stopping process id %d", pid);
        if (pid > 0)
        {
          g_sigterm(pid);
        }
        g_file_close(fd);
      }
      g_exit(0);
    }
    else if (g_strncasecmp(argv[1], "-nodaemon", 255) == 0 ||
             g_strncasecmp(argv[1], "--nodaemon", 255) == 0 ||
             g_strncasecmp(argv[1], "-nd", 255) == 0 ||
             g_strncasecmp(argv[1], "--nd", 255) == 0 ||
             g_strncasecmp(argv[1], "-ns", 255) == 0 ||
             g_strncasecmp(argv[1], "--ns", 255) == 0)
    {
      no_daemon = 1;
    }
    else if (g_strncasecmp(argv[1], "-help", 255) == 0 ||
             g_strncasecmp(argv[1], "--help", 255) == 0 ||
             g_strncasecmp(argv[1], "-h", 255) == 0)
    {
      g_writeln("");
      g_writeln("xrdp: A Remote Desktop Protocol server.");
      g_writeln("Copyright (C) Jay Sorg 2004-2011");
      g_writeln("See http://xrdp.sourceforge.net for more information.");
      g_writeln("");
      g_writeln("Usage: xrdp [options]");
      g_writeln("   -h: show help");
      g_writeln("   -nodaemon: don't fork into background");
      g_writeln("   -kill: shut down xrdp");
      g_writeln("");
      g_exit(0);
    }
    else if ((g_strncasecmp(argv[1], "-v", 255) == 0) ||
             (g_strncasecmp(argv[1], "--version", 255) == 0))
    {
      g_writeln("");
      g_writeln("xrdp: A Remote Desktop Protocol server.");
      g_writeln("Copyright (C) Jay Sorg 2004-2011");
      g_writeln("See http://xrdp.sourceforge.net for more information.");
      g_writeln("Version %s",PACKAGE_VERSION);
      g_writeln("");
      g_exit(0);
    }
    else
    {
      g_writeln("Unknown Parameter");
      g_writeln("xrdp -h for help");
      g_writeln("");
      g_exit(0);
    }
  }
  else if (argc > 1)
  {
    g_writeln("Unknown Parameter");
    g_writeln("xrdp -h for help");
    g_writeln("");
    g_exit(0);
  }
  if (g_file_exist(pid_file)) /* xrdp.pid */
  {
    g_writeln("It looks like xrdp is allready running,");
    g_writeln("if not delete the xrdp.pid file and try again");
    g_exit(0);
  }
  if (!no_daemon)
  {
    /* make sure we can write to pid file */
    fd = g_file_open(pid_file); /* xrdp.pid */
    if (fd == -1)
    {
      g_writeln("running in daemon mode with no access to pid files, quitting");
      g_exit(0);
    }
    if (g_file_write(fd, "0", 1) == -1)
    {
      g_writeln("running in daemon mode with no access to pid files, quitting");
      g_exit(0);
    }
    g_file_close(fd);
    g_file_delete(pid_file);
  }
  if (!no_daemon)
  {
    /* start of daemonizing code */
    pid = g_fork();
    if (pid == -1)
    {
      g_writeln("problem forking");
      g_exit(1);
    }
    if (0 != pid)
    {
      g_writeln("process %d started ok", pid);
      /* exit, this is the main process */
      g_exit(0);
    }
    g_sleep(1000);
    g_file_close(0);
    g_file_close(1);
    g_file_close(2);
    g_file_open("/dev/null");
    g_file_open("/dev/null");
    g_file_open("/dev/null");
    /* end of daemonizing code */
  }
  if (!no_daemon)
  {
    /* write the pid to file */
    pid = g_getpid();
    fd = g_file_open(pid_file); /* xrdp.pid */
    if (fd == -1)
    {
      g_writeln("trying to write process id to xrdp.pid");
      g_writeln("problem opening xrdp.pid");
      g_writeln("maybe no rights");
    }
    else
    {
      g_sprintf(text, "%d", pid);
      g_file_write(fd, text, g_strlen(text));
      g_file_close(fd);
    }
  }
  g_threadid = tc_get_threadid();
  g_listen = xrdp_listen_create();
  g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */
  g_signal_kill(xrdp_shutdown); /* SIGKILL */
  g_signal_pipe(pipe_sig); /* SIGPIPE */
  g_signal_terminate(xrdp_shutdown); /* SIGTERM */
  g_sync_mutex = tc_mutex_create();
  g_sync1_mutex = tc_mutex_create();
  pid = g_getpid();
  g_snprintf(text, 255, "xrdp_%8.8x_main_term", pid);
  g_term_event = g_create_wait_obj(text);
  g_snprintf(text, 255, "xrdp_%8.8x_main_sync", pid);
  g_sync_event = g_create_wait_obj(text);
  if (g_term_event == 0)
  {
    g_writeln("error creating g_term_event");
  }
  xrdp_listen_main_loop(g_listen);
  xrdp_listen_delete(g_listen);
  tc_mutex_delete(g_sync_mutex);
  tc_mutex_delete(g_sync1_mutex);
  g_delete_wait_obj(g_term_event);
  g_delete_wait_obj(g_sync_event);
  /* delete the xrdp.pid file */
  g_file_delete(pid_file);
  return 0;
}