Created
July 27, 2022 23:02
-
-
Save shimarin/6a3f504fd4e2e2113e6e2e05de7d7ce6 to your computer and use it in GitHub Desktop.
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 <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