1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
* (C) Copyright 2012 Red Hat, Inc.
22
* This example monitors whether NM is running by checking D-Bus
23
* NameOwnerChanged signal.
24
* It uses dbus-glib library.
26
* Standalone compilation:
27
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running
31
#include <dbus/dbus-glib.h>
32
#include <dbus/dbus-glib-bindings.h>
35
#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
38
proxy_name_owner_changed (DBusGProxy *proxy,
40
const char *old_owner,
41
const char *new_owner,
44
gboolean *nm_running = (gboolean *) user_data;
45
gboolean old_good = (old_owner && strlen (old_owner));
46
gboolean new_good = (new_owner && strlen (new_owner));
47
gboolean new_running = FALSE;
49
/* We are only interested in NetworkManager */
50
if (!name || strcmp (name, NM_DBUS_SERVICE) != 0)
53
if (!old_good && new_good)
55
else if (old_good && !new_good)
58
*nm_running = new_running;
60
g_print ("name: '%s', old_owner: '%s', new_owner: '%s'", name, old_owner, new_owner);
61
g_print (" => NM is %s\n", *nm_running ? "running" : "not running");
66
main (int argc, char *argv[])
69
DBusGProxy *bus_proxy;
70
GMainLoop *loop = NULL;
74
/* Initialize GType system */
77
g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
78
g_print ("===================================================\n");
81
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
83
/* Create a D-Bus proxy to D-Bus daemon */
84
bus_proxy = dbus_g_proxy_new_for_name (bus,
85
"org.freedesktop.DBus",
86
"/org/freedesktop/DBus",
87
"org.freedesktop.DBus");
90
g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus.");
91
dbus_g_connection_unref (bus);
95
/* Call NameHasOwner method to find out if NM is running. When NM runs it claims
96
* 'org.freedesktop.NetworkManager' service name on D-Bus */
97
if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) {
98
g_message ("Error: NameHasOwner request failed: %s",
99
(err && err->message) ? err->message : "(unknown)");
100
g_clear_error (&err);
101
g_object_unref (bus_proxy);
102
dbus_g_connection_unref (bus);
105
g_print ("NM is %s\n", nm_running ? "running" : "not running");
108
/* Connect to NameOwnerChanged signal to monitor NM running state */
109
dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged",
110
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
112
dbus_g_proxy_connect_signal (bus_proxy,
114
G_CALLBACK (proxy_name_owner_changed),
117
loop = g_main_loop_new (NULL, FALSE); /* Create main loop */
118
g_main_loop_run (loop); /* Run main loop */
120
g_object_unref (bus_proxy);
121
dbus_g_connection_unref (bus);