~ubuntu-branches/ubuntu/precise/network-manager/precise

« back to all changes in this revision

Viewing changes to examples/C/glib/monitor-nm-running-dbus-glib.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-02-09 16:45:41 UTC
  • mfrom: (1.1.53)
  • Revision ID: package-import@ubuntu.com-20120209164541-4h90zknlsfdb7x35
Tags: 0.9.2.0+git201202091925.c721477-0ubuntu1
* upstream snapshot 2012-02-09 19:25:59 (GMT)
  + c721477d11d4fe144111d6d2eec8f93f2e9186c9
* debian/patches/avoid-periodic-disk-wakeups.patch: refreshed.
* debian/patches/nl3-default-ip6-route.patch: refreshed.
* debian/libnm-glib4.symbols: add symbols:
  + nm_active_connection_get_master@Base
  + nm_client_new_async@Base
  + nm_client_new_finish@Base
  + nm_remote_settings_new_async@Base
  + nm_remote_settings_new_finish@Base
  + nm_device_get_state_reason@Base
* debian/libnm-util2.symbols: add symbols:
  + nm_setting_802_1x_get_pac_file@Base
  + nm_setting_infiniband_get_transport_mode@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: */
 
3
/*
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 * (C) Copyright 2012 Red Hat, Inc.
 
19
 */
 
20
 
 
21
/*
 
22
 * This example monitors whether NM is running by checking D-Bus
 
23
 * NameOwnerChanged signal.
 
24
 * It uses dbus-glib library.
 
25
 *
 
26
 * Standalone compilation:
 
27
 *   gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running
 
28
 */
 
29
 
 
30
#include <glib.h>
 
31
#include <dbus/dbus-glib.h>
 
32
#include <dbus/dbus-glib-bindings.h>
 
33
#include <string.h>
 
34
 
 
35
#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
 
36
 
 
37
static void
 
38
proxy_name_owner_changed (DBusGProxy *proxy,
 
39
                          const char *name,
 
40
                          const char *old_owner,
 
41
                          const char *new_owner,
 
42
                          gpointer user_data)
 
43
{
 
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;
 
48
 
 
49
        /* We are only interested in NetworkManager */
 
50
        if (!name || strcmp (name, NM_DBUS_SERVICE) != 0)
 
51
                return;
 
52
 
 
53
        if (!old_good && new_good)
 
54
                new_running = TRUE;
 
55
        else if (old_good && !new_good)
 
56
                new_running = FALSE;
 
57
 
 
58
        *nm_running = new_running;
 
59
 
 
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");
 
62
}
 
63
 
 
64
 
 
65
int
 
66
main (int argc, char *argv[])
 
67
{
 
68
        DBusGConnection *bus;
 
69
        DBusGProxy *bus_proxy;
 
70
        GMainLoop *loop = NULL;
 
71
        GError *err = NULL;
 
72
        gboolean nm_running;
 
73
 
 
74
        /* Initialize GType system */
 
75
        g_type_init ();
 
76
 
 
77
        g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n");
 
78
        g_print ("===================================================\n");
 
79
 
 
80
        /* Get system bus */
 
81
        bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
 
82
 
 
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");
 
88
 
 
89
        if (!bus_proxy) {
 
90
                g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus.");
 
91
                dbus_g_connection_unref (bus);
 
92
                return -1;
 
93
        }
 
94
 
 
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);
 
103
                return -1;
 
104
        }
 
105
        g_print ("NM is %s\n", nm_running ? "running" : "not running");
 
106
 
 
107
 
 
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,
 
111
                                 G_TYPE_INVALID);
 
112
        dbus_g_proxy_connect_signal (bus_proxy,
 
113
                                     "NameOwnerChanged",
 
114
                                     G_CALLBACK (proxy_name_owner_changed),
 
115
                                     &nm_running, NULL);
 
116
 
 
117
        loop = g_main_loop_new (NULL, FALSE);  /* Create main loop */
 
118
        g_main_loop_run (loop);                /* Run main loop */
 
119
 
 
120
        g_object_unref (bus_proxy);
 
121
        dbus_g_connection_unref (bus);
 
122
 
 
123
        return 0;
 
124
}
 
125