aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miciah Masters <miciah.masters@gmail.com>2016-12-24 03:27:09 -0500
committerGravatar Miciah Dashiel Butler Masters <mmasters@redhat.com>2017-01-15 01:59:49 -0500
commit0a6ee486b4a1171adc0fde02a880f99d5d9a2ebb (patch)
tree0c0a142fd9defa3901794ae5ee1bf86a20855a9c
parent8d17af9cc111d50a8b0be9ff2f35a9b333290781 (diff)
Fix logic for directory creation when saving state
When creating the /tmp/brightnessctl directory or its subdirectories, clear errno before calling mkdir, and call stat a second time after mkdir. Previously, errno was not cleared, so even if the error was handled (by creating the missing directory with mkdir), subsequent error handling code read the old errno value from the failed stat call and printed an error message. Additionally, because we did not call stat again after calling mkdir, the stat buffer had outdated information, and so S_ISDIR returned a false negative. As a result of these defects, invoking brightnessctl -s when /tmp/brightnessctl did not exist would create /tmp/brightnessctl and fail. Invoking brightnessctl -s a second time would create /tmp/brightnessctl/backlight and fail. Invoking the command a third time would succeed and write the state file under /tmp/brightnessctl/backlight/. After this commit, brightnessctl -s succeeds the first time.
-rw-r--r--brightnessctl.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/brightnessctl.c b/brightnessctl.c
index 86a0e21..3f7cfc1 100644
--- a/brightnessctl.c
+++ b/brightnessctl.c
@@ -395,8 +395,11 @@ int save_device_data(struct device *dev) {
if (stat(c_path, &sb)) {
if (errno != ENOENT)
goto fail;
+ errno = 0;
if (mkdir(c_path, 0777))
goto fail;
+ if (stat(c_path, &sb))
+ goto fail;
}
if (!S_ISDIR(sb.st_mode))
goto fail;
@@ -442,9 +445,12 @@ static int ensure_run_dir() {
if (stat(run_dir, &sb)) {
if (errno != ENOENT)
return 0;
+ errno = 0;
if (mkdir(run_dir, 0777)) {
return 0;
}
+ if (stat(run_dir, &sb))
+ return 0;
}
return S_ISDIR(sb.st_mode);
}