Mercurial > gnulib
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 |
rev | line source |
---|---|
13036 | 1 /* Assign a given terminal as controlling terminal and as standard input, |
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 | 4 |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | |
18 #include <config.h> | |
19 | |
20 /* Currently no specification header. */ | |
21 | |
22 #include <fcntl.h> | |
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 | 25 |
26 int | |
27 login_tty (int slave_fd) | |
28 { | |
29 int i; | |
30 | |
31 /* Create a new session. */ | |
32 setsid (); | |
33 | |
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 | 44 for (i = 0; i < 3; i++) |
45 if (i != slave_fd) | |
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 | 51 { |
52 char *slave_name; | |
53 int dummy_fd; | |
54 | |
55 slave_name = ttyname (slave_fd); | |
56 if (slave_name == NULL) | |
57 return -1; | |
58 dummy_fd = open (slave_name, O_RDWR); | |
59 if (dummy_fd < 0) | |
60 return -1; | |
61 close (dummy_fd); | |
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 | 64 |
13685 | 65 /* Assign fd to the standard input, standard output, and standard error of |
13036 | 66 the current process. */ |
67 for (i = 0; i < 3; i++) | |
68 if (slave_fd != i) | |
69 if (dup2 (slave_fd, i) < 0) | |
70 return -1; | |
71 if (slave_fd >= 3) | |
72 close (slave_fd); | |
73 | |
74 return 0; | |
75 } |