Skip to content

Commit

Permalink
use O_CLOEXEC with O_TMPFILE to avoid orphaned nameless files
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-kubacki committed Sep 15, 2018
1 parent 38623d3 commit f16ae3d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions protofile/otmpfile_linux.go
Expand Up @@ -17,16 +17,17 @@ func init() {
}

// unixProtoFile is the variant that utilizes O_TMPFILE.
// Although it might seem that data is written to the parent directory itself,
// it actually goes into a nameless file.
// Although it might seem as if we write data to a directory,
// it actually goes to a nameless file in said directory.
// The file gets discarded by the OS should it remain without a name but get closed.
type unixProtoFile ProtoFile

func intentNewUnix(path, filename string) (*ProtoFileBehaver, error) {
err := os.MkdirAll(path, permBitsDir)
if err != nil {
return nil, err
}
t, err := os.OpenFile(path, os.O_WRONLY|unix.O_TMPFILE, permBitsFile)
t, err := os.OpenFile(path, os.O_WRONLY|unix.O_TMPFILE|syscall.O_CLOEXEC, permBitsFile)
// did it fail because…
if err != nil {
perr, ok := err.(*os.PathError)
Expand Down

0 comments on commit f16ae3d

Please sign in to comment.