From nobody Wed Feb 11 00:58:55 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1695282953; cv=none; d=zohomail.com; s=zohoarc; b=R3Fx4skVPZ1RZCheXbwYEloPMq4DVrSg1xe2iC1sB0ivxWVIl/benxxhxHOaaOhYA4Xxw8y1EhpBLfZKGYf86hZIX1clyXY6ztEKLZrSZ9eoq4dvGRz9s54e4jr1rxw9qnanbNJmD8UV5efXF9KsKJU4axXK/HUksQyJoUfpJQE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695282953; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject; bh=uI/Kp4K+XtqHxtYAeT48aX1q/ar4g9aCgFPx0CsLxbY=; b=QlNYR5fBafsbqMoUo7Gav6oSCIIg0kdoBtJQdAWXfyWIQ2FzCzwNwZOqWLjG7yf8JxVRDnAoY2yx7Qbwz3wMuUdBZPeEKCmHFJtWemVsWhRd/wCmyWBjMjwFKkRgMNAhmFY88wexhX3zBUKxy2p0e7cHtoSDafrPHqw1JrvEGx8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695282953319293.4627354534018; Thu, 21 Sep 2023 00:55:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qjEWO-0004V1-W8; Thu, 21 Sep 2023 03:54:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qjEWO-0004Ut-Al for qemu-devel@nongnu.org; Thu, 21 Sep 2023 03:54:48 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qjEWL-0001MR-JH for qemu-devel@nongnu.org; Thu, 21 Sep 2023 03:54:48 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1c5dd017b30so179655ad.0 for ; Thu, 21 Sep 2023 00:54:45 -0700 (PDT) Received: from fedora.flets-east.jp ([2400:4050:a840:1e00:d54:e521:8bac:7bed]) by smtp.gmail.com with ESMTPSA id p22-20020a170902a41600b001ae0152d280sm761928plq.193.2023.09.21.00.54.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 00:54:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1695282884; x=1695887684; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uI/Kp4K+XtqHxtYAeT48aX1q/ar4g9aCgFPx0CsLxbY=; b=cW9KHB8SyXNfc5DwuMgqjEaCxWtM8lB6Fw2CBy6f0d4EkXVA7UxDrNY44H2xflonKi QLbnEnPqxVV1cdejdTo+IV3E1bxcbcWqsw3cYu8KJq+egoqpTrdEoA8TBaZz0G14JuwW Idw4duARI2qnbhZGSHZ46xYPqrzXVA2I3PQn6G+I99bamMGavlaATCbD3/2coweRCLGa Io4YjpZyu9leTA6cEJEh++N4jPuKZ12FaOGPQYL/9VKtiaDTgCRKBt8xtFS3QdXZSMA6 BbRmTYVfd785B/Op1QljtcLnHKJWX9DWkkGuLuafG4rxikJQc4uMNbmb8c3Gg6v9NYdQ 1S1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695282884; x=1695887684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uI/Kp4K+XtqHxtYAeT48aX1q/ar4g9aCgFPx0CsLxbY=; b=V5k8a9RGoSwDU9YhI76SxmIYA38+DEIv0fAC1+8Ml66reT9apIu0PsqWULQUbNck9D 48VKuqYl8UgfOhC9pVlGdlzWlWqOAJpFB6hHn8x2l0/UHOZ7YDb/0WPLqpC53JMc+C1V auxoCy257EmgY2P8SveUttQ5LxzGB/lj8Y9s2MRAgUmUCwV3PUFsPYNRZr/uakGKjgio DdlaxnvINBqphKtILfXzdbgvlFsWk5JENdr3wNbamQKz66es18NEPpzv/fbT5ABaTVbE j0gpia8oZGAbdgqcurs7SDgDjFncJTw1fq3jf3h0QPpePfO4FyK0i2LOAIRkC6R8PJTU f+uA== X-Gm-Message-State: AOJu0YwzgmTHY4HMxvDczre9xemw9aL8nvhLjma1sYmHr55XaZG7Fmb6 FNoc+Rqbp5L9n/YXiNtmFBe2iGyymfLkx6QaBE4= X-Google-Smtp-Source: AGHT+IFGVzmlIcIC0WnjXKBiPerXJcLp7Gw8+wBpSNQ9MyxqF6awYw7mKVrGAe8wGqEtAPk+tIpgSw== X-Received: by 2002:a17:902:ecc5:b0:1c5:9d00:be84 with SMTP id a5-20020a170902ecc500b001c59d00be84mr9678037plh.33.1695282884183; Thu, 21 Sep 2023 00:54:44 -0700 (PDT) From: Akihiko Odaki To: Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, virtio-fs@redhat.com, Yuval Shaia , Marcel Apfelbaum , Konstantin Kostiuk , Michael Roth , Paolo Bonzini , Fam Zheng , "Dr . David Alan Gilbert" , Stefan Hajnoczi , Gerd Hoffmann , Stefan Weil , Yan Vugenfirer , Akihiko Odaki Subject: [PATCH v3 1/8] util: Introduce qemu_get_runtime_dir() Date: Thu, 21 Sep 2023 16:54:15 +0900 Message-ID: <20230921075425.16738-2-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230921075425.16738-1-akihiko.odaki@daynix.com> References: <20230921075425.16738-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::62b; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1695282954335100001 Content-Type: text/plain; charset="utf-8" qemu_get_runtime_dir() returns a dynamically allocated directory path that is appropriate for storing runtime files. It corresponds to "run" directory in Unix. With a tree-wide search, it was found that there are several cases where such a functionality is implemented so let's have one as a common utlity function. A notable feature of qemu_get_runtime_dir() is that it uses $XDG_RUNTIME_DIR if available. While the function is often called by executables which requires root privileges, it is still possible that they are called from a user without privilege to write the system runtime directory. In fact, I decided to write this patch when I ran virtiofsd in a Linux namespace created by a normal user and realized it tries to write the system runtime directory, not writable in this case. $XDG_RUNTIME_DIR should provide a writable directory in such cases. This function does not use qemu_get_local_state_dir() or its logic for Windows. Actually the implementation of qemu_get_local_state_dir() for Windows seems not right as it calls g_get_system_data_dirs(), which refers to $XDG_DATA_DIRS. In Unix terminology, it is basically "/usr/share", not "/var", which qemu_get_local_state_dir() is intended to provide. Instead, this function try to use the following in order: - $XDG_RUNTIME_DIR - LocalAppData folder - get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run") This function does not use g_get_user_runtime_dir() either as it falls back to g_get_user_cache_dir() when $XDG_DATA_DIRS is not available. In the case, we rather use: get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run") Signed-off-by: Akihiko Odaki --- include/qemu/osdep.h | 12 ++++++++++++ util/oslib-posix.c | 11 +++++++++++ util/oslib-win32.c | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 2897720fac..bb857c910f 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -636,6 +636,18 @@ void qemu_set_cloexec(int fd); */ char *qemu_get_local_state_dir(void); =20 +/** + * qemu_get_runtime_dir: + * + * Return a dynamically allocated directory path that is appropriate for s= toring + * runtime files. It corresponds to "run" directory in Unix, and uses + * $XDG_RUNTIME_DIR if available. + * + * The caller is responsible for releasing the value returned with g_free() + * after use. + */ +char *qemu_get_runtime_dir(void); + /** * qemu_getauxval: * @type: the auxiliary vector key to lookup diff --git a/util/oslib-posix.c b/util/oslib-posix.c index e86fd64e09..0c82717be5 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -273,6 +273,17 @@ qemu_get_local_state_dir(void) return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR); } =20 +char * +qemu_get_runtime_dir(void) +{ + char *env =3D getenv("XDG_RUNTIME_DIR"); + if (env) { + return g_strdup(env); + } + + return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run"); +} + void qemu_set_tty_echo(int fd, bool echo) { struct termios tty; diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 19a0ea7fbe..38df7b57b5 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -27,6 +27,8 @@ */ =20 #include "qemu/osdep.h" +#include +#include #include #include "qapi/error.h" #include "qemu/main-loop.h" @@ -237,6 +239,30 @@ qemu_get_local_state_dir(void) return g_strdup(data_dirs[0]); } =20 +char * +qemu_get_runtime_dir(void) +{ + size_t size =3D GetEnvironmentVariableA("XDG_RUNTIME_DIR", NULL, 0); + if (size) { + char *env =3D g_malloc(size); + GetEnvironmentVariableA("XDG_RUNTIME_DIR", env, size); + return env; + } + + PWSTR wpath; + const wchar_t *cwpath; + if (!SHGetKnownFolderPath(&FOLDERID_LocalAppData, KF_FLAG_DEFAULT, NUL= L, &wpath)) { + cwpath =3D wpath; + size =3D wcsrtombs(NULL, &cwpath, 0, &(mbstate_t){0}) + 1; + char *path =3D g_malloc(size); + wcsrtombs(path, &cwpath, size, &(mbstate_t){0}); + CoTaskMemFree(wpath); + return path; + } + + return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR "/run"); +} + void qemu_set_tty_echo(int fd, bool echo) { HANDLE handle =3D (HANDLE)_get_osfhandle(fd); --=20 2.41.0