1*9fb66d81Schristos /*
2*9fb66d81Schristos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
3*9fb66d81Schristos *
4*9fb66d81Schristos * Permission to use, copy, modify, and distribute this software for any
5*9fb66d81Schristos * purpose with or without fee is hereby granted, provided that the above
6*9fb66d81Schristos * copyright notice and this permission notice appear in all copies.
7*9fb66d81Schristos *
8*9fb66d81Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*9fb66d81Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*9fb66d81Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*9fb66d81Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*9fb66d81Schristos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13*9fb66d81Schristos * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14*9fb66d81Schristos * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*9fb66d81Schristos */
16*9fb66d81Schristos
17*9fb66d81Schristos #include <sys/types.h>
18*9fb66d81Schristos #include <sys/ioctl.h>
19*9fb66d81Schristos
20*9fb66d81Schristos #include <fcntl.h>
21*9fb66d81Schristos #include <stdlib.h>
22*9fb66d81Schristos #include <unistd.h>
23*9fb66d81Schristos
24*9fb66d81Schristos #include "compat.h"
25*9fb66d81Schristos
26*9fb66d81Schristos void fatal(const char *, ...);
27*9fb66d81Schristos void fatalx(const char *, ...);
28*9fb66d81Schristos
29*9fb66d81Schristos pid_t
forkpty(int * master,char * name,struct termios * tio,struct winsize * ws)30*9fb66d81Schristos forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
31*9fb66d81Schristos {
32*9fb66d81Schristos int slave = -1;
33*9fb66d81Schristos char *path;
34*9fb66d81Schristos pid_t pid;
35*9fb66d81Schristos
36*9fb66d81Schristos if ((*master = open("/dev/ptmx", O_RDWR|O_NOCTTY)) == -1)
37*9fb66d81Schristos return (-1);
38*9fb66d81Schristos if (grantpt(*master) != 0)
39*9fb66d81Schristos goto out;
40*9fb66d81Schristos if (unlockpt(*master) != 0)
41*9fb66d81Schristos goto out;
42*9fb66d81Schristos
43*9fb66d81Schristos if ((path = ptsname(*master)) == NULL)
44*9fb66d81Schristos goto out;
45*9fb66d81Schristos if (name != NULL)
46*9fb66d81Schristos strlcpy(name, path, TTY_NAME_MAX);
47*9fb66d81Schristos if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
48*9fb66d81Schristos goto out;
49*9fb66d81Schristos
50*9fb66d81Schristos switch (pid = fork()) {
51*9fb66d81Schristos case -1:
52*9fb66d81Schristos goto out;
53*9fb66d81Schristos case 0:
54*9fb66d81Schristos close(*master);
55*9fb66d81Schristos
56*9fb66d81Schristos setsid();
57*9fb66d81Schristos if (ioctl(slave, TIOCSCTTY, NULL) == -1)
58*9fb66d81Schristos fatal("ioctl failed");
59*9fb66d81Schristos
60*9fb66d81Schristos if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
61*9fb66d81Schristos fatal("tcsetattr failed");
62*9fb66d81Schristos if (ioctl(slave, TIOCSWINSZ, ws) == -1)
63*9fb66d81Schristos fatal("ioctl failed");
64*9fb66d81Schristos
65*9fb66d81Schristos dup2(slave, 0);
66*9fb66d81Schristos dup2(slave, 1);
67*9fb66d81Schristos dup2(slave, 2);
68*9fb66d81Schristos if (slave > 2)
69*9fb66d81Schristos close(slave);
70*9fb66d81Schristos return (0);
71*9fb66d81Schristos }
72*9fb66d81Schristos
73*9fb66d81Schristos close(slave);
74*9fb66d81Schristos return (pid);
75*9fb66d81Schristos
76*9fb66d81Schristos out:
77*9fb66d81Schristos if (*master != -1)
78*9fb66d81Schristos close(*master);
79*9fb66d81Schristos if (slave != -1)
80*9fb66d81Schristos close(slave);
81*9fb66d81Schristos return (-1);
82*9fb66d81Schristos }
83