static void post_socketpair(struct syscallrecord *rec) { //TODO: on success we should put the fd's that // were created into a child-local fd array. freeptr(&rec->a4); }
static void post_sendmsg(__unused__ struct syscallrecord *rec) { struct msghdr *msg = (struct msghdr *) rec->a2; if (msg != NULL) { free(msg->msg_name); // free sockaddr freeptr(&rec->a2); } }
static void post_bpf(struct syscallrecord *rec) { union bpf_attr *attr = (union bpf_attr *) rec->a2; switch (rec->a1) { case BPF_MAP_CREATE: //TODO: add fd to local object cache break; case BPF_PROG_LOAD: //TODO: add fd to local object cache if (attr->prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { void *ptr = (void *) attr->insns; free(ptr); } break; default: break; } freeptr(&rec->a2); }
static void post_send(struct syscallrecord *rec) { freeptr(&rec->a2); }
static void post_poll(struct syscallrecord *rec) { freeptr(&rec->a1); }
static void post_mincore(struct syscallrecord *rec) { freeptr(&rec->a3); }
long moveFile(char* filename){ /* Validate input */ if(filename==NULL || strlen(filename)<=0){ fprintf(stderr, "ERROR: No filename provided for a move thread.\n"); return -1; } /* Ensure desination directory global variable exists */ if(destDirectoryName==NULL || strlen(destDirectoryName)<=0){ fprintf(stderr, "ERROR: No desination directory provided for a move thread.\n"); return -1; } /* Determine the if 'filename' is path. If so, find the start of the actual filename */ int filenameStart = 0; //Index of the start of the acutally filename in the 'filename' variable int a; //Iterator /* Start at the end of the string to find the last / (assume not the last character) */ for(a=strlen(filename)-2; a>=0; a--){ if(filename[a]=='/'){ filenameStart=a+1; break; } } /* The file's actual name is the filepath offet but the path's length */ char* actualFilename = filename+filenameStart; /* Output details if debug */ if(verbose>=3){ printf("filenameStart: %d - filename: %s - actualFilename: %s\n",filenameStart,filename,actualFilename); } /* Validate actualFilename doesn't end with '/' or '\' */ int actualFilename_lastCharIndex = strlen(actualFilename)-1; char actualFilename_lastChar = actualFilename[actualFilename_lastCharIndex]; if(actualFilename_lastChar=='/' || actualFilename_lastChar=='\\'){ printf("Source file '%s' may not end with a slash.\n",actualFilename); return -1; } /* Determine the new file's filepath; allocate memory and confirm malloc worked. */ /* New filepath will be the string length of destDirectoryName, actualFilename, a '/', and a null terminator */ long newPathSize = ((long)strlen(destDirectoryName) + (long)strlen(actualFilename) + 1 + 1)*(long)sizeof(char); char* tofilename = malloc(newPathSize); if(tofilename==NULL){ fprintf(stderr, "ERROR: Could allocate memory for the destination filename for '%s'\n",filename); return -1; } /* Add the filename to the directory to get the final path */ strcpy(tofilename,destDirectoryName); int destDirectoryName_lastCharIndex = strlen(destDirectoryName)-1; if(destDirectoryName[destDirectoryName_lastCharIndex]!='/'){ /* If the desination does not inlude a '/', add it */ strcat(tofilename,"/"); } strcat(tofilename,actualFilename); /* Ensure 'tofilename' was created */ if(tofilename==NULL || strlen(tofilename)<=0){ fprintf(stderr, "ERROR: Could not determine destination filename for '%s'\n",filename); return -1; } /* Check for source file existance; print and return on error */ if(access(filename, F_OK)==-1){ printf("Source file '%s' not found.\n",filename); freeptr(tofilename); return -1; } /* Check for desination file existance; print and return on error */ if(access(tofilename, F_OK)!=-1){ printf("Destination file for '%s' ('%s') already exists.\n",filename,tofilename); freeptr(tofilename); return -1; } /* Print status message */ if(verbose>=1){ printf("Moving '%s' \t-->\t '%s'\n",filename,tofilename); } /* Setup the buffer and file pointers */ int f_dest, f_source; int errornum; char buffer[bufferSize]; ssize_t bRead; ssize_t bWritten; long totalSize=0; /* Open the source file to read from it; print and return on error */ f_source = open(filename, O_RDONLY); if(f_source<0){ fprintf(stderr,"ERROR: Could not read from file '%s'\n",filename); freeptr(tofilename); return -1; } /* Open the destination file to wrtie to it; print and return on error */ f_dest = open(tofilename, O_WRONLY | O_CREAT | O_EXCL, 0666); if(f_dest<0){ fprintf(stderr,"ERROR: Could create the destination file '%s'\n",tofilename); closeIfPossible(f_source, 0); freeptr(tofilename); return -1; } size_t bufferBytes = sizeof buffer; /* Read from the source file in increments without exceding the buffer's size */ while(bRead=read(f_source, buffer, bufferBytes) ){ /* Break the loop if read() returned -1 (an error) */ if(bRead<=0){ break; } /* Keep track of the file's total size */ totalSize+=(long)bRead; /* Create a pointer to move along the buffer as it is written out */ char* cBufferPtr = buffer; /* Write to the destination file while there are still bytes in the buffer */ while(bRead>0){ /* Write out data to the destination file */ bWritten = write(f_dest, cBufferPtr, bRead); /* Ensure bytes were output and write did not return -1 (error) */ if(bWritten>0){ bRead -= bWritten; //Decrease the number of bytes remaining in the buffer cBufferPtr += bWritten; //Move the buffer pointer along for the next write }else if(errno!=EINTR){ /* write() returned 0 or -1, and was not interupted, there was an error */ fprintf(stderr,"ERROR: Could not write to destination file '%s'\n",tofilename); closeIfPossible(f_dest, 0); closeIfPossible(f_source, 0); freeptr(tofilename); return -1; } } } /* Ensure read() in the loop above did not return -1 (error) */ if(bRead!=0){ fprintf(stderr,"ERROR: Could not read from source file '%s'\n",filename); closeIfPossible(f_dest, 0); closeIfPossible(f_source, 0); freeptr(tofilename); return -1; } /* Else, arrived at end of file because read() returned 0; copy was successfull; close file pointers */ closeIfPossible(f_dest, 1); closeIfPossible(f_source, 1); /* Remove the orginal file */ if(unlink(filename)!=0){ /* unlink() returned -1 (error); print an error message */ fprintf(stderr,"ERROR: Desination file successfully created, but the \ source file '%s' could not be removed.\n",filename); freeptr(tofilename); return -1; }