seatd: Improve socket permission error handling

chmod/chown errors were logged, but did not result in failure opening
the seatd socket. This meant that errors would not get caught by CI.
This commit is contained in:
Kenny Levinsen 2021-08-08 18:16:58 +02:00
parent 48727a0b6b
commit 2cfc56d5ed

View file

@ -33,23 +33,27 @@ static int open_socket(const char *path, int uid, int gid) {
socklen_t size = offsetof(struct sockaddr_un, sun_path) + strlen(addr.unix.sun_path); socklen_t size = offsetof(struct sockaddr_un, sun_path) + strlen(addr.unix.sun_path);
if (bind(fd, &addr.generic, size) == -1) { if (bind(fd, &addr.generic, size) == -1) {
log_errorf("Could not bind socket: %s", strerror(errno)); log_errorf("Could not bind socket: %s", strerror(errno));
close(fd); goto error;
return -1;
} }
if (listen(fd, LISTEN_BACKLOG) == -1) { if (listen(fd, LISTEN_BACKLOG) == -1) {
log_errorf("Could not listen on socket: %s", strerror(errno)); log_errorf("Could not listen on socket: %s", strerror(errno));
close(fd); goto error;
return -1;
} }
if (uid != -1 || gid != -1) { if (uid != -1 || gid != -1) {
if (fchown(fd, uid, gid) == -1) { if (fchown(fd, uid, gid) == -1) {
log_errorf("Could not chown socket to uid %d, gid %d: %s", uid, gid, log_errorf("Could not chown socket to uid %d, gid %d: %s", uid, gid,
strerror(errno)); strerror(errno));
} else if (fchmod(fd, 0770) == -1) { goto error;
}
if (fchmod(fd, 0770) == -1) {
log_errorf("Could not chmod socket: %s", strerror(errno)); log_errorf("Could not chmod socket: %s", strerror(errno));
goto error;
} }
} }
return fd; return fd;
error:
close(fd);
return -1;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {