0

I'm writing a ruby wrapper for Hitachi-44780 library for Raspberry Pi. The library itself is written in C. I have the following code that tries to open /dev/mem

void setup_io()
{

   /* open /dev/mem */
   if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
      printf("can't open /dev/mem \n");
      exit (-1);
   }
   // Allocate MAP block
   if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
      printf("allocation error \n");
      exit (-1);
   }
   // Make sure pointer is on 4K boundary
   if ((unsigned long)gpio_mem % PAGE_SIZE)
     gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
   // Now map it
   gpio_map = (unsigned char *)mmap(
      (caddr_t)gpio_mem,
      BLOCK_SIZE,
      PROT_READ|PROT_WRITE,
      MAP_SHARED|MAP_FIXED,
      mem_fd,
      GPIO_BASE
   );
   if ((long)gpio_map < 0) {
      printf("mmap error %d\n", (int)gpio_map);
      exit (-1);
   }
   gpio = (volatile unsigned *)gpio_map;
}

The question is how can i use the gem without being a superuser? Now it fails when the memory is opened.

roman
  • 5,100
  • 14
  • 44
  • 77
  • Can you change the permissions on /dev/mem? – Fred Apr 07 '13 at 15:08
  • @Fred Don't think this is a good solution. – roman Apr 07 '13 at 15:13
  • @Fred The permissions on the /dev/mem already set to `crwxrwxrwx 1 root kmem 1, 1 Jan 1 1970 mem` – roman Apr 07 '13 at 15:39
  • I'm understanding you to say the open() fails because the process isn't root. If that is correct, what else can be done other than to permit a non-root process to open memory for O_RDWR? (Added: or making the program setuid root) – Fred Apr 07 '13 at 15:40
  • Sorry, we seem to be paying edit-the-comments! OK, what is the failure when memory is opened? – Fred Apr 07 '13 at 15:47
  • @Fred I've set permissions on the test script: `-rwsrwsrwt 1 root root 127 Apr 7 16:52 test.rb` But still get `can't open /dev/mem` – roman Apr 07 '13 at 15:53
  • What is the value of errno? – Fred Apr 07 '13 at 15:56
  • @Fred It's EACCES 13 /* Permission denied */ – roman Apr 07 '13 at 16:08
  • If /dev/mem is rwx for world and you get EACCES, well, something strange is happening. – Fred Apr 07 '13 at 19:54
  • @Fred I've noticed that after reboot the permissions for /dev/mem are restored to defaults. That is why I was getting EACCESS – roman Apr 08 '13 at 12:23

0 Answers0