annotate lib/login_tty.c @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Assign a given terminal as controlling terminal and as standard input,
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 standard output, standard error of the current process.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
3 Copyright (C) 2010-2013 Free Software Foundation, Inc.
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Currently no specification header. */
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <fcntl.h>
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <unistd.h>
13691
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
24 #include <sys/ioctl.h>
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 int
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 login_tty (int slave_fd)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 {
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 int i;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Create a new session. */
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 setsid ();
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Make fd the controlling terminal for the current process.
13691
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
35 On BSD and OSF/1: There is ioctl TIOCSCTTY for this purpose.
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
36 On Solaris:
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
37 A terminal becomes the controlling terminal of a session
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
38 if it is being open()ed, at a moment when
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
39 1. it is not already the controlling terminal of some session, and
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
40 2. the process that open()s it is a session leader that does not have
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
41 a controlling terminal.
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
42 We assume condition 1, try to ensure condition 2, and then open() it.
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
43 */
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 for (i = 0; i < 3; i++)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 if (i != slave_fd)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 close (i);
13691
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
47 #ifdef TIOCSCTTY
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
48 if (ioctl (slave_fd, TIOCSCTTY, NULL) < 0)
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
49 return -1;
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
50 #else
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 {
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 char *slave_name;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 int dummy_fd;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 slave_name = ttyname (slave_fd);
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (slave_name == NULL)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 return -1;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 dummy_fd = open (slave_name, O_RDWR);
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (dummy_fd < 0)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 return -1;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 close (dummy_fd);
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 }
13691
353ba0b294e1 login_tty: Make the replacement code work on BSD systems.
Bruno Haible <bruno@clisp.org>
parents: 13685
diff changeset
63 #endif
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
13685
b370816ef4c2 Fix typos in comments.
Bruno Haible <bruno@clisp.org>
parents: 13036
diff changeset
65 /* Assign fd to the standard input, standard output, and standard error of
13036
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 the current process. */
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 for (i = 0; i < 3; i++)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (slave_fd != i)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 if (dup2 (slave_fd, i) < 0)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 return -1;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 if (slave_fd >= 3)
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 close (slave_fd);
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 return 0;
2aebdd9d61a2 New module 'login_tty'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 }