summaryrefslogtreecommitdiffstats
path: root/hw4/chttpd/chttpd-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw4/chttpd/chttpd-server.c')
-rw-r--r--hw4/chttpd/chttpd-server.c113
1 files changed, 57 insertions, 56 deletions
diff --git a/hw4/chttpd/chttpd-server.c b/hw4/chttpd/chttpd-server.c
index 7d540d0..ba1eea6 100644
--- a/hw4/chttpd/chttpd-server.c
+++ b/hw4/chttpd/chttpd-server.c
@@ -273,7 +273,7 @@ static void chttpd_main_loop (ChttpdLog* hlog, LbsListMeta* slist) {
/* Check whether we are going to shutdown the server */
if (server_shutdown) {
- server_shutdown = 0;
+ server_shutdown++;
pthread_mutex_lock (&chttpd_server_notify_mutex);
chttpd_server_notify = 1;
pthread_mutex_unlock (&chttpd_server_notify_mutex);
@@ -336,68 +336,69 @@ static void chttpd_main_loop (ChttpdLog* hlog, LbsListMeta* slist) {
nfds = 0;
}
-
/* Log current server info */
- chttpd_log_write_str (hlog, "[Server status change notification]");
+ if (server_notify || server_shutdown < 1) {
+ chttpd_log_write_str (hlog, "[Server status change notification]");
- unsigned long long count;
- pthread_rwlock_rdlock (&chttpd_server_count_lock);
- count = chttpd_server_count;
- pthread_rwlock_unlock (&chttpd_server_count_lock);
+ unsigned long long count;
+ pthread_rwlock_rdlock (&chttpd_server_count_lock);
+ count = chttpd_server_count;
+ pthread_rwlock_unlock (&chttpd_server_count_lock);
- chttpd_log_write (hlog, "%lld connections has been accepted", count);
+ chttpd_log_write (hlog, "%lld connections has been accepted", count);
- unsigned c = 0;
- for (iter = slist->first; iter != NULL; iter = iter->next, c++) {
- ChttpdServer* server = iter->data;
- struct sockaddr_storage addr;
- char* type;
- size_t conn_count;
-
- pthread_rwlock_rdlock (&server->lock);
- addr = server->addr;
- type = server->attr_admin ? "ADMIN" : "HTTP";
- conn_count = server->conn->len;
- pthread_rwlock_unlock (&server->lock);
+ unsigned c = 0;
+ for (iter = slist->first; iter != NULL; iter = iter->next, c++) {
+ ChttpdServer* server = iter->data;
+ struct sockaddr_storage addr;
+ char* type;
+ size_t conn_count;
+
+ pthread_rwlock_rdlock (&server->lock);
+ addr = server->addr;
+ type = server->attr_admin ? "ADMIN" : "HTTP";
+ conn_count = server->conn->len;
+ pthread_rwlock_unlock (&server->lock);
- char ipstr[INET6_ADDRSTRLEN];
- _Static_assert (INET6_ADDRSTRLEN >= INET_ADDRSTRLEN,
- "Why IPv6 address is shorter than IPv4 address?");
- switch (addr.ss_family) {
- case AF_UNIX:
- CHTTPD_SOCKET_SOCKADDR_UN_SET_NULL (SOCKADDR_UN (&addr));
- chttpd_log_write (hlog,
- "Server %u: type %s, UNIX socket, path %s"
- " (%zu active connections)",
- c, type, SOCKADDR_UN (&addr)->sun_path, conn_count);
- break;
- case AF_INET:
- if (!inet_ntop (AF_INET, &(SOCKADDR_IN (&addr)->sin_addr),
- ipstr, INET_ADDRSTRLEN)) {
- strcpy (ipstr, "unknown");
- }
- chttpd_log_write (hlog, "Server %u: type %s, "
- "IPv4 socket, address %s, port %" PRIu16
- " (%zu active connections)", c, type, ipstr,
- ntohs (SOCKADDR_IN (&addr)->sin_port), conn_count);
- break;
- case AF_INET6:
- if (!inet_ntop (AF_INET6, &(SOCKADDR_IN6 (&addr)->sin6_addr),
- ipstr, INET6_ADDRSTRLEN)) {
- strcpy (ipstr, "unknown");
- }
- chttpd_log_write (hlog, "Server %u: type %s, "
- "IPv6 socket, address %s, port %" PRIu16
- " (%zu active connections)", c, type, ipstr,
- ntohs (SOCKADDR_IN6 (&addr)->sin6_port), conn_count);
- break;
- default:
- chttpd_log_write (hlog,
- "Server %u: type %s, unknown socket", c, type);
- break;
+ char ipstr[INET6_ADDRSTRLEN];
+ _Static_assert (INET6_ADDRSTRLEN >= INET_ADDRSTRLEN,
+ "Why IPv6 address is shorter than IPv4 address?");
+ switch (addr.ss_family) {
+ case AF_UNIX:
+ CHTTPD_SOCKET_SOCKADDR_UN_SET_NULL (SOCKADDR_UN (&addr));
+ chttpd_log_write (hlog,
+ "Server %u: type %s, UNIX socket, path %s"
+ " (%zu active connections)",
+ c, type, SOCKADDR_UN (&addr)->sun_path, conn_count);
+ break;
+ case AF_INET:
+ if (!inet_ntop (AF_INET, &(SOCKADDR_IN (&addr)->sin_addr),
+ ipstr, INET_ADDRSTRLEN)) {
+ strcpy (ipstr, "unknown");
+ }
+ chttpd_log_write (hlog, "Server %u: type %s, "
+ "IPv4 socket, address %s, port %" PRIu16
+ " (%zu active connections)", c, type, ipstr,
+ ntohs (SOCKADDR_IN (&addr)->sin_port), conn_count);
+ break;
+ case AF_INET6:
+ if (!inet_ntop (AF_INET6, &(SOCKADDR_IN6 (&addr)->sin6_addr),
+ ipstr, INET6_ADDRSTRLEN)) {
+ strcpy (ipstr, "unknown");
+ }
+ chttpd_log_write (hlog, "Server %u: type %s, "
+ "IPv6 socket, address %s, port %" PRIu16
+ " (%zu active connections)", c, type, ipstr,
+ ntohs (SOCKADDR_IN6 (&addr)->sin6_port), conn_count);
+ break;
+ default:
+ chttpd_log_write (hlog,
+ "Server %u: type %s, unknown socket", c, type);
+ break;
+ }
}
+ chttpd_log_write (hlog, "%u servers are active", slist->len);
}
- chttpd_log_write (hlog, "%u servers are active", slist->len);
} else {
pthread_mutex_unlock (&chttpd_server_notify_mutex);
}