The open() syscall is probably the most strong way how to check write
accessibility in all situations, but it's overkill and on some
paranoid systems with enabled audit/selinux. It fills logs with
"Permission denied" entries. Let's use eaccess() if available.
Signed-off-by: Karel Zak <kzak@redhat.com>
__fpending \
secure_getenv \
__secure_getenv \
+ eaccess \
err \
errx \
explicit_bzero \
static int try_write(const char *filename)
{
- int fd, rc = 0;
+ int rc = 0;
if (!filename)
return -EINVAL;
- fd = open(filename, O_RDWR|O_CREAT|O_CLOEXEC,
- S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
- if (fd < 0)
+#ifdef HAVE_EACCESS
+ if (eaccess(filename, R_OK|W_OK) != 0)
rc = -errno;
- else
- close(fd);
+#else
+ {
+ int fd = open(filename, O_RDWR|O_CREAT|O_CLOEXEC,
+ S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
+ if (fd < 0)
+ rc = -errno;
+ else
+ close(fd);
+ }
+#endif
return rc;
}