Skip to content

Commit efe7d8b

Browse files
committed
Addressed all the latest suggestions by Teo, except for remaking the Makefiles
in the demo folder and resolving the checkpatch errors. Signed-off-by: Cosmin-Andrei Iacobai <[email protected]>
1 parent 4759fec commit efe7d8b

File tree

284 files changed

+3529
-15743
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+3529
-15743
lines changed

chapters/app-interact/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ open: $(SITE)
3232

3333
clean:
3434
-for i in $$(find -name "*-generated.gif"); do rm -f $$i; done
35-
-rm -f *~
35+
-rm -f *~

chapters/app-interact/dbus/drills/tasks/dbus/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# D-Bus - battery level
1+
# D-Bus - Battery level
22

33
Use D-Bus to find out the computer's battery level.
44
There is the `org.freedesktop.UPower` interface on the system bus that can provide this information.

chapters/app-interact/dbus/reading/dbus.md

-230
Original file line numberDiff line numberDiff line change
@@ -11,233 +11,3 @@ This name can be something human-readable, like `org.freedesktop.Notifications`,
1111
Once a process is connected, it can expose one or multiple `objects`.
1212
An object has a path-like name, consisting of strings separated by a slash character (for example, `/org/freedesktop/Notifications`).
1313
Each object contains one or more `interfaces`, which have the methods that can be called on that object.
14-
15-
## D-Bus Inspection with D-Feet
16-
17-
In order to better understand these concepts, we'll use a graphical tool (`D-Feet`) to inspect all the available D-Bus objects on our system.
18-
19-
Run D-Feet and select `Session Bus` from the top button:
20-
21-
![dfeet-session-bus](../media/dfeet_session_bus.png)
22-
23-
On the left panel, we can see all the processes connected to D-Bus with their associated `connection names`.
24-
Scroll down and find `org.freedesktop.Notifications`.
25-
On the right side, expand `/org/freedesktop/Notifications` and then expand the `org.freedesktop.Notifications` interface.
26-
The window should look like this:
27-
28-
![dfeet-notifications](../media/dfeet_notifications.png)
29-
30-
Some observations:
31-
32-
* The bus communication happens over a Unix socket, with the path `/run/user/1000/bus`.
33-
34-
* `org.freedesktop.Notifications` on the left panel is the `connection name`.
35-
36-
* The process that has connected with this name is `/usr/bin/gjs /usr/share/gnome-shell/org.gnome.Shell.Notifications` and has the pid of `4373`.
37-
38-
* This process exposes one object: `/org/freedesktop/Notifications`.
39-
Note that the object name is the same as the connection name, where the dots have been replaced with slashes.
40-
This is not a requirement, as the objects exposed by a process can have any name.
41-
42-
<!-- markdownlint-disable MD101 -->
43-
* The object has 4 interfaces: `org.freedesktop.DBus.Introspectable`, `org.freedesktop.DBus.Peer`, `org.freedesktop.DBus.Properties` and `org.freedesktop.Notifications`.
44-
Note that the last one (`org.freedesktop.Notifications`) is the same as the connection name, but this again is just a coincidence, not a requirement.
45-
<!-- markdownlint-enable MD101 -->
46-
47-
* The interface `org.freedesktop.Notifications` has some methods that can be called, such as `Notify`.
48-
49-
## Calling D-Bus Methods
50-
51-
The application behind `org.freedesktop.Notifications` is responsible with desktop notifications (the small bubbles of text that appear at the top of the screen when some event happens).
52-
When an application wants to send a notification it needs to connect to D-Bus and call the `Notify` method from the `org.freedesktop.Notifications` interface.
53-
54-
In this example, we want to call the `Notify` method ourselves.
55-
To do this, we must first understand the signature of this method:
56-
57-
`Notify (String arg_0, UInt32 arg_1, String arg_2, String arg_3, String arg_4, Array of [String] arg_5, Dict of {String, Variant} arg_6, Int32 arg_7) ↦ (UInt32 arg_8)`
58-
59-
This doesn't tell us much, but we can find more documentation [here](https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#basic-design), since `freedesktop` is an open standard.
60-
61-
We'll set the arguments to the following (for our simple case, most of them will be unused):
62-
63-
* `app_name`: `""`
64-
65-
* `replaces_id`: `0`
66-
67-
* `app_icon`: `""`
68-
69-
* `summary`: `"This is the title"`
70-
71-
* `body`: `"This is the content"`
72-
73-
* `actions`: `[]`
74-
75-
* `hints`: `{}`
76-
77-
* `expire_timeout`: `-1`
78-
79-
Now the question is how to actually call the method.
80-
Normally, we would have to write an application that connects to D-Bus and executes the call.
81-
But for demonstrative purposes there are easier ways.
82-
83-
One way is directly from d-feet.
84-
If we double-click on the `Notify` method in the right-side pane of d-feet, a window will open that allows us to call the method with any arguments that we want:
85-
86-
![dfeet-execute-dialog](../media/dfeet_execute.png)
87-
88-
Then we click the `Execute` button and the notification will appear:
89-
90-
![dfeet-execute-](../media/dfeet_execute.gif)
91-
92-
Another way is from the command-line. There's the `gdbus` tool that can do this:
93-
94-
```console
95-
student@os:~$ gdbus call \
96-
--session \
97-
--dest org.freedesktop.Notifications \
98-
--object-path /org/freedesktop/Notifications \
99-
--method org.freedesktop.Notifications.Notify \
100-
-- \
101-
'""' \
102-
0 \
103-
'""' \
104-
"This is the title" \
105-
"This is the content" \
106-
[] \
107-
{} \
108-
-1
109-
```
110-
111-
Let's see how it works:
112-
113-
![gdbus-notify](../media/gdbus_notify.gif)
114-
115-
You can also find this `gdbus` call in the `support/dbus/send_notification.sh` script.
116-
117-
## Inspecting the Low-level Communication
118-
119-
Let's run `gdbus` under `strace` to see what's happening behind the scenes.
120-
Run the script in `support/dbus/send_notification_strace.sh`:
121-
122-
```console
123-
strace: Process 61888 attached
124-
[pid 61887] socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0) = 5
125-
[pid 61887] connect(5, {sa_family=AF_UNIX, sun_path="/run/user/1000/bus"}, 110) = 0
126-
[pid 61887] sendmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\0", iov_len=1}], msg_iovlen=1,
127-
msg_control=[{cmsg_len=28, cmsg_level=SOL_SOCKET, cmsg_type=SCM_CREDENTIALS, cmsg_data={pid=61887,
128-
uid=1000, gid=1000}}],
129-
msg_controllen=32, msg_flags=0}, MSG_NOSIGNAL) = 1
130-
strace: Process 61889 attached
131-
132-
[...]
133-
134-
[pid 61889] sendmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1T\0\0\0\3\0\0\0\237\0\0\0\1
135-
\1o\0\36\0\0\0/org/freedesktop/Notifications\0\0\2\1s\0\35\0\0\0org.freedesktop.Notifications\0\0\0\6\1s\0\35
136-
\0\0\0org.freedesktop.Notifications\0\0\0\10\1g\0\rsusssasa{sv}i\0\0\0\0\0\0\3\1s\0\6\0\0\0Notify\0\0\0\0\0\0
137-
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\21\0\0\0This is the title\0\0\0\23\0\0\0This is the content\0\0\0\0\0\0\0\0\0
138-
\0\0\0\0\377\377\377\377", iov_len=260}], msg_iovlen=1, msg_controllen=0,
139-
msg_flags=0}, MSG_NOSIGNAL) = 260
140-
[pid 61889] recvmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\2\1\1\4\0\0\0\312\0\0\0.\0\0\0", iov_len=16}],
141-
msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 16
142-
[pid 61889] recvmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\6\1s\0\6\0\0\0:1.497\0\0\10\1g\0\1u\0\0\5\1u\0
143-
\3\0\0\0\7\1s\0\5\0\0\0:1.49\0\0\0\36\0\0\0", iov_len=52}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 52
144-
(uint32 30,)
145-
[pid 61889] +++ exited with 0 +++
146-
[pid 61888] +++ exited with 0 +++
147-
+++ exited with 0 +++
148-
```
149-
150-
We see a Unix socket being created and a connection made to `/run/user/1000/bus`, as expected.
151-
Then a series of messages are exchanged on the socket, which are part of the D-Bus protocol.
152-
On a closer look, we can even identify some strings from our notification, like `This is the title` or `This is the content`:
153-
154-
```console
155-
[pid 61889] sendmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\1\0\1T\0\0\0\3\0\0\0\237\0\0\0\1
156-
\1o\0\36\0\0\0/org/freedesktop/Notifications\0\0\2\1s\0\35\0\0\0org.freedesktop.Notifications\0\0\0\6\1s\0\35
157-
\0\0\0org.freedesktop.Notifications\0\0\0\10\1g\0\rsusssasa{sv}i\0\0\0\0\0\0\3\1s\0\6\0\0\0Notify\0\0\0\0\0\0
158-
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\21\0\0\0This is the title\0\0\0\23\0\0\0This is the content\0\0\0\0\0\0\0\0\0
159-
\0\0\0\0\377\377\377\377", iov_len=260}], msg_iovlen=1, msg_controllen=0,
160-
```
161-
162-
## D-Bus usage in Python
163-
Use the `dbus` python bindings to get the computer's battery level using a python script.
164-
You can start from the documentation [here](https://dbus.freedesktop.org/doc/dbus-python/tutorial.html#).
165-
You need to read the sections `Connecting to the Bus`, `Proxy objects`, and `Interfaces and methods`.
166-
167-
There's also a skeleton you can use in `chapters/app-interact/arena/support/dbus/get_battery_level.py`.
168-
169-
In summary, your script will start by connecting to the `System Bus`.
170-
Then you'll use the `get_object` method to obtain a proxy object.
171-
On this proxy object, you can actually do the method call as explained [here](https://dbus.freedesktop.org/doc/dbus-python/tutorial.html#interfaces-and-methods):
172-
173-
```text
174-
To call a method, call the method of the same name on the proxy object, passing in the interface name via the dbus_interface keyword argument
175-
```
176-
177-
So, if you want to call the method `this.is.an.interface.method` with the arguments `A` and `B` you can do it like this:
178-
179-
```python
180-
result = proxy.method(A, B, dbus_interface = "this.is.an.interface")
181-
```
182-
183-
## Firefox
184-
185-
Let's do the following experiment:
186-
187-
- Open the Firefox browser
188-
189-
- From a terminal run `firefox www.google.com`
190-
191-
![firefox-url-open](../media/firefox_url_open.gif)
192-
193-
Notice that the URL we passed in the command-line was opened in the existing Firefox window as a new tab.
194-
Even though we started a separate Firefox process, which should have created a separate new window, this didn't actually happen.
195-
Instead, the process that we started from the command-line exited immediately and the site was opened in the already running Firefox instance.
196-
197-
Without any precise knowledge about Firefox internals, we can guess that something like this happened:
198-
199-
- The newly started Firefox process detected that another instance of Firefox is already running
200-
201-
- The newly started Firefox process sent a message to the existing running process, requesting it to open a URL in a new tab
202-
203-
Since we're talking about message passing between 2 processes, there's a chance that maybe D-Bus was involved.
204-
Let's check: we'll use a tool called `dbus-monitor` that will print all messages passed through D-Bus.
205-
206-
```console
207-
student@os:~$ dbus-monitor
208-
```
209-
210-
Then, in another terminal, we'll run `firefox www.google.com` again.
211-
212-
Going back to the `dbus-monitor` output, we find the following:
213-
214-
```console
215-
...
216-
method call time=1655809062.813923 sender=:1.757 -> destination=org.mozilla.firefox.ZGVmYXVsdC1yZWxlYXNl serial=2 path=/org/mozilla/firefox/Remote; interface=org.mozilla.firefox; member=OpenURL
217-
array of bytes [
218-
02 00 00 00 1a 00 00 00 2f 00 00 00 2f 68 6f 6d 65 2f 61 64 72 69 61 6e
219-
73 00 2f 6f 70 74 2f 66 69 72 65 66 6f 78 2f 66 69 72 65 66 6f 78 00 77
220-
77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 00
221-
]
222-
```
223-
224-
There was a D-Bus call to `org.mozilla.firefox.ZGVmYXVsdC1yZWxlYXNl`, on the object `/org/mozilla/firefox/Remote`, method `OpenURL` from the `org.mozilla.firefox` interface.
225-
Indeed, we see that this object exists in d-feet as well:
226-
227-
![dfeet-firefox](../media/dfeet_firefox.png)
228-
229-
We can try to call the `OpenURL` method ourselves, directly from d-feet.
230-
The method has only one argument of the type `Array of [Byte]`.
231-
Although there's no documentation for it, we can use the same byte array that we saw in `dbus-monitor`:
232-
233-
```console
234-
array of bytes [
235-
02 00 00 00 1a 00 00 00 2f 00 00 00 2f 68 6f 6d 65 2f 61 64 72 69 61 6e
236-
73 00 2f 6f 70 74 2f 66 69 72 65 66 6f 78 2f 66 69 72 65 66 6f 78 00 77
237-
77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 00
238-
]
239-
```
240-
241-
(Note that `77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d` at the end is the string `www.google.com`, so that's another confirmation that we're on the right track).
242-
243-
![dfeet-url-open](../media/dfeet_url_open.gif)

chapters/app-interact/os-cloud/drills/questions/cgroups-vs-namespaces.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cgroups versus namespaces
1+
# Cgroups Versus namespaces
22

33
## Question Text
44

chapters/app-interact/os-cloud/drills/questions/container-vs-vm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Container versus VM
1+
# Container Versus VM
22

33
## Question Text
44

0 commit comments

Comments
 (0)