tmate/compat/forkpty-sunos.c

90 lines
2.1 KiB
C
Raw Normal View History

/* $Id: forkpty-sunos.c,v 1.9 2009-10-15 07:11:25 nicm Exp $ */
2008-06-18 21:52:29 +02:00
/*
2008-06-23 23:54:48 +02:00
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
2008-06-18 21:52:29 +02:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
2008-06-23 23:54:48 +02:00
#include <sys/ioctl.h>
2008-06-18 21:52:29 +02:00
2008-06-18 22:11:25 +02:00
#include <fcntl.h>
2008-06-18 21:52:29 +02:00
#include <stdlib.h>
2008-06-18 22:11:25 +02:00
#include <stropts.h>
#include <unistd.h>
2008-06-18 21:52:29 +02:00
#include "tmux.h"
pid_t
forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
2008-06-18 21:52:29 +02:00
{
2008-06-18 22:11:25 +02:00
int slave;
2008-06-18 21:52:29 +02:00
char *path;
2008-06-18 22:11:25 +02:00
pid_t pid;
2008-06-18 21:52:29 +02:00
2008-06-19 00:00:49 +02:00
if ((*master = open("/dev/ptmx", O_RDWR|O_NOCTTY)) == -1)
2008-06-18 21:52:29 +02:00
return (-1);
if (grantpt(*master) != 0)
goto out;
if (unlockpt(*master) != 0)
goto out;
2008-06-19 00:21:51 +02:00
2008-06-18 21:52:29 +02:00
if ((path = ptsname(*master)) == NULL)
goto out;
if (name != NULL)
strlcpy(name, path, TTY_NAME_MAX);
2008-06-19 00:00:49 +02:00
if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
2008-06-18 21:52:29 +02:00
goto out;
2008-06-19 00:21:51 +02:00
2008-06-18 21:52:29 +02:00
switch (pid = fork()) {
case -1:
goto out;
case 0:
close(*master);
2008-06-18 23:14:42 +02:00
setsid();
2008-06-23 23:54:48 +02:00
#ifdef TIOCSCTTY
2008-06-18 23:14:42 +02:00
if (ioctl(slave, TIOCSCTTY, NULL) == -1)
fatal("ioctl failed");
2008-06-23 23:54:48 +02:00
#endif
2008-06-18 23:14:42 +02:00
if (ioctl(slave, I_PUSH, "ptem") == -1)
fatal("ioctl failed");
if (ioctl(slave, I_PUSH, "ldterm") == -1)
fatal("ioctl failed");
2008-06-19 00:21:51 +02:00
2009-09-20 20:31:16 +02:00
if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
2009-08-19 18:06:45 +02:00
fatal("tcsetattr failed");
2008-06-18 23:14:42 +02:00
if (ioctl(slave, TIOCSWINSZ, ws) == -1)
fatal("ioctl failed");
2008-06-19 00:21:51 +02:00
2008-06-18 23:14:42 +02:00
dup2(slave, 0);
dup2(slave, 1);
dup2(slave, 2);
if (slave > 2)
2009-08-19 18:06:45 +02:00
close(slave);
2008-06-18 21:52:29 +02:00
return (0);
}
2008-06-18 22:11:25 +02:00
close(slave);
2008-06-18 21:52:29 +02:00
return (pid);
out:
if (*master != -1)
close(*master);
2008-06-18 22:11:25 +02:00
if (slave != -1)
close(slave);
2008-06-18 21:52:29 +02:00
return (-1);
}