xrdp/xrdp/xrdp_listen.c

249 lines
6.0 KiB
C
Raw Normal View History

2004-08-03 11:37:55 +08:00
/*
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.
2004-09-27 11:26:07 +08:00
xrdp: A Remote Desktop Protocol server.
2007-01-12 12:51:48 +08:00
Copyright (C) Jay Sorg 2004-2007
2004-08-03 11:37:55 +08:00
listen for incoming connection
*/
#include "xrdp.h"
2007-02-04 15:53:15 +08:00
/* 'g_process' is protected by the semaphore 'g_process_sem'. One thread sets
g_process and waits for the other to process it */
static long g_process_sem = 0;
2004-08-03 11:37:55 +08:00
static struct xrdp_process* g_process = 0;
/*****************************************************************************/
2005-06-28 11:00:43 +08:00
struct xrdp_listen* APP_CC
xrdp_listen_create(void)
2004-08-03 11:37:55 +08:00
{
struct xrdp_listen* self;
self = (struct xrdp_listen*)g_malloc(sizeof(struct xrdp_listen), 1);
self->process_list_max = 100;
return self;
}
/*****************************************************************************/
2005-06-28 11:00:43 +08:00
void APP_CC
xrdp_listen_delete(struct xrdp_listen* self)
2004-08-03 11:37:55 +08:00
{
g_free(self);
}
/*****************************************************************************/
2006-10-24 12:05:38 +08:00
static int APP_CC
2005-06-28 11:00:43 +08:00
xrdp_listen_term_processes(struct xrdp_listen* self)
2004-08-03 11:37:55 +08:00
{
int i;
/* tell all xrdp processes to end */
for (i = 0; i < self->process_list_count; i++)
{
2004-08-03 11:37:55 +08:00
if (self->process_list[i] != 0)
{
2004-08-03 11:37:55 +08:00
self->process_list[i]->term = 1;
}
}
2004-08-03 11:37:55 +08:00
/* make sure they are done */
for (i = 0; i < self->process_list_count; i++)
{
2004-08-03 11:37:55 +08:00
if (self->process_list[i] != 0)
{
2004-08-03 11:37:55 +08:00
while (self->process_list[i]->status > 0)
{
2004-08-03 11:37:55 +08:00
g_sleep(10);
}
}
}
2004-08-03 11:37:55 +08:00
/* free them all */
for (i = 0; i < self->process_list_count; i++)
{
2004-08-03 11:37:55 +08:00
if (self->process_list[i] != 0)
{
2004-08-03 11:37:55 +08:00
xrdp_process_delete(self->process_list[i]);
}
}
2004-08-03 11:37:55 +08:00
return 0;
}
/*****************************************************************************/
/* returns error */
2006-10-24 12:05:38 +08:00
static int APP_CC
2005-06-28 11:00:43 +08:00
xrdp_listen_add_pro(struct xrdp_listen* self)
2004-08-03 11:37:55 +08:00
{
int i;
for (i = 0; i < self->process_list_max; i++)
{
/* add process in new slot */
if (self->process_list[i] == 0)
{
self->process_list[i] = g_process;
self->process_list_count++;
return 0;
}
/* add process in unused slot */
2004-09-12 05:27:39 +08:00
/* this shouldn't happen */
2004-08-03 11:37:55 +08:00
if (self->process_list[i]->status <= 0)
{
xrdp_process_delete(self->process_list[i]);
self->process_list[i] = g_process;
return 0;
}
}
return 1;
}
2004-09-12 05:27:39 +08:00
/*****************************************************************************/
2005-06-28 11:00:43 +08:00
int APP_CC
xrdp_listen_delete_pro(struct xrdp_listen* self, struct xrdp_process* pro)
2004-09-12 05:27:39 +08:00
{
int i;
for (i = 0; i < self->process_list_max; i++)
{
if (self->process_list[i] == pro)
{
2006-04-29 09:47:06 +08:00
DEBUG(("process deleted"));
2004-09-12 05:27:39 +08:00
xrdp_process_delete(pro);
self->process_list[i] = 0;
return 0;
}
}
return 0;
}
2004-08-03 11:37:55 +08:00
/*****************************************************************************/
/* i can't get stupid in_val to work, hum using global var for now */
2005-06-28 11:00:43 +08:00
THREAD_RV THREAD_CC
xrdp_process_run(void* in_val)
2004-08-03 11:37:55 +08:00
{
2007-02-04 15:53:15 +08:00
struct xrdp_process* process;
2006-04-29 09:47:06 +08:00
DEBUG(("process started"));
2007-02-04 15:53:15 +08:00
process = g_process;
g_process = 0;
tc_sem_inc(g_process_sem);
xrdp_process_main_loop(process);
2006-04-29 09:47:06 +08:00
DEBUG(("process done"));
2004-08-03 11:37:55 +08:00
return 0;
}
/*****************************************************************************/
/* wait for incoming connections */
2005-06-28 11:00:43 +08:00
int APP_CC
xrdp_listen_main_loop(struct xrdp_listen* self)
2004-08-03 11:37:55 +08:00
{
int error;
2005-11-30 09:27:08 +08:00
int fd;
int index;
char port[8];
char* val;
struct list* names;
struct list* values;
2007-02-04 15:53:15 +08:00
self->status = 1;
g_process_sem = tc_sem_create(0);
2005-11-30 09:27:08 +08:00
/* default to port 3389 */
g_strncpy(port, "3389", 7);
/* see if port is in xrdp.ini file */
2005-12-02 12:57:51 +08:00
fd = g_file_open(XRDP_CFG_FILE);
2005-11-30 09:27:08 +08:00
if (fd > 0)
{
names = list_create();
names->auto_free = 1;
values = list_create();
values->auto_free = 1;
if (file_read_section(fd, "globals", names, values) == 0)
{
for (index = 0; index < names->count; index++)
{
val = (char*)list_get_item(names, index);
if (val != 0)
{
if (g_strncasecmp(val, "port", 5) == 0)
{
val = (char*)list_get_item(values, index);
error = g_atoi(val);
if (error > 0 && error < 65000)
{
g_strncpy(port, val, 7);
}
break;
}
}
}
}
list_delete(names);
list_delete(values);
g_file_close(fd);
}
2004-08-03 11:37:55 +08:00
self->sck = g_tcp_socket();
g_tcp_set_non_blocking(self->sck);
2005-11-30 09:27:08 +08:00
error = g_tcp_bind(self->sck, port);
if (error != 0)
{
2006-04-29 09:47:06 +08:00
g_writeln("bind error in xrdp_listen_main_loop");
g_tcp_close(self->sck);
2005-03-12 23:59:54 +08:00
self->status = -1;
return 1;
}
2004-08-03 11:37:55 +08:00
error = g_tcp_listen(self->sck);
if (error == 0)
{
while (!g_is_term() && !self->term)
{
error = g_tcp_accept(self->sck);
2007-02-04 15:53:15 +08:00
if ((error == -1) && g_tcp_last_error_would_block(self->sck))
{
2004-08-03 11:37:55 +08:00
g_sleep(100);
2005-08-18 08:32:11 +08:00
g_loop();
}
2004-08-03 11:37:55 +08:00
else if (error == -1)
{
2004-08-03 11:37:55 +08:00
break;
}
2004-08-03 11:37:55 +08:00
else
{
g_process = xrdp_process_create(self);
if (xrdp_listen_add_pro(self) == 0)
{
/* start thread */
g_process->sck = error;
2007-02-03 14:45:14 +08:00
tc_thread_create(xrdp_process_run, 0);
2007-02-04 15:53:15 +08:00
tc_sem_dec(g_process_sem); /* this will wait */
g_sleep(250); /* just for safety */
2004-08-03 11:37:55 +08:00
}
else
{
2004-08-03 11:37:55 +08:00
xrdp_process_delete(g_process);
}
2004-08-03 11:37:55 +08:00
}
}
}
2004-09-12 05:27:39 +08:00
else
{
2006-04-29 09:47:06 +08:00
DEBUG(("listen error in xrdp_listen_main_loop"));
2004-09-12 05:27:39 +08:00
}
2004-08-03 11:37:55 +08:00
xrdp_listen_term_processes(self);
g_tcp_close(self->sck);
2007-02-04 15:53:15 +08:00
tc_sem_delete(g_process_sem);
2004-08-03 11:37:55 +08:00
self->status = -1;
return 0;
}