malloca: Silence a warning from clang's memory sanitizer.
[gnulib.git] / tests / test-login_tty.c
blob0b9d684c1b87a4430c84618b5f03a2eb7a65eb56
1 /* Test of login_tty() function.
2 Copyright (C) 2010-2017 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 <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 extern int login_tty (int);
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 int
30 main ()
32 int master;
33 int slave;
35 /* Open a pseudo-terminal, as a master-slave pair. */
37 int res = openpty (&master, &slave, NULL, NULL, NULL);
38 if (res != 0)
40 fprintf (stderr, "openpty returned %d\n", res);
41 return 1;
45 /* Create a new session and make it the controlling tty of this session. */
47 int res = login_tty (slave);
48 if (res < 0)
50 fprintf (stderr, "login_tty failed\n");
51 return 1;
55 /* From here on, we cannot use stderr for error messages any more.
56 If a test fails, just abort. */
58 /* Check that fd = 0, 1, 2 are now open to the controlling terminal for the
59 current process and that it is a session of its own. */
61 int fd;
62 for (fd = 0; fd < 3; fd++)
63 if (!(tcgetpgrp (fd) == getpid ()))
64 abort ();
65 for (fd = 0; fd < 3; fd++)
67 int sid = tcgetsid (fd);
68 if (!(sid == -1 ? errno == ENOSYS : sid == getpid ()))
69 abort ();
73 return 0;