usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-login_tty.c
blobac3b405893651b54a22211457499fef3d03aa0ae
1 /* Test of login_tty() function.
2 Copyright (C) 2010-2024 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 #include <config.h>
19 /* Specification. */
20 #include <utmp.h>
22 #include <errno.h>
23 #include <pty.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "ignore-value.h"
31 int
32 main ()
34 int master;
35 int slave;
37 /* Open a pseudo-terminal, as a master-slave pair. */
39 int res = openpty (&master, &slave, NULL, NULL, NULL);
40 if (res != 0)
42 fprintf (stderr, "openpty returned %d\n", res);
43 return 1;
47 /* Create a new session and make it the controlling tty of this session. */
49 int res = login_tty (slave);
50 if (res < 0)
52 fprintf (stderr, "login_tty failed\n");
53 return 1;
57 /* From here on, we cannot use stderr for error messages any more.
58 If a test fails, write error information into a file named 'err',
59 then abort. */
61 /* Check that fd = 0, 1, 2 are now open to the controlling terminal for the
62 current process and that it is a session of its own. */
64 int fd;
65 for (fd = 0; fd < 3; fd++)
66 if (!(tcgetpgrp (fd) == getpid ()))
68 ignore_value (freopen ("err", "w+", stderr));
69 fprintf (stderr, "tcgetpgrp(%d) = %ld whereas getpid() = %ld\n",
70 fd, (long) tcgetpgrp (fd), (long) getpid ());
71 fflush (stderr);
72 abort ();
74 for (fd = 0; fd < 3; fd++)
76 pid_t sid = tcgetsid (fd);
77 if (sid == -1)
79 if (!(errno == ENOSYS))
81 ignore_value (freopen ("err", "w+", stderr));
82 fprintf (stderr, "tcgetsid(%d) = -1 and errno = %d\n",
83 fd, errno);
84 fflush (stderr);
85 abort ();
88 else
90 if (!(sid == getpid ()))
92 ignore_value (freopen ("err", "w+", stderr));
93 fprintf (stderr, "tcgetsid(%d) = %ld whereas getpid() = %ld\n",
94 fd, (long) tcgetsid (fd), (long) getpid ());
95 fflush (stderr);
96 abort ();
102 return 0;