#include #include #include #include #include #include #include int main(int argc, char **argv) { cap_rights_t rights; int fd, dir_fd, error; char frompath[] = "/tmp/from.XXXXXXX"; char topath[] = "/tmp/to.XXXXXXX"; fd = mkstemp(frompath); if (fd < 0) { err(1, "Failed to create from"); } close(fd); mktemp(topath); dir_fd = open("/tmp", O_RDONLY); if (dir_fd < 0) { err(1, "Failed to open /tmp"); } cap_rights_init(&rights, CAP_LOOKUP); error = cap_rights_limit(fd, &rights); if (error != 0) { err(1, "cap_rights_limit() failed"); } cap_enter(); error = renameat(dir_fd, basename(frompath), dir_fd, basename(topath)); if (error != 0) { err(0, "Got expected error on rename"); } return (1); }