/* a new connection is coming in */ int DEFAULT_CC xrdp_listen_conn_in(struct trans *self, struct trans *new_self) { struct xrdp_process *process; struct xrdp_listen *lis; lis = (struct xrdp_listen *)(self->callback_data); if (lis->startup_params->fork) { return xrdp_listen_fork(lis, new_self); } process = xrdp_process_create(lis, lis->pro_done_event); if (xrdp_listen_add_pro(lis, process) == 0) { /* start thread */ process->server_trans = new_self; g_process = process; tc_thread_create(xrdp_process_run, 0); tc_sem_dec(g_process_sem); /* this will wait */ } else { xrdp_process_delete(process); } return 0; }
static int APP_CC xrdp_listen_fork(struct xrdp_listen *self, struct trans *server_trans) { int pid; struct xrdp_process *process; pid = g_fork(); if (pid == 0) { /* child */ /* recreate some main globals */ xrdp_child_fork(); /* recreate the process done wait object, not used in fork mode */ /* close, don't delete this */ g_close_wait_obj(self->pro_done_event); xrdp_listen_create_pro_done(self); /* delete listener, child need not listen */ trans_delete(self->listen_trans); self->listen_trans = 0; /* new connect instance */ process = xrdp_process_create(self, 0); process->server_trans = server_trans; g_process = process; xrdp_process_run(0); xrdp_process_delete(process); /* mark this process to exit */ g_set_term(1); return 0; } /* parent */ trans_delete(server_trans); return 0; }
/* wait for incoming connections */ int APP_CC xrdp_listen_main_loop(struct xrdp_listen* self) { int error; int robjs_count; int cont; char port[8]; tbus robjs[8]; tbus term_obj; tbus sync_obj; tbus sck_obj; tbus done_obj; struct xrdp_process* process; self->status = 1; xrdp_listen_get_port(port, sizeof(port)); self->sck = g_tcp_socket(); g_tcp_set_non_blocking(self->sck); error = g_tcp_bind(self->sck, port); if (error != 0) { g_writeln("bind error in xrdp_listen_main_loop"); g_tcp_close(self->sck); self->status = -1; return 1; } error = g_tcp_listen(self->sck); if (error == 0) { term_obj = g_get_term_event(); sync_obj = g_get_sync_event(); sck_obj = g_create_wait_obj_from_socket(self->sck, 0); done_obj = self->pro_done_event; cont = 1; while (cont) { /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = term_obj; robjs[robjs_count++] = sync_obj; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = done_obj; /* wait */ if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0) { /* error, should not get here */ g_sleep(100); } if (g_is_wait_obj_set(term_obj)) /* term */ { break; } if (g_is_wait_obj_set(sync_obj)) /* sync */ { g_reset_wait_obj(sync_obj); g_loop(); } if (g_is_wait_obj_set(sck_obj)) /* incomming connection */ { error = g_tcp_accept(self->sck); if ((error == -1) && g_tcp_last_error_would_block(self->sck)) { /* should not get here */ g_sleep(100); } else if (error == -1) { /* error, should not get here */ break; } else { process = xrdp_process_create(self, self->pro_done_event); if (xrdp_listen_add_pro(self, process) == 0) { /* start thread */ process->sck = error; g_process = process; tc_thread_create(xrdp_process_run, 0); tc_sem_dec(g_process_sem); /* this will wait */ } else { xrdp_process_delete(process); } } } if (g_is_wait_obj_set(done_obj)) /* pro_done_event */ { g_reset_wait_obj(done_obj); xrdp_listen_delete_done_pro(self); } } /* stop listening */ g_delete_wait_obj_from_socket(sck_obj); g_tcp_close(self->sck); /* second loop to wait for all process threads to close */ cont = 1; while (cont) { if (self->process_list->count == 0) { break; } /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = sync_obj; robjs[robjs_count++] = done_obj; /* wait */ if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0) { /* error, should not get here */ g_sleep(100); } if (g_is_wait_obj_set(sync_obj)) /* sync */ { g_reset_wait_obj(sync_obj); g_loop(); } if (g_is_wait_obj_set(done_obj)) /* pro_done_event */ { g_reset_wait_obj(done_obj); xrdp_listen_delete_done_pro(self); } } } else { DEBUG(("listen error in xrdp_listen_main_loop")); } self->status = -1; return 0; }