X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Flibadacl.c;h=a1923601669b66efb0ffa45e62a9b73695354ddb;hb=0b4ff086871b13edb9da3bda67ac4b6f710e73dd;hp=b25a43b38cba13d055f26ca848241e041fb6d1c7;hpb=81233b65dc8bbccba15394751f240ad59f15f342;p=apply-default-acl.git diff --git a/src/libadacl.c b/src/libadacl.c index b25a43b..a192360 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -26,22 +26,39 @@ #include "libadacl.h" +/* Even though most other library functions reliably return -1 for + * error, it feels a little wrong to re-use the ACL_ERROR constant. + */ +#define CLOSE_ERROR -1 +#define OPEN_ERROR -1 +#define SNPRINTF_ERROR -1 +#define STAT_ERROR -1 + + /** * @brief The recursive portion of the @c safe_open function, used to * open a file descriptor in a symlink-safe way when combined with * the @c O_NOFOLLOW flag. * + * The @c O_PATH flag is not used because we want to fail upon + * encountering any symlinks. + * * @param at_fd * A file descriptor relative to which @c pathname will be opened. * * @param pathname * The path to the file/directory/whatever whose descriptor you want. * + * @param flags + * File status flags to be passed to @c openat. + * * @return a file descriptor for @c pathname if everything goes well, * and @c OPEN_ERROR if not. */ int safe_open_ex(int at_fd, char* pathname, int flags) { - if (pathname != NULL && strlen(pathname) == 0) { + /* We're only called by safe_open(), so pathname is guaranteed to be + non-NULL */ + if (strlen(pathname) == 0) { /* Oops, went one level to deep with nothing to do. */ return at_fd; } @@ -97,6 +114,9 @@ int safe_open_ex(int at_fd, char* pathname, int flags) { * @param pathname * The path to the file/directory/whatever whose descriptor you want. * + * @param flags + * File status flags to be passed to @c openat. + * * @return a file descriptor for @c pathname if everything goes well, * and @c OPEN_ERROR if not. */ @@ -145,6 +165,11 @@ int safe_open(const char* pathname, int flags) { } int fd = open("/", flags); + if (fd == OPEN_ERROR) { + perror("safe_open (open)"); + return OPEN_ERROR; + } + if (strcmp(abspath, "/") == 0) { return fd; }