gitee.com/mysnapcore/mysnapd@v0.1.0/secboot/luks2/fifo.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2022 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package luks2 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 28 "golang.org/x/sys/unix" 29 "golang.org/x/xerrors" 30 31 "gitee.com/mysnapcore/mysnapd/dirs" 32 ) 33 34 func mkFifo() (string, func(), error) { 35 // /run is not world writable but we create a unique directory here because this 36 // code can be invoked by a public API and we shouldn't fail if more than one 37 // process reaches here at the same time. 38 dir, err := ioutil.TempDir(dirs.RunDir, filepath.Base(os.Args[0])+".") 39 if err != nil { 40 return "", nil, xerrors.Errorf("cannot create temporary directory: %w", err) 41 } 42 43 cleanup := func() { 44 if err := os.RemoveAll(dir); err != nil { 45 fmt.Fprintf(stderr, "luks2.mkFifo: cannot remove fifo: %v\n", err) 46 } 47 } 48 49 succeeded := false 50 defer func() { 51 if succeeded { 52 return 53 } 54 cleanup() 55 }() 56 57 fifo := filepath.Join(dir, "fifo") 58 if err := unix.Mkfifo(fifo, 0600); err != nil { 59 return "", nil, xerrors.Errorf("cannot create FIFO: %w", err) 60 } 61 62 succeeded = true 63 return fifo, cleanup, nil 64 }