Want more features on Pastebin? Sign Up, it's FREE!
Guest

memlockd locking section

By: a guest on Oct 11th, 2013  |  syntax: C++  |  size: 1.06 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. int open_map(int fd, struct stat *sb, const char * const name)
  2. {
  3.   // !!! Map process into memory
  4.   new_files[num_new_files].start = mmap(NULL, sb->st_size, PROT_READ, MAP_SHARED, fd, 0);
  5.   if(new_files[num_new_files].start == MAP_FAILED)
  6.   {
  7.     log(LOG_ERR, "Error mmaping %s: %s", name, strerror(errno));
  8.     close(fd);
  9.     return 0;
  10.   }
  11.   // !!! Lock in-memory process down
  12.   if(mlock(new_files[num_new_files].start, sb->st_size) == -1)
  13.   {
  14.     log(LOG_ERR, "Can't lock memory for %s, error %s", name, strerror(errno));
  15.     munmap(new_files[num_new_files].start, sb->st_size);
  16.     close(fd);
  17.     return 0;
  18.   }
  19.   if(sb->st_size % page_size)
  20.     new_files[num_new_files].map_size = sb->st_size - (sb->st_size % page_size)
  21.                                       + page_size;
  22.   else
  23.     new_files[num_new_files].map_size = sb->st_size;
  24.   new_files[num_new_files].fd = fd;
  25.   memcpy(&new_files[num_new_files].sb, sb, sizeof(struct stat));
  26.   new_files[num_new_files].name = strdup(name);
  27.   num_new_files++;
  28.   log(LOG_INFO, "Mapped file %s", name);
  29.   return 1;
  30. }
clone this paste RAW Paste Data