1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Adding some automagic to PoL shortcuts

Discussion in 'PlayOnLinux' started by Daerandin, Apr 8, 2014.

  1. Daerandin

    Daerandin Well-Known Member

    Joined:
    Oct 18, 2013
    Messages:
    1,130
    Likes Received:
    243
    Trophy Points:
    63
    Location:
    Northern Norway
    Home page:
    It is actually quite easy to add some automatic toggles or other changes to happen while you launch a PoL game, and then to revert once the game is done. I will provide a detailed explanation here on how to do this, using StarCraft 2 as example, and how to make the ptrace protection disable automatically while the game is running.

    For this example, it is important to mention a few things:

    Doing this will require you to edit the /etc/sudoers file. This is dangerous unless you know what you are doing. The default way of editing it is through the "visudo" command. This will prevent you from saving the file if there are errors in it.

    Premise

    Note: StarCraft II can run now without disabling ptrace protection, so the result of this guide is not useful anymore. However, the described steps here might point people on the right track for other modifications.

    StarCraft II will not run unless you disable the ptrace protection in the Linux kernel. Making this change requires you to edit a file as root, the command to do this might be tedious to type every time you want to play. It is possible to edit your .bashrc to add an alias which would allow you to type a much shorter custom command for the same effect, but you still need to type your password.

    We will now make it fully automatic and bypass the password requirement.

    Script

    A python script will do the trick. This script is written in Python 3, so you need to make sure you have Python 3 installed. Just check the software manager for your distro.

    I suggest you create a new directory in your home folder, and name it "scripts". Here you can create a new empty file which you can call "ptrace.py"

    Now you can edit your file with any text editor such as gedit, mousepad. Copy the code below into your file.

    Code:
    #! /usr/bin/env python3
    # This script must be run with either -enable or -disable argument
    # Will either enable or disable ptrace protection
    
    import sys
    
    ptrace_file = open("/proc/sys/kernel/yama/ptrace_scope", "r+")
    arg = sys.argv[1]
    if arg == "-enable":
        value = 1
    elif arg == "-disable":
        value = 0
    ptrace_file.write(str(value))
    ptrace_file.close()
    This is a script that will recognize the -enable and -disable arguments. This script must be run as root, otherwise it will exit with a permission error when it attempts to open the ptrace_scope file for editing. However, the point is not to run this script by itself.

    We are going to allow your user to run this script without requiring any password. This is a security risk, but we can minimize any risk by simply locking this script from anyone else than the root user.

    Open a terminal and navigate to the directory where you saved this script. Assuming you followed my earlier advice about saving it in a 'scripts' directory in your home folder, then you would open a terminal and type:

    Code:
    cd ~/scripts
    In case you are unfamiliar with the terminal, the ~ character actually means: '/home/username' where 'username' is the name of your user.

    Now to change ownership of the file, type (assuming you named the script ptrace.py):

    Code:
    sudo chown root:root ptrace.py
    Now we want to ensure that only root have any access to the script. We don't even want other users to be able to read the script, so type:

    Code:
    sudo chmod 700 ptrace.py
    Now you can't even look at the contents of the file with your normal user, which is exactly what we want.

    Letting your user run this script as root without password

    This here is probably the most tricky part for those unfamiliar with more advanced system administration. We will not edit the sudoers file. If you edit the file directly with some text editor, you run the risk of breaking sudo for your system. So I will say it right away, do NOT try to change the file with some text editor of your own choice as root.

    To edit the sudoers file, run the command:

    Code:
    sudo visudo
    Depending on your distro (or your own custom setup) the default editor might be nano, vi, vim, emacs. I believe both Ubuntu and Mint use nano as default editor. Arch use vi. Personally I have set it up to use vim. Nano is pretty similar to what most people are used to. If however things don't seem to respond the way you expect, then chances are you are actually using vi/vim. If that is the case and you worry about having messed up things, then simply press ESC on your keyboard once, then type

    :q!

    and press enter to close the editor without saving anything.

    I will not go into the details on how to operate the different editors, but feel free to comment and ask if you are struggling and I will help.

    You should scroll further down until you reach a section that should look somewhat similar to this

    sudoers.png

    Every line that begins with # is a comment. You are going to want to ensure that there are no more uncommented lines below. Just position your marker so that it is under any lines that do not begin with #

    Then you can make an empty line and type in the following:

    username ALL=(ALL) NOPASSWD: /home/username/scripts/ptrace.py

    Change username with the name of your user both places in the line above. If you see in my screenshot, the name of my user is wolf, so it says wolf both those places.
    Note that if you named the script anything else, or placed it a different place, then you must of course type in the correct path to the script.

    You can now close and save the file. If you made any errors, it will actually complain and not allow you to save. If that is the case, then you should discard changes and try again. It is very important that the sudoers file have no syntax errors or such.

    Now you are able to run the script with sudo, without needing to type in your password. Normally this would be a very bad thing, but since we have restricted all access to the file for everyone except root then this is no longer a risk.

    Initializing script
    Unfortunately it is not as simple as adding this script to run as root from the PlayOnLinux shortcut. The PlayOnLinux shortcuts check for any commands that try to use sudo, such commands then prevent the shortcut from running. This is of course a security feature in PoL, but makes our attempt a little more difficult.

    To work around this, we will create a second script that initialize our original script as root. It would also be nice to let this script function as a toggle, so it simply toggles ptrace protection.

    Navigate back to your scripts folder, and create a new empty file there and named it "starter.py" and then open it with a text editor and copy the script below into it

    Code:
    #! /usr/bin/env python3
    # Starter script to workaround PoL shortcuts not allowing usage of sudo on script
    
    import os
    
    test = open("/proc/sys/kernel/yama/ptrace_scope", "r")
    value = int(test.read())
    test.close()
    script = "sudo /home/username/scripts/ptrace.py "
    if value:
        argument = "-disable"
    else:
        argument = "-enable"
    os.system(script + argument)
    
    It is important that you make one change in this script. On the line that reads:

    script = "sudo /home/username/scripts/ptrace.py "

    Be sure that you change 'username' into the name of your user so the path is correct. In case you have the original script in a different location, then you must of course change this to point to the correct location.

    Now we must give this script executable permission, otherwise it will not exectute when we try to run it. So open a terminal and write:

    Code:
    chmod +x ~/scripts/starter.py
    This script functions as a toggle. When this script is run, it will check it ptrace protection is on or off, and will then initialize the original script, as root, to make the change. Since we have now allowed your user to run the original script as root without password, this will work fine.

    Editing PlayOnLinux shortcut
    Now we can change the PlayOnLinux shortcut for Starcraft II so that this is handled automatically each time the game launches.

    Open your home folder, then press CTRL + h to display hidden files and folders. Find the one named .PlayOnLinux

    folder.png

    Open it, and then open the "shortcuts" folder. Find your StarCraft II shortcut.

    SC2.png

    Do NOT just double click, as that will launch it. Instead right-click and then open with your preferred text editor.

    You should see the following code:

    Code:
    #!/bin/bash
    [ "$PLAYONLINUX" = "" ] && exit 0
    source "$PLAYONLINUX/lib/sources"
    export WINEPREFIX="/home/username/.PlayOnLinux//wineprefix/Starcraft2"
    export WINEDEBUG="-all"
    cd "/home/username/.PlayOnLinux//wineprefix/Starcraft2/drive_c/./Program Files (x86)/StarCraft II"
    POL_Wine 'StarCraft II.exe' "$@"
    
    We are going to make a few changes. We want to run our script first so that ptrace protection is switched off before anything else happens. Then we want it to turn back on when we are done playing. For this game, it will not work to simply run the toggle script once at start, and again at the end of the script. StarCraft II actually stops the regular executable when the updater start, so we will instead add a little loop that checks to see if wineserver is running. As long as wineserver is running, it means we are playing the game. When wineserver stops running, we want the script to toggle ptrace back on.

    Below is the changed StarCraft II shortcut, with the new code that runs our script at the correct time. It does not toggle ptrace back on immediately after closing the game, it keeps checking every tenth second if wineserver is still running.

    Don't copy this code directly into your shortcut, as the location might be different for you. Just look at the original code for the shortcut, and the edited code below. You will see that the additions that you need to add are the second and third line at the top. And the three lines at the bottom.

    Code:
    #!/bin/bash
    TOGGLE=/home/username/scripts/starter.py
    $TOGGLE
    [ "$PLAYONLINUX" = "" ] && exit 0
    source "$PLAYONLINUX/lib/sources"
    export WINEPREFIX="/home/username/.PlayOnLinux//wineprefix/Starcraft2"
    export WINEDEBUG="-all"
    cd "/home/username/.PlayOnLinux//wineprefix/Starcraft2/drive_c/./Program Files (x86)/StarCraft II"
    POL_Wine 'StarCraft II.exe' "$@"
    PID=$(pidof wineserver)
    while (ps -p $PID) > /dev/null; do sleep 10; done;
    $TOGGLE
    
    On the second line, make sure you change 'username' with the name of your user. If you placed the starter.py script in a different location or named it differently, then you will of course need to edit the line so it points to the correct location and script.

    That is all
    This might seem a bit daunting at first, but it will let you run the game from now on without having to worry about needing to change this yourself before and after running the game.

    This should also serve as a good indication of how to make changes of your own for any specific games.
    Last edited: Sep 3, 2014
  2. booman

    booman Grand High Exalted Mystic Emperor of Linux Gaming Staff Member

    Joined:
    Dec 17, 2012
    Messages:
    8,278
    Likes Received:
    614
    Trophy Points:
    113
    Location:
    Linux, Virginia
    Home page:
    Very nice!
    Seems fairly easy once you are comfortable with terminal text editors. I use "nano"
    Could this automation also work with .asoundrc we were messing with?
    Automate launching a game to set pulseaudio to only use PlayOnLinux, then disable when finished playing?
  3. Daerandin

    Daerandin Well-Known Member

    Joined:
    Oct 18, 2013
    Messages:
    1,130
    Likes Received:
    243
    Trophy Points:
    63
    Location:
    Northern Norway
    Home page:
    I actually already do this for my virtual drive for the Arkham games. I have a file called .asoundrc with the configuration to use Alsa stored in my home folder under /scripts/temporary. To the shortcut I simply added two lines. First:

    mv /home/wolf/scripts/temporary/.asoundrc /home/wolf/.asoundrc

    So that it will use the .asoundrc config when launching wine. At the end of the shortcut I added the reverse:

    mv /home/wolf/.asoundrc /home/wolf/scripts/temporary/.asoundrc

    So it reverts once I stop playing.

    For most games you will not need to have a loop to check if wineserver is running.

    Some games, like StarCraft II, actually close the executable shortly after you launch it, since it launches an updater. So without the loop that wait for wineserver to end, it would actually toggle ptrace back on a few seconds after launching.
  4. booman

    booman Grand High Exalted Mystic Emperor of Linux Gaming Staff Member

    Joined:
    Dec 17, 2012
    Messages:
    8,278
    Likes Received:
    614
    Trophy Points:
    113
    Location:
    Linux, Virginia
    Home page:
    I knew it... that is the first thing I thought of when you mentioned automation for PlayOnLinux shortcuts.
    Seems like a real pain when pulseaudio developers should just fix the problem.
  5. Daerandin

    Daerandin Well-Known Member

    Joined:
    Oct 18, 2013
    Messages:
    1,130
    Likes Received:
    243
    Trophy Points:
    63
    Location:
    Northern Norway
    Home page:
    Posting late at night certainly has its disadvantages. I made a typo in the command for editing the sudoers file. It should be

    Code:
    sudo visudo
    I am no longer able to edit my post. It would be much appreciated if someone with mod rights could edit my post with the correct command.
  6. booman

    booman Grand High Exalted Mystic Emperor of Linux Gaming Staff Member

    Joined:
    Dec 17, 2012
    Messages:
    8,278
    Likes Received:
    614
    Trophy Points:
    113
    Location:
    Linux, Virginia
    Home page:
    Changed. You still can't edit your posts?
  7. Daerandin

    Daerandin Well-Known Member

    Joined:
    Oct 18, 2013
    Messages:
    1,130
    Likes Received:
    243
    Trophy Points:
    63
    Location:
    Northern Norway
    Home page:
    Thanks a lot!

    I can only edit my posts in the guide section. I lost access to edit my posts after some time has passed for all other parts of the forum.

    If you were feeling particularly generous, I noticed I made another minor mistake in the code. It is not really a mistake as such, but it is still bad coding practice. I forgot to close the file in the starter.py script. The file is closed automatically by your OS when the script has finished, but it is still bad practice and I would like to have my script updated to reflect better coding practice.

    Below is the revised code for the starter.py script, really just one added line which closes the file just after reading the value.

    Code:
    
    #! /usr/bin/env python3
    # Starter script to workaround PoL shortcuts not allowing usage of sudo on script
    
    import os
    
    test = open("/proc/sys/kernel/yama/ptrace_scope", "r")
    value = int(test.read())
    test.close()
    script = "sudo /home/username/scripts/ptrace.py "
    if value:
        argument = "-disable"
    else:
        argument = "-enable"
    os.system(script + argument)
  8. booman

    booman Grand High Exalted Mystic Emperor of Linux Gaming Staff Member

    Joined:
    Dec 17, 2012
    Messages:
    8,278
    Likes Received:
    614
    Trophy Points:
    113
    Location:
    Linux, Virginia
    Home page:
    For you... I'm always feeling generous.
    I updated the script for ya!
  9. Daerandin

    Daerandin Well-Known Member

    Joined:
    Oct 18, 2013
    Messages:
    1,130
    Likes Received:
    243
    Trophy Points:
    63
    Location:
    Northern Norway
    Home page:
    You are awesome! Thanks a lot.

Share This Page