~ubuntu-core-dev/update-notifier/ubuntu

953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
1
/* livepatch-utils.c
953.1.7 by Andrea Azzarone
Fix the copyright header.
2
 * Copyright (C) 2019 Canonical Ltd
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, write to the
16
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
17
 * Boston, MA  02110-1301 USA.
18
 */
19
20
#include "livepatch-utils.h"
21
22
#include <gio/gdesktopappinfo.h>
23
#include <glib.h>
24
#include <json-glib/json-glib.h>
25
26
#define OS_RELEASE_PATH "/etc/os-release"
27
28
GQuark
29
livepatch_error_quark(void)
30
{
31
  static GQuark q = 0;
32
33
  if (G_UNLIKELY(!q))
34
    q = g_quark_from_static_string("livepatch-error-quark");
35
36
  return q;
37
}
38
39
static GKeyFile *
40
parse_osrelease()
41
{
953.1.5 by Andrea Azzarone
Init keys to NULL in parse_osrelease.
42
    g_autoptr(GKeyFile) keys = NULL;
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
43
    g_autofree gchar *content = NULL;
44
    g_autofree gchar *content_with_group = NULL;
45
    const gchar *group = "[os-release]\n";
46
    g_autoptr(GError) error = NULL;
47
48
    if (!g_file_get_contents(OS_RELEASE_PATH, &content, NULL, &error))
49
      {
50
        g_warning("Failed to read '%s', error: %s",
51
                  OS_RELEASE_PATH, error->message);
52
        return NULL;
53
      }
54
55
    content_with_group = g_strdup_printf("%s%s", group, content);
56
57
    keys = g_key_file_new();
58
    if (!g_key_file_load_from_data(keys, content_with_group, -1,
59
                                   G_KEY_FILE_NONE, &error))
60
      {
61
        g_warning("Failed to parse /etc/os-release: %s", error->message);
62
        return NULL;
63
      }
64
65
    return g_steal_pointer(&keys);
66
}
67
68
static JsonNode *
69
livepatch_get_status(GError **error)
70
{
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
71
  g_autofree gchar *std_output = NULL;
72
  g_autofree gchar *std_error = NULL;
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
73
  g_autoptr(JsonParser) json_parser = NULL;
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
74
  JsonNode *root_node;
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
75
76
  if (!g_spawn_command_line_sync("canonical-livepatch status --format=json",
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
77
                                 &std_output, &std_error, NULL, error))
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
78
    return NULL;
79
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
80
  if (std_output == NULL || strlen(std_output) == 0)
81
    {
82
      if (std_error == NULL || strlen(std_error) == 0)
83
        {
84
          g_set_error(error,
85
                      LIVEPATCH_ERROR, LIVEPATCH_ERROR_CMD_FAILED,
86
                      "canonical-livepatch returned an empty status.");
87
        }
88
      else
89
        {
90
          g_set_error(error,
91
                      LIVEPATCH_ERROR, LIVEPATCH_ERROR_CMD_FAILED,
92
                      "canonical-livepatch status returned an error: %s",
93
                      std_error);
94
        }
95
96
      return NULL;
97
    }
98
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
99
  json_parser = json_parser_new();
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
100
  if (!json_parser_load_from_data(json_parser, std_output, -1, error))
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
101
    return NULL;
102
953.1.6 by Andrea Azzarone
Make sure livepatch_get_status returns NULL (and sets an error) if 'canonical-livepatch status' command returns an empty status.
103
  root_node = json_parser_get_root(json_parser);
104
  if (root_node == NULL)
105
    {
106
      g_set_error(error,
107
                  LIVEPATCH_ERROR, LIVEPATCH_ERROR_CMD_FAILED,
108
                  "The output of canonical-livepatch has no json root node.");
109
      return NULL;
110
    }
111
112
  return json_node_copy(root_node);
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
113
}
114
115
static JsonNode *
116
livepatch_get_status_node(const gchar *expr, GError **error)
117
{
118
  g_autoptr(JsonNode) root = NULL;
119
120
  root = livepatch_get_status(error);
121
  if (root == NULL)
122
    return NULL;
123
124
  return json_path_query(expr, root, error);
125
}
126
127
gboolean
128
livepatch_has_settings_ui()
129
{
130
  g_autoptr(GDesktopAppInfo) info = NULL;
131
132
  info = g_desktop_app_info_new(LIVEPATCH_DESKTOP_FILE);
133
  return info != NULL;
134
}
135
136
gboolean
137
livepatch_is_supported()
138
{
139
  g_autoptr(GKeyFile) file = NULL;
140
  g_autofree gchar *version = NULL;
141
  g_autoptr(GError) error = NULL;
142
143
  file = parse_osrelease();
144
  if (file == NULL)
145
    return FALSE;
146
147
  version = g_key_file_get_string(file, "os-release", "VERSION", &error);
148
  if (version == NULL)
149
    {
150
      g_warning("Failed to get the version from the file %s: %s",
151
                OS_RELEASE_PATH, error->message);
152
      return FALSE;
153
    }
154
155
  return g_strstr_len(version, -1, "LTS") != NULL;
156
}
157
158
gboolean
159
livepatch_is_running()
160
{
161
  g_autoptr(JsonNode) result_node = NULL;
162
  JsonNode *running_node = NULL;
163
  JsonArray *result_array;
164
165
  result_node = livepatch_get_status_node("$.Status[0].Running", NULL);
166
  if (result_node == NULL)
167
    return FALSE;
168
169
  result_array = json_node_get_array(result_node);
170
171
  if (json_array_get_length(result_array) == 0)
172
    return FALSE;
173
174
  running_node = json_array_get_element(result_array, 0);
175
  return json_node_get_boolean(running_node);
176
}
177
178
gchar *
179
livepatch_get_state(GError **error)
180
{
181
  g_autoptr(JsonNode) result_node = NULL;
182
  JsonNode *state_node = NULL;
183
  JsonArray *result_array;
184
  const gchar *expr = "$.Status[0].Livepatch.State";
185
186
  result_node = livepatch_get_status_node(expr, error);
187
  if (result_node == NULL)
188
    return NULL;
189
190
  result_array = json_node_get_array(result_node);
191
192
  if (json_array_get_length(result_array) == 0)
193
    {
194
      g_set_error(error,
195
                  LIVEPATCH_ERROR, LIVEPATCH_ERROR_NOMATCH,
196
                  "No matches for: %s", expr);
197
      return NULL;
198
    }
199
200
  state_node = json_array_get_element(result_array, 0);
201
  return g_strdup(json_node_get_string(state_node));
202
}
203
204
gchar *
205
livepatch_get_check_state(GError **error)
206
{
207
  g_autoptr(JsonNode) result_node = NULL;
208
  JsonNode *state_node = NULL;
209
  JsonArray *result_array;
210
  const gchar *expr = "$.Status[0].Livepatch.CheckState";
211
212
  result_node = livepatch_get_status_node(expr, error);
213
  if (result_node == NULL)
214
    return NULL;
215
216
  result_array = json_node_get_array(result_node);
217
218
  if (json_array_get_length(result_array) == 0)
219
    {
220
      g_set_error(error,
221
                  LIVEPATCH_ERROR, LIVEPATCH_ERROR_NOMATCH,
222
                  "No matches for: %s", expr);
223
      return NULL;
224
    }
225
226
  state_node = json_array_get_element(result_array, 0);
227
  return g_strdup(json_node_get_string(state_node));
228
}
229
973 by Iain Lane
Make livepatch_get_num_fixes (private API) return 'ssize_t' instead of
230
ssize_t
953.1.1 by Andrea Azzarone
Add a livepatch indicator in the system tray.
231
livepatch_get_num_fixes(GError **error)
232
{
233
  g_autoptr(JsonNode) node = NULL;
234
  JsonArray *array;
235
236
  node = livepatch_get_status_node("$.Status[0].Livepatch.Fixes[*]", error);
237
  if (node == NULL)
238
    return -1;
239
240
  array = json_node_get_array(node);
241
  return json_array_get_length(array);
242
}