tracking shared dirty pages in a mmap file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





Hi
I want to track pages of a mmap file that has been written to,
using a segv handler.

I have given an example program below to illustrate my idea.


What I want to know is whether the modifications to the mmap file will
be guaranteed to be unaffected by the interruption of the signal
handler, i.e. would the resultant changes in the mmap file be as if the
mmap file had write access set.


It can also be assumped that only the current process would be writing
to the mmap file.


Thanks
Kandan


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <signal.h>


typedef struct TwoInts_tag {
   int i;
   int j;



} TwoInts;


const int maxPages = 10;

char* start = 0;
int fd;
TwoInts *array = 0;
int pageSize;
int max;
int elemsPerPage;


void memory_fault(int signum, siginfo_t *info, void *ptr)
{
   char *faultAddr  = (char *)info->si_addr;


   /* we only handle the faults within our known range , anything else
is really an illegal access */
   if (faultAddr >= start && faultAddr < start + (pageSize * maxPages))
{


      int faultPageIdx = (faultAddr - start)/pageSize;


      fprintf(stderr, "fault at page index %d\n", faultPageIdx);


      if (mprotect(start + (faultPageIdx * pageSize), pageSize,
PROT_READ | PROT_WRITE)) {
         perror("mprotect failed");
         abort();
      }
   }
   else
      abort();



}


int main()
{
   struct sigaction act;

   act.sa_sigaction = memory_fault;
   act.sa_flags = SA_SIGINFO;
   sigemptyset (&act.sa_mask);


   int status = sigaction (SIGSEGV, &act, NULL);
   if (status) {
      perror ("sigaction");
      exit (1);
   }


   pageSize = getpagesize();


   elemsPerPage = pageSize/sizeof(TwoInts);


   max = elemsPerPage * maxPages;


   fprintf(stderr, "max elements is %d\n", max);


   assert(pageSize % sizeof(TwoInts) == 0);


   /* open/create the file */
   if ((fd = open ("kandan", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0)
{
      fprintf(stderr, "can't create file for writing");
      exit(1);
   }


   /* set the lenght of file */
   if (ftruncate(fd, maxPages * pageSize) == -1) {
      fprintf(stderr, "error on ftruncate\n");
      exit(1);
   }


   /* mmap the file with read access*/


   if ((start = mmap(0, maxPages * pageSize, PROT_READ, MAP_SHARED, fd,
0)) == MAP_FAILED) {


      perror("mmap error");
      exit(1);
   }


   array = (TwoInts *)start;

   /* this would case a seg fault */
   array[0].i = 5;

   fprintf(stderr, "value = %d\n", array[0].i);

   /* this will not cause a seg fault */
   array[0].i = 7;

   fprintf(stderr, "value = %d\n", array[0].i);


   array[1].i = 9;


   fprintf(stderr, "value = %d\n", array[1].i);


   array[elemsPerPage].i = 14;


   fprintf(stderr, "value = %d\n", array[elemsPerPage].i);


   close(fd);


   return 0;


}

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux