[PATCH] logind: change to -1+errno

David Herrmann dh.herrmann at gmail.com
Thu Nov 21 00:14:46 PST 2013


Set errno and return -1 in public API calls like all other weston code
does. Most systemd+dbus calls return negative error-codes instead of -1
and setting errno. Thus, we need to explicitly set errno before returning.

Also note that we must set errno _after_ the cleanup path. Calling
functions like close() in the cleanup path might overwrite errno (which is
not what we want). So protect errno until the final return -1;
---
 src/logind-util.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/logind-util.c b/src/logind-util.c
index 6bd0c26..d71613a 100644
--- a/src/logind-util.c
+++ b/src/logind-util.c
@@ -168,9 +168,11 @@ weston_logind_open(struct weston_logind *wl, const char *path,
 
 	r = stat(path, &st);
 	if (r < 0)
-		return -errno;
-	if (!S_ISCHR(st.st_mode))
-		return -ENODEV;
+		return -1;
+	if (!S_ISCHR(st.st_mode)) {
+		errno = ENODEV;
+		return -1;
+	}
 
 	fd = weston_logind_take_device(wl, major(st.st_rdev),
 				       minor(st.st_rdev), NULL);
@@ -220,7 +222,8 @@ err_close:
 	close(fd);
 	weston_logind_release_device(wl, major(st.st_rdev),
 				     minor(st.st_rdev));
-	return r;
+	errno = -r;
+	return -1;
 }
 
 WL_EXPORT void
@@ -263,7 +266,7 @@ weston_logind_activate_vt(struct weston_logind *wl, int vt)
 
 	r = ioctl(wl->vt, VT_ACTIVATE, vt);
 	if (r < 0)
-		return -errno;
+		return -1;
 
 	return 0;
 }
@@ -898,7 +901,8 @@ err_wl:
 	free(wl);
 err_out:
 	weston_log("logind: cannot setup systemd-logind helper (%d), using legacy fallback\n", r);
-	return r;
+	errno = -r;
+	return -1;
 }
 
 WL_EXPORT void
-- 
1.8.4.2



More information about the wayland-devel mailing list