Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash redirect how to open file with O_CLOEXEC flag to prevent command inherit the fd

the bash Redirections can’t specified O_CLOEXEC flag when open() file, but sometimes we need it.

{
    flock -x 9
    ./do_some_work
} 9< lock

do_some_work will inherit the fd 9. if do_some_work unlock fd 9. then the lock will be buggy.

as KamiCuk’s solution

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    flock -x 9
    {
         do_work1
         do_work2
    } <&9-
} 9< lock

>Solution :

how to open file with O_CLOEXEC flag

It’s not possible. You have to write small a Bash built-in that will open the file descriptor. I would imagine:

cloexec 9 file
flock -x 9
stuff
exec <&9-

to prevent command inherit the fd

To prevent a command from inheriting a file descriptor, close it.

command <&9-
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading