Merge lp:~xnox/whoopsie/stable-id-through-boot into lp:whoopsie

Proposed by Dimitri John Ledkov
Status: Merged
Merged at revision: 634
Proposed branch: lp:~xnox/whoopsie/stable-id-through-boot
Merge into: lp:whoopsie
Diff against target: 187 lines (+94/-2)
4 files modified
src/identifier.c (+33/-1)
src/identifier.h (+4/-0)
src/tests/test_identifier.c (+53/-0)
src/whoopsie.c (+4/-1)
To merge this branch: bzr merge lp:~xnox/whoopsie/stable-id-through-boot
Reviewer Review Type Date Requested Status
Brian Murray Approve
John Lenton (community) Needs Fixing
Review via email: mp+229593@code.launchpad.net
To post a comment you must log in.
Revision history for this message
John Lenton (chipaca) wrote :

A bug and a question, inline.

review: Needs Fixing
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Replied inline.

627. By Dimitri John Ledkov

Make whoopsie_identifier_generate() stable throughout the system boot.

That means, whilst whoopsied is running that api call will return the
current ID used by whoopsie itself, irrespective if the caller is root
or non-root, and additional MAC network interfaces or IMEI modems
appearing, and takes into consideration if custom system id was set.

628. By Dimitri John Ledkov

Drop unlink.

Revision history for this message
Brian Murray (brian-murray) wrote :

It seems to me that in whoopsie.c we could check to see if whoopsie_identifier is the same as the one in the existing WHOOPSIE_ID_PATH (if it exists) rather than writing it again. Otherwise, this looks good and I'll merge and upload it. Thanks!

review: Approve
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

did I change the cache path from /run to somewhere persistent? if not, do
update it. I recall discussing /var/lib/ or some such.
On 17 Sep 2014 20:48, "Brian Murray" <email address hidden> wrote:

