int main(int argc, const char * argv[]) { // handling zombie processes signal(SIGCHLD, handleSIGCHLD); srand(time(0)); init_client_list(); init_global_pipe_list(); init_message_boxes(); struct sockaddr_in my_addr; struct sockaddr_in client_addr; int port = 2000 + rand() % 100; printf("Port : %d\n", port); char port_str[5]; sprintf(port_str, "%d", port); int sc_fd = passivesock(port_str, "tcp", 5); printf("accepting.....\n"); int addrlen = sizeof(client_addr); while(1) { int new_client_sock = accept(sc_fd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen); int pid = fork(); if(pid == 0) { // child client_node_t* client = create_client_node(new_client_sock, "CGILAB"/*inet_ntoa(client_addr.sin_addr)*/, 511/*ntohs(client_addr.sin_port)*/); close(sc_fd); if(insert_to_client_list(client) != -1) { serve(client); close(new_client_sock); return 0; } } else { close(new_client_sock); } } return 0; }
void lgssd_run() { int ret; struct sigaction dn_act; int fd; time_t child_check = 0; pid_t child_pid; /* Taken from linux/Documentation/dnotify.txt: */ dn_act.sa_sigaction = dir_notify_handler; sigemptyset(&dn_act.sa_mask); dn_act.sa_flags = SA_SIGINFO; sigaction(DNOTIFY_SIGNAL, &dn_act, NULL); if ((fd = open(pipefs_dir, O_RDONLY)) == -1) { printerr(0, "ERROR: failed to open %s: %s\n", pipefs_dir, strerror(errno)); return; } fcntl(fd, F_SETSIG, DNOTIFY_SIGNAL); fcntl(fd, F_NOTIFY, DN_CREATE|DN_DELETE|DN_MODIFY|DN_MULTISHOT); init_client_list(); while (1) { while (dir_changed) { dir_changed = 0; printerr(2, "pipefs root dir changed\n"); if (update_client_list()) { printerr(0, "ERROR: couldn't update " "client list\n"); goto out; } } /* every 5s cleanup possible zombies of child processes */ if (time(NULL) - child_check >= 5) { printerr(3, "check zombie children...\n"); while (1) { child_pid = waitpid(-1, NULL, WNOHANG); if (child_pid <= 0) break; printerr(2, "terminate zombie child: %d\n", child_pid); } child_check = time(NULL); } /* race condition here: dir_changed could be set before we * enter the poll, and we'd never notice if it weren't for the * timeout. */ ret = poll(pollarray, pollsize, POLL_MILLISECS); if (ret < 0) { if (errno != EINTR) printerr(0, "WARNING: error return from poll\n"); } else if (ret == 0) { /* timeout */ } else { /* ret > 0 */ scan_poll_results(ret); } } out: close(fd); return; }