/** * * @brief checks if there's a server running on a display * @param display the display to check * @return 0 if there isn't a display running, nonzero otherwise * */ static int DEFAULT_CC x_server_running_check_ports(int display) { char text[256]; int x_running; int sck; g_sprintf(text, "/tmp/.X11-unix/X%d", display); x_running = g_file_exist(text); if (!x_running) { g_sprintf(text, "/tmp/.X%d-lock", display); x_running = g_file_exist(text); } if (!x_running) /* check 59xx */ { sck = g_tcp_socket(); g_sprintf(text, "59%2.2d", display); x_running = g_tcp_bind(sck, text); g_tcp_close(sck); } if (!x_running) /* check 60xx */ { sck = g_tcp_socket(); g_sprintf(text, "60%2.2d", display); x_running = g_tcp_bind(sck, text); g_tcp_close(sck); } if (!x_running) /* check 62xx */ { sck = g_tcp_socket(); g_sprintf(text, "62%2.2d", display); x_running = g_tcp_bind(sck, text); g_tcp_close(sck); } return x_running; }
int APP_CC trans_listen(struct trans* self, char* port) { if (self->sck != 0) { g_tcp_close(self->sck); } self->sck = g_tcp_socket(); g_tcp_set_non_blocking(self->sck); if (g_tcp_bind(self->sck, port) == 0) { if (g_tcp_listen(self->sck) == 0) { self->status = 1; /* ok */ self->type1 = 1; /* listener */ return 0; } } return 1; }
/* 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; }