diff options
author | Damien Miller <djm@mindrot.org> | 2008-02-10 22:26:51 +1100 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2008-02-10 22:26:51 +1100 |
commit | 3397d0e0c5820464bea4c96e91b5e02cca98330a (patch) | |
tree | 8728630b2ba57219246df3deff3e7aaf5eede432 /sftp-server.c | |
parent | aec5cf8a30c1d44a0cc2da939d39d46cd078d602 (diff) |
- djm@cvs.openbsd.org 2008/01/21 17:24:30
[sftp-server.c]
Remove the fixed 100 handle limit in sftp-server and allocate as many
as we have available file descriptors. Patch from miklos AT szeredi.hu;
ok dtucker@ markus@
Diffstat (limited to 'sftp-server.c')
-rw-r--r-- | sftp-server.c | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/sftp-server.c b/sftp-server.c index ee0b4a62e..5c84c728c 100644 --- a/sftp-server.c +++ b/sftp-server.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sftp-server.c,v 1.74 2007/09/13 04:39:04 djm Exp $ */ | 1 | /* $OpenBSD: sftp-server.c,v 1.75 2008/01/21 17:24:30 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. |
4 | * | 4 | * |
@@ -169,6 +169,7 @@ struct Handle { | |||
169 | int fd; | 169 | int fd; |
170 | char *name; | 170 | char *name; |
171 | u_int64_t bytes_read, bytes_write; | 171 | u_int64_t bytes_read, bytes_write; |
172 | int next_unused; | ||
172 | }; | 173 | }; |
173 | 174 | ||
174 | enum { | 175 | enum { |
@@ -177,40 +178,46 @@ enum { | |||
177 | HANDLE_FILE | 178 | HANDLE_FILE |
178 | }; | 179 | }; |
179 | 180 | ||
180 | Handle handles[100]; | 181 | Handle *handles = NULL; |
182 | u_int num_handles = 0; | ||
183 | int first_unused_handle = -1; | ||
181 | 184 | ||
182 | static void | 185 | static void handle_unused(int i) |
183 | handle_init(void) | ||
184 | { | 186 | { |
185 | u_int i; | 187 | handles[i].use = HANDLE_UNUSED; |
186 | 188 | handles[i].next_unused = first_unused_handle; | |
187 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) | 189 | first_unused_handle = i; |
188 | handles[i].use = HANDLE_UNUSED; | ||
189 | } | 190 | } |
190 | 191 | ||
191 | static int | 192 | static int |
192 | handle_new(int use, const char *name, int fd, DIR *dirp) | 193 | handle_new(int use, const char *name, int fd, DIR *dirp) |
193 | { | 194 | { |
194 | u_int i; | 195 | int i; |
195 | 196 | ||
196 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) { | 197 | if (first_unused_handle == -1) { |
197 | if (handles[i].use == HANDLE_UNUSED) { | 198 | if (num_handles + 1 <= num_handles) |
198 | handles[i].use = use; | 199 | return -1; |
199 | handles[i].dirp = dirp; | 200 | num_handles++; |
200 | handles[i].fd = fd; | 201 | handles = xrealloc(handles, num_handles, sizeof(Handle)); |
201 | handles[i].name = xstrdup(name); | 202 | handle_unused(num_handles - 1); |
202 | handles[i].bytes_read = handles[i].bytes_write = 0; | ||
203 | return i; | ||
204 | } | ||
205 | } | 203 | } |
206 | return -1; | 204 | |
205 | i = first_unused_handle; | ||
206 | first_unused_handle = handles[i].next_unused; | ||
207 | |||
208 | handles[i].use = use; | ||
209 | handles[i].dirp = dirp; | ||
210 | handles[i].fd = fd; | ||
211 | handles[i].name = xstrdup(name); | ||
212 | handles[i].bytes_read = handles[i].bytes_write = 0; | ||
213 | |||
214 | return i; | ||
207 | } | 215 | } |
208 | 216 | ||
209 | static int | 217 | static int |
210 | handle_is_ok(int i, int type) | 218 | handle_is_ok(int i, int type) |
211 | { | 219 | { |
212 | return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) && | 220 | return i >= 0 && (u_int)i < num_handles && handles[i].use == type; |
213 | handles[i].use == type; | ||
214 | } | 221 | } |
215 | 222 | ||
216 | static int | 223 | static int |
@@ -300,12 +307,12 @@ handle_close(int handle) | |||
300 | 307 | ||
301 | if (handle_is_ok(handle, HANDLE_FILE)) { | 308 | if (handle_is_ok(handle, HANDLE_FILE)) { |
302 | ret = close(handles[handle].fd); | 309 | ret = close(handles[handle].fd); |
303 | handles[handle].use = HANDLE_UNUSED; | ||
304 | xfree(handles[handle].name); | 310 | xfree(handles[handle].name); |
311 | handle_unused(handle); | ||
305 | } else if (handle_is_ok(handle, HANDLE_DIR)) { | 312 | } else if (handle_is_ok(handle, HANDLE_DIR)) { |
306 | ret = closedir(handles[handle].dirp); | 313 | ret = closedir(handles[handle].dirp); |
307 | handles[handle].use = HANDLE_UNUSED; | ||
308 | xfree(handles[handle].name); | 314 | xfree(handles[handle].name); |
315 | handle_unused(handle); | ||
309 | } else { | 316 | } else { |
310 | errno = ENOENT; | 317 | errno = ENOENT; |
311 | } | 318 | } |
@@ -333,7 +340,7 @@ handle_log_exit(void) | |||
333 | { | 340 | { |
334 | u_int i; | 341 | u_int i; |
335 | 342 | ||
336 | for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) | 343 | for (i = 0; i < num_handles; i++) |
337 | if (handles[i].use != HANDLE_UNUSED) | 344 | if (handles[i].use != HANDLE_UNUSED) |
338 | handle_log_close(i, "forced"); | 345 | handle_log_close(i, "forced"); |
339 | } | 346 | } |
@@ -1271,8 +1278,6 @@ main(int argc, char **argv) | |||
1271 | logit("session opened for local user %s from [%s]", | 1278 | logit("session opened for local user %s from [%s]", |
1272 | pw->pw_name, client_addr); | 1279 | pw->pw_name, client_addr); |
1273 | 1280 | ||
1274 | handle_init(); | ||
1275 | |||
1276 | in = dup(STDIN_FILENO); | 1281 | in = dup(STDIN_FILENO); |
1277 | out = dup(STDOUT_FILENO); | 1282 | out = dup(STDOUT_FILENO); |
1278 | 1283 | ||