mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-14 12:05:03 +00:00
examples: add C examples for monitoring whether NM runs
- the first uses dbus-glib and D-Bus "NameOwnerChanged" signal directly - the second uses GIO's g_bus_watch_name() - the third uses Qt and QDBusServiceWatcher class
This commit is contained in:
parent
8ad3ff24ad
commit
38382770aa
|
@ -300,6 +300,9 @@ if (test "${enable_qt}" = "yes"); then
|
|||
fi
|
||||
AC_SUBST(QT_CFLAGS)
|
||||
AC_SUBST(QT_LIBS)
|
||||
# Check for moc-qt4 and if not found then moc
|
||||
QT4_BINDIR=`$PKG_CONFIG Qt --variable bindir`
|
||||
AC_CHECK_PROGS(MOC, [moc-qt4 moc],, [$QT4_BINDIR:$PATH])
|
||||
fi
|
||||
AM_CONDITIONAL(WITH_QT, test "${enable_qt}" = "yes")
|
||||
|
||||
|
|
|
@ -13,7 +13,9 @@ noinst_PROGRAMS = \
|
|||
get-active-connections-dbus-glib \
|
||||
list-connections-dbus-glib \
|
||||
list-connections-libnm-glib \
|
||||
get-ap-info-libnm-glib
|
||||
get-ap-info-libnm-glib \
|
||||
monitor-nm-running-dbus-glib \
|
||||
monitor-nm-running-GDBus
|
||||
|
||||
add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c
|
||||
add_connection_dbus_glib_LDADD = \
|
||||
|
@ -54,11 +56,22 @@ get_ap_info_libnm_glib_LDADD = \
|
|||
$(DBUS_LIBS) \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c
|
||||
monitor_nm_running_dbus_glib_LDADD = \
|
||||
$(DBUS_LIBS) \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c
|
||||
monitor_nm_running_GDBus_LDADD = \
|
||||
$(GIO_LIBS)
|
||||
|
||||
EXTRA_DIST = \
|
||||
add-connection-dbus-glib.c \
|
||||
add-connection-libnm-glib.c \
|
||||
get-active-connections-dbus-glib.c \
|
||||
list-connections-dbus-glib.c \
|
||||
list-connections-libnm-glib.c \
|
||||
get-ap-info-libnm-glib.c
|
||||
get-ap-info-libnm-glib.c \
|
||||
monitor-nm-running-dbus-glib.c \
|
||||
monitor-nm-running-GDBus.c
|
||||
|
||||
|
|
86
examples/C/glib/monitor-nm-running-GDBus.c
Normal file
86
examples/C/glib/monitor-nm-running-GDBus.c
Normal file
|
@ -0,0 +1,86 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* (C) Copyright 2012 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example monitors whether NM is running by checking if
|
||||
* "org.freedesktop.NetworkManager" is owned by a process on D-Bus.
|
||||
* It uses g_bus_watch_name().
|
||||
*
|
||||
* See also http://developer.gnome.org/gio/stable/gio-Watching-Bus-Names.html
|
||||
*
|
||||
* Standalone compilation:
|
||||
* gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` monitor-nm-running-GDBus.c -o monitor-nm-running-GDBus
|
||||
*/
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
static void
|
||||
on_name_appeared (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
const gchar *name_owner,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_print ("Name '%s' on the system bus is owned by %s => NM is running\n",
|
||||
name, name_owner);
|
||||
}
|
||||
|
||||
static void
|
||||
on_name_vanished (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_print ("Name '%s' does not exist on the system bus => NM is not running\n", name);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
guint watcher_id;
|
||||
GMainLoop *loop;
|
||||
GBusNameWatcherFlags flags;
|
||||
|
||||
/* Initialize GType system */
|
||||
g_type_init ();
|
||||
|
||||
g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
|
||||
g_print ("===================================================\n");
|
||||
|
||||
flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
|
||||
|
||||
/* Start to watch "org.freedesktop.NetworkManager" bus name */
|
||||
watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
|
||||
"org.freedesktop.NetworkManager",
|
||||
flags,
|
||||
on_name_appeared,
|
||||
on_name_vanished,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
/* Run main loop */
|
||||
loop = g_main_loop_new (NULL, FALSE);
|
||||
g_main_loop_run (loop);
|
||||
|
||||
/* Stop watching the name */
|
||||
g_bus_unwatch_name (watcher_id);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
125
examples/C/glib/monitor-nm-running-dbus-glib.c
Normal file
125
examples/C/glib/monitor-nm-running-dbus-glib.c
Normal file
|
@ -0,0 +1,125 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* (C) Copyright 2012 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example monitors whether NM is running by checking D-Bus
|
||||
* NameOwnerChanged signal.
|
||||
* It uses dbus-glib library.
|
||||
*
|
||||
* Standalone compilation:
|
||||
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include <dbus/dbus-glib-bindings.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
|
||||
|
||||
static void
|
||||
proxy_name_owner_changed (DBusGProxy *proxy,
|
||||
const char *name,
|
||||
const char *old_owner,
|
||||
const char *new_owner,
|
||||
gpointer user_data)
|
||||
{
|
||||
gboolean *nm_running = (gboolean *) user_data;
|
||||
gboolean old_good = (old_owner && strlen (old_owner));
|
||||
gboolean new_good = (new_owner && strlen (new_owner));
|
||||
gboolean new_running = FALSE;
|
||||
|
||||
/* We are only interested in NetworkManager */
|
||||
if (!name || strcmp (name, NM_DBUS_SERVICE) != 0)
|
||||
return;
|
||||
|
||||
if (!old_good && new_good)
|
||||
new_running = TRUE;
|
||||
else if (old_good && !new_good)
|
||||
new_running = FALSE;
|
||||
|
||||
*nm_running = new_running;
|
||||
|
||||
g_print ("name: '%s', old_owner: '%s', new_owner: '%s'", name, old_owner, new_owner);
|
||||
g_print (" => NM is %s\n", *nm_running ? "running" : "not running");
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
DBusGConnection *bus;
|
||||
DBusGProxy *bus_proxy;
|
||||
GMainLoop *loop = NULL;
|
||||
GError *err = NULL;
|
||||
gboolean nm_running;
|
||||
|
||||
/* Initialize GType system */
|
||||
g_type_init ();
|
||||
|
||||
g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
|
||||
g_print ("===================================================\n");
|
||||
|
||||
/* Get system bus */
|
||||
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
|
||||
|
||||
/* Create a D-Bus proxy to D-Bus daemon */
|
||||
bus_proxy = dbus_g_proxy_new_for_name (bus,
|
||||
"org.freedesktop.DBus",
|
||||
"/org/freedesktop/DBus",
|
||||
"org.freedesktop.DBus");
|
||||
|
||||
if (!bus_proxy) {
|
||||
g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus.");
|
||||
dbus_g_connection_unref (bus);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Call NameHasOwner method to find out if NM is running. When NM runs it claims
|
||||
* 'org.freedesktop.NetworkManager' service name on D-Bus */
|
||||
if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) {
|
||||
g_message ("Error: NameHasOwner request failed: %s",
|
||||
(err && err->message) ? err->message : "(unknown)");
|
||||
g_clear_error (&err);
|
||||
g_object_unref (bus_proxy);
|
||||
dbus_g_connection_unref (bus);
|
||||
return -1;
|
||||
}
|
||||
g_print ("NM is %s\n", nm_running ? "running" : "not running");
|
||||
|
||||
|
||||
/* Connect to NameOwnerChanged signal to monitor NM running state */
|
||||
dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged",
|
||||
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
|
||||
G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (bus_proxy,
|
||||
"NameOwnerChanged",
|
||||
G_CALLBACK (proxy_name_owner_changed),
|
||||
&nm_running, NULL);
|
||||
|
||||
loop = g_main_loop_new (NULL, FALSE); /* Create main loop */
|
||||
g_main_loop_run (loop); /* Run main loop */
|
||||
|
||||
g_object_unref (bus_proxy);
|
||||
dbus_g_connection_unref (bus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -8,7 +8,8 @@ AM_CPPFLAGS = \
|
|||
noinst_PROGRAMS = \
|
||||
add-connection-wired \
|
||||
list-connections \
|
||||
change-ipv4-addresses
|
||||
change-ipv4-addresses \
|
||||
monitor-nm-running
|
||||
|
||||
add_connection_wired_SOURCES = add-connection-wired.cpp
|
||||
add_connection_wired_LDADD = \
|
||||
|
@ -25,8 +26,22 @@ change_ipv4_addresses_LDADD = \
|
|||
$(DBUS_LIBS) \
|
||||
$(QT_LIBS)
|
||||
|
||||
monitor_nm_running_SOURCES = monitor-nm-running.cpp
|
||||
monitor_nm_running_LDADD = \
|
||||
$(DBUS_LIBS) \
|
||||
$(QT_LIBS)
|
||||
|
||||
monitor-nm-running.moc: monitor-nm-running.cpp
|
||||
$(AM_V_GEN) $(MOC) -i $< -o $@
|
||||
|
||||
BUILT_SOURCES = \
|
||||
monitor-nm-running.moc
|
||||
|
||||
EXTRA_DIST = \
|
||||
add-connection-wired.cpp \
|
||||
list-connections.cpp \
|
||||
change-ipv4-addresses.cpp
|
||||
change-ipv4-addresses.cpp \
|
||||
monitor-nm-running.cpp
|
||||
|
||||
CLEANFILES = $(BUILT_SOURCES)
|
||||
|
||||
|
|
89
examples/C/qt/monitor-nm-running.cpp
Normal file
89
examples/C/qt/monitor-nm-running.cpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* vim: set ft=c ts=4 sts=4 sw=4 expandtab smartindent: */
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* (C) Copyright 2012 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example monitors whether NM is running by checking if
|
||||
* "org.freedesktop.NetworkManager" is owned by a process on D-Bus.
|
||||
* It uses QDBusServiceWatcher class.
|
||||
*
|
||||
* Standalone compilation:
|
||||
* moc-qt4 monitor-nm-running.cpp -o monitor-nm-running.moc
|
||||
* g++ -Wall `pkg-config --libs --cflags QtCore QtDBus` monitor-nm-running.cpp -o monitor-nm-running
|
||||
*
|
||||
* You don't need to have NetworkManager devel package installed.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QtDBus/QDBusServiceWatcher>
|
||||
#include <QtDBus/QDBusConnection>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkManager";
|
||||
|
||||
// Define a class with slots
|
||||
class NMWatcher: public QObject {
|
||||
Q_OBJECT;
|
||||
|
||||
public slots:
|
||||
void serviceRegistered(const QString& name);
|
||||
void serviceUnregistered(const QString& name);
|
||||
};
|
||||
|
||||
|
||||
void NMWatcher::serviceRegistered(const QString& name)
|
||||
{
|
||||
std::cout << "Name '" << name.toStdString() << "' registered"
|
||||
<< " => NM is running" << std::endl;
|
||||
}
|
||||
|
||||
void NMWatcher::serviceUnregistered(const QString& name)
|
||||
{
|
||||
std::cout << "Name '" << name.toStdString() << "' unregistered"
|
||||
<< " => NM is not running" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app (argc, argv);
|
||||
|
||||
qDebug() << "Monitor 'org.freedesktop.NetworkManager' D-Bus name";
|
||||
qDebug() << "===================================================";
|
||||
|
||||
NMWatcher nm_watcher;
|
||||
|
||||
// Watch all changes of D-Bus NM_DBUS_SERVICE name
|
||||
QDBusServiceWatcher *watcher = new QDBusServiceWatcher(NM_DBUS_SERVICE,
|
||||
QDBusConnection::systemBus());
|
||||
|
||||
QObject::connect(watcher, SIGNAL(serviceRegistered(const QString&)),
|
||||
&nm_watcher, SLOT(serviceRegistered(const QString&)));
|
||||
|
||||
QObject::connect(watcher, SIGNAL(serviceUnregistered(const QString&)),
|
||||
&nm_watcher, SLOT(serviceUnregistered(const QString&)));
|
||||
|
||||
app.exec();
|
||||
|
||||
delete watcher;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "monitor-nm-running.moc"
|
Loading…
Reference in a new issue