1 | /* Copyright (C) 1998-2018 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <fcntl.h> |
21 | #include <stdlib.h> |
22 | #include <unistd.h> |
23 | #include <paths.h> |
24 | #include <sys/statfs.h> |
25 | |
26 | #include "linux_fsinfo.h" |
27 | |
28 | /* Path to the master pseudo terminal cloning device. */ |
29 | #define _PATH_DEVPTMX _PATH_DEV "ptmx" |
30 | /* Directory containing the UNIX98 pseudo terminals. */ |
31 | #define _PATH_DEVPTS _PATH_DEV "pts" |
32 | |
33 | /* Prototype for function that opens BSD-style master pseudo-terminals. */ |
34 | extern int __bsd_getpt (void) attribute_hidden; |
35 | |
36 | /* Open a master pseudo terminal and return its file descriptor. */ |
37 | int |
38 | __posix_openpt (int oflag) |
39 | { |
40 | static int have_no_dev_ptmx; |
41 | int fd; |
42 | |
43 | if (!have_no_dev_ptmx) |
44 | { |
45 | fd = __open (_PATH_DEVPTMX, oflag); |
46 | if (fd != -1) |
47 | { |
48 | struct statfs fsbuf; |
49 | static int devpts_mounted; |
50 | |
51 | /* Check that the /dev/pts filesystem is mounted |
52 | or if /dev is a devfs filesystem (this implies /dev/pts). */ |
53 | if (devpts_mounted |
54 | || (__statfs (_PATH_DEVPTS, &fsbuf) == 0 |
55 | && fsbuf.f_type == DEVPTS_SUPER_MAGIC) |
56 | || (__statfs (_PATH_DEV, &fsbuf) == 0 |
57 | && fsbuf.f_type == DEVFS_SUPER_MAGIC)) |
58 | { |
59 | /* Everything is ok. */ |
60 | devpts_mounted = 1; |
61 | return fd; |
62 | } |
63 | |
64 | /* If /dev/pts is not mounted then the UNIX98 pseudo terminals |
65 | are not usable. */ |
66 | __close (fd); |
67 | have_no_dev_ptmx = 1; |
68 | __set_errno (ENOENT); |
69 | } |
70 | else |
71 | { |
72 | if (errno == ENOENT || errno == ENODEV) |
73 | have_no_dev_ptmx = 1; |
74 | else |
75 | return -1; |
76 | } |
77 | } |
78 | else |
79 | __set_errno (ENOENT); |
80 | |
81 | return -1; |
82 | } |
83 | weak_alias (__posix_openpt, posix_openpt) |
84 | |
85 | |
86 | int |
87 | __getpt (void) |
88 | { |
89 | int fd = __posix_openpt (O_RDWR); |
90 | if (fd == -1) |
91 | fd = __bsd_getpt (); |
92 | return fd; |
93 | } |
94 | |
95 | |
96 | #define PTYNAME1 "pqrstuvwxyzabcde"; |
97 | #define PTYNAME2 "0123456789abcdef"; |
98 | |
99 | #define __getpt __bsd_getpt |
100 | #define HAVE_POSIX_OPENPT |
101 | #include <sysdeps/unix/bsd/getpt.c> |
102 | |