Skip to content

Commit

Permalink
Implement LogControl1 protocol for dynamic log level changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bluca authored and jrybar-rh committed Aug 7, 2024
1 parent 64f5e4d commit 79d84d0
Showing 1 changed file with 115 additions and 8 deletions.
123 changes: 115 additions & 8 deletions src/polkitbackend/polkitbackendauthority.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ polkit_backend_authority_revoke_temporary_authorization_by_id (PolkitBackendAuth
typedef struct
{
guint authority_registration_id;
guint log_control_registration_id;

GDBusNodeInfo *introspection_info;

Expand All @@ -526,6 +527,9 @@ server_free (Server *server)
if (server->authority_registration_id > 0)
g_dbus_connection_unregister_object (server->connection, server->authority_registration_id);

if (server->log_control_registration_id > 0)
g_dbus_connection_unregister_object (server->connection, server->log_control_registration_id);

if (server->connection != NULL)
g_object_unref (server->connection);

Expand Down Expand Up @@ -650,6 +654,17 @@ static const gchar *server_introspection_data =
" <property type='s' name='BackendVersion' access='read'/>"
" <property type='u' name='BackendFeatures' access='read'/>"
" </interface>"
" <interface name='org.freedesktop.LogControl1'>"
" <property type='s' name='LogLevel' access='readwrite'>"
" <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
" </property>"
" <property type='s' name='LogTarget' access='readwrite'>"
" <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
" </property>"
" <property type='s' name='SyslogIdentifier' access='read'>"
" <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='false'/>"
" </property>"
" </interface>"
"</node>";

/* ---------------------------------------------------------------------------------------------------- */
Expand Down Expand Up @@ -1349,22 +1364,95 @@ server_handle_get_property (GDBusConnection *connection,

result = NULL;

if (g_strcmp0 (property_name, "BackendName") == 0)
if (g_strcmp0 (interface_name, "org.freedesktop.PolicyKit1.Authority") == 0)
{
result = g_variant_new_string (polkit_backend_authority_get_name (server->authority));
if (g_strcmp0 (property_name, "BackendName") == 0)
{
result = g_variant_new_string (polkit_backend_authority_get_name (server->authority));
}
else if (g_strcmp0 (property_name, "BackendVersion") == 0)
{
result = g_variant_new_string (polkit_backend_authority_get_version (server->authority));
}
else if (g_strcmp0 (property_name, "BackendFeatures") == 0)
{
result = g_variant_new_uint32 (polkit_backend_authority_get_features (server->authority));
}
else
g_assert_not_reached ();
}
else if (g_strcmp0 (property_name, "BackendVersion") == 0)
else if (g_strcmp0 (interface_name, "org.freedesktop.LogControl1") == 0)
{
result = g_variant_new_string (polkit_backend_authority_get_version (server->authority));
if (g_strcmp0 (property_name, "LogLevel") == 0)
{
switch (polkit_authority_log_level)
{
case LOG_LEVEL_EMERG:
result = g_variant_new_string ("emerg");
break;
case LOG_LEVEL_ALERT:
result = g_variant_new_string ("alert");
break;
case LOG_LEVEL_CRIT:
result = g_variant_new_string ("crit");
break;
case LOG_LEVEL_ERROR:
result = g_variant_new_string ("err");
break;
case LOG_LEVEL_WARNING:
result = g_variant_new_string ("warn");
break;
case LOG_LEVEL_NOTICE:
result = g_variant_new_string ("notice");
break;
case LOG_LEVEL_INFO:
result = g_variant_new_string ("info");
break;
case LOG_LEVEL_DEBUG:
result = g_variant_new_string ("debug");
break;
}
}
else if (g_strcmp0 (property_name, "LogTarget") == 0)
{
result = g_variant_new_string ("syslog");
}
else if (g_strcmp0 (property_name, "SyslogIdentifier") == 0)
{
result = g_variant_new_string ("polkitd");
}
else
g_assert_not_reached ();

}
else if (g_strcmp0 (property_name, "BackendFeatures") == 0)

return result;
}

static gboolean
server_handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data)
{
if (g_strcmp0 (interface_name, "org.freedesktop.LogControl1") != 0)
return FALSE;

if (g_strcmp0 (property_name, "LogLevel") == 0)
{
result = g_variant_new_uint32 (polkit_backend_authority_get_features (server->authority));
const gchar *level;

g_variant_get (value, "&s", &level);
polkit_backend_authority_set_log_level (level);
}
else
g_assert_not_reached ();
return FALSE;

return result;
return TRUE;
}

/* ---------------------------------------------------------------------------------------------------- */
Expand All @@ -1376,6 +1464,13 @@ static const GDBusInterfaceVTable server_vtable =
NULL, /* server_handle_set_property */
};

static const GDBusInterfaceVTable logcontrol_vtable =
{
NULL, /* server_handle_method_call */
server_handle_get_property,
server_handle_set_property,
};

/**
* polkit_backend_authority_unregister:
* @registration_id: A #gpointer obtained from polkit_backend_authority_register().
Expand Down Expand Up @@ -1431,6 +1526,18 @@ polkit_backend_authority_register (PolkitBackendAuthority *authority,
goto error;
}

server->log_control_registration_id = g_dbus_connection_register_object (server->connection,
"/org/freedesktop/LogControl1",
g_dbus_node_info_lookup_interface (server->introspection_info, "org.freedesktop.LogControl1"),
&logcontrol_vtable,
server,
NULL,
error);
if (server->log_control_registration_id == 0)
{
goto error;
}

server->authority = g_object_ref (authority);

server->authority_changed_id = g_signal_connect (server->authority,
Expand Down

0 comments on commit 79d84d0

Please sign in to comment.