From 1cee71713f75dbee653ea86bd4e7c87efe677cf6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 31 May 2011 16:14:04 -0700 Subject: Change sys_getcd() to take no arguments and always return malloc'ed memory (or NULL). Part of the efforts to remove PATH_MAX on modern systems. --- source3/client/client.c | 5 +++-- source3/include/proto.h | 2 +- source3/lib/system.c | 41 +++++++++++++++++++++++++++++++++------- source3/modules/vfs_default.c | 6 ++++-- source3/printing/print_generic.c | 16 ++++++++-------- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/source3/client/client.c b/source3/client/client.c index 7cffddb76c..f088c7c815 100644 --- a/source3/client/client.c +++ b/source3/client/client.c @@ -3876,11 +3876,12 @@ static int cmd_lcd(void) buf, strerror(errno)); } } - d = TALLOC_ARRAY(ctx, char, PATH_MAX+1); + d = sys_getwd(); if (!d) { return 1; } - DEBUG(2,("the local directory is now %s\n",sys_getwd(d))); + DEBUG(2,("the local directory is now %s\n",d)); + SAFE_FREE(d); return 0; } diff --git a/source3/include/proto.h b/source3/include/proto.h index 23654e1ec6..83382594f9 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -391,7 +391,7 @@ void sys_rewinddir(SMB_STRUCT_DIR *dirp); int sys_closedir(SMB_STRUCT_DIR *dirp); int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev); int sys_waitpid(pid_t pid,int *status,int options); -char *sys_getwd(char *s); +char *sys_getwd(void); void set_effective_capability(enum smbd_capability capability); void drop_effective_capability(enum smbd_capability capability); long sys_random(void); diff --git a/source3/lib/system.c b/source3/lib/system.c index 0dd4b81a43..a6f7de0af5 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -959,18 +959,45 @@ int sys_waitpid(pid_t pid,int *status,int options) } /******************************************************************* - System wrapper for getwd + System wrapper for getwd. Always returns MALLOC'ed memory, or NULL + on error (malloc fail usually). ********************************************************************/ -char *sys_getwd(char *s) +char *sys_getwd(void) { - char *wd; -#ifdef HAVE_GETCWD - wd = (char *)getcwd(s, PATH_MAX); +#ifdef GETCWD_TAKES_NULL + return getcwd(NULL, 0); +#elif HAVE_GETCWD + char *wd = NULL, *s = NULL; + size_t allocated = PATH_MAX; + + while (1) { + s = SMB_REALLOC_ARRAY(s, char, allocated); + if (s == NULL) { + return NULL; + } + wd = getcwd(s, allocated); + if (wd) { + break; + } + if (errno != ERANGE) { + SAFE_FREE(s); + break; + } + allocated *= 2; + if (allocated < PATH_MAX) { + SAFE_FREE(s); + break; + } + } + return wd; #else - wd = (char *)getwd(s); + char *s = SMB_MALLOC_ARRAY(char, PATH_MAX); + if (s == NULL) { + return NULL; + } + return getwd(s); #endif - return wd; } #if defined(HAVE_POSIX_CAPABILITIES) diff --git a/source3/modules/vfs_default.c b/source3/modules/vfs_default.c index 4d06a10f42..faacf25599 100644 --- a/source3/modules/vfs_default.c +++ b/source3/modules/vfs_default.c @@ -753,9 +753,11 @@ static char *vfswrap_getwd(vfs_handle_struct *handle, char *path) char *result; START_PROFILE(syscall_getwd); - result = sys_getwd(path); + result = sys_getwd(); END_PROFILE(syscall_getwd); - return result; + /* FIXME - with a VFS change. JRA !! */ + strlcpy(path, result, PATH_MAX); + return path; } /********************************************************************* diff --git a/source3/printing/print_generic.c b/source3/printing/print_generic.c index b925bedace..14f4c6dbe7 100644 --- a/source3/printing/print_generic.c +++ b/source3/printing/print_generic.c @@ -155,17 +155,17 @@ static int generic_job_submit(int snum, struct printjob *pjob) /* we print from the directory path to give the best chance of parsing the lpq output */ - current_directory = TALLOC_ARRAY(ctx, - char, - PATH_MAX+1); - if (!current_directory) { - return -1; - } - wd = sys_getwd(current_directory); + wd = sys_getwd(); if (!wd) { return -1; } + current_directory = talloc_strdup(ctx, wd); + SAFE_FREE(wd); + + if (!current_directory) { + return -1; + } print_directory = talloc_strdup(ctx, pjob->filename); if (!print_directory) { return -1; @@ -205,7 +205,7 @@ static int generic_job_submit(int snum, struct printjob *pjob) out: - if (chdir(wd) == -1) { + if (chdir(current_directory) == -1) { smb_panic("chdir failed in generic_job_submit"); } TALLOC_FREE(current_directory); -- cgit