1 /* Test of ptsname_r(3).
2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
18 may "optimize" the null_ptr function, when its result gets passed to a
19 function that has an argument declared as _GL_ARG_NONNULL. */
20 #define _GL_ARG_NONNULL(params)
26 #include "signature.h"
27 SIGNATURE_CHECK (ptsname_r
, int, (int, char *, size_t));
37 #include "same-inode.h"
39 #if GNULIB_defined_ptsname_r
40 # include "null-ptr.h"
45 /* Compare two slave names.
46 On some systems, there are hard links in the /dev/ directory.
47 For example, on OSF/1 5.1,
48 /dev/ttyp0 == /dev/pts/0
49 /dev/ttyp9 == /dev/pts/9
50 /dev/ttypa == /dev/pts/10
51 /dev/ttype == /dev/pts/14
54 same_slave (const char *slave_name1
, const char *slave_name2
)
59 return (strcmp (slave_name1
, slave_name2
) == 0
60 || (stat (slave_name1
, &statbuf1
) >= 0
61 && stat (slave_name2
, &statbuf2
) >= 0
62 && psame_inode (&statbuf1
, &statbuf2
)));
66 test_errors (int fd
, const char *slave
)
76 if (buflen_max
> sizeof buffer
)
77 buflen_max
= sizeof buffer
;
78 for (buflen
= 0; buflen
<= buflen_max
; buflen
++)
80 memset (buffer
, 'X', sizeof buffer
);
81 result
= ptsname_r (fd
, buffer
, buflen
);
85 ASSERT (buffer
[0] == '/');
90 ASSERT (result
== ERANGE
);
91 ASSERT (buffer
[0] == 'X');
95 /* This test works only if the ptsname_r implementation comes from gnulib.
96 If it comes from libc, we have no way to prevent gcc from "optimizing"
97 the null_ptr function in invalid ways. See
98 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156>. */
99 #if GNULIB_defined_ptsname_r
100 result
= ptsname_r (fd
, null_ptr (), 0);
101 ASSERT (result
!= 0);
102 ASSERT (result
== EINVAL
);
110 /* Declare failure if test takes too long, by using default abort
111 caused by SIGALRM. */
113 int alarm_value
= 20;
117 signal (SIGALRM
, SIG_DFL
);
125 result
= ptsname_r (-1, buffer
, sizeof buffer
);
126 ASSERT (result
!= 0);
127 ASSERT (result
== EBADF
|| result
== ENOTTY
);
135 /* Open the controlling tty of the current process. */
136 fd
= open ("/dev/tty", O_RDONLY
);
139 if (test_exit_status
!= EXIT_SUCCESS
)
140 return test_exit_status
;
141 fprintf (stderr
, "Skipping test: cannot open controlling tty\n");
145 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
146 /* The result is usually NULL, because /dev/tty is a slave, not a
150 ASSERT (memcmp (buffer
, "/dev/", 5) == 0);
156 #if defined __sun || defined __DragonFly__
157 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
158 ptsname() does not work on them.
159 DragonFly BSD has only /dev/ptmx. */
165 /* Open a pty master. */
166 fd
= open ("/dev/ptmx", O_RDWR
| O_NOCTTY
);
169 if (test_exit_status
!= EXIT_SUCCESS
)
170 return test_exit_status
;
171 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
175 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
176 ASSERT (result
== 0);
177 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
179 test_errors (fd
, buffer
);
185 /* AIX has BSD-style /dev/ptyp[0-9a-f] files, and the function ptsname()
186 produces the corresponding /dev/ttyp[0-9a-f] file for each. But opening
187 such a pty causes the process to hang in a state where it does not even
188 react to the SIGALRM signal for N * 15 seconds, where N is the number of
189 opened ptys, either in the close (fd) call or - when this close (fd) call
190 is commented out - at the process exit.
191 So, better don't use these BSD-style ptys. The modern way to open a pty
192 is to go through /dev/ptc. */
198 /* Open a pty master. */
199 fd
= open ("/dev/ptc", O_RDWR
| O_NOCTTY
);
202 if (test_exit_status
!= EXIT_SUCCESS
)
203 return test_exit_status
;
204 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
208 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
209 ASSERT (result
== 0);
210 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
212 test_errors (fd
, buffer
);
214 /* This close (fd) call takes 15 seconds. It would be interruptible by the
215 SIGALRM timer, but then this test would report failure. */
219 #elif defined __GNU__ /* Hurd */
221 /* Try various master names of Hurd: /dev/pty[p-q][0-9a-v] */
226 for (char1
= 'p'; char1
<= 'q'; char1
++)
227 for (char2
= '0'; char2
<= 'v'; (char2
== '9' ? char2
= 'a' : char2
++))
229 char master_name
[32];
232 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
233 fd
= open (master_name
, O_RDONLY
);
240 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
241 ASSERT (result
== 0);
242 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
243 ASSERT (same_slave (buffer
, slave_name
));
245 test_errors (fd
, buffer
);
254 /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f] */
259 for (char1
= 'p'; char1
<= 'w'; char1
++)
260 for (char2
= '0'; char2
<= 'f'; (char2
== '9' ? char2
= 'a' : char2
++))
262 char master_name
[32];
265 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
266 fd
= open (master_name
, O_RDONLY
);
273 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
274 ASSERT (result
== 0);
275 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
276 ASSERT (same_slave (buffer
, slave_name
));
278 test_errors (fd
, buffer
);
280 /* This call hangs on AIX. */
286 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */
292 for (upper
= 0; upper
<= 1; upper
++)
293 for (char1
= (upper
? 'P' : 'p'); char1
<= (upper
? 'S' : 's'); char1
++)
294 for (char2
= '0'; char2
<= 'v'; (char2
== '9' ? char2
= 'a' : char2
++))
296 char master_name
[32];
299 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
300 fd
= open (master_name
, O_RDONLY
);
307 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
308 ASSERT (result
== 0);
309 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
310 ASSERT (same_slave (buffer
, slave_name
));
312 test_errors (fd
, buffer
);
321 return test_exit_status
;