[icinga-checkins] icinga.org: icinga-core/mfriedrich/core: base/nebmods.c: Replace local file-copy hack with my_fcopy() (Andreas Ericsson)

git at icinga.org git at icinga.org
Wed May 12 23:06:36 CEST 2010


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

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Wed May 12 22:56:07 2010 +0200

base/nebmods.c: Replace local file-copy hack with my_fcopy() (Andreas Ericsson)

[--snip--]
my_fcopy() 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.

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.
[--snip--]

fixes #424

---

 Changelog      |    1 +
 base/nebmods.c |   23 ++++-------------------
 2 files changed, 5 insertions(+), 19 deletions(-)

diff --git a/Changelog b/Changelog
index 10147dd..36e60d5 100644
--- a/Changelog
+++ b/Changelog
@@ -42,6 +42,7 @@ FIXES
 * core: header files: Remove dead prototypes (Andreas Ericsson)
 * core: remove grab_contactgroup_macros() and its prototype (Andreas Ericsson)
 * core: base/commands.c: Fix error path of opening checkresult files (Andreas Ericsson)
+* core: base/nebmods.c: Replace local file-copy hack with my_fcopy() (Andreas Ericsson)
 
 * idoutils: Host DB inserts use string 'NULL\n' instead of NULL (William Preston)
 * idoutils: ndo2db_get_object_id fails to return existing IDs (William Preston)
diff --git a/base/nebmods.c b/base/nebmods.c
index ce64030..422f7e8 100644
--- a/base/nebmods.c
+++ b/base/nebmods.c
@@ -167,10 +167,6 @@ int neb_load_module(nebmodule *mod){
 	int (*initfunc)(int,char *,void *);
 	int *module_version_ptr=NULL;
 	char *output_file=NULL;
-	int dest_fd=-1;
-	int source_fd=-1;
-	char buffer[MAX_INPUT_BUFFER]={0};
-	int bytes_read=0;
 	int result=OK;
 
 	if(mod==NULL || mod->filename==NULL)
@@ -200,22 +196,11 @@ int neb_load_module(nebmodule *mod){
 
 	/* open a temp file for copying the module */
 	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));
+	if (my_fcopy(mod->filename, output_file) == 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;
-		}
-	/* 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;
-		}
-
+	}
 	/* load the module (use the temp copy we just made) */
 #ifdef USE_LTDL
 	mod->module_handle=lt_dlopen(output_file);





More information about the icinga-checkins mailing list