/* * SIPB service monitor: status report * * Nickolai Zeldovich * February 2002 */ #include #include "sipbmon.h" static void status_close (int s, bool ok) { close (s); } static void status_accept (struct sm_service *svc, int fd) { sockaddr_in sin; bzero (&sin, sizeof (sin)); socklen_t sinlen = sizeof (sin); int s = accept (fd, (sockaddr *) &sin, &sinlen); if (s < 0) return; make_async (s); strbuf st ("%-20s %-5s %-17s %s\n", "Host", "Port", "Service", "Status"); while (svc) { st << strbuf ("%-20s %-5d %-17s %s%s\n", svc->host.cstr (), svc->port, svc->name.cstr (), svc->up ? "up" : "down: ", svc->up ? "" : svc->errmsg.cstr ()); svc = svc->next; } net_write (s, st.tosuio (), wrap (&status_close, s)); } void status_init (int port, struct sm_service *svc_head) { int fd = inetsocket (SOCK_STREAM, port, INADDR_ANY); if (fd < 0) fatal ("could not bind TCP port %d: %m\n", port); close_on_exec (fd); listen (fd, 5); fdcb (fd, selread, wrap (&status_accept, svc_head, fd)); }