[icinga-checkins] icinga.org: icinga-core/master: base/nebmods: Replace local file-copy hack with my_fdcopy() (Andreas Ericsson) #424

git at icinga.org git at icinga.org
Fri Nov 5 08:30:55 CET 2010


Module: icinga-core
Branch: master
Commit: efc8a231b907ef16b4b8330240ad74bf1436317d
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=efc8a231b907ef16b4b8330240ad74bf1436317d

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Mon Oct 25 13:12:51 2010 +0200

base/nebmods: Replace local file-copy hack with my_fdcopy() (Andreas Ericsson) #424

my_f{,d}copy() exists so we can copy files safely and have all
the error checking (and logging) done in one place. Implementing
a local version of it with less error-checking and logging is a
Bad Idea(tm).

While we're at it, we also make sure to release the output_file
variable if we fail to copy the file, plugging a small and largely
insignificant leak.

Signed-off-by: Andreas Ericsson <ae at op5.se>

Author: Andreas Ericsson <ae at op5.se>

refs #424

---

 Changelog      |    1 +
 base/nebmods.c |   36 +++++++++++++++---------------------
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/Changelog b/Changelog
index f220157..bbccadf 100644
--- a/Changelog
+++ b/Changelog
@@ -9,6 +9,7 @@ ENHANCEMENTS
 
 FIXES
 * core: base/utils: Refactor my_fcopy() (Andreas Ericsson) #427
+* core: base/nebmods: Replace local file-copy hack with my_fdcopy() (Andreas Ericsson) #424
 
 
 1.2.1 - 25/10/2010
diff --git a/base/nebmods.c b/base/nebmods.c
index 697e95a..137c9cf 100644
--- a/base/nebmods.c
+++ b/base/nebmods.c
@@ -167,11 +167,7 @@ int neb_load_module(nebmodule *mod){
 	int (*initfunc)(int,char *,void *);
 	int *module_version_ptr=NULL;
 	char *output_file=NULL;
-	int result=OK;
-        int dest_fd=-1;
-        int source_fd=-1;
-        char buffer[MAX_INPUT_BUFFER]={0};
-        int bytes_read=0;
+	int dest_fd, result=OK;
 
 
 	if(mod==NULL || mod->filename==NULL)
@@ -199,23 +195,21 @@ int neb_load_module(nebmodule *mod){
 	   So... the trick is to (1) copy the module to a temp file, (2) dlopen() the temp file, and (3) immediately delete the temp file. 
 	************/
 
-	/* open a temp file for copying the module */
+	/*
+	 * open a temp file for copying the module. We use my_fdcopy() so
+	 * we re-use the destination file descriptor returned by mkstemp(3),
+	 * which we have to close ourselves.
+	 */
 	asprintf(&output_file,"%s/nebmodXXXXXX",temp_path);
-        if((dest_fd=mkstemp(output_file))==-1){
-                logit(NSLOG_RUNTIME_ERROR,FALSE,"Error: Could not safely copy module '%s'.  The module will not be loaded: %s\n",mod->filename,strerror(errno));
-		return ERROR;
-		}
-        /* open module file for reading and copy it */
-        if((source_fd=open(mod->filename,O_RDONLY,0644))>0){
-                while((bytes_read=read(source_fd,buffer,sizeof(buffer)))>0)
-                        write(dest_fd,buffer,bytes_read);
-                close(source_fd);
-                close(dest_fd);
-                }
-        else{
-                logit(NSLOG_RUNTIME_ERROR,FALSE,"Error: Could not safely copy module '%s'.  The module will not be loaded: %s\n",mod->filename,strerror(errno));
-                return ERROR;
-                }
+
+	dest_fd = mkstemp(output_file);
+	result = my_fdcopy(mod->filename, output_file, dest_fd);
+	close(dest_fd);
+	if (result == ERROR) {
+		logit(NSLOG_RUNTIME_ERROR,FALSE,"Error: Failed to safely copy module '%s'. The module will not be loaded\n", mod->filename);
+		free(output_file);
+ 		return ERROR;
+	}
 
 	/* load the module (use the temp copy we just made) */
 #ifdef USE_LTDL





More information about the icinga-checkins mailing list