Why i am having free(): invalid next size (normal) error. here is code
#define _GNU_SOURCE
#include<stdio.h> #include<stdlib.h> #include<sys/queue.h> #include<string.h> #include<fcntl.h> #include<unistd.h> #include "cal_record_header.h" #include "cal_handle_recording.h" #define LUN_LOGGING 1 #define CLIENT_LOGGING 2 #define DELAY 100000 #define SUCCESS 0 #define FILE_OPENING_ERROR 1 #define MEMORY_ALLOCATION_ERROR 3 #define INVALID_LOG_FILE 4 #define INVALID_COMMAND_LINE_OPTION 5 file_header fileheader; int policy=2; int output_options[10]={0}; int req_flag; typedef struct filepointer { int flag; __u32 policy_id; }FILE_POINTERS; typedef struct file_names { char name[100]; }filenames; struct details { __u32 unique_id; char *device_name; char *device_id; }mapping_details[200]; int mapping_cnt=0; //function declarations int parse_arguments(int,char*[]); int usage_message(); int apply_policy(int); int islogfile(int,int,char*); //end of function declarations int main(int argc, char *argv[]) { system("clear"); if(argc > 2) { return(usage_message()); } else return(parse_arguments(argc, argv)); }//end of main int usage_message() { printf("\nUsage: Filesplitter [-options] "); printf("\nOptions :-"); printf("\n -h/-H help"); printf("\n -v/-V verbose"); printf("\nIf no options are specified then default option will be without verbose."); printf("\nOn execution file splitter will open file /opt/nHosts/etc/replay_config."); printf("\nThen it will split the giant files specified in replay_config file."); printf("\nThese splitted files will be placed in /nHosts/tmp/split directory."); printf("\nExample :./Filesplitter"); printf("\nExample :./Filesplitter -v \n\n"); return(INVALID_COMMAND_LINE_OPTION); } int parse_arguments(int argc, char *argv[]) { if(argc ==2) { if(argv[1][0]!='-') { return(usage_message()); } else { switch(argv[1][1]) { case 'v' : output_options[0] = 1; break; case 'V' : output_options[0] = 1; break; default : return(usage_message()); }//end of switch }//end of else return(apply_policy(argc)); }//end of if else { return(apply_policy(argc)); }//end of else }//end of function parse_arugments int islogfile(int fd,int fd1,char *name) { char *header=NULL,*desc=NULL,*temp_buff=NULL,*p=NULL,*num=NULL,ch,*t=" \n"; int location1=0,location2=0,bus,channel,id,lun,i,j,ret; header=(char *)malloc(sizeof(char)*5); read(fd,header,4); read(fd,&ch,sizeof(char));//for : read(fd,&ch,sizeof(char));//for /n header[5]='\0'; if(strncmp("SOFH",header,4)!=0) { printf("\nFile %s is not a valid log file. \n",name); return(-1); } else { if(read(fd,&fileheader,sizeof(file_header))) { if(fileheader.desc_size) { desc=(char *)malloc(fileheader.desc_size+1); read(fd,desc,fileheader.desc_size+1); } read(fd,header,sizeof(char)*5); //for:EOFH read(fd,&ch,sizeof(char)); //for \n if(strncmp(":EOFH",header,5)!=0) { printf("\nGiven file %s is not a valid log file. \n",name); return(-1); } if(output_options[0]==1) { printf("\n\nINPUT FILE DETAILS"); printf("\nFile Name : %s",name); printf("\nFile Version : %s",fileheader.version); printf("\nFile Description : %s",desc); switch(fileheader.is_data_logged) { case 1: printf("\nFile Data : File is logged without data for Read/Write commands."); break; case 2: printf("\nFile Data : File is logged without data for Read commands."); break; case 3: printf("\nFile Data : File is logged without data for Write commands."); break; case 4: printf("\nFile Data : File is logged with data for all commands."); break; }//end of switch }//end of if }//end of if }//end of else if(desc) { free(desc); //desc is no more of use desc=NULL; } location1=lseek64(fd,0,SEEK_CUR); lseek64(fd1,location1,SEEK_SET); read(fd,header,sizeof(char)*5); if(strncmp("SODH:",header,5)!=0) { printf("\nGiven file %s is not a valid log file. \n",name); return(-1); } read(fd,&ch,sizeof(char));//for \n while(1) { read(fd,&ch,sizeof(char)); if(ch==':') { read(fd,header,sizeof(char)*4); header[4]='\0'; if(strncmp(header,"EODH",4)==0) break; } }//end of while location2= lseek64(fd,0,SEEK_CUR); temp_buff=(char *)malloc(location2-location1-11); read(fd1,header,sizeof(char)*4); read(fd1,&ch,sizeof(char)); read(fd1,&ch,sizeof(char)); read(fd1,temp_buff,location2-location1-5); read(fd,&ch,sizeof(char)); printf("\nHeader is : \n%s",temp_buff); ret=lseek(fd,0,SEEK_CUR);//used to return value from islogfile location1=1; //reusing variable for parsing p=strtok(temp_buff,t); while(p!=NULL) { switch(location1) { case 1: mapping_details[mapping_cnt].device_id=(char *)malloc(sizeof(p)); strcpy(mapping_details[mapping_cnt].device_id,p); location1++; break; case 2: mapping_details[mapping_cnt].device_name=(char *)malloc(sizeof(p)); strcpy(mapping_details[mapping_cnt].device_name,p); location1++; break; case 3: if(strstr(mapping_details[mapping_cnt].device_name,"-")!=NULL) { mapping_details[mapping_cnt].device_name=(char *)malloc(sizeof(p)); strcpy(mapping_details[mapping_cnt].device_name,p); } location1=1; mapping_cnt++; break; }//end of switch p=strtok(NULL,t); }//end of while t="[:]"; for(location1=0;location1<mapping_cnt;location1++) { location2=1; num=NULL; num=(char *)malloc(sizeof(mapping_details[location1].device_id)); strcpy(num,mapping_details[location1].device_id); p=strtok(num,t); while(p!=NULL) { switch(location2) { case 1: bus=atoi(p);location2++;break; case 2: channel=atoi(p);location2++;break; case 3: id=atoi(p);location2++;break; case 4: lun=atoi(p);location2++;break; }//end of switch p=strtok(NULL,t); }//end of while mapping_details[location1].unique_id=(bus<<24)+(channel<<16)+(id<<8)+lun; //printf("\nBus: %d, channel :%d id:%d, lun :%d ",bus,channel,id,lun); //printf("\n%d)Device: %s Device id : %s Unique id : 0x%08x",location1,mapping_details[location1].device_name,mapping_details[location1].device_id,mapping_details[location1].unique_id); }//end of for if(header) { free(header); header=NULL; } printf("\nGiven file %s is successfully verified.",name); return(ret); }//end of function is log file int apply_policy(int argc) { FILE_POINTERS file_pointer[200]; filenames FILENAMES[200]; filenames GIANT_FILENAMES[30]; __u32 policy_id,id; int input_file_pointer,response_pointer; FILE *config_file_pointer=NULL; log_header *logheader=NULL; scsi_cmd_req *req=NULL; scsi_cmd_resp *resp=NULL; char input_file_name[4096],path_name[4096]; char ch,s[1024],*t=" ",*p=NULL,*buff=NULL,*fname=NULL,*temp_fname=NULL; int i,giant_file_counter=0,file_counter=0,flag,curReqLoc,curr_pos; int splitter_cnt,maxlength=0; config_file_pointer=fopen("/opt/nHosts/etc/replay_config","r"); if(config_file_pointer == NULL) { printf("\nConfiguration file doesn't exist or Error in opening file. \n\n"); return(FILE_OPENING_ERROR); } else { while((fgets(s,1023,config_file_pointer))!=(char *)0) { p=strrchr(s,'.'); if(p!=NULL) { p=strtok(s,t); flag=0; //for loop used for checking duplicate names for(i=0;i<giant_file_counter;i++) { if(strcmp(p,GIANT_FILENAMES[i].name)==0) flag=1; }//end of for if(flag==0) { strcpy(GIANT_FILENAMES[giant_file_counter].name,p); giant_file_counter++; } }//end of if }//end of while }//end of else of if used for checking configuration file pointer not null printf("\nTotal giant files in replay configuration file are : "); for(i=0;i<giant_file_counter;i++) { printf("\nFile name : %s",GIANT_FILENAMES[i].name); fflush(stdout); } logheader=(log_header *)malloc(sizeof(log_header)); req=(scsi_cmd_req *)malloc(sizeof(scsi_cmd_req)); resp=(scsi_cmd_resp *)malloc(sizeof(scsi_cmd_resp)); ///////////////////Loop for scanning all the giant file and splitting them for(splitter_cnt=0;splitter_cnt!=giant_file_counter;splitter_cnt++) { maxlength=0; curr_pos=0; file_counter=1; strcpy(input_file_name,GIANT_FILENAMES[splitter_cnt].name); input_file_pointer=open(input_file_name, O_RDONLY | O_LARGEFILE); response_pointer=open(input_file_name, O_RDONLY | O_LARGEFILE); if(input_file_pointer < 0 || response_pointer<0) { printf("\nFile doesn't exist or Error in opening file %s\n",input_file_name); return(FILE_OPENING_ERROR); } //for loop use for initializing the file pointer structure //This structure is used to store file discriptors of splitted files for(i=0;i<200;i++) { file_pointer[i].flag=0; file_pointer[i].policy_id=0; } // islogfile() returns negative value when file is not valid log file //but the program must be continue for other log files. so if negative //it directly send to end of for. for this GOTO is used. i=islogfile(input_file_pointer,response_pointer,input_file_name); if(i<0) { // goto ff; } lseek64(input_file_pointer,i,SEEK_SET); printf("\n\nSplitting for giant file %s started. Please wait....",input_file_name); fflush(stdout); ///////main while loop starting here/////////// while(1) { ////while for searching requests starts here while(1) { printf("\nIn while loop of request searching"); req_flag=0; curReqLoc=lseek64(input_file_pointer,0,SEEK_CUR); lseek64(response_pointer,curReqLoc,SEEK_SET); if(read(input_file_pointer,logheader,sizeof(log_header))==0) { for(i=0;i<=maxlength;i++) close(file_pointer[i].flag); printf("\nTotal number of newly created file is : %d",file_counter-1); printf("\nSplitted file names are as follows : "); for(i=1;i<file_counter;i++) { printf("\n%d) %s",i,FILENAMES[i].name); } fflush(stdout); goto zz; //cant use return as their may be more no. of log files; // so going out of main while and continuing with giant for loop }//end of if //following if is used for checking for request if(output_options[0]==1) { printf("\n Now printing logheader"); printlogheader(logheader); getchar(); } if(logheader->is_request==0) { lseek64(input_file_pointer,logheader->recordSize - sizeof(log_header),SEEK_CUR); continue; } //follwing if used for set policy id if(policy==1) policy_id=logheader->initiator_id; else policy_id=logheader->unique_id; //logic for searching particular file if(file_pointer[curr_pos].policy_id==policy_id) goto xx; for(i=0;i<=maxlength;i++) { //printf("\npolicy_id : %llu, file_pointer[i].policy id: %llu",policy_id,file_pointer[i].policy_id); if(file_pointer[i].policy_id==policy_id) { curr_pos=i; goto xx; } }//end of for curr_pos=maxlength; xx: if(file_pointer[curr_pos].flag==0) { file_pointer[curr_pos].policy_id=policy_id; fname=(char *)malloc(sizeof(char)*1024); temp_fname=(char *)malloc(sizeof(char)*1024); strcpy(fname,"/nHosts/tmp/split/"); strcpy(temp_fname,input_file_name); temp_fname=strrchr(temp_fname,'/')+1; temp_fname=strtok(temp_fname,"."); strcat(fname,temp_fname); for(i=0;i<mapping_cnt;i++) { id=mapping_details[i].unique_id; if(id==file_pointer[curr_pos].policy_id) { strcpy(temp_fname,mapping_details[i].device_name); temp_fname=strrchr(temp_fname,'/')+1; break; } }//end of for strcat(fname,"_"); strcat(fname,temp_fname); if((file_pointer[curr_pos].flag=open(fname, O_CREAT | O_RDWR | O_LARGEFILE)) < 0) { printf("\nError in opening file %s. Check the path. Quitting............",fname); getchar(); return(FILE_OPENING_ERROR); } strcpy(FILENAMES[file_counter].name,fname); file_counter++; maxlength++; write(file_pointer[curr_pos].flag,&fileheader,sizeof(file_header)); write(file_pointer[curr_pos].flag,":EOH\n",5); if(fname) { free(fname); fname=NULL; } /*if(temp_fname) { printf("\nTest"); getchar(); free(temp_fname); temp_fname=NULL; printf("\nTest"); getchar(); }*/ }//end of if ////////////end of searching logic //now writing record by taking total record size // buff=(char *)realloc(buff,logheader->recordSize); buff=(char *)malloc(logheader->recordSize); if(buff==NULL) { printf("\nMalloc error"); exit(MEMORY_ALLOCATION_ERROR); } if(output_options[0]==1) { printf("\n Logheader->record size = %llu",logheader->recordSize); getchar(); } read(response_pointer,buff,logheader->recordSize); write(file_pointer[curr_pos].flag,buff,logheader->recordSize); read(input_file_pointer,&ch,sizeof(char)); read(input_file_pointer,req,sizeof(scsi_cmd_req)); if(buff) { free(buff); buff=NULL; } break; }//end of while for searching request curReqLoc=lseek64(response_pointer,0,SEEK_CUR); lseek64(input_file_pointer,curReqLoc,SEEK_SET); while(1) { printf("\nIn while loop of response searching"); curReqLoc=lseek64(response_pointer,0,SEEK_CUR); if(read(response_pointer,logheader,sizeof(log_header))==0) { if(req_flag==0) { printf("\nWhile searching response logheader size found zero. So no more response found for the request until the file ended. No more requests. So Quittig........\n"); goto zz; } else break; } if(logheader->is_request) { req_flag=1; lseek64(response_pointer,logheader->recordSize-sizeof(log_header),SEEK_CUR); continue; } read(response_pointer,&ch,sizeof(char)); // for : read(response_pointer,resp,sizeof(scsi_cmd_resp)); if(resp->serial != req->serial) { lseek64(response_pointer,logheader->recordSize-sizeof(log_header)-sizeof(scsi_cmd_resp)-1,SEEK_CUR); continue; } else { //Request response matching lseek64(response_pointer,curReqLoc,SEEK_SET); // buff=realloc(buff,logheader->recordSize) buff=(char *)malloc(200); if(buff==NULL) { printf("\nMalloc error"); exit(MEMORY_ALLOCATION_ERROR); } if(output_options[0]==1) { printf("\n Logheader->record size = %llu",logheader->recordSize); getchar(); } read(response_pointer,buff,200); write(file_pointer[curr_pos].flag,buff,200); if(buff) { free(buff); buff=NULL; } if(output_options[0]==1) { printf("\n Printing"); getchar(); } break; }//end of else }//end of while of searching response }//end of main while zz: printf("\nSplitting completed for given giant file."); ff: printf("\n"); }//end of for of giant file loop ///////////////////Loop for scanning all the giant file and splitting them ends here return(SUCCESS); }//end of function apply policy |
All times are GMT -5. The time now is 09:46 PM. |