Blame libnm/nm-object.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2007 - 2008 Novell, Inc.
Packit 5756e2
 * Copyright (C) 2007 - 2012 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-libnm.h"
Packit 5756e2
Packit 5756e2
#include "nm-object.h"
Packit 5756e2
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
#include <stdio.h>
Packit 5756e2
Packit 5756e2
#include "nm-utils.h"
Packit 5756e2
#include "nm-dbus-interface.h"
Packit 5756e2
#include "nm-object-private.h"
Packit 5756e2
#include "nm-dbus-helpers.h"
Packit 5756e2
#include "nm-client.h"
Packit 5756e2
#include "nm-core-internal.h"
Packit 5756e2
#include "c-list/src/c-list.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_PATH, );
Packit 5756e2
Packit 5756e2
typedef struct _NMObjectPrivate {
Packit Service a1bd4f
    NMClient *     client;
Packit Service a1bd4f
    NMLDBusObject *dbobj;
Packit 5756e2
} NMObjectPrivate;
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_ABSTRACT_TYPE(NMObject, nm_object, G_TYPE_OBJECT);
Packit 5756e2
Packit 5756e2
#define NM_OBJECT_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR(self, NMObject, NM_IS_OBJECT)
Packit 5756e2
Packit 5756e2
static NMObjectClass *_nm_object_class = NULL;
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static gpointer
Packit Service a1bd4f
_nm_object_get_private(NMObjectClass *klass, NMObject *self, guint16 extra_offset)
Packit 5756e2
{
Packit Service a1bd4f
    char *ptr;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(klass->priv_ptr_offset > 0);
Packit 5756e2
Packit Service a1bd4f
    ptr = (char *) self;
Packit Service a1bd4f
    ptr += klass->priv_ptr_offset;
Packit Service a1bd4f
    if (klass->priv_ptr_indirect)
Packit Service a1bd4f
        ptr = *((gpointer *) ptr);
Packit Service a1bd4f
    return ptr + extra_offset;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
NMLDBusObject *
Packit Service a1bd4f
_nm_object_get_dbobj(gpointer self)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_OBJECT_GET_PRIVATE(self)->dbobj;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
const char *
Packit Service a1bd4f
_nm_object_get_path(gpointer self)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_OBJECT_GET_PRIVATE(self)->dbobj->dbus_path->str;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
NMClient *
Packit Service a1bd4f
_nm_object_get_client(gpointer self)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_OBJECT_GET_PRIVATE(self)->client;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_object_get_path:
Packit 5756e2
 * @object: a #NMObject
Packit 5756e2
 *
Packit 5756e2
 * Gets the DBus path of the #NMObject.
Packit 5756e2
 *
Packit 5756e2
 * Returns: the object's path. This is the internal string used by the
Packit 5756e2
 * object, and must not be modified.
Packit 5756e2
 *
Packit 5756e2
 * Note that the D-Bus path of an NMObject never changes, even
Packit 5756e2
 * if the instance gets removed from the cache. To find out
Packit 5756e2
 * whether the object is still alive/cached, check nm_object_get_client().
Packit 5756e2
 **/
Packit 5756e2
const char *
Packit Service a1bd4f
nm_object_get_path(NMObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_OBJECT(object), NULL);
Packit 5756e2
Packit Service a1bd4f
    return _nm_object_get_path(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_object_get_client:
Packit 5756e2
 * @object: a #NMObject
Packit 5756e2
 *
Packit 5756e2
 * Returns the #NMClient instance in which object is cached.
Packit 5756e2
 * Also, if the object got removed from the client cached,
Packit 5756e2
 * this returns %NULL. So it can be used to check whether the
Packit 5756e2
 * object is still alive.
Packit 5756e2
 *
Packit 5756e2
 * Returns: (transfer none): the #NMClient cache in which the
Packit 5756e2
 * object can be found, or %NULL if the object is no longer
Packit 5756e2
 * cached.
Packit 5756e2
 *
Packit 5756e2
 * Since: 1.24
Packit 5756e2
 **/
Packit 5756e2
NMClient *
Packit Service a1bd4f
nm_object_get_client(NMObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_OBJECT(object), NULL);
Packit 5756e2
Packit Service a1bd4f
    return _nm_object_get_client(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
clear_properties(NMObject *self, NMClient *client)
Packit 5756e2
{
Packit Service a1bd4f
    NMObjectClass *                klass = NM_OBJECT_GET_CLASS(self);
Packit Service a1bd4f
    const _NMObjectClassFieldInfo *p;
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(NM_IS_OBJECT(self));
Packit Service a1bd4f
    nm_assert(!client || NM_IS_CLIENT(client));
Packit Service a1bd4f
Packit Service a1bd4f
    for (p = klass->property_o_info; p; p = p->parent) {
Packit Service a1bd4f
        nml_dbus_property_o_clear_many(_nm_object_get_private(p->klass, self, p->offset),
Packit Service a1bd4f
                                       p->num,
Packit Service a1bd4f
                                       client);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    for (p = klass->property_ao_info; p; p = p->parent) {
Packit Service a1bd4f
        nml_dbus_property_ao_clear_many(_nm_object_get_private(p->klass, self, p->offset),
Packit Service a1bd4f
                                        p->num,
Packit Service a1bd4f
                                        client);
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
is_ready(NMObject *self)
Packit 5756e2
{
Packit Service a1bd4f
    NMObjectClass *                klass  = NM_OBJECT_GET_CLASS(self);
Packit Service a1bd4f
    NMClient *                     client = _nm_object_get_client(self);
Packit Service a1bd4f
    const _NMObjectClassFieldInfo *p;
Packit Service a1bd4f
    guint16                        i;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(NM_IS_CLIENT(client));
Packit 5756e2
Packit Service a1bd4f
    for (p = klass->property_o_info; p; p = p->parent) {
Packit Service a1bd4f
        NMLDBusPropertyO *fields = _nm_object_get_private(p->klass, self, p->offset);
Packit 5756e2
Packit Service a1bd4f
        for (i = 0; i < p->num; i++) {
Packit Service a1bd4f
            if (!nml_dbus_property_o_is_ready(&fields[i]))
Packit Service a1bd4f
                return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    for (p = klass->property_ao_info; p; p = p->parent) {
Packit Service a1bd4f
        NMLDBusPropertyAO *fields = _nm_object_get_private(p->klass, self, p->offset);
Packit 5756e2
Packit Service a1bd4f
        for (i = 0; i < p->num; i++) {
Packit Service a1bd4f
            if (!nml_dbus_property_ao_is_ready(&fields[i]))
Packit Service a1bd4f
                return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
obj_changed_notify(NMObject *self)
Packit 5756e2
{
Packit Service a1bd4f
    NMObjectClass *                klass  = NM_OBJECT_GET_CLASS(self);
Packit Service a1bd4f
    NMClient *                     client = _nm_object_get_client(self);
Packit Service a1bd4f
    const _NMObjectClassFieldInfo *p;
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(NM_IS_CLIENT(client));
Packit Service a1bd4f
Packit Service a1bd4f
    for (p = klass->property_o_info; p; p = p->parent) {
Packit Service a1bd4f
        nml_dbus_property_o_notify_changed_many(_nm_object_get_private(p->klass, self, p->offset),
Packit Service a1bd4f
                                                p->num,
Packit Service a1bd4f
                                                client);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    for (p = klass->property_ao_info; p; p = p->parent) {
Packit Service a1bd4f
        nml_dbus_property_ao_notify_changed_many(_nm_object_get_private(p->klass, self, p->offset),
Packit Service a1bd4f
                                                 p->num,
Packit Service a1bd4f
                                                 client);
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
register_client(NMObject *self, NMClient *client, NMLDBusObject *dbobj)
Packit 5756e2
{
Packit Service a1bd4f
    NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE(self);
Packit 5756e2
Packit Service a1bd4f
    nm_assert(!priv->client);
Packit Service a1bd4f
    nm_assert(NML_IS_DBUS_OBJECT(dbobj));
Packit Service a1bd4f
    nm_assert(dbobj->nmobj == G_OBJECT(self));
Packit 5756e2
Packit Service a1bd4f
    priv->client = client;
Packit Service a1bd4f
    priv->dbobj  = nml_dbus_object_ref(dbobj);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
unregister_client(NMObject *self, NMClient *client, NMLDBusObject *dbobj)
Packit 5756e2
{
Packit Service a1bd4f
    NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE(self);
Packit 5756e2
Packit Service a1bd4f
    nm_assert(NM_IS_CLIENT(client));
Packit Service a1bd4f
    nm_assert(priv->client == client);
Packit Service a1bd4f
    priv->client = NULL;
Packit 5756e2
Packit Service a1bd4f
    clear_properties(self, client);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NMObject *self = NM_OBJECT(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_PATH:
Packit Service a1bd4f
        g_value_set_string(value, nm_object_get_path(self));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_object_init(NMObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMObject *       self = NM_OBJECT(object);
Packit Service a1bd4f
    NMObjectPrivate *priv;
Packit 5756e2
Packit Service a1bd4f
    priv = G_TYPE_INSTANCE_GET_PRIVATE(self, NM_TYPE_OBJECT, NMObjectPrivate);
Packit 5756e2
Packit Service a1bd4f
    self->_priv = priv;
Packit 5756e2
Packit Service a1bd4f
    c_list_init(&self->obj_base.queue_notify_lst);
Packit Service 8f75d2
Packit Service 8f75d2
    NML_DBUS_LOG(_NML_NMCLIENT_LOG_LEVEL_COERCE(NML_DBUS_LOG_LEVEL_TRACE),
Packit Service 8f75d2
                 "nmobj[" NM_HASH_OBFUSCATE_PTR_FMT "]: creating",
Packit Service 8f75d2
                 NM_HASH_OBFUSCATE_PTR(self));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
dispose(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMObject *       self = NM_OBJECT(object);
Packit Service a1bd4f
    NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE(self);
Packit 5756e2
Packit Service 8f75d2
    if (!self->obj_base.is_disposing) {
Packit Service 8f75d2
        NML_DBUS_LOG(_NML_NMCLIENT_LOG_LEVEL_COERCE(NML_DBUS_LOG_LEVEL_TRACE),
Packit Service 8f75d2
                     "nmobj[" NM_HASH_OBFUSCATE_PTR_FMT "]: disposing",
Packit Service 8f75d2
                     NM_HASH_OBFUSCATE_PTR(self));
Packit Service 8f75d2
    }
Packit Service 8f75d2
Packit Service a1bd4f
    self->obj_base.is_disposing = TRUE;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(c_list_is_empty(&self->obj_base.queue_notify_lst));
Packit Service a1bd4f
    nm_assert(!priv->client);
Packit Service a1bd4f
    nm_assert(!priv->dbobj || !priv->dbobj->nmobj);
Packit 5756e2
Packit Service a1bd4f
    clear_properties(self, NULL);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_object_parent_class)->dispose(object);
Packit 5756e2
Packit Service a1bd4f
    nm_clear_pointer(&priv->dbobj, nml_dbus_object_unref);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_object_class_init(NMObjectClass *klass)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
Packit 5756e2
Packit Service a1bd4f
    _nm_object_class = klass;
Packit 5756e2
Packit Service a1bd4f
    g_type_class_add_private(klass, sizeof(NMObjectPrivate));
Packit 5756e2
Packit Service a1bd4f
    object_class->get_property = get_property;
Packit Service a1bd4f
    object_class->dispose      = dispose;
Packit 5756e2
Packit Service a1bd4f
    klass->register_client    = register_client;
Packit Service a1bd4f
    klass->unregister_client  = unregister_client;
Packit Service a1bd4f
    klass->is_ready           = is_ready;
Packit Service a1bd4f
    klass->obj_changed_notify = obj_changed_notify;
Packit 5756e2
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMObject:path:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The D-Bus object path.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_PATH] = g_param_spec_string(NM_OBJECT_PATH,
Packit Service a1bd4f
                                                    "",
Packit Service a1bd4f
                                                    "",
Packit Service a1bd4f
                                                    NULL,
Packit Service a1bd4f
                                                    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit 5756e2
Packit Service a1bd4f
    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
Packit 5756e2
}