]> git.proxmox.com Git - mirror_lxc.git/blob - src/include/openpty.c
spelling: timeout
[mirror_lxc.git] / src / include / openpty.c
1 /*
2 * openpty: glibc implementation
3 *
4 * Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
5 *
6 * Authors:
7 * Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _XOPEN_SOURCE /* See feature_test_macros(7) */
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34
35 #define _PATH_DEVPTMX "/dev/ptmx"
36
37 int openpty (int *amaster, int *aslave, char *name, struct termios *termp,
38 struct winsize *winp)
39 {
40 char buf[PATH_MAX];
41 int master, slave;
42
43 master = open(_PATH_DEVPTMX, O_RDWR);
44 if (master == -1)
45 return -1;
46
47 if (grantpt(master))
48 goto fail;
49
50 if (unlockpt(master))
51 goto fail;
52
53 if (ptsname_r(master, buf, sizeof buf))
54 goto fail;
55
56 slave = open(buf, O_RDWR | O_NOCTTY);
57 if (slave == -1)
58 goto fail;
59
60 /* XXX Should we ignore errors here? */
61 if (termp)
62 tcsetattr(slave, TCSAFLUSH, termp);
63 if (winp)
64 ioctl(slave, TIOCSWINSZ, winp);
65
66 *amaster = master;
67 *aslave = slave;
68 if (name != NULL)
69 strcpy(name, buf);
70
71 return 0;
72
73 fail:
74 close(master);
75 return -1;
76 }