void controller_kill_all_sessions() { session_entry_t *entry = first_session; while(entry) { if(!entry->session->is_shutdown) session_kill(entry->session); entry = entry->next; } }
void DEFAULT_CC sig_sesman_session_end(int sig) { int pid; if (g_getpid() != g_pid) { return; } pid = g_waitchild(); if (pid > 0) { session_kill(pid); } }
void controller_destroy() { session_entry_t *prev_session = NULL; session_entry_t *entry = first_session; while(entry) { if(!entry->session->is_shutdown) session_kill(entry->session); session_destroy(entry->session); prev_session = entry; entry = entry->next; safe_free(prev_session); } }
void session_destroy(session_t *session) { if(!session->is_shutdown) session_kill(session); if(session->name) safe_free(session->name); if(session->driver) driver_destroy(session->driver); if(session->outgoing_buffer) buffer_destroy(session->outgoing_buffer); safe_free(session); }
static void kill_ignored_sessions() { session_entry_t *entry = first_session; if(max_retransmits < 0) return; while(entry) { if(!entry->session->is_shutdown && entry->session->missed_transmissions > max_retransmits) { LOG_ERROR("The server hasn't returned a valid response in the last %d attempts.. closing session.", entry->session->missed_transmissions - 1); session_kill(entry->session); } entry = entry->next; } }
/* Polls the driver for data and puts it in our own buffer. This is necessary * because the session needs to ACK data and such. */ static void poll_for_data(session_t *session) { size_t length = -1; /* Read all the data we can. */ uint8_t *data = driver_get_outgoing(session->driver, &length, -1); /* If a driver returns NULL, it means it's done - once the driver is * done and all our data is sent, go into 'shutdown' mode. */ if(!data) { if(buffer_get_remaining_bytes(session->outgoing_buffer) == 0) session_kill(session); } else { if(length) buffer_add_bytes(session->outgoing_buffer, data, length); safe_free(data); } }
static DBusHandlerResult session_message_dispatch( Session *s, DBusConnection *connection, DBusMessage *message) { const BusProperty properties[] = { { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id }, { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s }, { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name }, { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec, "t", &s->timestamp.realtime }, { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec, "t", &s->timestamp.monotonic }, { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path }, { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr }, { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s }, { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty }, { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display }, { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote }, { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user }, { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host }, { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service }, { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader }, { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id }, { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type }, { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s }, { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers }, { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers }, { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes }, { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint, "b", s }, { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since, "t", s }, { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s }, { NULL, NULL, NULL, NULL, NULL } }; DBusError error; DBusMessage *reply = NULL; int r; assert(s); assert(connection); assert(message); dbus_error_init(&error); if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) { r = session_stop(s); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); reply = dbus_message_new_method_return(message); if (!reply) goto oom; } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) { r = session_activate(s); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); reply = dbus_message_new_method_return(message); if (!reply) goto oom; } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") || dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) { if (session_send_signal(s, streq(dbus_message_get_member(message), "Lock")) < 0) goto oom; reply = dbus_message_new_method_return(message); if (!reply) goto oom; } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) { dbus_bool_t b; unsigned long ul; if (!dbus_message_get_args( message, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID)) return bus_send_error_reply(connection, message, &error, -EINVAL); ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error); if (ul == (unsigned long) -1) return bus_send_error_reply(connection, message, &error, -EIO); if (ul != 0 && ul != s->user->uid) return bus_send_error_reply(connection, message, NULL, -EPERM); session_set_idle_hint(s, b); reply = dbus_message_new_method_return(message); if (!reply) goto oom; } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) { const char *swho; int32_t signo; KillWho who; if (!dbus_message_get_args( message, &error, DBUS_TYPE_STRING, &swho, DBUS_TYPE_INT32, &signo, DBUS_TYPE_INVALID)) return bus_send_error_reply(connection, message, &error, -EINVAL); if (isempty(swho)) who = KILL_ALL; else { who = kill_who_from_string(swho); if (who < 0) return bus_send_error_reply(connection, message, &error, -EINVAL); } if (signo <= 0 || signo >= _NSIG) return bus_send_error_reply(connection, message, &error, -EINVAL); r = session_kill(s, who, signo); if (r < 0) return bus_send_error_reply(connection, message, NULL, r); reply = dbus_message_new_method_return(message); if (!reply) goto oom; } else return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); if (reply) { if (!dbus_connection_send(connection, reply, NULL)) goto oom; dbus_message_unref(reply); } return DBUS_HANDLER_RESULT_HANDLED; oom: if (reply) dbus_message_unref(reply); dbus_error_free(&error); return DBUS_HANDLER_RESULT_NEED_MEMORY; }
NBBOOL session_data_incoming(session_t *session, uint8_t *data, size_t length) { /* Parse the packet to get the session id */ packet_t *packet = packet_parse(data, length, session->options); /* Set to TRUE if data was properly ACKed and we should send more right away. */ NBBOOL send_right_away = FALSE; /* Suck in any data we can from the driver. */ poll_for_data(session); /* Print packet data if we're supposed to. */ if(packet_trace) { printf("INCOMING: "); packet_print(packet, session->options); } if(session->is_ping) { /* This only returns if the receive is bad. */ driver_data_received(session->driver, (uint8_t*)packet->body.ping.data, strlen(packet->body.ping.data)); } else { switch(session->state) { case SESSION_STATE_NEW: if(packet->packet_type == PACKET_TYPE_SYN) { LOG_INFO("In SESSION_STATE_NEW, received SYN (ISN = 0x%04x)", packet->body.syn.seq); session->their_seq = packet->body.syn.seq; session->options = (options_t) packet->body.syn.options; session->state = SESSION_STATE_ESTABLISHED; /* Since we established a valid session, we can send stuff right away. */ session->last_transmit = 0; session->missed_transmissions = 0; send_right_away = TRUE; } else if(packet->packet_type == PACKET_TYPE_MSG) { LOG_WARNING("In SESSION_STATE_NEW, received unexpected MSG (ignoring)"); } else if(packet->packet_type == PACKET_TYPE_FIN) { /* TODO: I shouldn't exit here. */ LOG_FATAL("In SESSION_STATE_NEW, received FIN: %s", packet->body.fin.reason); exit(0); } else { /* TODO: I shouldn't exit here. */ LOG_FATAL("Unknown packet type: 0x%02x", packet->packet_type); exit(1); } break; case SESSION_STATE_ESTABLISHED: if(packet->packet_type == PACKET_TYPE_SYN) { LOG_WARNING("In SESSION_STATE_ESTABLISHED, recieved SYN (ignoring)"); } else if(packet->packet_type == PACKET_TYPE_MSG) { LOG_INFO("In SESSION_STATE_ESTABLISHED, received a MSG"); /* Validate the SEQ */ if(packet->body.msg.options.normal.seq == session->their_seq) { /* Verify the ACK is sane */ uint16_t bytes_acked = packet->body.msg.options.normal.ack - session->my_seq; /* If there's still bytes waiting in the buffer.. */ if(bytes_acked <= buffer_get_remaining_bytes(session->outgoing_buffer)) { /* Since we got a valid response back, the connection isn't dying. */ session->missed_transmissions = 0; /* Reset the retransmit counter since we got some valid data. */ if(bytes_acked > 0) { /* Only reset the counter if we want to re-transmit * right away. */ if(transmit_instantly_on_data) { session->last_transmit = 0; session->missed_transmissions = 0; send_right_away = TRUE; } } /* Increment their sequence number */ session->their_seq = (session->their_seq + packet->body.msg.data_length) & 0xFFFF; /* Remove the acknowledged data from the buffer */ buffer_consume(session->outgoing_buffer, bytes_acked); /* Increment my sequence number */ if(bytes_acked != 0) { session->my_seq = (session->my_seq + bytes_acked) & 0xFFFF; } /* Print the data, if we received any, and then immediately receive more. */ if(packet->body.msg.data_length > 0) { driver_data_received(session->driver, packet->body.msg.data, packet->body.msg.data_length); } } else { LOG_WARNING("Bad ACK received (%d bytes acked; %d bytes in the buffer)", bytes_acked, buffer_get_remaining_bytes(session->outgoing_buffer)); } } else { LOG_WARNING("Bad SEQ received (Expected %d, received %d)", session->their_seq, packet->body.msg.options.normal.seq); } } else if(packet->packet_type == PACKET_TYPE_FIN) { LOG_FATAL("In SESSION_STATE_ESTABLISHED, received FIN: %s - closing session", packet->body.fin.reason); session->last_transmit = 0; session->missed_transmissions = 0; session_kill(session); } else { LOG_FATAL("Unknown packet type: 0x%02x - closing session", packet->packet_type); session_kill(session); } break; default: LOG_FATAL("Wound up in an unknown state: 0x%x", session->state); packet_destroy(packet); session_kill(session); exit(1); } } packet_destroy(packet); return send_right_away; }