Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 900
Lothar Avatar asked Jan 14 '17 21:01

Lothar


1 Answers

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);
like image 78
Lothar Avatar answered Oct 23 '22 00:10

Lothar