~ubuntu-branches/ubuntu/hardy/dash/hardy-security

« back to all changes in this revision

Viewing changes to debian/diff/0014-REDIR-Replace-copyfd-by-savefd-and-use-dup2-elsewhe.diff

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-07-18 15:38:47 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070718153847-pef1uwy72j9gs9pw
Tags: 0.5.4-1ubuntu1
* Merge with Debian; remaining changes:
  - Build against glibc instead of dietlibc
  - Change default answer for "Install dash as /bin/sh?" question to true.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
From f393ff40ef49c582f614ff44107d1132753693a1 Mon Sep 17 00:00:00 2001
2
 
From: Herbert Xu <herbert@gondor.apana.org.au>
3
 
Date: Sat, 12 May 2007 18:00:57 +1000
4
 
Subject: [PATCH] [REDIR] Replace copyfd by savefd and use dup2 elsewhere
5
 
 
6
 
There are two kinds of users to copyfd, those that want to copy an fd to
7
 
an exact value and those that want to move an fd to a value >= 10.  The
8
 
former can simply use dup2 directly while the latter share a lot of common
9
 
code that now constitutes savefd.
10
 
---
11
 
 ChangeLog   |    1 +
12
 
 src/input.c |    8 +-----
13
 
 src/jobs.c  |   14 ++++--------
14
 
 src/redir.c |   65 +++++++++++++++++++++++++++++++---------------------------
15
 
 src/redir.h |    2 +-
16
 
 5 files changed, 44 insertions(+), 46 deletions(-)
17
 
 
18
 
diff --git a/ChangeLog b/ChangeLog
19
 
index 20c55d1..a7635fb 100644
20
 
--- a/ChangeLog
21
 
+++ b/ChangeLog
22
 
@@ -3,6 +3,7 @@
23
 
        * Removed unnecessary inclusion of redir.h from parser.c.
24
 
        * Invoke sh_error on error in copyfd.
25
 
        * Use dup2 instead of copyfd in evalbackcmd.
26
 
+       * Replace copyfd by savefd and dup2.
27
 
 
28
 
 2007-05-05  Herbert Xu <herbert@gondor.apana.org.au>
29
 
 
30
 
diff --git a/src/input.c b/src/input.c
31
 
index 49a2972..7f99d4a 100644
32
 
--- a/src/input.c
33
 
+++ b/src/input.c
34
 
@@ -428,7 +428,6 @@ int
35
 
 setinputfile(const char *fname, int flags)
