| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* cg_map.c cgroup v2 cache |
| 4 |
* |
| 5 |
* Authors: Dmitry Yakunin <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <limits.h> |
| 13 |
|
| 14 |
#include <linux/types.h> |
| 15 |
#include <ftw.h> |
| 16 |
|
| 17 |
#include "cg_map.h" |
| 18 |
#include "list.h" |
| 19 |
#include "utils.h" |
| 20 |
|
| 21 |
struct cg_cache { |
| 22 |
struct hlist_node id_hash; |
| 23 |
__u64 id; |
| 24 |
char path[]; |
| 25 |
}; |
| 26 |
|
| 27 |
#define IDMAP_SIZE 1024 |
| 28 |
static struct hlist_head id_head[IDMAP_SIZE]; |
| 29 |
|
| 30 |
static struct cg_cache *cg_get_by_id(__u64 id) |
| 31 |
{ |
| 32 |
unsigned int h = id & (IDMAP_SIZE - 1); |
| 33 |
struct hlist_node *n; |
| 34 |
|
| 35 |
hlist_for_each(n, &id_head[h]) { |
| 36 |
struct cg_cache *cg; |
| 37 |
|
| 38 |
cg = container_of(n, struct cg_cache, id_hash); |
| 39 |
if (cg->id == id) |
| 40 |
return cg; |
| 41 |
} |
| 42 |
|
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
static struct cg_cache *cg_entry_create(__u64 id, const char *path) |
| 47 |
{ |
| 48 |
unsigned int h = id & (IDMAP_SIZE - 1); |
| 49 |
struct cg_cache *cg; |
| 50 |
|
| 51 |
cg = malloc(sizeof(*cg) + strlen(path) + 1); |
| 52 |
if (!cg) { |
| 53 |
fprintf(stderr, |
| 54 |
"Failed to allocate memory for cgroup2 cache entry"); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
cg->id = id; |
| 58 |
strcpy(cg->path, path); |
| 59 |
|
| 60 |
hlist_add_head(&cg->id_hash, &id_head[h]); |
| 61 |
|
| 62 |
return cg; |
| 63 |
} |
| 64 |
|
| 65 |
static int mntlen; |
| 66 |
|
| 67 |
static int nftw_fn(const char *fpath, const struct stat *sb, |
| 68 |
int typeflag, struct FTW *ftw) |
| 69 |
{ |
| 70 |
const char *path; |
| 71 |
__u64 id; |
| 72 |
|
| 73 |
if (typeflag != FTW_D) |
| 74 |
return 0; |
| 75 |
|
| 76 |
id = get_cgroup2_id(fpath); |
| 77 |
if (!id) |
| 78 |
return -1; |
| 79 |
|
| 80 |
path = fpath + mntlen; |
| 81 |
if (*path == '\0') |
| 82 |
/* root cgroup */ |
| 83 |
path = "/"; |
| 84 |
if (!cg_entry_create(id, path)) |
| 85 |
return -1; |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static void cg_init_map(void) |
| 91 |
{ |
| 92 |
char *mnt; |
| 93 |
|
| 94 |
mnt = find_cgroup2_mount(false); |
| 95 |
if (!mnt) |
| 96 |
return; |
| 97 |
|
| 98 |
mntlen = strlen(mnt); |
| 99 |
(void) nftw(mnt, nftw_fn, 1024, FTW_MOUNT); |
| 100 |
|
| 101 |
free(mnt); |
| 102 |
} |
| 103 |
|
| 104 |
const char *cg_id_to_path(__u64 id) |
| 105 |
{ |
| 106 |
static int initialized; |
| 107 |
static char buf[64]; |
| 108 |
|
| 109 |
const struct cg_cache *cg; |
| 110 |
char *path; |
| 111 |
|
| 112 |
if (!initialized) { |
| 113 |
cg_init_map(); |
| 114 |
initialized = 1; |
| 115 |
} |
| 116 |
|
| 117 |
cg = cg_get_by_id(id); |
| 118 |
if (cg) |
| 119 |
return cg->path; |
| 120 |
|
| 121 |
path = get_cgroup2_path(id, false); |
| 122 |
if (path) { |
| 123 |
cg = cg_entry_create(id, path); |
| 124 |
free(path); |
| 125 |
if (cg) |
| 126 |
return cg->path; |
| 127 |
} |
| 128 |
|
| 129 |
snprintf(buf, sizeof(buf), "unreachable:%llx", id); |
| 130 |
return buf; |
| 131 |
} |