struct session *connect_session(struct session *ses) { int sock; push_call("connection_session(%p)",ses); ses->connect_retry = utime() + gts->connect_retry; reconnect: sock = connect_mud(ses, ses->host, ses->port); if (sock == -1) { cleanup_session(ses); pop_call(); return NULL; } if (sock) { gtd->ses = ses; ses->socket = sock; ses->connect_retry = 0; SET_BIT(ses->flags, SES_FLAG_CONNECTED); tintin_printf2(ses, ""); tintin_printf(ses, "#SESSION '%s' CONNECTED TO '%s' PORT '%s'", ses->name, ses->host, ses->port); check_all_events(ses, SUB_ARG|SUB_SEC, 0, 4, "SESSION CONNECTED", ses->name, ses->host, ses->ip, ses->port); pop_call(); return ses; } if (ses->connect_retry > utime()) { goto reconnect; } if (ses->connect_error) { tintin_printf(ses, "#SESSION '%s' FAILED TO CONNECT.", ses->name); } cleanup_session(ses); pop_call(); return NULL; }
static void pingconn(evutil_socket_t s, short evt, void *raw) { session *sess = raw; if(nghttp2_submit_ping(sess->S.h2sess, NGHTTP2_FLAG_NONE, NULL) || nghttp2_session_send(sess->S.h2sess)) { fprintf(stderr, "Ping failed\n"); cleanup_session(&sess->S); } }
nu_error_t delete_client_by_socket_ext(int socket, int use_lock) { gpointer key; user_session_t *session; nu_error_t ret; if (use_lock) { lock_client_datas(); } session = (user_session_t *) (g_hash_table_lookup(client_conn_hash, GINT_TO_POINTER(socket))); if (!session) { log_message(WARNING, DEBUG_AREA_USER, "Could not find user session in hash"); if (use_lock) unlock_client_datas(); return NU_EXIT_ERROR; } ev_io_stop(session->srv_context->loop, &session->client_watcher); ret = cleanup_session(session); if (ret != NU_EXIT_OK) { if (use_lock) unlock_client_datas(); return ret; } if (shutdown(socket, SHUT_RDWR) != 0) { log_message(VERBOSE_DEBUG, DEBUG_AREA_USER, "Could not shutdown socket: %s", strerror(errno)); } if (close(socket) != 0) { log_message(VERBOSE_DEBUG, DEBUG_AREA_USER, "Could not close socket: %s", strerror(errno)); } key = GINT_TO_POINTER(session->socket); g_hash_table_remove(client_conn_hash, key); if (use_lock) { unlock_client_datas(); } return NU_EXIT_OK; }
static void newconn(struct evconnlistener *lev, evutil_socket_t sock, struct sockaddr *cli, int socklen, void *raw) { server *serv = raw; session *sess; printf("New client\n"); sess = calloc(1, sizeof(*sess)); if(sess) { sess->serv = serv; sess->S.build_stream = buildstream; sess->S.cleanup = &cleanup_session; /* periodic timer */ sess->pingtimer = event_new(serv->base, -1, EV_PERSIST, pingconn, sess); assert(sess->pingtimer); sess->S.bev = bufferevent_socket_new(serv->base, sock, BEV_OPT_CLOSE_ON_FREE); if(sess->S.bev) { h2session_setup_bev(&sess->S); bufferevent_enable(sess->S.bev, EV_READ); if(prepare_h2_session(sess)) { bufferevent_free(sess->S.bev); free(sess); printf("Client failed\n"); return; } else { nghttp2_settings_entry iv[] = { {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}, {NGHTTP2_SETTINGS_ENABLE_PUSH, 0} }; int rv; if ((rv=nghttp2_submit_settings(sess->S.h2sess, NGHTTP2_FLAG_NONE, iv, ARRLEN(iv))) || (rv=nghttp2_session_send(sess->S.h2sess))) { printf("submit error: %s", nghttp2_strerror(rv)); cleanup_session(&sess->S); } else { const struct timeval itvl = {5,0}; printf("Connection ready\n"); evtimer_add(sess->pingtimer, &itvl); } } } } if(!sess || !sess->S.bev) { fprintf(stderr, "No memory\n"); free(sess); close(sock); return; } }
static nu_error_t delete_client_by_session(user_session_t * session) { nu_error_t ret; ev_io_stop(session->srv_context->loop, &session->client_watcher); ret = cleanup_session(session); if (ret != NU_EXIT_OK) { return ret; } return NU_EXIT_OK; }
void write_line_mud(struct session *ses, char *line, int size) { static int retry; push_call("write_line_mud(%p,%p)",line,ses); if (ses == gts) { tintin_printf2(ses, "#NO SESSION ACTIVE. USE: %csession {name} {host} {port} TO START ONE.", gtd->tintin_char); pop_call(); return; } if (!HAS_BIT(ses->flags, SES_FLAG_CONNECTED)) { tintin_printf2(ses, "#THIS SESSION IS NOT CONNECTED."); pop_call(); return; } if (write(ses->socket, line, size) == -1) { if (retry++ < 10) { usleep(100000); write_line_mud(ses, line, size); pop_call(); return; } perror("write in write_line_mud"); cleanup_session(ses); pop_call(); return; } retry = 0; check_all_events(ses, SUB_ARG|SUB_SEC, 0, 1, "SEND OUTPUT", line); pop_call(); return; }
void quitmsg(char *message) { struct session *ses; SET_BIT(gtd->flags, TINTIN_FLAG_TERMINATE); while ((ses = gts->next) != NULL) { cleanup_session(ses); } if (gtd->chat) { close(gtd->chat->fd); } check_all_events(gts, SUB_ARG|SUB_SEC, 0, 0, "PROGRAM TERMINATION"); /* if (gtd->history_size) { char filename[BUFFER_SIZE]; sprintf(filename, "%s/%s", getenv("HOME"), HISTORY_FILE); history_write(gts, filename); } */ restore_terminal(); clean_screen(gts); if (message) { printf("\n%s\n", message); } printf("\nGoodbye from TinTin++\n\n"); fflush(NULL); exit(0); }
static int session_policy_config_cb(struct connman_session *session, struct connman_session_config *config, void *user_data, int err) { struct creation_data *creation_data = user_data; struct session_info *info, *info_last; DBusMessage *reply; DBG("session %p config %p", session, config); if (err < 0) goto err; session->policy_config = config; session->mark = session_mark++; session->index = -1; err = init_firewall_session(session); if (err < 0) goto err; err = init_routing_table(session); if (err < 0) goto err; info = session->info; info_last = session->info_last; if (session->policy_config->ecall) ecall_session = session; info->state = CONNMAN_SESSION_STATE_DISCONNECTED; info->config.type = apply_policy_on_type( session->policy_config->type, creation_data->type); info->config.priority = session->policy_config->priority; info->config.roaming_policy = session->policy_config->roaming_policy; session->user_allowed_bearers = creation_data->allowed_bearers; creation_data->allowed_bearers = NULL; apply_policy_on_bearers( session->policy_config->allowed_bearers, session->user_allowed_bearers, &info->config.allowed_bearers); g_hash_table_replace(session_hash, session->session_path, session); DBG("add %s", session->session_path); if (!g_dbus_register_interface(connection, session->session_path, CONNMAN_SESSION_INTERFACE, session_methods, NULL, NULL, session, NULL)) { connman_error("Failed to register %s", session->session_path); g_hash_table_remove(session_hash, session->session_path); err = -EINVAL; goto err; } reply = g_dbus_create_reply(creation_data->pending, DBUS_TYPE_OBJECT_PATH, &session->session_path, DBUS_TYPE_INVALID); g_dbus_send_message(connection, reply); creation_data->pending = NULL; info_last->state = info->state; info_last->config.priority = info->config.priority; info_last->config.roaming_policy = info->config.roaming_policy; info_last->config.allowed_bearers = info->config.allowed_bearers; session->append_all = true; cleanup_creation_data(creation_data); session_activate(session); return 0; err: reply = __connman_error_failed(creation_data->pending, -err); g_dbus_send_message(connection, reply); creation_data->pending = NULL; cleanup_session(session); cleanup_creation_data(creation_data); return err; }
int main(int argc, char **argv) { int ret; char tmppath[] = "/tmp/lttng-rotate-XXXXXX"; char *session_name, *path, *ext_program; int delay, nr; if (argc != 5) { usage(argv[0]); ret = -1; goto end; } session_name = argv[1]; delay = atoi(argv[2]); nr = atoi(argv[3]); ext_program = argv[4]; if (delay < 0) { fprintf(stderr, "delay-sec must be a positive values\n"); ret = -1; goto end; } if (signal(SIGINT, sighandler) == SIG_ERR) { perror("signal handler"); goto end; } path = mkdtemp(tmppath); if (!path) { fprintf(stderr, "Failed to create temporary path\n"); } printf("Output directory: %s\n", path); ret = setup_session(session_name, path); if (ret) { goto end_cleanup_dir; } if (nr > 0) { unsigned int sleep_time; int i; for (i = 0; i < nr; i++) { ret = rotate_session(session_name, ext_program); if (ret) { goto end_cleanup; } sleep_time = delay; while (sleep_time > 0) { sleep_time = sleep(sleep_time); } } } else { for(;;) { if (quit) { break; } ret = rotate_session(session_name, ext_program); if (ret) { goto end_cleanup; } sleep(delay); } } end_cleanup: ret = cleanup_session(session_name); if (ret) { goto end; } end_cleanup_dir: ret = cleanup_dir(path); end: return ret; }