Created
July 25, 2013 07:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
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
The
O_TMPFILE
definitions are not needed anymore, simply add#define _GNU_SOURCE 1
before the#include
s. AlsoO_RDWR
can be replaced byO_WRONLY
.