summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@cvs.openbsd.org>2008-01-21 17:24:31 +0000
committerDamien Miller <djm@cvs.openbsd.org>2008-01-21 17:24:31 +0000
commitdd33d768f22fd7735689ab6c1b328b7816d51f62 (patch)
treef91348d81768e61dd116f177adc22200ed017871
parent99b30812d91fc9e566ac4107df9a01dd30e11d87 (diff)
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@
-rw-r--r--usr.bin/ssh/sftp-server.c57
1 files changed, 31 insertions, 26 deletions
diff --git a/usr.bin/ssh/sftp-server.c b/usr.bin/ssh/sftp-server.c
index 1df83511273..fb288fc6e86 100644
--- a/usr.bin/ssh/sftp-server.c
+++ b/usr.bin/ssh/sftp-server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp-server.c,v 1.74 2007/09/13 04:39:04 djm Exp $ */
+/* $OpenBSD: sftp-server.c,v 1.75 2008/01/21 17:24:30 djm Exp $ */
/*
* Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
*
@@ -164,6 +164,7 @@ struct Handle {
int fd;
char *name;
u_int64_t bytes_read, bytes_write;
+ int next_unused;
};
enum {
@@ -172,40 +173,46 @@ enum {
HANDLE_FILE
};
-Handle handles[100];
+Handle *handles = NULL;
+u_int num_handles = 0;
+int first_unused_handle = -1;
-static void
-handle_init(void)
+static void handle_unused(int i)
{
- u_int i;
-
- for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
- handles[i].use = HANDLE_UNUSED;
+ handles[i].use = HANDLE_UNUSED;
+ handles[i].next_unused = first_unused_handle;
+ first_unused_handle = i;
}
static int
handle_new(int use, const char *name, int fd, DIR *dirp)
{
- u_int i;
+ int i;
- for (i = 0; i < sizeof(handles)/sizeof(Handle); i++) {
- if (handles[i].use == HANDLE_UNUSED) {
- handles[i].use = use;
- handles[i].dirp = dirp;
- handles[i].fd = fd;
- handles[i].name = xstrdup(name);
- handles[i].bytes_read = handles[i].bytes_write = 0;
- return i;
- }
+ if (first_unused_handle == -1) {
+ if (num_handles + 1 <= num_handles)
+ return -1;
+ num_handles++;
+ handles = xrealloc(handles, num_handles, sizeof(Handle));
+ handle_unused(num_handles - 1);
}
- return -1;
+
+ i = first_unused_handle;
+ first_unused_handle = handles[i].next_unused;
+
+ handles[i].use = use;
+ handles[i].dirp = dirp;
+ handles[i].fd = fd;
+ handles[i].name = xstrdup(name);
+ handles[i].bytes_read = handles[i].bytes_write = 0;
+
+ return i;
}
static int
handle_is_ok(int i, int type)
{
- return i >= 0 && (u_int)i < sizeof(handles)/sizeof(Handle) &&
- handles[i].use == type;
+ return i >= 0 && (u_int)i < num_handles && handles[i].use == type;
}
static int
@@ -295,12 +302,12 @@ handle_close(int handle)
if (handle_is_ok(handle, HANDLE_FILE)) {
ret = close(handles[handle].fd);
- handles[handle].use = HANDLE_UNUSED;
xfree(handles[handle].name);
+ handle_unused(handle);
} else if (handle_is_ok(handle, HANDLE_DIR)) {
ret = closedir(handles[handle].dirp);
- handles[handle].use = HANDLE_UNUSED;
xfree(handles[handle].name);
+ handle_unused(handle);
} else {
errno = ENOENT;
}
@@ -328,7 +335,7 @@ handle_log_exit(void)
{
u_int i;
- for (i = 0; i < sizeof(handles)/sizeof(Handle); i++)
+ for (i = 0; i < num_handles; i++)
if (handles[i].use != HANDLE_UNUSED)
handle_log_close(i, "forced");
}
@@ -1249,8 +1256,6 @@ main(int argc, char **argv)
logit("session opened for local user %s from [%s]",
pw->pw_name, client_addr);
- handle_init();
-
in = dup(STDIN_FILENO);
out = dup(STDOUT_FILENO);