[Gambas-bugtracker] Bug #2534: Libc mmap() call always returns -1

bugtracker at gambaswiki.org bugtracker at gambaswiki.org
Thu Apr 28 03:00:30 CEST 2022


http://gambaswiki.org/bugtracker/edit?object=BUG.2534&from=L21haW4-

Gary SPRANG reported a new bug.

Summary
-------

Libc mmap() call always returns -1

Type             : Bug
Priority         : Medium
Gambas version   : Unknown
Product          : Unknown


Description
-----------

I rewrote the shared memory part of SPOCS because the shmget/shmat functions are being deprecated.  The main module does this - 

	/* shared memory is aligned on pages (usually 4096 bytes) */
	int shm_size=1048576;  /* was 524288 */
	char shm_id[] = {"/SHM_SEG"};
	int shm_fd; /* descriptor for shared memory */
	void *shm_addr;

	/* get shared memory file descriptor.  O_flags come from fcntl.h */
	/* O_CREAT says to create the shared memory segment if it does not exist. */
	/* O_RDWR confers read/write permissions */
	shm_fd = shm_open(shm_id, O_CREAT | O_RDWR, 0777);
	if (shm_fd == -1)
	{
		perror("open");
		exit(-10);
	}
	/* set the shared memory length */
	/* this is necessary, because it defaults to zero! */
	ftruncate(shm_fd, shm_size);
	/* map shared memory to process address space */
	/* void * mmap (void *address, size_t length(# of bytes), int protect, int flags, int filedes, off_t offset) flags: MAP_SHARED | MAP_ANONYMOUS */
	/* if MAP_ANONYMOUS is used, no file is used, and shm_fd should be -1 (DO NOT use it) */
 	shm_addr = mmap(NULL, shm_size, PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd, 0);
	if (shm_addr == MAP_FAILED)
	{
		perror("mmap");
		exit(-30);
	}
	shmaddr2 = (char *)shm_addr; /* tie into existing pointer */
The main module crestes the shared memory segment, above.
............................................................................................................
Other modules use this logic to connect to shared memory...
  contents of one of several modules that can sucessfully connect to shared memory
  	/* get shared memory file descriptor)  O_flags come from fcntl.h */
	shm_fd = shm_open(shm_id, O_RDWR, 0777);
	if (shm_fd == -1)
	{
		perror("open");
		exit(-10);
	}
	/* map shared memory to process address space */
	/* void * mmap (void *address, size_t length, int protect, int flags, int filedes, off_t offset) */
 	shm_addr = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
	if (shm_addr == MAP_FAILED)
	{
		perror("mmap");
		exit(-30);
	}
	lshmaddr = (char *)shm_addr; /* tie into existing pointer */
.... this works flawlessly.
.............................................................................................
Now in Gambas, we are doing this.  First we define the extern calls...
contents of FMAIN where extern functions are declared:
'***********************************************************************************
'********************************* declare calls to Linux system.
Library "libc:6"
Extern killme(pid As Integer, sig As Integer) As Integer Exec "kill"
Extern getlogin() As Pointer Exec "getlogin"
Extern shm_open(shm_id As String, permissions As Integer, user_perms As Integer) As Integer Exec "shm_open"
'
Extern mmap(address As Pointer, shm_size As Integer, protection As Integer, visibility As Integer, fd As Integer, Offset As Integer) As Pointer Exec "mmap"
.................... then the calls themselves, in contents of Public Sub Form Open
  Dim buf1 As String
  Dim shm_size As Integer = 1048576
  Dim shm_fd As Integer
  Dim shm_addr As Pointer
  Dim shm_id As String = "/SHM_SEG"
  Dim vptr As Pointer = Null
  Dim mmap_err As Integer
  
  protection = 3 ' PROT_READ | PROT_WRITE use the numbers, since Gambas does not know the contents of fctrl.h
  visibility = 4 ' MAP_SHARED(4)

  tb_consolerevlev.text = global.revlev
  Me.Center ' this centers the form on the screen, instead of in upper-left corner.
  uidptr = getlogin()
  tb_username.text = String@(uidptr)
  shm_fd = shm_open(shm_id, 2, 777) ' 2=read/write  = 777
  If shm_fd == -1 Then 
      Message.Info("shm_open() failed!", " OK ")
      Quit 
  Endif 
 ' map shared Memory To process address space
 shm_addr = mmap(0, shm_size, protection, visibility, shm_fd, 0)
 mmap_err = shm_addr ' convert the pointer to an integer
 If mmap_err < 1 Then 
  Message.Info("mmap() failed!", " OK ")
  Quit 
Endif 

  shmaddr2 = shm_addr
................... smaddr2 becomes the base pointer for the segment.
When I step through the logic, the shm_open() call works fine.
The mmap() call gives no error, but always returns -1 (or 0xffffffffffffffff)
It might not be a bug, as such, but I'm running out of ideas.  I hope you can help.  Thanks for any assistance!


System information
------------------

Fedora 35 / XFCE desktop
AMD quad core CPU
Gambas 3.16.2




More information about the Bugtracker mailing list