writePidFile writes the pid atomically to a file. From https://github.com/containerd/containerd/blob/v1.7.0-rc.2/cmd/ctr/commands/commands.go#L265-L282
(path string, pid int)
| 770 | // writePidFile writes the pid atomically to a file. |
| 771 | // From https://github.com/containerd/containerd/blob/v1.7.0-rc.2/cmd/ctr/commands/commands.go#L265-L282 |
| 772 | func writePidFile(path string, pid int) error { |
| 773 | path, err := filepath.Abs(path) |
| 774 | if err != nil { |
| 775 | return err |
| 776 | } |
| 777 | dir := filepath.Dir(path) |
| 778 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 779 | return err |
| 780 | } |
| 781 | tempPath := filepath.Join(dir, fmt.Sprintf(".%s", filepath.Base(path))) |
| 782 | f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666) |
| 783 | if err != nil { |
| 784 | return err |
| 785 | } |
| 786 | _, err = fmt.Fprint(f, pid) |
| 787 | f.Close() |
| 788 | if err != nil { |
| 789 | return err |
| 790 | } |
| 791 | return os.Rename(tempPath, path) |
| 792 | } |
| 793 | |
| 794 | func killProcessByPidFile(pidFile string) error { |
| 795 | pidData, err := os.ReadFile(pidFile) |
no test coverage detected
searching dependent graphs…