pidfile.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package utils
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strconv"
  20. "strings"
  21. "syscall"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. const errPidNotRunning = errors.Error("pid not running")
  25. type PidFile struct {
  26. Path string
  27. Comm string
  28. }
  29. func NewPidFile(path, comm string) *PidFile {
  30. pf := &PidFile{
  31. Path: path,
  32. Comm: comm,
  33. }
  34. return pf
  35. }
  36. func (pf *PidFile) findProcess() (*os.Process, error) {
  37. data, err := ioutil.ReadFile(pf.Path)
  38. if err != nil {
  39. return nil, err
  40. }
  41. s := strings.TrimSpace(string(data))
  42. pid, err := strconv.Atoi(s)
  43. if err != nil {
  44. return nil, err
  45. }
  46. proc, err := os.FindProcess(pid)
  47. if err != nil {
  48. return nil, errPidNotRunning
  49. }
  50. if err := proc.Signal(syscall.Signal(0)); err != nil {
  51. return nil, errPidNotRunning
  52. }
  53. return proc, nil
  54. }
  55. func (pf *PidFile) findComm(pid int) (string, error) {
  56. fp := fmt.Sprintf("/proc/%d/comm", pid)
  57. data, err := ioutil.ReadFile(fp)
  58. if err != nil {
  59. return "", err
  60. }
  61. comm := strings.TrimSpace(string(data))
  62. return comm, nil
  63. }
  64. // ConfirmedOrUnlink reads the pidfile, confirm that it's running and comm
  65. // matches pf.Comm
  66. //
  67. // When confirmed is true, proc must be non-nil
  68. func (pf *PidFile) ConfirmOrUnlink() (proc *os.Process, confirmed bool, err error) {
  69. proc, err = pf.findProcess()
  70. if err != nil {
  71. if err == errPidNotRunning {
  72. os.Remove(pf.Path)
  73. }
  74. err = errors.Wrapf(err, "pidfile %s: find process: %v", pf.Path, err)
  75. return
  76. }
  77. comm, err := pf.findComm(proc.Pid)
  78. if err != nil {
  79. err = errors.Wrapf(err, "pidfile %s: find comm: %v", pf.Path, err)
  80. return
  81. }
  82. if pf.Comm == comm {
  83. confirmed = true
  84. return
  85. }
  86. err = os.Remove(pf.Path)
  87. if err != nil {
  88. err = errors.Wrapf(err, "pidfile %s: unlink: %v", pf.Path, err)
  89. }
  90. return
  91. }
  92. func WritePidFile(pid int, pidFile string) error {
  93. data := fmt.Sprintf("%d\n", pid)
  94. err := ioutil.WriteFile(pidFile, []byte(data), FileModeFile)
  95. return err
  96. }