Skip to contentSkip to conversation

qga: correctly write to /sys/power/state on linux

PATCH In review Michael Tokarev 2 participants 2 messages Aug 1, 2025 – Aug 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: correctly write to /sys/power/state on linux

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

---
 qga/commands-linux.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
qga/commands-linux.c +8 -6
index 9e8a934b9a..9dc0c82503 100644
@@ -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);
     }
 }
 
Signature · 1 line
2.47.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Michael Tokarev

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.

Quoted 6 lines
Fixes 2048129625 "qga/commands-posix: don't do fork()/exec() when suspending via sysfs"

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3057

---
 qga/commands-linux.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)