| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
/* This gets logged as part of the (more verbose) protocol logging */ |
| 9 |
#ifdef G_LOG_DOMAIN |
| 10 |
#undef G_LOG_DOMAIN |
| 11 |
#endif |
| 12 |
#define G_LOG_DOMAIN "cockpit-protocol" |
| 13 |
|
| 14 |
#include "cockpitsystem.h" |
| 15 |
|
| 16 |
#include <glib/gstdio.h> |
| 17 |
|
| 18 |
#include <systemd/sd-login.h> |
| 19 |
|
| 20 |
#include <sys/types.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
static const gchar *os_release_fields[] = { |
| 27 |
"NAME", |
| 28 |
"VERSION", |
| 29 |
"ID", |
| 30 |
"VERSION_ID", |
| 31 |
"PRETTY_NAME", |
| 32 |
"VARIANT", |
| 33 |
"VARIANT_ID", |
| 34 |
"CPE_NAME", |
| 35 |
"DOCUMENTATION_URL", |
| 36 |
NULL |
| 37 |
}; |
| 38 |
|
| 39 |
const gchar ** |
| 40 |
cockpit_system_os_release_fields (void) |
| 41 |
{ |
| 42 |
return os_release_fields; |
| 43 |
} |
| 44 |
|
| 45 |
GHashTable * |
| 46 |
cockpit_system_load_os_release (void) |
| 47 |
{ |
| 48 |
GError *error = NULL; |
| 49 |
GHashTable *result = NULL; |
| 50 |
gchar *contents = NULL; |
| 51 |
gsize len; |
| 52 |
gchar **lines = NULL; |
| 53 |
guint n; |
| 54 |
gchar *line, *val; |
| 55 |
|
| 56 |
g_file_get_contents ("/etc/os-release", &contents, &len, &error); |
| 57 |
|
| 58 |
if (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
| 59 |
{ |
| 60 |
g_clear_error (&error); |
| 61 |
g_file_get_contents ("/usr/lib/os-release", &contents, &len, &error); |
| 62 |
} |
| 63 |
|
| 64 |
if (error) |
| 65 |
{ |
| 66 |
g_message ("error loading contents of os-release: %s", error->message); |
| 67 |
goto out; |
| 68 |
} |
| 69 |
|
| 70 |
result = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 71 |
|
| 72 |
lines = g_strsplit (contents, "\n", -1); |
| 73 |
for (n = 0; lines != NULL && lines[n] != NULL; n++) |
| 74 |
{ |
| 75 |
line = lines[n]; |
| 76 |
val = strchr (line, '='); |
| 77 |
|
| 78 |
if (val) |
| 79 |
{ |
| 80 |
*val = '\0'; |
| 81 |
val++; |
| 82 |
|
| 83 |
/* Remove quotes from value */ |
| 84 |
len = strlen (val); |
| 85 |
if (len && val[0] == '\"' && val[len - 1] == '\"') |
| 86 |
{ |
| 87 |
val[len - 1] = '\0'; |
| 88 |
val++; |
| 89 |
} |
| 90 |
|
| 91 |
g_hash_table_replace (result, line, val); |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
g_free (line); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
out: |
| 100 |
g_clear_error (&error); |
| 101 |
g_free (lines); |
| 102 |
g_free (contents); |
| 103 |
|
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
cockpit_setenv_check (const char *variable, |
| 109 |
const char *value, |
| 110 |
gboolean overwrite) |
| 111 |
{ |
| 112 |
if (!g_setenv (variable, value, overwrite)) |
| 113 |
g_error("could not set $%s to %s", variable, value); |
| 114 |
} |