Daerandin
Well-Known Member
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.
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:
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):
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:
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:
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
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
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:
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
Open it, and then open the "shortcuts" folder. Find your StarCraft II shortcut.
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:
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.
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.
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
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
Open it, and then open the "shortcuts" folder. Find your StarCraft II shortcut.
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: