[PATCH] tools/laptop/dslm: fix inverted open() error check

From: Gu Bin

Date: Wed Nov 04 2026 - 01:53:17 EST


open() returns -1 on failure and a non-negative fd on success, but the
check

if (!(fd = open(disk, O_RDONLY|O_NONBLOCK)))

treats only fd == 0 as an error, which is wrong in both directions:

- on a real failure (fd == -1) the condition is false, so the
program continues with fd = -1: measure() runs check_powermode()
on the invalid fd, ioctl() fails with EBADF, and the tool reports
the drive state as "unknown" instead of printing the intended
"Can't open" message.
- when open() succeeds and returns fd 0 (e.g. stdin was closed, so
the lowest free descriptor is 0), the condition is true and the
tool wrongly prints "Can't open" and exits.

Test with fd < 0 instead, which handles both cases correctly.

Fixes: d49129accc21 ("Documentation/laptop/: split txt and source files")
Signed-off-by: Gu Bin <gubin@xxxxxxxxxxxxxxxxxxxx>
---
tools/laptop/dslm/dslm.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/laptop/dslm/dslm.c b/tools/laptop/dslm/dslm.c
index d5dd2d4b04d8..fe99565d384a 100644
--- a/tools/laptop/dslm/dslm.c
+++ b/tools/laptop/dslm/dslm.c
@@ -144,7 +144,8 @@ int main(int argc, char **argv)
} else
usage();

- if (!(fd = open(disk, O_RDONLY|O_NONBLOCK))) {
+ fd = open(disk, O_RDONLY|O_NONBLOCK);
+ if (fd < 0) {
printf("Can't open %s, because: %s\n", disk, strerror(errno));
exit(-1);
}
--
2.55.0.windows.3