2 title: Creating a New Service
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, network servers... They can be split into 2 categories:
8 - system services: those usually run as a privileged user and make use of shared system
9 resources which they should have exclusive access to
10 - user services: such services run as part of an unprivileged user's session and can
11 only be called by said user
15 The only mandatory requirement is that service packages provide a `.service` file
16 so they can be properly managed by `systemd`. This file must be installed to a specific
17 location, determined by the service type (system or user):
18 - `/usr/lib/systemd/system/` for system services
19 - `/usr/lib/systemd/user/` for user services
21 Below is an example of a simple user service, running in a graphical session and
22 therefore requiring a compositor to be already running before the service starts:
26 Requires=agl-compositor.service
27 After=agl-compositor.service
31 ExecStart=/usr/bin/homescreen
35 WantedBy=agl-session.target
38 The `WantedBy=agl-session.target` indicates the service is part of the default AGL
39 user session, as mentioned in the [Application Framework](1_Application_Framework/1_Introduction/#user-session-management)
42 The `Restart=on-failure` directive ensures the service will be automatically
43 restarted by `systemd` in case it crashes.
45 More details about `systemd` service files can be found in the
46 [systemd documentation](https://www.freedesktop.org/software/systemd/man/systemd.service.html).
50 Services can also provide a D-Bus interface. In this case, they need not be started
51 on system boot (or user session startup in the case of user services) but can be
52 automatically started only when a client sends a request to the D-Bus name the service
55 D-Bus activated services must name their `systemd` service file `dbus-NAME.service`
56 where `NAME` is the D-Bus name registered by the service. This file must include the
63 ExecStart=/path/to/executable
66 In addition, they must provide a D-Bus service file named `NAME.service` and installed
67 to one of the following locations:
68 - `/usr/share/dbus-1/system-services` for system services
69 - `/usr/share/dbus-1/services` for user services
71 The contents of the D-Bus service file must be the following:
76 Exec=/path/to/executable
77 SystemdService=dbus-NAME.service
80 This ensures the service can be safely activated through D-Bus and no conflict will occur
81 between `systemd` and the D-Bus daemon.
83 More details about D-Bus activation can be found in the
84 [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-starting-services),
85 under the "Message Bus Starting Services (Activation)" section.
89 For D-Bus activated services, no additional action is required as those will be automatically
90 started whenever needed. Other services, however, need a few more steps in order to be
91 executed on system or session startup.
95 System services can take advantage of the Yocto `systemd` class which automates the process of
96 enabling such services.
98 1. Ensure the recipe inherits from the `systemd` class:
104 2. Declare the system services that needs to be enabled on boot:
107 SYSTEMD_SERVICE:${PN} = "NAME.service"
110 3. Ensure the `FILES` variable includes the systemd service directory the corresponding
111 file will be installed to:
116 ${systemd_system_unitdir}/* \
122 The `systemd` class doesn't provide an equivalent mechanism for user services. This must
123 therefore be done manually as part of the package's install process.
125 1. Make the service a part of the user session:
128 do_install:append() {
129 install -d ${D}${systemd_user_unitdir}/agl-session.target.wants
130 ln -s ../NAME.service ${D}${systemd_user_unitdir}/agl-session.target.wants/NAME.service
134 This ensures `agl-session.target` depends on `NAME.service`, the latter being therefore
135 automatically started on session creation.
137 2. Ensure the `FILES` variable includes the systemd service directory the corresponding
138 file will be installed to:
143 ${systemd_user_unitdir}/* \