5 #include <sys/socket.h>
12 session_info
*sessionlist
= NULL
;
13 int session_count
= 0;
14 pthread_mutex_t sessionlist_mutex
= PTHREAD_MUTEX_INITIALIZER
;
18 session_info
* add_session (int sock
)
20 session_info
*ps
= (session_info
*)malloc(sizeof(session_info
));
21 if (ps
== NULL
) return NULL
;
35 ps
->observer_count
= 0;
37 pthread_mutex_init (&ps
->mutex
, NULL
);
38 ps
->last_activity
= time(NULL
);
41 pthread_mutex_lock (&sessionlist_mutex
);
44 if (sessionlist
== NULL
)
50 session_info
*p
= sessionlist
;
51 while (p
->next
!= NULL
) p
= p
->next
;
57 pthread_mutex_unlock (&sessionlist_mutex
);
64 void remove_session (session_info
*sess
)
68 pthread_mutex_lock (&sessionlist_mutex
);
69 pthread_mutex_lock (&sess
->mutex
);
71 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
73 // remove sess from the list
76 if (s
->next
) s
->next
->prev
= s
->prev
;
77 if (s
->prev
) s
->prev
->next
= s
->next
;
79 if (sessionlist
== sess
) sessionlist
= sess
->next
;
82 // remove socket from observers
86 // no need to lock s->mutex because add_observer() is blocked by sessionlist_mutex
87 for (i
= 0; i
< s
->observer_count
; i
++)
89 if (s
->observers
[i
] == sess
->socket
)
91 s
->observers
[i
--] = s
->observers
[s
->observer_count
--];
97 pthread_mutex_unlock (&sessionlist_mutex
);
99 pthread_mutex_unlock (&sess
->mutex
);
100 pthread_mutex_destroy (&sess
->mutex
);
107 int user_is_connected (int id
)
111 pthread_mutex_lock (&sessionlist_mutex
);
113 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
115 if (s
->user_id
== id
)
117 pthread_mutex_unlock (&sessionlist_mutex
);
122 pthread_mutex_unlock (&sessionlist_mutex
);
128 void stop_observing (session_info
*sess
)
132 pthread_mutex_lock (&sessionlist_mutex
);
134 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
136 // remove socket from observers
138 // no need to lock s->mutex because add_observer() is blocked by sessionlist_mutex
139 for (i
= 0; i
< s
->observer_count
; i
++)
141 if (s
->observers
[i
] == sess
->socket
)
143 s
->observers
[i
--] = s
->observers
[--s
->observer_count
];
148 pthread_mutex_unlock (&sessionlist_mutex
);
153 int add_observer (int n
, int sock
)
158 pthread_mutex_lock (&sessionlist_mutex
);
160 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
162 if (s
->game
[0] == 0 || s
->observer_count
== -1) continue;
163 if (i
++ != n
) continue;
165 pthread_mutex_lock (&s
->mutex
);
166 s
->observers
= (int*)realloc(s
->observers
, (s
->observer_count
+ 1) * sizeof(int));
167 s
->observers
[s
->observer_count
++] = sock
;
168 pthread_mutex_unlock (&s
->mutex
);
173 if (s
->term
!= NULL
) term_copy_data (s
->term
, sock
);
175 pthread_mutex_unlock (&sessionlist_mutex
);
181 void close_sessions (void)
184 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
186 if (s
->pty_master
!= -1) close (s
->pty_master
);
193 void release_sessions (void)
196 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
199 pthread_mutex_destroy (&s
->mutex
);
205 void terminate_games (void)
210 pthread_mutex_lock (&sessionlist_mutex
);
212 for (s
= sessionlist
; s
!= NULL
; s
= s
->next
)
214 if (!s
->child_pid
) continue;
215 kill (s
->child_pid
, SIGTERM
);
218 pthread_mutex_unlock (&sessionlist_mutex
);