36
 
 {
37
 
        int fd;
38
 
-       int fd2;
39
 
 
40
 
        INTOFF;
41
 
        if ((fd = open(fname, O_RDONLY)) < 0) {
42
 
@@ -436,11 +435,8 @@ setinputfile(const char *fname, int flags)
43
 
                        goto out;
44
 
                sh_error("Can't open %s", fname);
45
 
        }
46
 
-       if (fd < 10) {
47
 
-               fd2 = copyfd(fd, 10);
48
 
-               close(fd);
49
 
-               fd = fd2;
50
 
-       }
51
 
+       if (fd < 10)
52
 
+               fd = savefd(fd);
53
 
        setinputfd(fd, flags & INPUT_PUSH_FILE);
54
 
 out:
55
 
        INTON;
56
 
diff --git a/src/jobs.c b/src/jobs.c
57
 
index 9e28adb..7285d0d 100644
58
 
--- a/src/jobs.c
59
 
+++ b/src/jobs.c
60
 
@@ -188,18 +188,14 @@ setjobctl(int on)
61
 
        if (on == jobctl || rootshell == 0)
62
 
                return;
63
 
        if (on) {
64
 
-               int ofd;
65
 
-               ofd = fd = open(_PATH_TTY, O_RDWR);
66
 
+               fd = open(_PATH_TTY, O_RDWR);
67
 
                if (fd < 0) {
68
 
                        fd += 3;
69
 
-                       while (!isatty(fd) && --fd >= 0)
70
 
-                               ;
71
 
+                       while (!isatty(fd))
72
 
+                               if (--fd < 0)
73
 
+                                       goto out;
74
 
                }
75
 
-               fd = fcntl(fd, F_DUPFD, 10);
76
 
-               close(ofd);
77
 
-               if (fd < 0)
78
 
-                       goto out;
79
 
-               fcntl(fd, F_SETFD, FD_CLOEXEC);
80
 
+               fd = savefd(fd);
81
 
                do { /* while we are in the background */
82
 
                        if ((pgrp = tcgetpgrp(fd)) < 0) {
83
 
 out:
84
 
diff --git a/src/redir.c b/src/redir.c
85
 
index aab1585..9e0fae5 100644
86
 
--- a/src/redir.c
87
 
+++ b/src/redir.c
88
 
@@ -138,21 +138,10 @@ redirect(union node *redir, int flags)
89
 
                if (fd == newfd)
90
 
                        continue;
91
 
                if (sv && *(p = &sv->renamed[fd]) == EMPTY) {
92
 
-                       int i = fcntl(fd, F_DUPFD, 10);
93
 
-                       if (i == -1) {
94
 
-                               i = errno;
95
 
-                               if (i != EBADF) {
96
 
-                                       const char *m = strerror(i);
97
 
-                                       close(newfd);
98
 
-                                       sh_error("%d: %s", fd, m);
99
 
-                                       /* NOTREACHED */
100
 
-                               }
101
 
-                       } else {
102
 
+                       int i = savefd(fd);
103
 
+
104
 
+                       if (i >= 0)
105
 
                                *p = i;
106
 
-                               close(fd);
107
 
-                       }
108
 
-               } else {
109
 
-                       close(fd);
110
 
                }
111
 
 #ifdef notyet
112
 
                dupredirect(n, newfd, memory);
113
 
@@ -244,6 +233,7 @@ dupredirect(redir, f)
114
 
 #endif
115
 
        {
116
 
        int fd = redir->nfile.fd;
117
 
+       int err = 0;
118
 
 
119
 
 #ifdef notyet
120
 
        memory[fd] = 0;
121
 
@@ -255,16 +245,24 @@ dupredirect(redir, f)
122
 
                                memory[fd] = 1;
123
 
                        else
124
 
 #endif
125
 
-                               copyfd(redir->ndup.dupfd, fd);
126
 
+                               if (dup2(f, fd) < 0) {
127
 
+                                       err = errno;
128
 
+                                       goto err;
129
 
+                               }
130
 
+                       return;
131
 
                }
132
 
-               return;
133
 
-       }
134
 
+               f = fd;
135
 
+       } else if (dup2(f, fd) < 0)
136
 
+               err = errno;
137
 
+
138
 
+       close(f);
139
 
+       if (err < 0)
140
 
+               goto err;
141
 
 
142
 
-       if (f != fd) {
143
 
-               copyfd(f, fd);
144
 
-               close(f);
145
 
-       }
146
 
        return;
147
 
+
148
 
+err:
149
 
+       sh_error("%d: %s", f, strerror(err));
150
 
 }
151
 
 
152
 
 
153
 
@@ -327,10 +325,8 @@ popredir(int drop)
154
 
        rp = redirlist;
155
 
        for (i = 0 ; i < 10 ; i++) {
156
 
                if (rp->renamed[i] != EMPTY) {
157
 
-                       if (!drop) {
158
 
-                               close(i);
159
 
-                               copyfd(rp->renamed[i], i);
160
 
-                       }
161
 
+                       if (!drop)
162
 
+                               dup2(rp->renamed[i], i);
163
 
                        close(rp->renamed[i]);
164
 
                }
165
 
        }
166
 
@@ -372,17 +368,26 @@ clearredir(int drop)
167
 
 
168
 
 
169
 
 /*
170
 
- * Copy a file descriptor to be >= to.  Invokes sh_error on error.
171
 
+ * Move a file descriptor to > 10.  Invokes sh_error on error unless
172
 
+ * the original file dscriptor is not open.
173
 
  */
174
 
 
175
 
 int
176
 
-copyfd(int from, int to)
177
 
+savefd(int from)
178
 
 {
179
 
        int newfd;
180
 
+       int err;
181
 
+
182
 
+       newfd = fcntl(from, F_DUPFD, 10);
183
 
+       err = newfd < 0 ? errno : 0;
184
 
+       if (err != EBADF) {
185
 
+               close(from);
186
 
+               if (err)
187
 
+                       sh_error("%d: %s", from, strerror(err));
188
 
+               else
189
 
+                       fcntl(newfd, F_SETFD, FD_CLOEXEC);
190
 
+       }
191
 
 
192
 
-       newfd = fcntl(from, F_DUPFD, to);
193
 
-       if (newfd < 0)
194
 
-               sh_error("%d: %s", from, strerror(errno));
195
 
        return newfd;
196
 
 }
197
 
 
198
 
diff --git a/src/redir.h b/src/redir.h
199
 
index f4347ce..2c43511 100644
200
 
--- a/src/redir.h
201
 
+++ b/src/redir.h
202
 
@@ -45,6 +45,6 @@ union node;
203
 
 void redirect(union node *, int);
204
 
 void popredir(int);
205
 
 void clearredir(int);
206
 
-int copyfd(int, int);
207
 
+int savefd(int);
208
 
 int redirectsafe(union node *, int);
209
 
 
210
 
1.5.2.1
211