another [patch] Solaris fix for cfg_dir Re: Bug: cfg_dir doesn't work on solaris..?

Jan Grant jan.grant at bristol.ac.uk
Wed Jan 23 20:03:57 CET 2008


On Wed, 23 Jan 2008, Andreas Ericsson wrote:

> Care to resend?

Attached; much cleaner. Seems ok on Solaris. Note, existing indentation 
style preserved.

Cheers,
jan

-- 
jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/
Tel +44 (0)117 3317661   http://ioctl.org/jan/
"I like oranges more than apples!?" - that's like comparing apples and oranges!
-------------- next part --------------
--- xdata/xodtemplate.c.orig	Wed Jan 23 14:56:37 2008
+++ xdata/xodtemplate.c	Wed Jan 23 19:00:46 2008
@@ -369,7 +369,7 @@
 
 	/* open the directory for reading */
 	dirp=opendir(dirname);
-        if(dirp==NULL){
+	if(dirp==NULL){
 #ifdef NSCORE
 		snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: Could not open config directory '%s' for reading.\n",dirname);
 		temp_buffer[sizeof(temp_buffer)-1]='\x0';
@@ -381,139 +381,57 @@
 	/* process all files in the directory... */
 	while((dirfile=readdir(dirp))!=NULL){
 
+		/* Skip hidden files and directories, and current and parent dir */
+		if(dirfile->d_name[0]=='.')
+			continue;
+
 		/* create /path/to/file */
 		snprintf(file,sizeof(file),"%s/%s",dirname,dirfile->d_name);
 		file[sizeof(file)-1]='\x0';
 
 		/* process this if it's a non-hidden config file... */
-		x=strlen(dirfile->d_name);
-		if(x>4 && dirfile->d_name[0]!='.' && !strcmp(dirfile->d_name+(x-4),".cfg")){
-
-#ifdef _DIRENT_HAVE_D_TYPE
-			/* only process normal files and symlinks */
-			if(dirfile->d_type==DT_UNKNOWN){
-				x=stat(file,&stat_buf);
-				if(x==0){
-					if(!S_ISREG(stat_buf.st_mode) && !S_ISLNK(stat_buf.st_mode))
-						continue;
-				        }
-			        }
-			else{
-				if(dirfile->d_type!=DT_REG && dirfile->d_type!=DT_LNK)
-					continue;
-			        }
+		if (stat(file,&stat_buf) == -1) {
+			/* An error */
+#ifdef NSCORE
+			snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: Could not open config directory member '%s' for reading.\n",file);
+			temp_buffer[sizeof(temp_buffer)-1]='\x0';
+			write_to_logs_and_console(temp_buffer,NSLOG_CONFIG_ERROR,TRUE);
 #endif
+			closedir(dirp);
+			return ERROR;
+			}
 
-			/* process the config file */
-			result=xodtemplate_process_config_file(file,options);
+		switch(stat_buf.st_mode & S_IFMT){
 
-			/* break out if we encountered an error */
-			if(result==ERROR)
-				break;
-		        }
+			case S_IFREG:
+				x=strlen(dirfile->d_name);
+				if(x<=4 || strcmp(dirfile->d_name+(x-4),".cfg"))
+					break;
 
-#ifdef _DIRENT_HAVE_D_TYPE
-		/* recurse into subdirectories... */
-		if(dirfile->d_type==DT_UNKNOWN || dirfile->d_type==DT_DIR || dirfile->d_type==DT_LNK){
+				/* process the config file */
+				result=xodtemplate_process_config_file(file,options);
 
-			if(dirfile->d_type==DT_UNKNOWN){
-				x=stat(file,&stat_buf);
-				if(x==0){
-					if(!S_ISDIR(stat_buf.st_mode) && !S_ISLNK(stat_buf.st_mode))
-						continue;
-				        }
-				else
-					continue;
-			        }
-
-			/* ignore current, parent and hidden directory entries */
-			if(dirfile->d_name[0]=='.')
-				continue;
-
-			/* check that a symlink points to a dir */
-
-			if(dirfile->d_type==DT_LNK || (dirfile->d_type==DT_UNKNOWN && S_ISLNK(stat_buf.st_mode))){
-
-				readlink_count=readlink(file,link_buffer,MAX_FILENAME_LENGTH);
-
-				/* Handle special case with maxxed out buffer */
-				if(readlink_count==MAX_FILENAME_LENGTH){
-#ifdef NSCORE
-					snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: Cannot follow symlink '%s' - Too big!\n",file);
-					temp_buffer[sizeof(temp_buffer)-1]='\x0';
-					write_to_logs_and_console(temp_buffer,NSLOG_CONFIG_ERROR,TRUE);
-#endif
+				if(result==ERROR){
+					closedir(dirp);
 					return ERROR;
-				        }
+					}
 
-				/* Check if reading symlink failed */
-				if(readlink_count==-1){
-#ifdef NSCORE
-					snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: Cannot read symlink '%s': %s!\n",file, strerror(errno));
-					temp_buffer[sizeof(temp_buffer)-1]='\x0';
-					write_to_logs_and_console(temp_buffer,NSLOG_CONFIG_ERROR,TRUE);
-#endif
-					return ERROR;
-				        }
+				break;
 
-				/* terminate string */
-				link_buffer[readlink_count]='\x0';
-			
-				/* create new symlink buffer name */
-				if(link_buffer[0]=='/'){
-					/* full path */
-					snprintf(linked_to_buffer,sizeof(linked_to_buffer)-1,"%s",link_buffer);
-					linked_to_buffer[sizeof(linked_to_buffer)-1]='\x0';
-				        }
-				else{
-					/* relative path */
-					snprintf(linked_to_buffer,sizeof(linked_to_buffer)-1,"%s/%s",dirname,link_buffer);
-					linked_to_buffer[sizeof(linked_to_buffer)-1]='\x0';
-				        }
+			case S_IFDIR:
+				/* recurse into subdirectories... */
+				result=xodtemplate_process_config_dir(file,options);
 
-				/* 
-				 * At this point, we know it's a symlink -
-				 * now check for whether it points to a 
-				 * directory or not
-				 */
-
-				x=stat(linked_to_buffer,&stat_buf);
-				if(x!=0){
-
-					/* non-existent symlink - bomb out */
-					if(errno==ENOENT){
-#ifdef NSCORE
-						snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: symlink '%s' points to non-existent '%s'!\n",file,link_buffer);
-						temp_buffer[sizeof(temp_buffer)-1]='\x0';
-						write_to_logs_and_console(temp_buffer,NSLOG_CONFIG_ERROR,TRUE);
-#endif
-						return ERROR;
-					        }
-
-#ifdef NSCORE
-					snprintf(temp_buffer,sizeof(temp_buffer)-1,"Error: Cannot stat symlinked from '%s' to %s!\n",file,link_buffer);
-					temp_buffer[sizeof(temp_buffer)-1]='\x0';
-					write_to_logs_and_console(temp_buffer,NSLOG_CONFIG_ERROR,TRUE);
-#endif
+				if(result==ERROR){
+					closedir(dirp);
 					return ERROR;
-				        }
+					}
 
-				if(!S_ISDIR(stat_buf.st_mode)){
-					/* Not a symlink to a dir - skip */
-					continue;
-				        }
+				break;
 
-				/* Otherwise, we may proceed! */
-			        }
+			/* Everything else we ignore */
+			}
 
-			/* process the config directory */
-			result=xodtemplate_process_config_dir(file,options);
-
-			/* break out if we encountered an error */
-			if(result==ERROR)
-				break;
-		        }
-#endif
 		}
 
 	closedir(dirp);
@@ -523,7 +441,7 @@
 #endif
 
 	return result;
-        }
+	}
 
 
 /* process data in a specific config file */
-------------- next part --------------
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list