int EXPORT_CC mod_exit(struct mod* mod) { if (mod == 0) { return 0; } g_delete_wait_obj_from_socket(mod->sck_obj); g_tcp_close(mod->sck); g_free(mod); return 0; }
/* return error */ int DEFAULT_CC lib_mod_end(struct mod* mod) { rdp_rdp_delete(mod->rdp_layer); mod->rdp_layer = 0; free_stream(mod->in_s); mod->in_s = 0; if (mod->sck_obj != 0) { g_delete_wait_obj_from_socket(mod->sck_obj); mod->sck_obj = 0; } if (mod->sck != 0) { g_tcp_close(mod->sck); mod->sck = 0; } 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; }
/** * * @brief Starts sesman main loop * */ static void DEFAULT_CC sesman_main_loop(void) { int in_sck; int error; int robjs_count; int cont; tbus sck_obj; tbus robjs[8]; /*main program loop*/ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: " "listening..."); g_sck = g_tcp_socket(); g_tcp_set_non_blocking(g_sck); error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port); if (error == 0) { error = g_tcp_listen(g_sck); if (error == 0) { sck_obj = g_create_wait_obj_from_socket(g_sck, 0); cont = 1; while (cont) { /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = g_term_event; robjs[robjs_count++] = g_sync_event; /* 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(g_term_event)) /* term */ { break; } if (g_is_wait_obj_set(g_sync_event)) /* sync */ { g_reset_wait_obj(g_sync_event); session_sync_start(); } if (g_is_wait_obj_set(sck_obj)) /* incomming connection */ { in_sck = g_tcp_accept(g_sck); if ((in_sck == -1) && g_tcp_last_error_would_block(g_sck)) { /* should not get here */ g_sleep(100); } else if (in_sck == -1) { /* error, should not get here */ break; } else { /* we've got a connection, so we pass it to scp code */ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: " "new connection"); thread_scp_start(in_sck); /* todo, do we have to wait here ? */ } } } g_delete_wait_obj_from_socket(sck_obj); } else { log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "sesman[sesman_main_loop]: " "listen error %d (%s)", g_get_errno(), g_get_strerror()); } } else { log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "bind error on " "port '%s': %d (%s)", g_cfg->listen_port, g_get_errno(), g_get_strerror()); } g_tcp_close(g_sck); }
int APP_CC xrdp_process_main_loop(struct xrdp_process* self) { int robjs_count; int wobjs_count; int cont; int timeout; tbus robjs[32]; tbus wobjs[32]; tbus term_obj; tbus sck_obj; self->status = 1; self->session = libxrdp_init((long)self, self->sck); /* this callback function is in xrdp_wm.c */ self->session->callback = callback; /* this function is just above */ self->session->is_term = xrdp_is_term; g_tcp_set_non_blocking(self->sck); g_tcp_set_no_delay(self->sck); if (libxrdp_process_incomming(self->session) == 0) { term_obj = g_get_term_event(); sck_obj = g_create_wait_obj_from_socket(self->sck, 0); cont = 1; while (cont) { /* build the wait obj list */ timeout = -1; robjs_count = 0; wobjs_count = 0; robjs[robjs_count++] = term_obj; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = self->self_term_event; xrdp_wm_get_wait_objs(self->wm, robjs, &robjs_count, wobjs, &wobjs_count, &timeout); /* wait */ if (g_obj_wait(robjs, robjs_count, wobjs, wobjs_count, timeout) != 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(self->self_term_event)) { break; } if (g_is_wait_obj_set(sck_obj)) /* incomming client data */ { if (xrdp_process_loop(self) != 0) { break; } } if (xrdp_wm_check_wait_objs(self->wm) != 0) { break; } } g_delete_wait_obj_from_socket(sck_obj); libxrdp_disconnect(self->session); } xrdp_process_mod_end(self); libxrdp_exit(self->session); self->session = 0; g_tcp_close(self->sck); self->status = -1; g_set_wait_obj(self->done_event); return 0; }
/** * * @brief Starts sesman main loop * */ static void DEFAULT_CC sesman_main_loop(void) { int in_sck; int error; int robjs_count; int cont; tbus sck_obj; tbus robjs[8]; g_sck = g_tcp_socket(); if (g_sck < 0) { log_message(LOG_LEVEL_ERROR, "error opening socket, g_tcp_socket() failed..."); return; } g_tcp_set_non_blocking(g_sck); error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port); if (error == 0) { error = g_tcp_listen(g_sck); if (error == 0) { log_message(LOG_LEVEL_INFO, "listening to port %s on %s", g_cfg->listen_port, g_cfg->listen_address); sck_obj = g_create_wait_obj_from_socket(g_sck, 0); cont = 1; while (cont) { /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = g_term_event; /* 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(g_term_event)) /* term */ { break; } if (g_is_wait_obj_set(sck_obj)) /* incoming connection */ { in_sck = g_tcp_accept(g_sck); if ((in_sck == -1) && g_tcp_last_error_would_block(g_sck)) { /* should not get here */ g_sleep(100); } else if (in_sck == -1) { /* error, should not get here */ break; } else { /* we've got a connection, so we pass it to scp code */ LOG_DBG("new connection"); scp_process_start((void*)(tintptr)in_sck); g_sck_close(in_sck); } } } g_delete_wait_obj_from_socket(sck_obj); } else { log_message(LOG_LEVEL_ERROR, "listen error %d (%s)", g_get_errno(), g_get_strerror()); } } else { log_message(LOG_LEVEL_ERROR, "bind error on " "port '%s': %d (%s)", g_cfg->listen_port, g_get_errno(), g_get_strerror()); } g_tcp_close(g_sck); }