Search code examples
clinuxsystem-callsglibc

No "renameat2" system call function on Ubuntu 16.04


The man page for renameat2() says I need to include <stdio.h> but this does not work.

When I do a

cd /usr/include
grep -r renameat2

I see that the __SYSCALL is defined but no glibc function. The flags for the system call are available in <linux/fs.h> but this is not included.


Solution

  • Okay i found the answer here, the general problem with glibc not adding system calls and the man page missing the

    Note: There is no glibc wrapper for this system call; see NOTES.

    note which is shown on other pages. So i got confused.

    Found the answer by reading this article https://lwn.net/Articles/655028/

    And this is the code

    #include <sys/syscall.h>
    #include <linux/fs.h>
    
    //Open the old directories to obtain fds
    int src_fd = open("old_dir", O_PATH);
    int dest_fd = open("new_dir", O_PATH);
    const char* src_path = "old_name.txt";
    const char* dest_path = "new_name.txt";
    
    unsigned int flags = RENAME_NOREPLACE;
    int rc = syscall(SYS_renameat2, src_fd, src_path, dest_fd, dest_path, flags);