> Review: Approve
>
> It seems to me that in whoopsie.c we could check to see if
> whoopsie_identifier is the same as the one in the existing WHOOPSIE_ID_PATH
> (if it exists) rather than writing it again. Otherwise, this looks good
> and I'll merge and upload it. Thanks!
> --
>
> https://code.launchpad.net/~xnox/whoopsie/stable-id-through-boot/+merge/229593
> You are the owner of lp:~xnox/whoopsie/stable-id-through-boot.
>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/identifier.c'
2--- src/identifier.c 2014-06-12 22:47:27 +0000
3+++ src/identifier.c 2014-08-05 13:49:18 +0000
4@@ -11,6 +11,7 @@
5 #include "identifier.h"
6
7 static char* ofono_name = "org.ofono";
8+static char* cache_file = WHOOPSIE_ID_PATH;
9 static gint fail_next_get_mac = 0;
10 static gint fail_next_get_uuid = 0;
11
12@@ -18,6 +19,10 @@
13 ofono_name = name;
14 }
15
16+void whoopsie_identifier_set_cache_file (char *filename) {
17+ cache_file = filename;
18+}
19+
20 void
21 whoopsie_hex_to_char (char* buf, const char *str, int len)
22 {
23@@ -64,12 +69,30 @@
24 }
25
26 void
27+whoopsie_identifier_get_cached (char** res, GError** error)
28+{
29+ g_return_if_fail (res);
30+
31+ gsize length = 0;
32+
33+ if (!g_file_get_contents (cache_file, res, &length, NULL)) {
34+ g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
35+ "Could not load cached identifier file.");
36+ } else if (length != HASHLEN) {
37+ g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
38+ "Got an unexpected length reading cached id.");
39+ g_free (*res);
40+ *res = NULL;
41+ }
42+ return;
43+}
44+
45+void
46 whoopsie_identifier_get_mac_address (char** res, GError** error)
47 {
48 glob_t globbuf;
49 char data[200] = {0};
50 gboolean success = FALSE;
51- int i = 0;
52
53 g_return_if_fail (res);
54
55@@ -250,6 +273,15 @@
56
57 g_return_if_fail (res);
58
59+ whoopsie_identifier_get_cached (res, error);
60+ if ((!error || !(*error)) && *res)
61+ return;
62+
63+ if (error && *error) {
64+ g_error_free (*error);
65+ *error = NULL;
66+ }
67+
68 whoopsie_identifier_get_system_uuid (&identifier, error);
69 if ((!error || !(*error)) && identifier)
70 goto out;
71
72=== modified file 'src/identifier.h'
73--- src/identifier.h 2014-06-12 22:47:27 +0000
74+++ src/identifier.h 2014-08-05 13:49:18 +0000
75@@ -8,8 +8,12 @@
76
77 /* for testing: */
78 void whoopsie_identifier_set_ofono_name (char* name) __attribute__ ((__warning__ ("Only for testing")));
79+void whoopsie_identifier_set_cache_file (char* filename) __attribute__ ((__warning__ ("Only for testing")));
80 void whoopsie_identifier_fail_next_get_mac (gint how) __attribute__ ((__warning__ ("Only for testing")));
81 void whoopsie_identifier_fail_next_get_uuid () __attribute__ ((__warning__ ("Only for testing")));
82 #define WHOOPSIE_FAIL_GENERIC 1
83 #define WHOOPSIE_FAIL_NO_IFACES 2
84 #define WHOOPSIE_FAILED_BY_REQUEST 42
85+
86+/* The file path for publishing whoopsie_identifier */
87+#define WHOOPSIE_ID_PATH "/run/whoopsie-id"
88
89=== modified file 'src/tests/test_identifier.c'
90--- src/tests/test_identifier.c 2014-06-12 22:47:27 +0000
91+++ src/tests/test_identifier.c 2014-08-05 13:49:18 +0000
92@@ -469,6 +469,55 @@
93 brofono_stop ();
94 }
95
96+static void
97+test_identifier_cache (void)
98+{
99+ const char* expected = "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382"
100+ "d624741d0dc6638326e282c41be5e4254d8820772c5518a"
101+ "2c5a8c0c7f7eda19594a7eb539453e1ed7";
102+ char* res = NULL;
103+ GError* error = NULL;
104+
105+ gchar *test_path = NULL;
106+ gchar *test_dir = NULL;
107+ gchar test_tmpl[PATH_MAX] = "/tmp/whoopsie-test-XXXXXX";
108+ test_dir = g_mkdtemp (test_tmpl);
109+ g_assert (test_dir != NULL);
110+
111+ /* Check non-existent id file fails lookup */
112+ test_path = g_strdup_printf ("%s/non-existant", test_dir);
113+ whoopsie_identifier_set_cache_file (test_path);
114+ whoopsie_identifier_get_cached (&res, &error);
115+ g_assert_null (res);
116+ g_assert_error (error, g_quark_from_static_string ("whoopsie-quark"), 0);
117+ g_free (test_path);
118+ g_error_free (error);
119+ error = NULL;
120+
121+ /* Check good cache is loaded */
122+ test_path = g_strdup_printf ("%s/good", test_dir);
123+ g_assert (g_file_set_contents (test_path, expected, -1, NULL));
124+ whoopsie_identifier_set_cache_file (test_path);
125+ whoopsie_identifier_get_cached (&res, &error);
126+ g_assert_nonnull (res);
127+ g_assert_no_error (error);
128+ g_assert_cmpstr (res, ==, expected);
129+ g_unlink (test_path);
130+ g_free (test_path);
131+
132+ /* Check bad cache is not loaded */
133+ test_path = g_strdup_printf ("%s/good", test_dir);
134+ g_assert (g_file_set_contents (test_path, expected, HASHLEN-3, NULL));
135+ whoopsie_identifier_set_cache_file (test_path);
136+ whoopsie_identifier_get_cached (&res, &error);
137+ g_assert_null (res);
138+ g_assert_error (error, g_quark_from_static_string ("whoopsie-quark"), 0);
139+ g_unlink (test_path);
140+ g_free (test_path);
141+
142+ g_assert (g_rmdir (test_dir) == 0);
143+}
144+
145 int
146 main (int argc, char** argv)
147 {
148@@ -478,6 +527,9 @@
149 #endif
150 g_test_init (&argc, &argv, NULL);
151
152+ /* Prevent reading currently running system id */
153+ whoopsie_identifier_set_cache_file ("/tmp");
154+
155 g_test_add_func ("/whoopsie/get-system-uuid", test_get_system_uuid);
156 g_test_add_func ("/whoopsie/get-uuid-fails-when-told", test_get_system_uuid_fail_when_told_to);
157 g_test_add_func ("/whoopsie/hex-to-char", test_hex_to_char);
158@@ -493,6 +545,7 @@
159 g_test_add_func ("/whoopsie/imei-empty-reply", test_get_imei_empty_reply);
160 g_test_add_func ("/whoopsie/imei-bad", test_get_imei_bad_imei);
161 g_test_add_func ("/whoopsie/imei-missing", test_get_imei_no_imei);
162+ g_test_add_func ("/whoopsie/get-cached", test_identifier_cache);
163
164 /* Do not consider warnings to be fatal. */
165 g_log_set_always_fatal (G_LOG_FATAL_MASK);
166
167=== modified file 'src/whoopsie.c'
168--- src/whoopsie.c 2014-07-29 20:49:35 +0000
169+++ src/whoopsie.c 2014-08-05 13:49:18 +0000
170@@ -964,6 +964,9 @@
171 whoopsie_identifier);
172 }
173
174+ /* Publish whoopsie-id */
175+ g_file_set_contents (WHOOPSIE_ID_PATH, whoopsie_identifier, -1, NULL);
176+
177 create_crash_directory ();
178
179 drop_privileges (&err);
180@@ -1011,6 +1014,6 @@
181
182 g_free (crash_db_url);
183 g_free (crash_db_submit_url);
184- return 0;
185+ return 0;
186 }
187 #endif

Subscribers

People subscribed via source and target branches

to status/vote changes: