29

I have migrated to Ubuntu 16.04 just 2 days ago from Windows. I like the way we can customise the Unity Desktop. I am just playing around with the look and feel of the Desktop environment. Just like in Windows, I wanted the launcher to be at the bottom of the screen. On Googling, I found a command which goes like:

gsettings set com.canonical.Unity.Launcher launcher-position Bottom

Also, there are the unity-tweak-tool and dconf editor to get the job done. But these are the GUI approach of getting things done.

My questions are:

  • Do these GUI-based applications also execute the same command in the background?
  • How to have a peep at the internal working of these applications? I mean, is there any way of actually looking at the commands that are being executed at every click of button?
  • Do these applications open up a terminal in the background and execute these commands?

The answer here tells how to get the process's standard file descriptor. But, I did't get anything in the output.

Moreover, the strace -p pid -o output.txt command throws a huge amount of text into the file.

So, in short, is doing things using GUI applications same as doing stuff from the commandline?

35

Do these GUI-based applications also execute the same command in the background?

Yes and no. They write to the dconf database of settings, but they could be using different ways to do so. Programs written in Python will likely use the gi.repository.Gio module (I know because I use it a lot) or they can instead use gsettings as an external command by calling subprocess.Popen(['gsettings','org.some.schema','some-key','value']), and it will basically run as a shell command. A C program will use something similar, likely a gio.h library, or it could even use the exec() family of functions to do the same as Popen does in python. So to answer your title question: "Do GUI based application execute shell commands in the background?" They can, but likely it's not necessary because there's a library for whatever language that app is written in, and likely it's gonna be a bit faster to use a library function, than spawning a new process.

To give you a sample of how it's done with libraries/modules, feel free to take a look at the source code of my launcher list indicator. There I have written a function to create an instance of the Gio.Settings class and then use it to modify the Unity launcher depending on the type of list you want to have there.

How to have a peep at the internal working of these applications? I mean, is there any way of actually looking at the commands that are being executed at every click of button?

No. If you want to see which command is issued in that app's programming language as you press a button or click on window elements, then it's not possible. Read the source code of application, if it is possible to obtain it. You can use dconf watch / to see what settings are being changed, but not how it's done.

Technically, if you know how to operate a debugger, read memory addresses, and know some assembly language, then you can know what an app does on the CPU and memory level. This is known as software reverse engineering and is frequently used by security professionals to analyze malicious software and discover vulnerabilities in legitimate software.

Do these applications open up a terminal in the background and execute these commands?

No, there's no terminal attached. Many programs know where the dconf database for the user is located and write there. There's also an inter-process communication bus known as dbus, where programs can send signals, and a program will be like "Hey, that's a message for me!"

Addendum

  • Can applications run other applications ? Yes, that's done via standard fork() and execve() system calls. The essence of creating processes on Linux and other *nix systems is largely based on these two. Shell mechanism for running non-builtin commands uses that a lot in particular. When you run interactively

    $ ls 
    

    the shell will create a new process via fork(), that process will run execve() which will start ls. Because of how execve() that new forked process will be ls. The pipe() system call is what will help read back the output of ls. I strongly suggest reading my answer for What is the difference between pipe and redirection to understand how pipe mechanism works - it's not just | operator, but actually a syscall.

  • Can applications run shell commands ? No. Shell syntax is only understood by shell itself. What you can do, however, is start a shell with a command-line -c switch and provide appropriate commands. This is often used for custom shortcuts set in GNOME or other desktop environments, since custom shortcuts operate on executables and there is no shell to understand the syntax. Thus as an example, you'd do bash -c 'xdotool key Ctrl+Alt+T' to indirectly run xdotool command or bash -c 'cd $HOME/Desktop; touch New_File' to create new file on desktop via shortcut. This is particularly interesting example as you can use a shell variable, since you are using a shell explicitly.

  • 2
    Thank you so much @Serg for the detailed explanation and answering each question separately and systematically! – Logan Nov 14 '16 at 14:03
  • @Logan my pleasure, always glad to help :) – Sergiy Kolodyazhnyy Nov 14 '16 at 14:26
  • 1
    Luckily, the source for the mentioned tools is available since they are FLOSS. Therefore I would say reversing them is a bit overkill in this case. :) – Andrea Lazzarotto Nov 15 '16 at 8:53
  • 1
    @AndreaLazzarotto Yep, Unity Tweak Tool and Dconf editor - those are open source, so there's no need to reverse engineer those. In my answer I kept everything very general and tried to cover not just those tools, but other possibilities – Sergiy Kolodyazhnyy Nov 15 '16 at 12:20
  • Faster is rarely the point for GUI applications. Escaping or marshalling the values for use with a shell tool is tedious, easy to get wrong and pointless if you can simply use the library. Re debugging: If the application is installed with debug symbols and written in a language supported by gdb (on debian e.g. by installing a corresponding -dbg package), you don’t need to know assembler: gdb will show you the source code using the debug info while you step through the application. What will be harder is to find a proper entry point to start debugging from, because of boring GUI boilerplate. – Jonas Schäfer Nov 15 '16 at 16:49
