Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a dbus service name as its app_id #921

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,40 @@ _xdp_parse_app_id_from_unit_name (const char *unit)

return g_steal_pointer (&app_id);
}

char *
_xdp_parse_app_id_from_dbus_name (const char *unit)
{
g_autoptr(GRegex) regex = NULL;
g_autoptr(GMatchInfo) match = NULL;
g_autoptr(GError) error = NULL;
g_autofree char *app_id = NULL;

g_assert (g_str_has_prefix (unit, "dbus-"));

// Parses a unit name such as dbus-:1.2-org.kde.kdeconnect@7.service
Copy link
Collaborator

@smcv smcv Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming convention for systemd units is specific to dbus-broker.

On systems that have systemd activation available, the reference implementation (dbus-daemon) does not guarantee that activated services are in any sort of systemd service or scope: D-Bus services with SystemdService=foo.service are run as foo.service (probably also true for dbus-broker), and D-Bus services without SystemdService are run via "traditional activation", mechanically the same as what happens on non-systemd systems like *BSD or Devuan, so they end up as child processes in dbus.service (this behaviour is unlike dbus-broker).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, if a unit name matches this pattern then you can probably be quite confident that it's named according to how dbus-broker does it.

regex = g_regex_new ("^dbus-:[[:digit:]]+\\.[[:digit:]]+-"
"(.+?\\..+?\\..+?)(?:[@\\-][[:alnum:]]*)\\.service$",
0, 0, &error);
g_assert (error == NULL);
if (g_regex_match (regex, unit, 0, &match))
{
const char *escaped_app_id;
/* Unescape the unit name which may have \x hex codes in it, e.g.
* "app-gnome-org.gnome.Evolution\x2dalarm\x2dnotify-2437.scope"
*/
escaped_app_id = g_match_info_fetch (match, 1);
if (cunescape (escaped_app_id, UNESCAPE_RELAX, &app_id) < 0)
app_id = g_strdup ("");
}
else
{
g_warning("Failed to parse dbus name %s", unit);
Copy link
Collaborator

@smcv smcv Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will warn on some entirely legitimate unit names. There is a weak convention that a D-Bus service with Name=com.example.Whatever often has SystemdService=dbus-com.example.Whatever.service, which starts with dbus- but does not match dbus-broker's pattern.

I don't know of any concrete examples of user services (session bus services) doing this, but systemd's own dbus-org.freedesktop.login1.service and friends use this pattern for their system services (system bus services).

app_id = g_strdup ("");
}

return g_steal_pointer (&app_id);
}
#endif /* HAVE_LIBSYSTEMD */

void
Expand All @@ -206,13 +240,23 @@ set_appid_from_pid (XdpAppInfo *app_info, pid_t pid)
* fetching our own systemd units or the unit might not be started by the
* desktop environment (e.g. it's a script run from terminal).
*/
if (res == -ENODATA || res < 0 || !unit || !g_str_has_prefix (unit, "app-"))
if (res == -ENODATA || res < 0 || !unit)
{
app_info->id = g_strdup ("");
return;
}

app_info->id = _xdp_parse_app_id_from_unit_name (unit);
if (g_str_has_prefix (unit, "app-"))
{
app_info->id = _xdp_parse_app_id_from_unit_name (unit);
}
else if (g_str_has_prefix (unit, "dbus-"))
{
app_info->id = _xdp_parse_app_id_from_dbus_name (unit);
}
else
{
app_info->id = g_strdup ("");
}
g_debug ("Assigning app ID \"%s\" to pid %ld which has unit \"%s\"",
app_info->id, (long) pid, unit);

Expand Down