mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-28 11:00:09 +01:00
examples: add C examples for listing connections from system settings service
This commit is contained in:
parent
e3ba9162c2
commit
ca6e6b620d
3 changed files with 282 additions and 2 deletions
|
|
@ -6,7 +6,11 @@ AM_CPPFLAGS = \
|
|||
$(DBUS_CFLAGS) \
|
||||
$(GLIB_CFLAGS)
|
||||
|
||||
noinst_PROGRAMS = add-connection-glib get-active-connections
|
||||
noinst_PROGRAMS = \
|
||||
add-connection-glib \
|
||||
get-active-connections \
|
||||
list-connections-dbus \
|
||||
list-connections-libnm-glib
|
||||
|
||||
add_connection_glib_SOURCES = add-connection-glib.c
|
||||
add_connection_glib_LDADD = \
|
||||
|
|
@ -20,7 +24,22 @@ get_active_connections_LDADD = \
|
|||
$(DBUS_LIBS) \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
list_connections_dbus_SOURCES = list-connections-dbus.c
|
||||
list_connections_dbus_LDADD = \
|
||||
$(top_builddir)/libnm-util/libnm-util.la \
|
||||
$(DBUS_LIBS) \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
list_connections_libnm_glib_SOURCES = list-connections-libnm-glib.c
|
||||
list_connections_libnm_glib_LDADD = \
|
||||
$(top_builddir)/libnm-util/libnm-util.la \
|
||||
$(top_builddir)/libnm-glib/libnm-glib.la \
|
||||
$(DBUS_LIBS) \
|
||||
$(GLIB_LIBS)
|
||||
|
||||
EXTRA_DIST = \
|
||||
add-connection-glib.c \
|
||||
get-active-connections.c
|
||||
get-active-connections.c \
|
||||
list-connections-dbus.c \
|
||||
list-connections-libnm-glib.c
|
||||
|
||||
|
|
|
|||
83
examples/C/list-connections-dbus.c
Normal file
83
examples/C/list-connections-dbus.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* 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 2010 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The example shows how to list connections from System Settings service using direct
|
||||
* D-Bus call of ListConnections method.
|
||||
* The example uses dbus-glib, libnm-util libraries.
|
||||
*
|
||||
* Compile with:
|
||||
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` list-connections-dbus.c -o list-connections-dbus
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <NetworkManager.h>
|
||||
#include <nm-utils.h>
|
||||
|
||||
#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))
|
||||
|
||||
static void
|
||||
list_connections (DBusGProxy *proxy)
|
||||
{
|
||||
int i;
|
||||
GError *error = NULL;
|
||||
GPtrArray *con_array;
|
||||
|
||||
/* Call ListConnections D-Bus method */
|
||||
dbus_g_proxy_call (proxy, "ListConnections", &error,
|
||||
/* No input arguments */
|
||||
G_TYPE_INVALID,
|
||||
DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, &con_array, /* Return values */
|
||||
G_TYPE_INVALID);
|
||||
|
||||
for (i = 0; con_array && i < con_array->len; i++) {
|
||||
char *connection_path = g_ptr_array_index (con_array, i);
|
||||
printf ("%s\n", connection_path);
|
||||
g_free (connection_path);
|
||||
}
|
||||
g_ptr_array_free (con_array, TRUE);
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
DBusGConnection *bus;
|
||||
DBusGProxy *proxy;
|
||||
|
||||
/* Initialize GType system */
|
||||
g_type_init ();
|
||||
|
||||
/* Get system bus */
|
||||
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
|
||||
|
||||
/* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */
|
||||
proxy = dbus_g_proxy_new_for_name (bus,
|
||||
NM_DBUS_SERVICE_SYSTEM_SETTINGS,
|
||||
NM_DBUS_PATH_SETTINGS,
|
||||
NM_DBUS_IFACE_SETTINGS);
|
||||
|
||||
/* List connections of system settings service */
|
||||
list_connections (proxy);
|
||||
|
||||
g_object_unref (proxy);
|
||||
dbus_g_connection_unref (bus);
|
||||
|
||||
return 0;
|
||||
}
|
||||
178
examples/C/list-connections-libnm-glib.c
Normal file
178
examples/C/list-connections-libnm-glib.c
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* 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 2010 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The example shows how to list connections from System Settings service using libnm-glib
|
||||
* (that wraps direct D-Bus calls).
|
||||
* The example uses dbus-glib, libnm-util and libnm-glib libraries.
|
||||
*
|
||||
* Compile with:
|
||||
* gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util libnm-glib` list-connections-libnm-glib.c -o list-connections-libnm-glib
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <nm-connection.h>
|
||||
#include <nm-setting-connection.h>
|
||||
#include <NetworkManager.h>
|
||||
#include <nm-utils.h>
|
||||
#include <nm-remote-settings.h>
|
||||
#include <nm-remote-settings-system.h>
|
||||
#include <nm-settings-interface.h>
|
||||
|
||||
|
||||
/* 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
|
||||
show_connection (NMConnection *data, gpointer user_data)
|
||||
{
|
||||
NMConnection *connection = (NMConnection *) data;
|
||||
NMSettingConnection *s_con;
|
||||
guint64 timestamp;
|
||||
char *timestamp_str;
|
||||
char timestamp_real_str[64];
|
||||
const char *val1, *val2, *val3, *val4, *val5;
|
||||
|
||||
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
|
||||
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 *) ×tamp));
|
||||
|
||||
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
|
||||
get_connections_cb (NMSettingsInterface *settings, gpointer user_data)
|
||||
{
|
||||
GSList *system_connections;
|
||||
|
||||
system_connections = nm_settings_interface_list_connections (settings);
|
||||
|
||||
printf ("System connections:\n===================\n");
|
||||
|
||||
g_slist_foreach (system_connections, (GFunc) show_connection, NULL);
|
||||
|
||||
g_slist_free (system_connections);
|
||||
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)
|
||||
{
|
||||
DBusGConnection *bus = (DBusGConnection *) data;
|
||||
NMRemoteSettingsSystem *system_settings;
|
||||
gboolean system_settings_running;
|
||||
|
||||
/* Get system settings */
|
||||
if (!(system_settings = nm_remote_settings_system_new (bus))) {
|
||||
g_message ("Error: Could not get system settings.");
|
||||
result = EXIT_FAILURE;
|
||||
g_main_loop_quit (loop);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Find out whether setting service is running */
|
||||
g_object_get (system_settings, NM_REMOTE_SETTINGS_SERVICE_RUNNING, &system_settings_running, NULL);
|
||||
|
||||
if (!system_settings_running) {
|
||||
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 */
|
||||
g_signal_connect (system_settings, NM_SETTINGS_INTERFACE_CONNECTIONS_READ,
|
||||
G_CALLBACK (get_connections_cb), NULL);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
DBusGConnection *bus;
|
||||
|
||||
/* Initialize GType system */
|
||||
g_type_init ();
|
||||
|
||||
/* 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;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue