Ejemplo n.º 1
0
UIObject::~UIObject() {
	if (parent) {
		parent->remove_child(this);
	}
	if (receiver) {
		Input::Input::get_cur_object()->remove_receiver(receiver->get_name());
	}
	clear_child();
}
Ejemplo n.º 2
0
void run_epoll(int sfd, int parent)
{
    struct epoll_event ev, events[MAX_EVENTS];
    int nfds, n, status;
    pid_t pid;
    struct signalfd_siginfo fdsi;
    ssize_t sz;

    int epollfd = epoll_create1(EPOLL_CLOEXEC);
    if (epollfd == -1) {
        handle_error("epoll_create1");
    }

    ev.events = EPOLLIN;
    ev.data.fd = sfd;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sfd, &ev) == -1) {
        handle_error("epoll_ctl: signalfd");
    }

    for (;;) {
        nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (nfds == -1) {
            handle_error("epoll_wait");
        }

        for (n = 0; n < nfds; ++n) {
            sz = read(events[n].data.fd, &fdsi, sizeof(struct signalfd_siginfo));
            if (sz != sizeof(struct signalfd_siginfo))
                handle_error("read");

            if (fdsi.ssi_signo == SIGINT) {
                fprintf(stderr, "%d: Got SIGINT from %d\n", getpid(), fdsi.ssi_pid);
            } else if (fdsi.ssi_signo == SIGQUIT) {
                fprintf(stderr, "%d: Got SIGQUIT from %d\n", getpid(), fdsi.ssi_pid);
                if (parent) {
                    exiting = 1;
                    notify_children(SIGQUIT);
                } else {
                    exit(EXIT_SUCCESS);
                }
            } else if (fdsi.ssi_signo == SIGTERM) {
                fprintf(stderr, "%d: Got SIGTERM from %d\n", getpid(), fdsi.ssi_pid);
                if (parent) {
                    exiting = 1;
                    notify_children(SIGTERM);
                } else {
                    exit(EXIT_SUCCESS);
                }
            } else if (fdsi.ssi_signo == SIGCHLD) {
                fprintf(stderr, "%d: Got SIGCHLD from %d\n", getpid(), fdsi.ssi_pid);
                do {
                    pid = waitpid(-1, &status, WNOHANG);
                    if (pid > 0) {
                        fprintf(stderr, "%d: Process %d exited\n", getpid(), pid);
                        clear_child(pid);

                        if (!exiting) {
                            do_forks(1, sfd);
                        } else {
                            if (n_children == 0) {
                                fprintf(stderr, "%d: All children exited\n", getpid());
                                exit(EXIT_SUCCESS);
                            }
                        }
                    }
                } while (pid > 0);
            } else {
                fprintf(stderr, "%d: Read unexpected signal from %d\n", getpid(), fdsi.ssi_pid);
            }
        }
    }
}