annotate lib/grantpt.c @ 15727:144db791c6fa

Ensure EBADF returns for socket functions on mingw. * lib/accept.c (rpl_accept): Fail with error EBADF if the file descriptor is invalid. * lib/bind.c (rpl_bind): Likewise. * lib/connect.c (rpl_connect): Likewise. * lib/getpeername.c (rpl_getpeername): Likewise. * lib/getsockname.c (rpl_getsockname): Likewise. * lib/getsockopt.c (rpl_getsockopt): Likewise. * lib/listen.c (rpl_listen): Likewise. * lib/recv.c (rpl_recv): Likewise. * lib/recvfrom.c (rpl_recvfrom): Likewise. * lib/send.c (rpl_send): Likewise. * lib/sendto.c (rpl_sendto): Likewise. * lib/setsockopt.c (rpl_setsockopt): Likewise. * lib/shutdown.c (rpl_shutdown): Likewise.
author Bruno Haible <bruno@clisp.org>
date Wed, 21 Sep 2011 00:20:59 +0200
parents 97fc9a21a8fb
children e4144dcb6f45
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13028
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Acquire ownership of the slave side of a pseudo-terminal.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13028
diff changeset
2 Copyright (C) 1998-2002, 2009-2011 Free Software Foundation, Inc.
13028
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <stdlib.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <assert.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <errno.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <string.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <sys/wait.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <unistd.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #if HAVE_SETRLIMIT
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # include <sys/types.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 # include <sys/time.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # include <sys/resource.h>
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #endif
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include "configmake.h"
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "pty-private.h"
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #ifndef _LIBC
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 # define __builtin_expect(expr,val) (expr)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 # define __set_errno(e) errno = (e)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 # define __dup2 dup2
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 # define __fork fork
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 # define __setrlimit setrlimit
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 # define __waitpid waitpid
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #endif
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 int
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 grantpt (int fd)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 {
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* This function is most often called from a process without 'root'
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 credentials. Use the helper program. */
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int retval = -1;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 pid_t pid = __fork ();
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (pid == -1)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 goto cleanup;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 else if (pid == 0)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 /* This is executed in the child process. */
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 #if HAVE_SETRLIMIT && defined RLIMIT_CORE
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Disable core dumps. */
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 struct rlimit rl = { 0, 0 };
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 __setrlimit (RLIMIT_CORE, &rl);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #endif
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 if (fd != PTY_FILENO)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 if (__dup2 (fd, PTY_FILENO) < 0)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 _exit (FAIL_EBADF);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 #ifdef CLOSE_ALL_FDS
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 CLOSE_ALL_FDS ();
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 execle (_PATH_PT_CHOWN, strrchr (_PATH_PT_CHOWN, '/') + 1, NULL, NULL);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 _exit (FAIL_EXEC);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 else
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 {
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 int w;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 if (__waitpid (pid, &w, 0) == -1)
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 goto cleanup;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (!WIFEXITED (w))
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 __set_errno (ENOEXEC);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 else
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 switch (WEXITSTATUS (w))
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 {
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 case 0:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 retval = 0;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 case FAIL_EBADF:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 __set_errno (EBADF);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 case FAIL_EINVAL:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 __set_errno (EINVAL);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 case FAIL_EACCES:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 __set_errno (EACCES);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 case FAIL_EXEC:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 __set_errno (ENOEXEC);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 case FAIL_ENOMEM:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 __set_errno (ENOMEM);
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 break;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 default:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 assert(! "getpt: internal error: invalid exit code from pt_chown");
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 }
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 }
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 cleanup:
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 return retval;
12650cb16dc5 New module 'grantpt'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 }