for (z = 0; z < num_domains; z++) { struct client *client = domains[z].clients; unsigned w;  for (w = 0; w < domains[z].num_clients; w++, client++) { close(client->sockfd); } }
for (z = 0; z < num_domains; z++) while (domains[z].num_clients > 0) kill_client(&domains[z], 0);
int main(int argc, char **argv) {    struct config config;    int ret, sockfd;    struct domain domains[MAX_DOMAINS];    unsigned num_domains = 1, z, next_client_id = 1;    struct sigaction sa;    fd_set rfds;    read_config(&config, argc, argv);    /* create domain 0 */    memset(domains, 0, sizeof(domains[0]));    /* server socket stuff */    sockfd = socket(PF_INET, SOCK_STREAM, 0);    if (sockfd < 0) {        fprintf(stderr, "failed to create socket: %s\n",                strerror(errno));        exit(1);    }    ret = bind(sockfd, config.bind_address->ai_addr,               config.bind_address->ai_addrlen);    if (ret < 0) {        fprintf(stderr, "failed to bind: %s\n",                strerror(errno));        exit(1);    }    ret = listen(sockfd, 4);    if (ret < 0) {        fprintf(stderr, "listen failed: %s\n",                strerror(errno));        exit(1);    }    /* signals */    memset(&sa, 0, sizeof(sa));    sa.sa_handler = exit_signal_handler;    sigaction(SIGHUP, &sa, NULL);    sigaction(SIGTERM, &sa, NULL);    sigaction(SIGINT, &sa, NULL);    sigaction(SIGQUIT, &sa, NULL);    sa.sa_handler = SIG_IGN;    sigaction(SIGUSR1, &sa, NULL);    sigaction(SIGUSR2, &sa, NULL);    /* main loop */    do {        int max_fd, i;        /* select() on all sockets */        FD_ZERO(&rfds);        FD_SET(sockfd, &rfds);        max_fd = sockfd;        for (i = 0; i < (int)num_domains; i++) {            struct client *client = domains[i].clients;            int w;            for (w = 0; w < (int)domains[i].num_clients; w++, client++) {                if (client->should_destroy) {                    kill_client(&domains[i], w--);                    client--;                    continue;                }                FD_SET(client->sockfd, &rfds);                if (client->sockfd > max_fd)                    max_fd = client->sockfd;            }            if (i > 0 && domains[i].num_clients == 0) {                /* empty domain, delete it */                num_domains--;                if (i < (int)num_domains)                    memmove(domains + i, domains + i + 1,                            sizeof(domains[i]) * (num_domains - i));                i--;            }        }        ret = select(max_fd + 1, &rfds, NULL, NULL, NULL);        if (ret < 0 && errno != EINTR) {            fprintf(stderr, "select failed: %s\n", strerror(errno));            break;        }        if (ret == 0) {            fprintf(stderr, "select returned zero\n");            sleep(1);        }        /* read on all sockets where FD_ISSET is true */        if (FD_ISSET(sockfd, &rfds)) {            struct sockaddr addr;            socklen_t addrlen = sizeof(addr);            ret = accept(sockfd, &addr, &addrlen);            if (ret >= 0) {                create_client(&domains[0], ret, next_client_id++);            } else {                fprintf(stderr, "accept failed: %s\n", strerror(errno));            }        }        for (z = 0; z < num_domains; z++) {            struct client *client = domains[z].clients;            unsigned w;            for (w = 0; w < domains[z].num_clients; w++, client++) {                if (FD_ISSET(client->sockfd, &rfds))                    client_data_available(client);            }        }    } while (!should_exit);    /* cleanup */    close(sockfd);    for (z = 0; z < num_domains; z++) {        struct client *client = domains[z].clients;        unsigned w;        for (w = 0; w < domains[z].num_clients; w++, client++) {            close(client->sockfd);        }    }    free_config(&config);}