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/>. */
29 #include "ignore-value.h"
37 /* Open a pseudo-terminal, as a master-slave pair. */
39 int res
= openpty (&master
, &slave
, NULL
, NULL
, NULL
);
42 fprintf (stderr
, "openpty returned %d\n", res
);
47 /* Create a new session and make it the controlling tty of this session. */
49 int res
= login_tty (slave
);
52 fprintf (stderr
, "login_tty failed\n");
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',
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. */
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 ());
74 for (fd
= 0; fd
< 3; fd
++)
76 pid_t sid
= tcgetsid (fd
);
79 if (!(errno
== ENOSYS
))
81 ignore_value (freopen ("err", "w+", stderr
));
82 fprintf (stderr
, "tcgetsid(%d) = -1 and errno = %d\n",
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 ());