-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid.go
More file actions
133 lines (123 loc) · 2.8 KB
/
pid.go
File metadata and controls
133 lines (123 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2022-2026 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package services
import (
"bufio"
"fmt"
"io"
"os"
"os/signal"
"strconv"
"strings"
)
// PidFileLocation pid file location for creating the PID file
var PidFileLocation = ""
func getPidFile() string {
host, err := os.Hostname()
if err != nil {
host = ""
} else {
h := strings.IndexRune(host, '.')
if h == -1 {
h = len(host)
}
host = "." + host[:h]
}
p := PidFileLocation
if p == "" {
p = os.Getenv("TEMP")
}
if p == "" {
p = "." + string(os.PathSeparator) + "tmp"
}
return p + string(os.PathSeparator) + "server" + host + ".pid"
}
// CreatePidFile create PID file
func CreatePidFile(flagPidFile string) error {
pidFile := flagPidFile
if pidFile == "" {
pidFile = getPidFile()
}
if pidFile == "" {
panic("PID File evaluation internal error")
}
fs, err := os.Stat(pidFile)
if err == nil {
if fs.IsDir() {
return NewError("SYS00006", pidFile)
}
return NewError("SYS00007", pidFile)
}
if !os.IsNotExist(err) {
return NewError("SYS00008", pidFile, err)
}
rf, err := os.OpenFile(pidFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
rf, err = os.OpenFile(pidFile, os.O_RDWR, 0666)
if err != nil {
ServerErrorMessage("SYS00029", pidFile)
return err
}
}
defer rf.Close()
pid := os.Getpid()
ServerMessage("Creating PID file for server pid=%d", pid)
fmt.Fprintf(rf, "%d"+LineBreak, pid)
c := make(chan os.Signal, 3)
signal.Notify(c, handleSignal...)
// signal.Notify(c, os.Kill, os.Interrupt)
go func() {
<-c // sig := <-c:
DeletePidFile(flagPidFile)
}()
return nil
}
// DeletePidFile delete PID file
func DeletePidFile(flagPidFile string) error {
pidFile := flagPidFile
if pidFile == "" {
pidFile = getPidFile()
}
err := os.Remove(pidFile)
ServerMessage("Removing PID file %s...", pidFile)
return err
}
// ReadPidFile read PID file
func ReadPidFile(flagPidFile string) (int, error) {
pidFile := flagPidFile
if pidFile == "" {
pidFile = getPidFile()
}
rf, err := os.OpenFile(pidFile, os.O_RDONLY, 0)
if err != nil {
return -1, fmt.Errorf("error reading server PID file %s: %v", pidFile, err)
}
defer rf.Close()
var pid int
reader := bufio.NewReader(rf)
var line string
for {
line, err = reader.ReadString('\n')
if err != nil && err != io.EOF {
return 0, fmt.Errorf("error reading server PID file %s: %v", pidFile, err)
}
line = strings.Trim(line, " ")
line = strings.Trim(line, LineBreak)
if line != "" {
pid, err = strconv.Atoi(line)
if err != nil {
return 0, fmt.Errorf("error reading server PID: %v", err)
}
break
}
}
return pid, nil
}