pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-login_tty.c
1 /* Test of login_tty() function.
2    Copyright (C) 2010-2013 Free Software Foundation, Inc.
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 /* Specification.  */
20 extern int login_tty (int);
21
22 #include <errno.h>
23 #include <pty.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <termios.h>
27 #include <unistd.h>
28
29 int
30 main ()
31 {
32   int master;
33   int slave;
34
35   /* Open a pseudo-terminal, as a master-slave pair.  */
36   {
37     int res = openpty (&master, &slave, NULL, NULL, NULL);
38     if (res != 0)
39       {
40         fprintf (stderr, "openpty returned %d\n", res);
41         return 1;
42       }
43   }
44
45   /* Create a new session and make it the controlling tty of this session.  */
46   {
47     int res = login_tty (slave);
48     if (res < 0)
49       {
50         fprintf (stderr, "login_tty failed\n");
51         return 1;
52       }
53   }
54
55   /* From here on, we cannot use stderr for error messages any more.
56      If a test fails, just abort.  */
57
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.  */
60   {
61     int fd;
62     for (fd = 0; fd < 3; fd++)
63       if (!(tcgetpgrp (fd) == getpid ()))
64         abort ();
65     for (fd = 0; fd < 3; fd++)
66       {
67         int sid = tcgetsid (fd);
68         if (!(sid == -1 ? errno == ENOSYS : sid == getpid ()))
69           abort ();
70       }
71   }
72
73   return 0;
74 }