Skip to content

Instantly share code, notes, and snippets.

@shimarin
Created July 27, 2022 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimarin/6a3f504fd4e2e2113e6e2e05de7d7ce6 to your computer and use it in GitHub Desktop.
Save shimarin/6a3f504fd4e2e2113e6e2e05de7d7ce6 to your computer and use it in GitHub Desktop.
#include <sys/mman.h>
#include <unistd.h>
#include <iostream>
int main()
{
auto fd = memfd_create("mytmpfile", 0);
if (fd < 0) throw std::runtime_error("memfd_create");
std::string tmp_filename = "/proc/" + std::to_string(getpid()) + "/fd/" + std::to_string(fd);
std::string msg = "Hello, World!\n";
write(fd, msg.c_str(), msg.length());
std::string cmd1 = "ls -l " + tmp_filename;
system(cmd1.c_str());
std::string cmd2 = "cat " + tmp_filename;
system(cmd2.c_str());
std::string cmd3 = "curl -s http://checkip.amazonaws.com/ >> " + tmp_filename;
//std::string cmd3 = "curl -s -o " + tmp_filename + " http://checkip.amazonaws.com/";
system(cmd3.c_str());
lseek(fd, 0, SEEK_SET);
char c;
while (read(fd, &c, 1) > 0) {
write(STDOUT_FILENO, &c, 1);
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment