aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-01-21 22:47:31 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-01-21 22:47:31 +0000
commit392e13534961b80984711f82230d9f34ca4a81bb (patch)
treea8f40eaba5b6b9e637fa6f469de8add9caea35f0 /server.c
parent9acc26711d180e45ff827d8b88b4adbf20fab949 (diff)
downloadrtmux-392e13534961b80984711f82230d9f34ca4a81bb.tar.gz
rtmux-392e13534961b80984711f82230d9f34ca4a81bb.tar.bz2
rtmux-392e13534961b80984711f82230d9f34ca4a81bb.zip
Handle SIGTERM (and kill-server which uses it), a bit more neatly - tidy up
properly and print a nicer message. Same effect though :-)
Diffstat (limited to 'server.c')
-rw-r--r--server.c43
1 files changed, 40 insertions, 3 deletions
diff --git a/server.c b/server.c
index fd2d5cd5..8a577b04 100644
--- a/server.c
+++ b/server.c
@@ -1,4 +1,4 @@
-/* $Id: server.c,v 1.110 2009-01-20 19:35:03 nicm Exp $ */
+/* $Id: server.c,v 1.111 2009-01-21 22:47:31 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -43,6 +43,7 @@
struct clients clients;
int server_main(const char *, int);
+void server_shutdown(void);
void server_fill_windows(struct pollfd **);
void server_handle_windows(struct pollfd **);
void server_fill_clients(struct pollfd **);
@@ -224,7 +225,11 @@ server_main(const char *srv_path, int srv_fd)
last = time(NULL);
pfds = NULL;
- while (!sigterm) {
+ for (;;) {
+ /* If sigterm, kill all windows and clients. */
+ if (sigterm)
+ server_shutdown();
+
/* Initialise pollfd array. */
nfds = 1;
for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
@@ -247,7 +252,7 @@ server_main(const char *srv_path, int srv_fd)
/* Update socket permissions. */
xtimeout = INFTIM;
- if (server_update_socket(srv_path) != 0)
+ if (sigterm || server_update_socket(srv_path) != 0)
xtimeout = 100;
/* Do the poll. */
@@ -326,6 +331,34 @@ server_main(const char *srv_path, int srv_fd)
return (0);
}
+/* Kill all clients. */
+void
+server_shutdown(void)
+{
+ struct session *s;
+ struct client *c;
+ u_int i;
+
+ for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
+ s = ARRAY_ITEM(&sessions, i);
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ c = ARRAY_ITEM(&clients, i);
+ if (c != NULL && c->session == s) {
+ s = NULL;
+ break;
+ }
+ }
+ if (s != NULL)
+ session_destroy(s);
+ }
+
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ c = ARRAY_ITEM(&clients, i);
+ if (c != NULL)
+ server_write_client(c, MSG_SHUTDOWN, NULL, 0);
+ }
+}
+
/* Fill window pollfds. */
void
server_fill_windows(struct pollfd **pfd)
@@ -575,6 +608,10 @@ server_accept_client(int srv_fd)
return (NULL);
fatal("accept failed");
}
+ if (sigterm) {
+ close(fd);
+ return (NULL);
+ }
return (server_create_client(fd));
}