Memory mapped files deleted when logging out

Need help with C, C++, perl, python, etc?

Memory mapped files deleted when logging out

Postby klausmedk » 2015-07-22 08:22

Hello

I have a somewhat strange problem that I seek help to solve.

My system is Debian Jessie 8.0 32bit

I have two processes (created in c++ by me) started as systemd services.
These two processes communicate with a memory mapped file created this way:

Code: Select all
MyShm * TaskImpl::getSharedMem()
{
    MyShm* shm;
   
    int fd = shm_open("/my_shared_mem", O_RDWR | O_CREAT, 0660);
    if(fd == -1) {
        //error logging removed
        return NULL;
    }

    ftruncate(fd, sizeof(MyShm));

    shm = (MyShm *)mmap(NULL, sizeof(MyShm),
                                       PROT_READ | PROT_WRITE,
                                       MAP_SHARED, fd, 0);

    close(fd);
    if(shm == MAP_FAILED) {
        //error logging removed
        return NULL;
    }

    return shm;
}



Once the processes are started with "sudo systemctl start myservice.service" everything works just fine.
I see the mmap files created in /dev/shm.

If one of the processes is restarted it regains access to the mmap file and communication continues.

Now comes the strange part.

As soon as I log out of the system (I'm logged in as the same user as systemd is set to start the procceses under), the mmap files seems to be somehow deleted.
Communication between the processes continues withput problem. But, if one of the processes is restarted it tries to regain access to the mmap file which is no longer there. It thus creates a new file which does not correspond to the mmap through which the other process communicates. The outcome, the processes no longer talk to each other.

Why is the mmap files deleted when the user logs out?
What is the solution? Should I change something in my code, or change something on the system to prevent the files from being deleted.

Regards
//Klaus
klausmedk
 
Posts: 4
Joined: 2015-06-03 12:15

Re: Memory mapped files deleted when logging out

Postby reinob » 2015-07-22 11:26

klausmedk wrote:As soon as I log out of the system (I'm logged in as the same user as systemd is set to start the procceses under), the mmap files seems to be somehow deleted.


Just guessing here: when you logout the user session is killed (by systemd). This might also delete the mappings (or close the files, or who knows what).

Try enabling "linger" for your user:
(as root)
Code: Select all
# loginctl enable-linger username


This should prevent killing the user session when logging out. If it works, you can take it from there and see what exactly needs to be conserved, and (hopefully) how :)

Cheers and good luck!
reinob
 
Posts: 556
Joined: 2014-06-30 11:42

Re: Memory mapped files deleted when logging out

Postby klausmedk » 2015-07-22 12:16

Thanks for the answer.

Yes, enabling lingering "works", meaning the mmap files are not deleted when the user logs out and my inter process communication keeps working.
But what I fail to understand is why a systemd service and its files started with "sudo systemctl start myservice.service" is affected by a user logging out. Event thought the user used for logging in and running the service ("User=myuser" in myservice.service) are the same.

Here's my service startup script for reference:

Code: Select all
[Unit]
Description=My Service

[Service]
ExecStart=/pro/bin/Myexecuable
User=myuser
Group=myuser
WorkingDirectory=/pro/bin
StandardOutput=syslog
StandardError=syslog
Environment=LD_LIBRARY_PATH=/pro/bin

RestartSec=5s
Restart=on-failure

KillSignal=SIGINT
KillMode=process

[Install]
WantedBy=multi-user.target


Regards
//Klaus
klausmedk
 
Posts: 4
Joined: 2015-06-03 12:15


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable