Created
February 23, 2016 18:40
-
-
Save gcmurphy/c4c5222075d8e501d7d1 to your computer and use it in GitHub Desktop.
Create tmpfs mountpoint in c.
This file contains hidden or 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <limits.h> | |
#include <sys/mount.h> | |
char * | |
ramdisk(const char *ns, const char *sz) | |
{ | |
int rc; | |
char *mountpoint = NULL, *options = NULL; | |
char path[PATH_MAX]; | |
memset(path, 0, sizeof(path)); | |
const char *tmpdir = getenv("TMPDIR"); | |
if (! tmpdir){ | |
tmpdir = "/tmp/"; | |
} | |
snprintf(path, sizeof(path)-1, "%s%s_XXXXXX", tmpdir, ns); | |
mountpoint = mkdtemp(path); | |
if (!mountpoint){ | |
return NULL; | |
} | |
asprintf(&options, "size=%s,uid=0,gid=0,mode=700", sz); | |
rc = mount("tmpfs", mountpoint, "tmpfs", 0, options); | |
free(options); | |
if (rc != 0){ | |
perror("tmpfs creation failed"); | |
rmdir(mountpoint); | |
return NULL; | |
} | |
return strdup(mountpoint); | |
} | |
#ifdef TESTING | |
int | |
main() | |
{ | |
char *tmpfs = ramdisk("ramdisk", "1M"); | |
if (tmpfs != NULL){ | |
printf("created tmpfs here: %s\n", tmpfs); | |
free(tmpfs); | |
} | |
return 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment