What is the c++ (std::ofstream) equivalent of:
int fd = open(fn,O_WRONLY|O_NDELAY|O_APPEND|O_CREAT|O_CLOEXEC,0600);
The application I would like to use this for will only run on newer versions of linux, therefor portability is not an issue.
There is (likely) no portable way to do this. There are at least two options.
There are lots of examples of "attaching a file descriptor", "getting file descriptor from fstream" etc. If you can find one that works, you're all set.
If you can do the first one, you can do a fcntl
on the file.
/* not checking return values since I am lazy; *you* should check them */
flags = fcntl(fd, F_GETFD);
flags |= FD_CLOEXEC;
fcntl(fd, F_SETFD, flags)
If you can do the second one, you can simply obtain your descriptor via open
and attach it.
FD_CLOEXEC
on themThis is less clean (but more likely to work). Open all the fstreams you don't want your children to inherit. Go to /proc/self/fd
. For each fd set the FC_CLOEXEC flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With