Skip to content

Instantly share code, notes, and snippets.

@WillDignazio
Created July 25, 2013 07:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillDignazio/6077679 to your computer and use it in GitHub Desktop.
Save WillDignazio/6077679 to your computer and use it in GitHub Desktop.
__very__ simple O_TMPFILE usage, took me a little to figure out the requirements. O_TMPFILE requires MAY_WRITE privileges in the kernel, which can be explicitly achieved with O_RDWR flag. Requires linux >=3.11-rc2, which introduced O_TMPFILE into the mainline.
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
/*
* There may not be a userspace definition yet since
* at the time of this gist, 3.11 is only in rc2.
*/
#ifndef O_TMPFILE
#define __O_TMPFILE 020000000
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
#define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT)
#endif
extern int errno;
int
main(void)
{
int fd;
fd = open(".", O_TMPFILE | O_RDWR);
printf("fd #: %d\n", fd);
if(fd == -1)
perror("open");
else
close(fd);
return 0;
}
@CAFxX
Copy link

CAFxX commented Jun 2, 2018

The O_TMPFILE definitions are not needed anymore, simply add #define _GNU_SOURCE 1 before the #includes. Also O_RDWR can be replaced by O_WRONLY.

@bdedward
Copy link

Still, the value 020000000 is very helpful if you are writing assembly instructions and populating the register values manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment