Running an emulator from a systemd service

Ask questions about Arch Linux ARM. Please search before making a new topic.

Running an emulator from a systemd service

Postby mashley » Tue May 23, 2017 8:13 am

Hi all

I've set up a RPi 2 as an emulation box and want it to boot directly to snes9x (Super Nintendo emulator). More specifically, it's the pisnes project which runs better on the RPi.

It was working well when set up with autologin and running the emulator from .bash_login, but this was a fairly slow startup (17s). I got it down to 10s by removing autologin and moving the emulator startup script into a systemd service:

Code: Select all
[Unit]
Description=Starts SNES Emulator
DefaultDependencies=false            # Very important! Without this line, the s$
                                     # would wait until networking.service
                                     # has finished initialization. This could $
                                     # more seconds because of DHCP, IP attribu$

[Service]
Type=simple
ExecStart=/root/snes.sh
WorkingDirectory=/root/

[Install]
WantedBy=local-fs.target


This works insofar as the service runs and the emulator starts. The problem is that the emulator segfaults when run this way. Here's the output I receive:

Code: Select all
[root@alarmpi ~]# systemctl status snes
* snes.service - Starts SNES Emulator
   Loaded: loaded (/etc/systemd/system/snes.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2016-12-10 01:11:08 UTC; 44s ago
 Main PID: 268 (snes.sh)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/snes.service
           `-268 /bin/sh /root/snes.sh

Dec 10 01:11:08 alarmpi systemd[1]: Started Starts SNES Emulator.
Dec 10 01:11:17 alarmpi snes.sh[268]: No ROM file header found.
Dec 10 01:11:17 alarmpi snes.sh[268]: /root/snes.sh: line 3:   332 Segmentation fault      (core dumped) /pisnes/snes9x2p $NEWROM


The "No ROM file header found." is a normal output from the emulator and is not related to the error. The segfault, however, is clearly abnormal.

If I disable the service, log in as root and manually run the snes.sh script then it works fine. So I'm really at a loss as to how to establish why the segfault occurs when the emulator is launched from a service.

Thanks in advance for any help!
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm

Re: Running an emulator from a systemd service

Postby mashley » Tue May 23, 2017 5:07 pm

I've got a build environment working on the Pi and have added a load of logging commands to the source code of the emulator to track down what is causing the segfault. It seems to be related to loading IPS patches (not sure what they are, but I don't think I need them) and loading the SRAM, which is the saved game file. When I commented out those two function calls, it booted the emulator and launched the game correctly. Now I just need to work out why they're causing a segfault on boot and see if it can be fixed.
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm

Re: Running an emulator from a systemd service

Postby mashley » Wed May 24, 2017 12:03 pm

Well it seems it is the call to the function to load the game's SRAM rather than something happening in the function itself which is causing this. I know this because I put a print statement at the top of the SRAM load function and it doesn't even print that before segfaulting. The problem therefore must be with accessing this function rather than something happening within it. It's pretty weird that this only happens when the emulator starts from a service though. I'm wondering if there is a limit to the mount of memory a service-loaded binary is allowed to take up and the function that is being called falls outside that region and thus is inaccessible. If anyone can shed light on this I would be grateful.
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm

Re: Running an emulator from a systemd service

Postby mashley » Wed May 24, 2017 1:21 pm

It turns out this problem was being caused by a function which constructs the snapshots directory for the emulator. Specifically, there was a call to strcat which seemed to be causing the segfault. I'm guessing this C function was inaccessible for some reason when run as a service. In any case, I have hard-coded the path and it all works fine now.
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm

Re: Running an emulator from a systemd service

Postby WarheadsSE » Thu May 25, 2017 12:17 pm

Where is the "normal" path for this directory? "~/.something" ?
Core Developer
Remember: Arch Linux ARM is entirely community donation supported!
WarheadsSE
Developer
 
Posts: 6730
Joined: Mon Oct 18, 2010 2:12 pm

Re: Running an emulator from a systemd service

Postby mashley » Thu Jun 01, 2017 6:07 pm

Yes, it's ~/.snes96_snapshots. Is that significant in some way to the cause of the problem?
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm

Re: Running an emulator from a systemd service

Postby WarheadsSE » Mon Jun 05, 2017 2:48 pm

The probable cause is access to the directory, and configuration files.

When you start a service, it does not behave as if it was a logged in user. You will need to investigate how to make this behavior work when there is no `~/` ~= $HOME defined. You may need to define this, you may need to define other variables as well. I would not trust any relative path (which `~/` is, but `/root/` is not)
Core Developer
Remember: Arch Linux ARM is entirely community donation supported!
WarheadsSE
Developer
 
Posts: 6730
Joined: Mon Oct 18, 2010 2:12 pm

Re: Running an emulator from a systemd service

Postby mashley » Tue Jun 06, 2017 3:19 pm

Thanks WarheadSE. It turns out I was able to set the path using an environment variable, and the function checks for it there before trying to construct it manually. If I set the environment variable in the script before starting the emulator, it works fine. So I don't think filesystem access was the problem - it was literally the calls to strcat() and strcpy() which were causing the problem while constructing the variable which contained the path, not attempting to access the path itself.
mashley
 
Posts: 28
Joined: Wed Apr 16, 2014 8:31 pm


Return to User Questions

Who is online

Users browsing this forum: No registered users and 3 guests