Annotation of embedaddon/tmux/compat/forkpty-aix.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
        !            13:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
        !            14:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <sys/types.h>
        !            18: #include <sys/ioctl.h>
        !            19: 
        !            20: #include <fcntl.h>
        !            21: #include <stdlib.h>
        !            22: #include <stropts.h>
        !            23: #include <unistd.h>
        !            24: #include <errno.h>
        !            25: 
        !            26: #include "compat.h"
        !            27: 
        !            28: void fatal(const char *, ...);
        !            29: void fatalx(const char *, ...);
        !            30: 
        !            31: pid_t
        !            32: forkpty(int *master, unused char *name, struct termios *tio, struct winsize *ws)
        !            33: {
        !            34:        int     slave = -1, fd, pipe_fd[2];
        !            35:        char   *path, dummy;
        !            36:        pid_t   pid;
        !            37: 
        !            38:        if (pipe(pipe_fd) == -1)
        !            39:                return (-1);
        !            40: 
        !            41:        if ((*master = open("/dev/ptc", O_RDWR|O_NOCTTY)) == -1)
        !            42:                goto out;
        !            43: 
        !            44:        if ((path = ttyname(*master)) == NULL)
        !            45:                goto out;
        !            46: 
        !            47:        if (name != NULL)
        !            48:                strlcpy(name, path, TTY_NAME_MAX);
        !            49: 
        !            50:        if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
        !            51:                goto out;
        !            52: 
        !            53:        switch (pid = fork()) {
        !            54:        case -1:
        !            55:                goto out;
        !            56:        case 0:
        !            57:                close(*master);
        !            58: 
        !            59:                close(pipe_fd[1]);
        !            60:                while (read(pipe_fd[0], &dummy, 1) == -1) {
        !            61:                        if (errno != EINTR)
        !            62:                                break;
        !            63:                }
        !            64:                close(pipe_fd[0]);
        !            65: 
        !            66:                fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
        !            67:                if (fd >= 0) {
        !            68:                        ioctl(fd, TIOCNOTTY, NULL);
        !            69:                        close(fd);
        !            70:                }
        !            71: 
        !            72:                if (setsid() < 0)
        !            73:                        fatal("setsid");
        !            74: 
        !            75:                fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
        !            76:                if (fd >= 0)
        !            77:                        fatalx("open succeeded (failed to disconnect)");
        !            78: 
        !            79:                fd = open(path, O_RDWR);
        !            80:                if (fd < 0)
        !            81:                        fatal("open failed");
        !            82:                close(fd);
        !            83: 
        !            84:                fd = open("/dev/tty", O_WRONLY);
        !            85:                if (fd < 0)
        !            86:                        fatal("open failed");
        !            87:                close(fd);
        !            88: 
        !            89:                if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
        !            90:                        fatal("tcsetattr failed");
        !            91:                if (ioctl(slave, TIOCSWINSZ, ws) == -1)
        !            92:                        fatal("ioctl failed");
        !            93: 
        !            94:                dup2(slave, 0);
        !            95:                dup2(slave, 1);
        !            96:                dup2(slave, 2);
        !            97:                if (slave > 2)
        !            98:                        close(slave);
        !            99: 
        !           100:                return (0);
        !           101:        }
        !           102: 
        !           103:        close(slave);
        !           104: 
        !           105:        close(pipe_fd[0]);
        !           106:        close(pipe_fd[1]);
        !           107:        return (pid);
        !           108: 
        !           109: out:
        !           110:        if (*master != -1)
        !           111:                close(*master);
        !           112:        if (slave != -1)
        !           113:                close(slave);
        !           114: 
        !           115:        close(pipe_fd[0]);
        !           116:        close(pipe_fd[1]);
        !           117:        return (-1);
        !           118: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>