Update release name.
[AGL/documentation.git] / docs / 04_Developer_Guides / 03_Creating_a_New_Service.md
1 ---
2 title: Creating a New Service
3 ---
4
5 Services are software running in the background and providing, as their name suggests,
6 various services to other software: access to specific system hardware, connectivity
7 management, and network servers. Services can be split into 2 categories:
8
9 - **System services:** those usually run as a privileged user and make use of shared system
10   resources which they should have exclusive access to
11
12 - **User services:** such services run as part of an unprivileged user's session and can
13   only be called by said user
14
15 # Basic requirements
16
17 The only mandatory requirement is that service packages provide a `.service` file
18 so they can be properly managed by `systemd`. This file must be installed to a specific
19 location, determined by the service type (system or user):
20
21 - `/usr/lib/systemd/system/` for system services
22
23 - `/usr/lib/systemd/user/` for user services
24
25 Below is an example of a simple system service that requires the compositor to be
26 already running before the service starts:
27
28 ```
29 [Unit]
30 Requires=agl-compositor.service
31 After=agl-compositor.service
32
33 [Service]
34 Type=simple
35 ExecStart=/usr/bin/homescreen
36 Restart=on-failure
37 ```
38
39 The `Restart=on-failure` directive ensures the service will be automatically
40 restarted by `systemd` in case it crashes.
41
42 More details about `systemd` service files can be found in the
43 [systemd documentation](https://www.freedesktop.org/software/systemd/man/systemd.service.html).
44
45 # D-Bus activation
46
47 Services can also provide a D-Bus interface. In this case, they need not be started
48 on system boot (or user session startup in the case of user services) but can be
49 automatically started only when a client sends a request to the D-Bus name the service
50 registers.
51
52 D-Bus activated services must name their `systemd` service file `dbus-NAME.service`
53 where `NAME` is the D-Bus name registered by the service. This file must include the
54 following lines:
55
56 ```
57 [Service]
58 Type=dbus
59 BusName=NAME
60 ExecStart=/path/to/executable
61 ```
62
63 In addition, they must provide a D-Bus service file named `NAME.service` and installed
64 to one of the following locations:
65
66 - `/usr/share/dbus-1/system-services` for system services
67
68 - `/usr/share/dbus-1/services` for user services
69
70 The contents of the D-Bus service file must be the following:
71
72 ```
73 [D-BUS Service]
74 Name=NAME
75 Exec=/path/to/executable
76 SystemdService=dbus-NAME.service
77 ```
78
79 This ensures the service can be safely activated through D-Bus and no conflict will occur
80 between `systemd` and the D-Bus daemon.
81
82 More details about D-Bus activation can be found in the
83 [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-starting-services),
84 under the "Message Bus Starting Services (Activation)" section.
85
86 # Services startup
87
88 For D-Bus activated services, no additional action is required as those will be automatically
89 started whenever needed. Other services, however, need a few more steps in order to be
90 executed on system or session startup.
91
92 ## System services
93
94 System services can take advantage of the Yocto `systemd` class which automates the process of
95 enabling such services.
96
97 1\. Ensure the recipe inherits from the `systemd` class:
98
99 ```
100 inherit systemd
101 ```
102
103 2\. Declare the system services that needs to be enabled on boot:
104
105 ```
106 SYSTEMD_SERVICE:${PN} = "NAME.service"
107 ```
108
109 3\. Ensure the `FILES` variable includes the systemd service directory the corresponding
110 file will be installed to:
111
112 ```
113 FILES:${PN} = "\
114     ...
115     ${systemd_system_unitdir}/* \
116 "
117 ```
118
119 ## User services
120
121 The `systemd` class doesn't provide an equivalent mechanism for user services. This must
122 therefore be done manually as part of the package's install process.
123
124 1\. Make the service a part of the user session:
125
126 ```
127 do_install:append() {
128     install -d ${D}${systemd_user_unitdir}/agl-session.target.wants
129     ln -s ../NAME.service ${D}${systemd_user_unitdir}/agl-session.target.wants/NAME.service
130 }
131 ```
132
133 This ensures `agl-session.target` depends on `NAME.service`, the latter being therefore
134 automatically started on session creation.
135
136 2\. Ensure the `FILES` variable includes the systemd service directory the corresponding
137 file will be installed to:
138
139 ```
140 FILES:${PN} = "\
141     ...
142     ${systemd_user_unitdir}/* \
143 "
144 ```