block/export: avoid g_return_val_if() input validation
[qemu/kevin.git] / util / selfmap.c
blob2ec99dfdda17f40e0c17b61f7b33cb406f1f620e
1 /*
2 * Utility function to get QEMU's own process map
4 * Copyright (c) 2020 Linaro Ltd
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "qemu/osdep.h"
10 #include "qemu/cutils.h"
11 #include "qemu/selfmap.h"
13 GSList *read_self_maps(void)
15 gchar *maps;
16 GSList *map_info = NULL;
18 if (g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) {
19 gchar **lines = g_strsplit(maps, "\n", 0);
20 int i, entries = g_strv_length(lines);
22 for (i = 0; i < entries; i++) {
23 gchar **fields = g_strsplit(lines[i], " ", 6);
24 if (g_strv_length(fields) > 4) {
25 MapInfo *e = g_new0(MapInfo, 1);
26 int errors;
27 const char *end;
29 errors = qemu_strtoul(fields[0], &end, 16, &e->start);
30 errors += qemu_strtoul(end + 1, NULL, 16, &e->end);
32 e->is_read = fields[1][0] == 'r';
33 e->is_write = fields[1][1] == 'w';
34 e->is_exec = fields[1][2] == 'x';
35 e->is_priv = fields[1][3] == 'p';
37 errors += qemu_strtoul(fields[2], NULL, 16, &e->offset);
38 e->dev = g_strdup(fields[3]);
39 errors += qemu_strtou64(fields[4], NULL, 10, &e->inode);
42 * The last field may have leading spaces which we
43 * need to strip.
45 if (g_strv_length(fields) == 6) {
46 e->path = g_strdup(g_strchug(fields[5]));
48 map_info = g_slist_prepend(map_info, e);
51 g_strfreev(fields);
53 g_strfreev(lines);
54 g_free(maps);
57 /* ensure the map data is in the same order we collected it */
58 return g_slist_reverse(map_info);
61 /**
62 * free_self_maps:
63 * @info: a GSlist
65 * Free a list of MapInfo structures.
67 static void free_info(gpointer data)
69 MapInfo *e = (MapInfo *) data;
70 g_free(e->dev);
71 g_free(e->path);
72 g_free(e);
75 void free_self_maps(GSList *info)
77 g_slist_free_full(info, &free_info);