list-connections-libnm.c 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 *
16
 * (C) Copyright 2011 Red Hat, Inc.
17 18 19
 */

/*
Dan Winship's avatar
Dan Winship committed
20
 * The example shows how to list connections from System Settings service using libnm
21 22 23
 * (that wraps direct D-Bus calls).
 *
 * Compile with:
24
 *   gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` list-connections-libnm.c -o list-connections-libnm
25 26 27 28 29 30 31
 */

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

32
#include <NetworkManager.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63


/* Global variables */
GMainLoop *loop = NULL; /* Main loop variable - needed for waiting for signal */
int result = EXIT_SUCCESS;

static void
signal_handler (int signo)
{
	if (signo == SIGINT || signo == SIGTERM) {
		g_message ("Caught signal %d, shutting down...", signo);
		g_main_loop_quit (loop);
	}
}

static void
setup_signals (void)
{
	struct sigaction action;
	sigset_t mask;

	sigemptyset (&mask);
	action.sa_handler = signal_handler;
	action.sa_mask = mask;
	action.sa_flags = 0;
	sigaction (SIGTERM,  &action, NULL);
	sigaction (SIGINT,  &action, NULL);
}

/* Print details of connection */
static void
64
show_connection (gpointer data, gpointer user_data)
65 66 67 68 69 70 71 72
{
	NMConnection *connection = (NMConnection *) data;
	NMSettingConnection *s_con;
	guint64 timestamp;
	char *timestamp_str;
	char timestamp_real_str[64];
	const char *val1, *val2, *val3, *val4, *val5;

73
	s_con = nm_connection_get_setting_connection (connection);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	if (s_con) {
		/* Get various info from NMSettingConnection and show it */
		timestamp = nm_setting_connection_get_timestamp (s_con);
		timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp);
		strftime (timestamp_real_str, sizeof (timestamp_real_str), "%c", localtime ((time_t *) &timestamp));

		val1 = nm_setting_connection_get_id (s_con);
		val2 = nm_setting_connection_get_uuid (s_con);
		val3 = nm_setting_connection_get_connection_type (s_con);
		val4 = nm_connection_get_path (connection);
		val5 = timestamp ? timestamp_real_str : "never";

		printf ("%-25s | %s | %-15s | %-43s | %s\n", val1, val2, val3, val4, val5);

		g_free (timestamp_str);
	}
}

/* This callback is called when connections from the settings service are ready.
 * Now the connections can be listed.
 */
static void
96
get_connections_cb (NMRemoteSettings *settings, gpointer user_data)
97
{
98
	GSList *connections;
99

100
	connections = nm_remote_settings_list_connections (settings);
101

102
	printf ("Connections:\n===================\n");
103

104
	g_slist_foreach (connections, show_connection, NULL);
105

106
	g_slist_free (connections);
107 108 109 110 111 112 113 114 115 116
	g_object_unref (settings);

	/* We are done, exit main loop */
	g_main_loop_quit (loop);
}

/* Get system settings and then connect to connections-read signal */
static gboolean
list_connections (gpointer data)
{
117 118
	NMRemoteSettings *settings;
	gboolean settings_running;
119
	GError *error = NULL;
120 121

	/* Get system settings */
122 123 124
	if (!(settings = nm_remote_settings_new (NULL, &error))) {
		g_message ("Error: Could not get system settings: %s.", error->message);
		g_error_free (error);
125 126 127 128 129 130
		result = EXIT_FAILURE;
		g_main_loop_quit (loop);
		return FALSE;
	}

	/* Find out whether setting service is running */
131
	g_object_get (settings, NM_REMOTE_SETTINGS_SERVICE_RUNNING, &settings_running, NULL);
132

133
	if (!settings_running) {
134 135 136 137 138 139 140
		g_message ("Error: Can't obtain connections: settings service is not running.");
		result = EXIT_FAILURE;
		g_main_loop_quit (loop);
		return FALSE;
	}

	/* Connect to signal "connections-read" - emitted when connections are fetched and ready */
141
	g_signal_connect (settings, NM_REMOTE_SETTINGS_CONNECTIONS_READ,
142 143 144 145 146 147 148 149 150
	                  G_CALLBACK (get_connections_cb), NULL);

	return FALSE;
}

int main (int argc, char *argv[])
{
	DBusGConnection *bus;

151
#if !GLIB_CHECK_VERSION (2, 35, 0)
152 153
	/* Initialize GType system */
	g_type_init ();
154
#endif
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	/* Get system bus */
	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);

	/* Run list_connections from main loop, because we need to wait for "connections-read"
	 * signal to have connections ready. The execution will be finished in get_connections_cb()
	 * callback on the signal.
	 */
	g_idle_add (list_connections, bus);

	loop = g_main_loop_new (NULL, FALSE);  /* Create main loop */
	setup_signals ();                      /* Setup UNIX signals */
	g_main_loop_run (loop);                /* Run main loop */

	g_main_loop_unref (loop);
	dbus_g_connection_unref (bus);

	return result;
}