Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Created December 21, 2010 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolaj86/749619 to your computer and use it in GitHub Desktop.
Save coolaj86/749619 to your computer and use it in GitHub Desktop.
Mem leak in /dev/shm
static int capture_file_write()
{
int fd;
int error_check;
static int buffer_counter = 0;
char filename[256];
snprintf(filename, sizeof filename, "%scapture.output.%02d.dat", DATA_PATH, buffer_counter);
buffer_counter = (buffer_counter + 1) % 14;
unlink(filename); //just unlink the file to make sure we write a fresh new file.
fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(-1 == fd)
{
perror("Opened bad file desciptor " DATA_PATH "capture.output.dat");
}
error_check = write(fd, capture_stream, CAPTURE_DATA_SIZE);
if (CAPTURE_DATA_SIZE != error_check)
{
if (-1 == error_check)
{
perror("capture file write failed.");
}
else
{
fprintf(stderr, "wrote the wrong number of bytes to the capture file. Wrote %d, expected to write %d", error_check, CAPTURE_DATA_SIZE);
}
exit(EXIT_FAILURE);
}
if(0 != close(fd))
{
perror("Failed to close capture output file");
exit(EXIT_FAILURE);
}
return 0;
}
static int capture_file_write()
{
struct stat sb;
void* p;
int fd;
int status;
static int buffer_counter = 0;
char filename[256];
snprintf(filename, sizeof filename, "%scapture.output.%02d.dat", DATA_PATH, buffer_counter);
buffer_counter = (buffer_counter + 1) % 14;
unlink(filename); //just unlink the file to make sure we write a fresh new file.
fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(-1 == fd)
{
perror("Opened bad file desciptor " DATA_PATH "capture.output.dat");
}
if (0 != ftruncate(fd, CAPTURE_DATA_SIZE))
{
perror("Failed to truncate capture output file");
exit(EXIT_FAILURE);
}
status = fstat(fd, &sb);
if(0 != status)
{
perror("Failed to fstat capture output file");
exit(EXIT_FAILURE);
}
p = mmap(0, sb.st_size, PROT_WRITE, MAP_SHARED, fd, 0);
if (MAP_FAILED == p)
{
perror("mmap");
exit(EXIT_FAILURE);
}
memcpy(p, capture_stream, CAPTURE_DATA_SIZE);
status = munmap(p, sb.st_size);
if(0 != status)
{
perror("Failed to unmap capture output file");
exit(EXIT_FAILURE);
}
status = close(fd);
if(0 != status)
{
perror("Failed to close capture output file");
exit(EXIT_FAILURE);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment