qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH trivial] qga: correctly write to /sys/power/state on linux
@ 2025-08-01 11:53 Michael Tokarev
  2025-08-05 18:56 ` Stefan Hajnoczi
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Tokarev @ 2025-08-01 11:53 UTC (permalink / raw)
  To: Michael Roth, Kostiantyn Kostiuk, qemu-devel
  Cc: Michael Tokarev, qemu-trivial

Commit v9.0.0-343-g2048129625 introduced usage of
g_file_set_contents() function to write to /sys/power/state.
This function uses G_FILE_SET_CONTENTS_CONSISTENT flag to
g_file_set_contents_full(), which is implemented by creating
a temp file in the same directory and renaming it to the final
destination.  Which is not how sysfs works.

Here, there's not a big deal to do open/write/close - it becomes
almost the same as using g_file_set_contents[_full]().  But it
does not have surprises like this.

Also, since this is linux code, it should be ok to use %m in
the error reporting function.

Fixes: 2048129625 "qga/commands-posix: don't do fork()/exec() when suspending via sysfs"
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3057
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 qga/commands-linux.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/qga/commands-linux.c b/qga/commands-linux.c
index 9e8a934b9a..9dc0c82503 100644
--- a/qga/commands-linux.c
+++ b/qga/commands-linux.c
@@ -1400,20 +1400,22 @@ static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp)
 
 static void linux_sys_state_suspend(SuspendMode mode, Error **errp)
 {
-    g_autoptr(GError) local_gerr = NULL;
     const char *sysfile_strs[3] = {"disk", "mem", NULL};
     const char *sysfile_str = sysfile_strs[mode];
+    int fd;
 
     if (!sysfile_str) {
         error_setg(errp, "unknown guest suspend mode");
         return;
     }
 
-    if (!g_file_set_contents(LINUX_SYS_STATE_FILE, sysfile_str,
-                             -1, &local_gerr)) {
-        error_setg(errp, "suspend: cannot write to '%s': %s",
-                   LINUX_SYS_STATE_FILE, local_gerr->message);
-        return;
+    fd = open(LINUX_SYS_STATE_FILE, O_WRONLY);
+    if (fd < 0 || write(fd, sysfile_str, strlen(sysfile_str)) < 0) {
+        error_setg(errp, "suspend: cannot write to '%s': %m",
+                   LINUX_SYS_STATE_FILE);
+    }
+    if (fd >= 0) {
+        close(fd);
     }
 }
 
-- 
2.47.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH trivial] qga: correctly write to /sys/power/state on linux
  2025-08-01 11:53 [PATCH trivial] qga: correctly write to /sys/power/state on linux Michael Tokarev
@ 2025-08-05 18:56 ` Stefan Hajnoczi
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Hajnoczi @ 2025-08-05 18:56 UTC (permalink / raw)
  To: Michael Tokarev
  Cc: Michael Roth, Kostiantyn Kostiuk, qemu-devel, qemu-trivial

[-- Attachment #1: Type: text/plain, Size: 1067 bytes --]

On Fri, Aug 01, 2025 at 02:53:14PM +0300, Michael Tokarev wrote:
> Commit v9.0.0-343-g2048129625 introduced usage of
> g_file_set_contents() function to write to /sys/power/state.
> This function uses G_FILE_SET_CONTENTS_CONSISTENT flag to
> g_file_set_contents_full(), which is implemented by creating
> a temp file in the same directory and renaming it to the final
> destination.  Which is not how sysfs works.
> 
> Here, there's not a big deal to do open/write/close - it becomes
> almost the same as using g_file_set_contents[_full]().  But it
> does not have surprises like this.
> 
> Also, since this is linux code, it should be ok to use %m in
> the error reporting function.
> 
> Fixes: 2048129625 "qga/commands-posix: don't do fork()/exec() when suspending via sysfs"
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3057
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
> ---
>  qga/commands-linux.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-05 18:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-01 11:53 [PATCH trivial] qga: correctly write to /sys/power/state on linux Michael Tokarev
2025-08-05 18:56 ` Stefan Hajnoczi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).