#include #include #include #include static int failures; static int successes; #define CHECK(ret) \ do \ if ((ret) < 0) \ { \ printf ("error: %s:\n %m\n", #ret); \ ++failures; \ } \ else \ { \ printf ("success: %s\n", #ret); \ ++successes; \ } \ while (0) static const char *const out_name = "linkat.out"; void setup_fd (int *pfd) { if (*pfd >= 0) close (*pfd); *pfd = open (".", O_RDWR | O_TMPFILE, 0); if (*pfd < 0) err (1, "open"); } int main (void) { unlink (out_name); int current_fd = open (".", O_RDONLY | O_DIRECTORY); if (current_fd < 0) err (1, "open (O_DIRECTORY)"); int proc_fd = open ("/proc/self/fd", O_RDONLY | O_DIRECTORY); if (proc_fd < 0) err (1, "open (O_DIRECTORY)"); int fd = -1; setup_fd (&fd); CHECK (linkat (fd, "", AT_FDCWD, out_name, AT_EMPTY_PATH)); unlink (out_name); setup_fd (&fd); CHECK (linkat (fd, "", current_fd, out_name, AT_EMPTY_PATH)); unlink (out_name); char proc_name[100]; snprintf (proc_name, sizeof (proc_name), "/proc/self/fd/%d", fd); setup_fd (&fd); CHECK (linkat (AT_FDCWD, proc_name, AT_FDCWD, out_name, AT_SYMLINK_FOLLOW)); unlink (out_name); setup_fd (&fd); CHECK (linkat (AT_FDCWD, proc_name, current_fd, out_name, AT_SYMLINK_FOLLOW)); unlink (out_name); snprintf (proc_name, sizeof (proc_name), "%d", fd); setup_fd (&fd); CHECK (linkat (proc_fd, proc_name, AT_FDCWD, out_name, AT_SYMLINK_FOLLOW)); unlink (out_name); setup_fd (&fd); CHECK (linkat (proc_fd, proc_name, current_fd, out_name, AT_SYMLINK_FOLLOW)); unlink (out_name); printf ("successes: %d, failures: %d\n", successes, failures); return 0; }