Mercurial > gnulib
annotate tests/test-ptsname.c @ 17356:639f60219717
tests: make it easier to bypass alarm time in debugger
While auditing alarm usage, I noticed that test-regex had a nice
idiom that made it easier to disable an alarm under glibc. Use
it elsewhere, so future copy-and-paste will preserve the idiom.
* tests/test-file-has-acl.c (main): Allow gdb to override alarm.
* tests/test-memmem.c (main): Likewise.
* tests/test-passfd.c (main): Likewise.
* tests/test-ptsname.c (main): Likewise.
* tests/test-ptsname_r.c (main): Likewise.
* tests/test-strcasestr.c (main): Likewise.
* tests/test-strstr.c (main): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Mon, 11 Mar 2013 15:40:32 -0600 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
13026 | 1 /* Test of ptsname(3). |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16939
diff
changeset
|
2 Copyright (C) 2010-2013 Free Software Foundation, Inc. |
13026 | 3 |
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. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 #include <config.h> | |
18 | |
19 #include <stdlib.h> | |
20 | |
21 #include "signature.h" | |
22 SIGNATURE_CHECK (ptsname, char *, (int)); | |
23 | |
16939
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
24 #include <errno.h> |
13026 | 25 #include <fcntl.h> |
15652
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
26 #include <signal.h> |
13026 | 27 #include <stdio.h> |
28 #include <string.h> | |
29 #include <unistd.h> | |
13998
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
30 #include <sys/stat.h> |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
31 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
32 #include "same-inode.h" |
13026 | 33 |
34 #include "macros.h" | |
35 | |
13998
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
36 /* Compare two slave names. |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
37 On some systems, there are hard links in the /dev/ directory. |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
38 For example, on OSF/1 5.1, |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
39 /dev/ttyp0 == /dev/pts/0 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
40 /dev/ttyp9 == /dev/pts/9 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
41 /dev/ttypa == /dev/pts/10 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
42 /dev/ttype == /dev/pts/14 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
43 */ |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
44 static int |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
45 same_slave (const char *slave_name1, const char *slave_name2) |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
46 { |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
47 struct stat statbuf1; |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
48 struct stat statbuf2; |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
49 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
50 return (strcmp (slave_name1, slave_name2) == 0 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
51 || (stat (slave_name1, &statbuf1) >= 0 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
52 && stat (slave_name2, &statbuf2) >= 0 |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
53 && SAME_INODE (statbuf1, statbuf2))); |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
54 } |
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
55 |
13026 | 56 int |
57 main (void) | |
58 { | |
15652
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
59 #if HAVE_DECL_ALARM |
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
60 /* Declare failure if test takes too long, by using default abort |
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
61 caused by SIGALRM. */ |
17356
639f60219717
tests: make it easier to bypass alarm time in debugger
Eric Blake <eblake@redhat.com>
parents:
17249
diff
changeset
|
62 int alarm_value = 5; |
15652
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
63 signal (SIGALRM, SIG_DFL); |
17356
639f60219717
tests: make it easier to bypass alarm time in debugger
Eric Blake <eblake@redhat.com>
parents:
17249
diff
changeset
|
64 alarm (alarm_value); |
15652
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
65 #endif |
66bab2a591f5
ptsname tests: Let the test fail rather than hang (e.g. on AIX 5.1).
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
66 |
13026 | 67 { |
16939
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
68 char *result; |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
69 |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
70 errno = 0; |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
71 result = ptsname (-1); |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
72 ASSERT (result == NULL); |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
73 ASSERT (errno == EBADF |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
74 || errno == ENOTTY /* seen on glibc */ |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
75 ); |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
76 } |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
77 |
76ca51754604
ptsname test: Extend test.
Bruno Haible <bruno@clisp.org>
parents:
16935
diff
changeset
|
78 { |
13026 | 79 int fd; |
80 char *result; | |
81 | |
82 /* Open the controlling tty of the current process. */ | |
83 fd = open ("/dev/tty", O_RDONLY); | |
84 if (fd < 0) | |
85 { | |
86 fprintf (stderr, "Skipping test: cannot open controlling tty\n"); | |
87 return 77; | |
88 } | |
89 | |
90 result = ptsname (fd); | |
91 /* The result is usually NULL, because /dev/tty is a slave, not a | |
92 master. */ | |
93 if (result != NULL) | |
94 { | |
95 ASSERT (memcmp (result, "/dev/", 5) == 0); | |
96 } | |
97 | |
98 close (fd); | |
99 } | |
100 | |
14030
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
101 #if defined __sun |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
102 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
103 ptsname() does not work on them. */ |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
104 { |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
105 int fd; |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
106 char *result; |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
107 |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
108 /* Open the controlling tty of the current process. */ |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
109 fd = open ("/dev/ptmx", O_RDWR | O_NOCTTY); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
110 if (fd < 0) |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
111 { |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
112 fprintf (stderr, "Skipping test: cannot open pseudo-terminal\n"); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
113 return 77; |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
114 } |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
115 |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
116 result = ptsname (fd); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
117 ASSERT (result != NULL); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
118 ASSERT (memcmp (result, "/dev/pts/", 9) == 0); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
119 |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
120 close (fd); |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
121 } |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
122 |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
123 #else |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
124 |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
125 /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f] */ |
13026 | 126 { |
127 int char1; | |
128 int char2; | |
129 | |
130 for (char1 = 'p'; char1 <= 'w'; char1++) | |
13162 | 131 for (char2 = '0'; char2 <= 'f'; (char2 == '9' ? char2 = 'a' : char2++)) |
13026 | 132 { |
133 char master_name[32]; | |
134 int fd; | |
135 | |
136 sprintf (master_name, "/dev/pty%c%c", char1, char2); | |
137 fd = open (master_name, O_RDONLY); | |
138 if (fd >= 0) | |
139 { | |
140 char *result; | |
141 char slave_name[32]; | |
142 | |
143 result = ptsname (fd); | |
144 ASSERT (result != NULL); | |
145 sprintf (slave_name, "/dev/tty%c%c", char1, char2); | |
13998
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
146 ASSERT (same_slave (result, slave_name)); |
13026 | 147 |
148 close (fd); | |
149 } | |
150 } | |
151 } | |
152 | |
13162 | 153 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */ |
154 { | |
155 int upper; | |
156 int char1; | |
157 int char2; | |
158 | |
159 for (upper = 0; upper <= 1; upper++) | |
160 for (char1 = (upper ? 'P' : 'p'); char1 <= (upper ? 'S' : 's'); char1++) | |
161 for (char2 = '0'; char2 <= 'v'; (char2 == '9' ? char2 = 'a' : char2++)) | |
162 { | |
163 char master_name[32]; | |
164 int fd; | |
165 | |
166 sprintf (master_name, "/dev/pty%c%c", char1, char2); | |
167 fd = open (master_name, O_RDONLY); | |
168 if (fd >= 0) | |
169 { | |
170 char *result; | |
171 char slave_name[32]; | |
172 | |
173 result = ptsname (fd); | |
174 ASSERT (result != NULL); | |
175 sprintf (slave_name, "/dev/tty%c%c", char1, char2); | |
13998
1ec8b76f7855
ptsname: Avoid test failure on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents:
13162
diff
changeset
|
176 ASSERT (same_slave (result, slave_name)); |
13162 | 177 |
178 close (fd); | |
179 } | |
180 } | |
181 } | |
182 | |
14030
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
183 #endif |
1adb3647af8f
ptsname test: Avoid failure on Solaris.
Bruno Haible <bruno@clisp.org>
parents:
13998
diff
changeset
|
184 |
13026 | 185 return 0; |
186 } |