summaryrefslogtreecommitdiff
path: root/liblightdm-gobject/system.c
blob: a8104a88a603380b5eb3e066892541ffa0a1df35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2010-2011 Robert Ancell.
 * Author: Robert Ancell <robert.ancell@canonical.com>
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 or version 3 of the License.
 * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
 */

#include <string.h>

#include "lightdm/system.h"

/**
 * SECTION:system
 * @title: System Information
 * @short_description: Get system information
 * @include: lightdm.h
 *
 * Helper functions to get system information.
 */

/**
 * lightdm_get_hostname:
 *
 * Return value: The name of the host we are running on.
 **/
const gchar *
lightdm_get_hostname (void)
{
    return g_get_host_name ();
}

static gboolean os_release_loaded = FALSE;
static gchar *os_id = NULL;
static gchar *os_name = NULL;
static gchar *os_version = NULL;
static gchar *os_version_id = NULL;
static gchar *os_pretty_name = NULL;

static void
use_os_value (const gchar *name, const gchar *value)
{
    if (strcmp (name, "ID") == 0)
        os_id = g_strdup (value);
    if (strcmp (name, "NAME") == 0)
        os_name = g_strdup (value);
    if (strcmp (name, "VERSION") == 0)
        os_version = g_strdup (value);
    if (strcmp (name, "VERSION_ID") == 0)
        os_version_id = g_strdup (value);
    if (strcmp (name, "PRETTY_NAME") == 0)
        os_pretty_name = g_strdup (value);
}

static void
load_os_release (void)
{
    if (os_release_loaded)
        return;

    g_autofree gchar *data = NULL;
    if (!g_file_get_contents ("/etc/os-release", &data, NULL, NULL))
        return;

    g_auto(GStrv) lines = g_strsplit (data, "\n", -1);
    for (guint i = 0; lines[i] != NULL; i++)
    {
        g_auto(GStrv) tokens = g_strsplit (lines[i], "=", 2);
        if (tokens[0] != NULL && tokens[1] != NULL)
        {
            const gchar *name = g_strstrip (tokens[0]);
            gchar *value = g_strstrip (tokens[1]);
            size_t value_length = strlen (value);
            if (value_length > 1 && value[0] == '\"' && value[value_length - 1] == '\"')
            {
                 value[value_length - 1] = '\0';
                 value++;
                 use_os_value (name, value);
            }
        }
    }

    os_release_loaded = TRUE;
}

/**
 * lightdm_get_os_id:
 *
 * Get a word describing the OS, suitable for checking which OS the greeter is running on.
 * e.g. "ubuntu"
 *
 * Return value: (nullable): a string (ID variable from /etc/os-release) or %NULL if not set.
 **/
const gchar *
lightdm_get_os_id (void)
{
    load_os_release ();
    return os_id;
}

/**
 * lightdm_get_os_name:
 *
 * Get a line of text describing the OS without version information, suitable for presentation to the user.
 * e.g. "Ubuntu"
 *
 * Return value: (nullable): a string (NAME variable from /etc/os-release) or %NULL if not set.
 **/
const gchar *
lightdm_get_os_name (void)
{
    load_os_release ();
    return os_name;
}

/**
 * lightdm_get_os_pretty_name:
 *
 * Get a line of text describing the OS, suitable for presentation to the user.
 * e.g. "Ubuntu 16.04.1 LTS"
 *
 * Return value: (nullable): a string (PRETTY_NAME variable from /etc/os-release) or %NULL if not set.
 **/
const gchar *
lightdm_get_os_pretty_name (void)
{
    load_os_release ();
    return os_pretty_name;
}

/**
 * lightdm_get_os_version:
 *
 * Get a line of text describing the OS version, suitable for presentation to the user.
 * e.g. "16.04.1 LTS (Xenial Xapus)"
 *
 * Return value: (nullable): a string (VERSION variable from /etc/os-release) or %NULL if not set.
 **/
const gchar *
lightdm_get_os_version (void)
{
    load_os_release ();
    return os_version;
}

/**
 * lightdm_get_os_version_id:
 *
 * Get a word descibing the OS version, suitable for checking which version of the OS this greeter is running on.
 * e.g. "16.04"
 *
 * Return value: (nullable): a string (VERSION_ID variable from /etc/os-release) or %NULL if not set.
 **/
const gchar *
lightdm_get_os_version_id (void)
{
    load_os_release ();
    return os_version_id;
}

/**
 * lightdm_get_motd:
 *
 * Get a system message that should be presented to the user.
 * e.g. "Welcome to Yoyodyne"
 *
 * Return value: (nullable): a string (the contents of /etc/motd) or %NULL if not set.
 **/
gchar *
lightdm_get_motd (void)
{
    gchar *data = NULL;
    g_file_get_contents ("/etc/motd", &data, NULL, NULL);
    return data;
}