@@ -787,6 +787,8 @@ error:
static void usage(char *prog)
{
+ char *base_filename = g_path_get_basename(prog);
+
fprintf(stderr, "usage: %s\n"
" -p|--path <path> 9p path to export\n"
" {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
@@ -795,7 +797,9 @@ static void usage(char *prog)
" access to this socket\n"
" \tNote: -s & -f can not be used together\n"
" [-n|--nodaemon] Run as a normal program\n",
- basename(prog));
+ base_filename);
+
+ g_free(base_filename);
}
static int process_reply(int sock, int type,
@@ -160,17 +160,17 @@ static int local_create_mapped_attr_dir(FsContext *ctx, const char *path)
{
int err;
char *attr_dir;
- char *tmp_path = g_strdup(path);
+ char *base_filename = g_path_get_basename(path);
attr_dir = g_strdup_printf("%s/%s/%s",
- ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR);
+ ctx->fs_root, base_filename, VIRTFS_META_DIR);
err = mkdir(attr_dir, 0700);
if (err < 0 && errno == EEXIST) {
err = 0;
}
g_free(attr_dir);
- g_free(tmp_path);
+ g_free(base_filename);
return err;
}
@@ -2413,7 +2413,7 @@ static int vfio_initfn(PCIDevice *pdev)
return -errno;
}
- vdev->vbasedev.name = g_strdup(basename(vdev->vbasedev.sysfsdev));
+ vdev->vbasedev.name = g_path_get_basename(vdev->vbasedev.sysfsdev);
vdev->vbasedev.ops = &vfio_pci_ops;
vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI;
@@ -2428,11 +2428,13 @@ static int vfio_initfn(PCIDevice *pdev)
group_path[len] = 0;
- group_name = basename(group_path);
+ group_name = g_path_get_basename(group_path);
if (sscanf(group_name, "%d", &groupid) != 1) {
error_report("vfio: error reading %s: %m", group_path);
+ g_free(group_name);
return -errno;
}
+ g_free(group_name);
trace_vfio_initfn(vdev->vbasedev.name, groupid);
@@ -557,7 +557,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev)
/* @sysfsdev takes precedence over @host */
if (vbasedev->sysfsdev) {
g_free(vbasedev->name);
- vbasedev->name = g_strdup(basename(vbasedev->sysfsdev));
+ vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
} else {
if (!vbasedev->name || strchr(vbasedev->name, '/')) {
return -EINVAL;
@@ -584,11 +584,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev)
group_path[len] = 0;
- group_name = basename(group_path);
+ group_name = g_path_get_basename(group_path);
if (sscanf(group_name, "%d", &groupid) != 1) {
error_report("vfio: error reading %s: %m", group_path);
+ g_free(group_name);
return -errno;
}
+ g_free(group_name);
trace_vfio_platform_base_device_init(vbasedev->name, groupid);
@@ -249,6 +249,12 @@ static char *get_prompt(void)
return prompt;
}
+static void cleanup_and_exit(int status)
+{
+ g_free(progname);
+ exit(status);
+}
+
static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
const char *fmt, ...)
{
@@ -440,7 +446,7 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
#endif
- progname = basename(argv[0]);
+ progname = g_path_get_basename(argv[0]);
qemu_init_exec_dir(argv[0]);
module_call_init(MODULE_INIT_QOM);
@@ -459,7 +465,7 @@ int main(int argc, char **argv)
case 'd':
if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
error_report("Invalid discard option: %s", optarg);
- exit(1);
+ cleanup_and_exit(1);
}
break;
case 'f':
@@ -480,26 +486,26 @@ int main(int argc, char **argv)
case 't':
if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
error_report("Invalid cache option: %s", optarg);
- exit(1);
+ cleanup_and_exit(1);
}
break;
case 'T':
if (!trace_init_backends()) {
- exit(1); /* error message will have been printed */
+ cleanup_and_exit(1); /* error message will have been printed */
}
break;
case 'V':
printf("%s version %s\n", progname, QEMU_VERSION);
- exit(0);
+ cleanup_and_exit(0);
case 'h':
usage(progname);
- exit(0);
+ cleanup_and_exit(0);
case OPTION_OBJECT: {
QemuOpts *qopts;
qopts = qemu_opts_parse_noisily(&qemu_object_opts,
optarg, true);
if (!qopts) {
- exit(1);
+ cleanup_and_exit(1);
}
} break;
case OPTION_IMAGE_OPTS:
@@ -507,30 +513,30 @@ int main(int argc, char **argv)
break;
default:
usage(progname);
- exit(1);
+ cleanup_and_exit(1);
}
}
if ((argc - optind) > 1) {
usage(progname);
- exit(1);
+ cleanup_and_exit(1);
}
if (format && imageOpts) {
error_report("--image-opts and -f are mutually exclusive");
- exit(1);
+ cleanup_and_exit(1);
}
if (qemu_init_main_loop(&local_error)) {
error_report_err(local_error);
- exit(1);
+ cleanup_and_exit(1);
}
if (qemu_opts_foreach(&qemu_object_opts,
user_creatable_add_opts_foreach,
NULL, &local_error)) {
error_report_err(local_error);
- exit(1);
+ cleanup_and_exit(1);
}
/* initialize commands */
@@ -557,7 +563,7 @@ int main(int argc, char **argv)
QemuOpts *qopts = NULL;
qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
if (!qopts) {
- exit(1);
+ cleanup_and_exit(1);
}
opts = qemu_opts_to_qdict(qopts, NULL);
openfile(NULL, flags, writethrough, opts);
@@ -578,5 +584,6 @@ int main(int argc, char **argv)
blk_unref(qemuio_blk);
g_free(readline_state);
+ g_free(progname);
return 0;
}
@@ -808,7 +808,9 @@ int main(int argc, char **argv)
if (device != NULL && sockpath == NULL) {
sockpath = g_malloc(128);
- snprintf(sockpath, 128, SOCKET_PATH, basename(device));
+ char *base_filename = g_path_get_basename(device);
+ snprintf(sockpath, 128, SOCKET_PATH, base_filename);
+ g_free(base_filename);
}
saddr = nbd_build_socket_address(sockpath, bindto, port);
@@ -805,7 +805,7 @@ static char *get_pci_driver(char const *syspath, int pathlen, Error **errp)
len = readlink(dpath, buf, sizeof(buf) - 1);
if (len != -1) {
buf[len] = 0;
- driver = g_strdup(basename(buf));
+ driver = g_path_get_basename(buf);
}
g_free(dpath);
g_free(path);
@@ -1048,7 +1048,7 @@ static void build_guest_fsinfo_for_device(char const *devpath,
}
if (!fs->name) {
- fs->name = g_strdup(basename(syspath));
+ fs->name = g_path_get_basename(syspath);
}
g_debug(" parse sysfs path '%s'", syspath);
Using g_strdup and g_basename to get the last component of filename is not the best solution, Only g_path_get_basename can achive the purpose we want. Signed-off-by: Wei Jiangang <weijg.fnst@cn.fujitsu.com> --- fsdev/virtfs-proxy-helper.c | 6 +++++- hw/9pfs/9p-local.c | 6 +++--- hw/vfio/pci.c | 6 ++++-- hw/vfio/platform.c | 6 ++++-- qemu-io.c | 33 ++++++++++++++++++++------------- qemu-nbd.c | 4 +++- qga/commands-posix.c | 4 ++-- 7 files changed, 41 insertions(+), 24 deletions(-)