diff options
author | Thomas Haller <thaller@redhat.com> | 2016-04-25 21:18:06 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-04-25 21:28:41 +0200 |
commit | d3d02e08163d795266ed4ea70fea522894b13bf1 (patch) | |
tree | 9611d6304b186d7bab7e61452d7d8004254505f7 | |
parent | 4979bdd82f85fd8701552f7e9b7abba08bd02359 (diff) | |
download | NetworkManager-d3d02e08163d795266ed4ea70fea522894b13bf1.tar.gz |
core: add nm_utils_get_machine_id() util
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/nm-core-utils.c | 49 | ||||
-rw-r--r-- | src/nm-core-utils.h | 2 | ||||
-rw-r--r-- | src/tests/Makefile.am | 1 |
4 files changed, 52 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index e567fe0a7c..4ec7b40156 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -209,6 +209,7 @@ libNetworkManager_base_la_CPPFLAGS = \ -DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_INSIDE_DAEMON \ -DNO_SYSTEMD_JOURNAL \ -DPREFIX=\"$(prefix)\" \ + -DLOCALSTATEDIR=\"$(localstatedir)\" \ -DNMSTATEDIR=\"$(nmstatedir)\" \ $(GLIB_CFLAGS) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 24d862aff8..ebbeccefcb 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2586,7 +2586,54 @@ nm_utils_is_specific_hostname (const char *name) return FALSE; } -/******************************************************************/ +/*****************************************************************************/ + +static gboolean +machine_id_parse (const char *in, guint8 *uu) +{ + const char *cp; + int i; + guint8 v0, v1; + + g_return_val_if_fail (in != NULL, FALSE); + + for (i = 0; i < 32; i++) { + if (!g_ascii_isxdigit (in[i])) + return FALSE; + } + if (in[i] != '\0') + return FALSE; + + if (uu) { + cp = in; + for (i = 0; i < 16; i++) { + v0 = g_ascii_xdigit_value (*(cp++)); + v1 = g_ascii_xdigit_value (*(cp++)); + uu[i] = (v0 << 4) + v1; + } + } + return TRUE; +} + +gboolean +nm_utils_get_machine_id (gpointer out_uuid) +{ + gs_free char *contents = NULL; + + /* Get the machine ID from /etc/machine-id; it's always in /etc no matter + * where our configured SYSCONFDIR is. Alternatively, it might be in + * LOCALSTATEDIR /lib/dbus/machine-id. + */ + if ( g_file_get_contents ("/etc/machine-id", &contents, NULL, NULL) + || g_file_get_contents (LOCALSTATEDIR "/lib/dbus/machine-id", &contents, NULL, NULL)) { + contents = g_strstrip (contents); + return machine_id_parse (contents, out_uuid); + } + + return FALSE; +} + +/*****************************************************************************/ char * nm_utils_get_secret_key (gsize *out_key_len, GError **error) diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 82d9424ec5..e6a6d4d0f6 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -307,6 +307,8 @@ const char *nm_utils_ip4_property_path (const char *ifname, const char *property gboolean nm_utils_is_specific_hostname (const char *name); +gboolean nm_utils_get_machine_id (gpointer out_uuid); + char *nm_utils_get_secret_key (gsize *out_key_len, GError **error); /* IPv6 Interface Identifer helpers */ diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index b93d4da3b9..5b4e6b729f 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -142,6 +142,7 @@ test_utils_DEPENDENCIES = \ test_utils_CPPFLAGS = \ $(AM_CPPFLAGS) \ -DPREFIX=\"/nonexistent\" \ + -DLOCALSTATEDIR=\"$(localstatedir)\" \ -DNMSTATEDIR=\"/nonsense\" test_utils_LDADD = \ |