2014-09-30 15:59:30 +04:00
|
|
|
/*
|
|
|
|
* Dealing with identifiers
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Markus Armbruster <armbru@redhat.com>,
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1
|
|
|
|
* or later. See the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:06 +03:00
|
|
|
#include "qemu/ctype.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/id.h"
|
2014-09-30 15:59:30 +04:00
|
|
|
|
|
|
|
bool id_wellformed(const char *id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!qemu_isalpha(id[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (i = 1; id[i]; i++) {
|
|
|
|
if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-13 02:36:49 +03:00
|
|
|
|
|
|
|
#define ID_SPECIAL_CHAR '#'
|
|
|
|
|
2015-11-26 00:03:37 +03:00
|
|
|
static const char *const id_subsys_str[ID_MAX] = {
|
2015-10-13 02:36:49 +03:00
|
|
|
[ID_QDEV] = "qdev",
|
|
|
|
[ID_BLOCK] = "block",
|
2019-10-21 17:31:31 +03:00
|
|
|
[ID_CHR] = "chr",
|
2021-02-15 12:02:25 +03:00
|
|
|
[ID_NET] = "net",
|
2015-10-13 02:36:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates an ID of the form PREFIX SUBSYSTEM NUMBER
|
|
|
|
* where:
|
|
|
|
*
|
|
|
|
* - PREFIX is the reserved character '#'
|
|
|
|
* - SUBSYSTEM identifies the subsystem creating the ID
|
|
|
|
* - NUMBER is a decimal number unique within SUBSYSTEM.
|
|
|
|
*
|
|
|
|
* Example: "#block146"
|
|
|
|
*
|
|
|
|
* Note that these IDs do not satisfy id_wellformed().
|
|
|
|
*
|
|
|
|
* The caller is responsible for freeing the returned string with g_free()
|
|
|
|
*/
|
|
|
|
char *id_generate(IdSubSystems id)
|
|
|
|
{
|
|
|
|
static uint64_t id_counters[ID_MAX];
|
|
|
|
uint32_t rnd;
|
|
|
|
|
2015-11-26 00:03:37 +03:00
|
|
|
assert(id < ARRAY_SIZE(id_subsys_str));
|
2015-10-13 02:36:49 +03:00
|
|
|
assert(id_subsys_str[id]);
|
|
|
|
|
|
|
|
rnd = g_random_int_range(0, 100);
|
|
|
|
|
|
|
|
return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
|
|
|
|
id_subsys_str[id],
|
|
|
|
id_counters[id]++,
|
|
|
|
rnd);
|
|
|
|
}
|