[systemd-devel] [RFC 12/12] console: add systemd-consoled

Lennart Poettering lennart at poettering.net
Wed Nov 27 14:39:40 PST 2013


On Wed, 27.11.13 19:48, David Herrmann (dh.herrmann at gmail.com) wrote:

> +
> +pid_t pty_new(unsigned short term_width, unsigned short term_height, Terminal *t, Pty **out) {
> +        Pty *pty;
> +        pid_t pid;
> +        int fd, comm[2], slave, r;
> +        char d;
> +
> +        pty = calloc(1, sizeof(*pty));
> +        if (!pty)
> +                return -ENOMEM;
> +
> +        fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
> +        if (fd < 0) {
> +                free(pty);
> +                return -errno;
> +        }
> +
> +        r = sd_event_add_io(t->m->event,
> +                            fd,
> +                            EPOLLHUP | EPOLLERR | EPOLLIN | EPOLLOUT | EPOLLET,
> +                            pty_io_fn,
> +                            pty,
> +                            &pty->fd_source);
> +        if (r < 0) {
> +                close(fd);
> +                free(pty);
> +                return r;
> +        }
> +
> +        r = sd_event_add_defer(t->m->event, pty_idle_fn, pty, &pty->idle_source);
> +        if (r < 0) {
> +                sd_event_source_set_enabled(pty->fd_source, SD_EVENT_OFF);
> +                sd_event_source_unref(pty->fd_source);
> +                close(fd);
> +                free(pty);
> +                return r;
> +        }
> +
> +        r = pipe2(comm, O_CLOEXEC);
> +        if (r < 0) {
> +                r = -errno;
> +                sd_event_source_set_enabled(pty->idle_source, SD_EVENT_OFF);
> +                sd_event_source_unref(pty->idle_source);
> +                sd_event_source_set_enabled(pty->fd_source, SD_EVENT_OFF);
> +                sd_event_source_unref(pty->fd_source);
> +                close(fd);
> +                free(pty);
> +                return r;
> +        }
> +
> +        pid = fork();
> +        if (pid < 0) {
> +                /* error */
> +                pid = -errno;
> +                close(comm[0]);
> +                close(comm[1]);
> +                sd_event_source_set_enabled(pty->idle_source, SD_EVENT_OFF);
> +                sd_event_source_unref(pty->idle_source);
> +                sd_event_source_set_enabled(pty->fd_source, SD_EVENT_OFF);
> +                sd_event_source_unref(pty->fd_source);
> +                close(fd);
> +                free(pty);
> +                return pid;

Grr. Just define a label to jump to to clean everything up that is
initialized, and skip over the bits that isn't. Duplicating the
destruction logic on every if block is just wrong...

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list