OK, so no worries. This is a great thread if, for no other reason, to inform people (e.g., me) about new(ish) and fairly obscure Linux system calls (here, the memfd_create).
I have two general comments on the implementation of the main program (memfd_create.c). Note that I don't have access to the actual code, since it somehow got posted to the forum as a graphic (png) file and I don't have OCR software available at the moment. (No, I don't need to be pointed at github or whatever that is...)
1) Here's (the general idea) of how I would have written it (if I follow your idea of doing it as a standalone executable; really, I'd do it the way I suggest later - as a bash "builtin"):
Code: Select all
/* After having opened the memfd file */
int pid;
if (!(pid = fork())) pause();
char buff[100];
/* Do the sprintf, using pid, not getpid() */
sprintf...
puts...
return 0;
So the child process sleeps forever (until killed), holding the memfd open, but the parent exits after printing the message.
2) My original suggestion was to do it as bash "builtin". To do this, you take your code and combine it with a bunch of boilerplate stuff from the bash distribution, then compile it as shared library. Then you use the "enable" command in bash to load your shared library into a running instance of bash, and then the new command (essentially) becomes part of bash itself.
I did something similar to what this thread is about, where the builtin command would set a bash variable, in your case, say memfd_fd, to the fd of the opened memfd object. Then, you can use it in bash code like this:
echo "THis is a test" >&$memfd_fd
Furthermore, any programs spawned by bash while the memfd object is still alive, will inherit fd 3 (or whatever, but in practice, it is almost always 3) as already open and usable.
In general, I think you'd like it. Note the key thing here is that (if you do it as a bash builtin) is that there is no pid to worry about. Or, to put it another way, the pid is just $$ (the pid of the shell itself).
Nitpickers notes on comment 2) above:
1) I tested the syntax of setting a variable, then doing: >&$varname
and it works. But there are some situations where you have to put the varname inside braces (e.g., {varname}) in order for it to be syntactically valid. I usually test this on a case-by-case basis, and use what works.
2) According to "man bash", inheritance of open fds (i.e., fds open in the shell at the time of a program launch) isn't guaranteed for fds greater than 2, but in practice, it seems to always work, so I don't worry about it too much...
Poster of inconvenient truths.
Back from a short, unplanned vacation. Did you miss me?