21

Spying on what happens

Most of what these settings editors do can be watched by running

dconf watch /

in a terminal.

gsettings

Also most of the time, to achieve what you see happening with the command above, these applications will need to edit the dconf database (further below). This can be done either directly, by using the cli options of dconf (which is not preferred), or by running the corresponding gsettings commands, like the one you mention.

To run these commands, no terminal window is needed, as you can see in the examples.

About, gsettings, dconf and the dconf database

gsettings is the cli frontend to dconf, which in its turn edits the dconf database, where most of the settings are stored, in binary format. See also this nice answer.

The dconfdatabase, by the way, can also be edited from the GUI by dconf editor, which is in the repositories:

enter image description here

Working samples

a. In python

enter image description here

To show you what happens under the hood, below a working sample to toggle your launcher position from GUI in a single (toggle) button:

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import subprocess

key = ["com.canonical.Unity.Launcher", "launcher-position"]

class ToggleWin(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Toggle")
        button = Gtk.Button("Toggle launcherposition")
        button.connect("clicked", self.toggle)
        self.add(button)

    def toggle(self, *args):
        # read the current setting on launcher position
        current = subprocess.check_output([
            "gsettings", "get", key[0], key[1]
            ]).decode("utf-8").strip()
        # toggle to the other option
        new = "'Left'" if current == "'Bottom'" else "'Bottom'"
        subprocess.Popen([
            "gsettings", "set", key[0], key[1], new
            ])

def delete_actions(*args):
    Gtk.main_quit()

def miniwindow():
    window = ToggleWin()
    window.connect("destroy", delete_actions)
    window.show_all()
    Gtk.main()

miniwindow()
  • Paste the code into an empty file.py
  • run it by the command:

    python3 /path/to/file.py
    

...and have fun.

b. Launcher icon

Even a simple launcher can do the job from the GUI:

enter image description here

[Desktop Entry]
Name=Set launcherposition
Exec=zenity --info --text="Right- click to set launcher position"
Type=Application
StartupNotify=False
Icon=preferences-system

Actions=Launcher to bottom;Launcher on the left;

[Desktop Action Launcher to bottom]
Name=Launcher to bottom
# right click option to set launcher to bottom
Exec=gsettings set com.canonical.Unity.Launcher launcher-position Bottom

[Desktop Action Launcher on the left]
Name=Launcher on the left
# right click option to set launcher to left
Exec=gsettings set com.canonical.Unity.Launcher launcher-position Left
  • Paste the code into an empty file, save it as setlauncher.desktop
  • Drag it on to the launcher and right-click

For permanent use, store it in ~/.local/share/applications (for local use) or ~/usr/share/applications for all users.

  • @Logan don't mention it :) – Jacob Vlijm Nov 14 '16 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.