2009-03-23 Ian Lance Taylor <iant@google.com>
[deliverable/binutils-gdb.git] / gold / descriptors.cc
1 // descriptors.cc -- manage file descriptors for gold
2
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27 #include <fcntl.h>
28 #include <unistd.h>
29
30 #include "parameters.h"
31 #include "options.h"
32 #include "gold-threads.h"
33 #include "descriptors.h"
34
35 // Very old systems may not define FD_CLOEXEC.
36 #ifndef FD_CLOEXEC
37 #define FD_CLOEXEC 1
38 #endif
39
40 // O_CLOEXEC is only available on newer systems.
41 #ifndef O_CLOEXEC
42 #define O_CLOEXEC 0
43 #endif
44
45 namespace gold
46 {
47
48 // Class Descriptors.
49
50 // The default for limit_ is meant to simply be large. It gets
51 // adjusted downward if we run out of file descriptors.
52
53 Descriptors::Descriptors()
54 : lock_(NULL), initialize_lock_(&this->lock_), open_descriptors_(),
55 stack_top_(-1), current_(0), limit_(8192 - 16)
56 {
57 this->open_descriptors_.reserve(128);
58 }
59
60 // Open a file.
61
62 int
63 Descriptors::open(int descriptor, const char* name, int flags, int mode)
64 {
65 // We don't initialize this until we are called, because we can't
66 // initialize a Lock until we have parsed the options to find out
67 // whether we are running with threads. We can be called before
68 // options are valid when reading a linker script.
69 bool lock_initialized = this->initialize_lock_.initialize();
70
71 gold_assert(lock_initialized || descriptor < 0);
72
73 if (descriptor >= 0)
74 {
75 Hold_lock hl(*this->lock_);
76
77 gold_assert(static_cast<size_t>(descriptor)
78 < this->open_descriptors_.size());
79 Open_descriptor* pod = &this->open_descriptors_[descriptor];
80 if (pod->name == name
81 || (pod->name != NULL && strcmp(pod->name, name) == 0))
82 {
83 gold_assert(!pod->inuse);
84 pod->inuse = true;
85 if (descriptor == this->stack_top_)
86 {
87 this->stack_top_ = pod->stack_next;
88 pod->stack_next = -1;
89 pod->is_on_stack = false;
90 }
91 return descriptor;
92 }
93 }
94
95 while (true)
96 {
97 // We always want to set the close-on-exec flag; we don't
98 // require callers to pass it.
99 flags |= O_CLOEXEC;
100
101 int new_descriptor = ::open(name, flags, mode);
102 if (new_descriptor < 0
103 && errno != ENFILE
104 && errno != EMFILE)
105 {
106 if (descriptor >= 0 && errno == ENOENT)
107 {
108 {
109 Hold_lock hl(*this->lock_);
110
111 gold_error(_("file %s was removed during the link"),
112 this->open_descriptors_[descriptor].name);
113 }
114
115 errno = ENOENT;
116 }
117
118 return new_descriptor;
119 }
120
121 if (new_descriptor >= 0)
122 {
123 // If we have any plugins, we really do need to set the
124 // close-on-exec flag, even if O_CLOEXEC is not defined.
125 // FIXME: In some cases O_CLOEXEC may be defined in the
126 // header file but not supported by the kernel.
127 // Unfortunately there doesn't seem to be any obvious way to
128 // detect that, as unknown flags passed to open are ignored.
129 if (O_CLOEXEC == 0
130 && parameters->options_valid()
131 && parameters->options().has_plugins())
132 fcntl(new_descriptor, F_SETFD, FD_CLOEXEC);
133
134 {
135 Hold_optional_lock hl(this->lock_);
136
137 if (static_cast<size_t>(new_descriptor)
138 >= this->open_descriptors_.size())
139 this->open_descriptors_.resize(new_descriptor + 64);
140
141 Open_descriptor* pod = &this->open_descriptors_[new_descriptor];
142 pod->name = name;
143 pod->stack_next = -1;
144 pod->inuse = true;
145 pod->is_write = (flags & O_ACCMODE) != O_RDONLY;
146 pod->is_on_stack = false;
147
148 ++this->current_;
149 if (this->current_ >= this->limit_)
150 this->close_some_descriptor();
151
152 return new_descriptor;
153 }
154 }
155
156 // We ran out of file descriptors.
157 {
158 Hold_optional_lock hl(this->lock_);
159
160 this->limit_ = this->current_ - 16;
161 if (this->limit_ < 8)
162 this->limit_ = 8;
163 if (!this->close_some_descriptor())
164 gold_fatal(_("out of file descriptors and couldn't close any"));
165 }
166 }
167 }
168
169 // Release a descriptor.
170
171 void
172 Descriptors::release(int descriptor, bool permanent)
173 {
174 Hold_optional_lock hl(this->lock_);
175
176 gold_assert(descriptor >= 0
177 && (static_cast<size_t>(descriptor)
178 < this->open_descriptors_.size()));
179 Open_descriptor* pod = &this->open_descriptors_[descriptor];
180
181 if (permanent
182 || (this->current_ > this->limit_ && !pod->is_write))
183 {
184 if (::close(descriptor) < 0)
185 gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
186 pod->name = NULL;
187 --this->current_;
188 }
189 else
190 {
191 pod->inuse = false;
192 if (!pod->is_write && !pod->is_on_stack)
193 {
194 pod->stack_next = this->stack_top_;
195 this->stack_top_ = descriptor;
196 pod->is_on_stack = true;
197 }
198 }
199 }
200
201 // Close some descriptor. The lock is held when this is called. We
202 // close the descriptor on the top of the free stack. Note that this
203 // is the opposite of an LRU algorithm--we close the most recently
204 // used descriptor. That is because the linker tends to cycle through
205 // all the files; after we release a file, we are unlikely to need it
206 // again until we have looked at all the other files. Return true if
207 // we closed a descriptor.
208
209 bool
210 Descriptors::close_some_descriptor()
211 {
212 int last = -1;
213 int i = this->stack_top_;
214 while (i >= 0)
215 {
216 gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size());
217 Open_descriptor* pod = &this->open_descriptors_[i];
218 if (!pod->inuse && !pod->is_write)
219 {
220 if (::close(i) < 0)
221 gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
222 --this->current_;
223 pod->name = NULL;
224 if (last < 0)
225 this->stack_top_ = pod->stack_next;
226 else
227 this->open_descriptors_[last].stack_next = pod->stack_next;
228 pod->stack_next = -1;
229 pod->is_on_stack = false;
230 return true;
231 }
232 last = i;
233 i = pod->stack_next;
234 }
235
236 // We couldn't find any descriptors to close. This is weird but not
237 // necessarily an error.
238 return false;
239 }
240
241 // The single global variable which manages descriptors.
242
243 Descriptors descriptors;
244
245 } // End namespace gold.
This page took 0.035725 seconds and 5 git commands to generate.