-
Notifications
You must be signed in to change notification settings - Fork 15
/
devstate.go
283 lines (231 loc) · 5.81 KB
/
devstate.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Per-device persistent state
*/
package main
import (
"bytes"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strconv"
)
// DevState manages a per-device persistent state (such as HTTP
// port allocation etc)
type DevState struct {
Ident string // Device identification
HTTPPort int // Allocated HTTP port
DNSSdName string // DNS-SD name, as reported by device
DNSSdOverride string // DNS-SD name after collision resolution
comment string // Comment in the state file
path string // Path to the disk file
}
// LoadDevState loads DevState from a disk file
//
// This function always succeeds, even in a case of file i/o errors.
// In a worst case we loose state persistence, not other functionality.
func LoadDevState(ident, comment string) *DevState {
state := &DevState{
Ident: ident,
comment: comment,
}
state.path = state.devStatePath()
// Read state file
ini, err := OpenIniFile(state.path)
if err == nil {
err = state.load(ini)
ini.Close()
}
if err != nil && err != io.EOF {
if !os.IsNotExist(err) {
Log.Error('!', "STATE LOAD: %s", state.error("%s", err))
}
}
return state
}
// LoadUsedPorts loads ports used by some of devices.
//
// The returned map contains one entry per used port. Value of this
// entry is a human-readable string, reasonable for logging
func LoadUsedPorts() (ports map[int]string) {
ports = make(map[int]string)
// Read the PathProgStateDev (normally "/var/ipp-usb/dev")
// directory.
var files []os.FileInfo
var err error
dir, err := os.Open(PathProgStateDev)
if err == nil {
files, err = dir.Readdir(0)
dir.Close()
}
if err != nil {
Log.Error('!', "Can't load existing ports allocation")
Log.Error('!', "%s", err)
return
}
if err != nil {
return
}
// Scan found files
for _, file := range files {
Log.Debug(' ', "== %s", file.Name())
if !file.Mode().IsRegular() {
continue
}
path := filepath.Join(PathProgStateDev, file.Name())
ini, err := OpenIniFile(path)
if err != nil {
Log.Error('!', "%s", err)
continue
}
state := &DevState{}
err = state.load(ini)
ini.Close()
if err != nil {
Log.Error('!', "%s", err)
continue
}
if state.HTTPPort != 0 {
ports[state.HTTPPort] = file.Name()
}
}
return
}
// load performs an actual work of loading the DevState file
func (state *DevState) load(ini *IniFile) error {
err := ini.Lock(FileLockWait)
if err == nil {
defer ini.Unlock()
}
for err == nil {
var rec *IniRecord
rec, err = ini.Next()
if err != nil {
break
}
switch rec.Section {
case "device":
switch rec.Key {
case "http-port":
err = state.loadTCPPort(&state.HTTPPort, rec)
case "dns-sd-name":
state.DNSSdName = rec.Value
case "dns-sd-override":
state.DNSSdOverride = rec.Value
}
}
}
if err == io.EOF {
err = nil
}
return err
}
// Load TCP port
func (state *DevState) loadTCPPort(out *int, rec *IniRecord) error {
port, err := strconv.Atoi(rec.Value)
if err != nil {
err = state.error("%s", err)
} else if port < 1 || port > 65535 {
err = state.error("%s: out of range", rec.Key)
}
if err != nil {
return err
}
*out = port
return nil
}
// Save updates DevState on disk
func (state *DevState) Save() {
os.MkdirAll(PathProgStateDev, 0755)
var buf bytes.Buffer
if state.comment != "" {
fmt.Fprintf(&buf, "; %s\n", state.comment)
}
fmt.Fprintf(&buf, "[device]\n")
fmt.Fprintf(&buf, "http-port = %d\n", state.HTTPPort)
fmt.Fprintf(&buf, "dns-sd-name = %q\n", state.DNSSdName)
fmt.Fprintf(&buf, "dns-sd-override = %q\n", state.DNSSdOverride)
err := state.save(buf.Bytes())
if err != nil {
err = state.error("%s", err)
Log.Error('!', "STATE SAVE: %s", err)
}
}
// save performs an actual work of saving state file
func (state *DevState) save(data []byte) error {
f, err := os.OpenFile(state.path,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
err = FileLock(f, FileLockWait)
if err != nil {
f.Close()
return err
}
_, err = f.Write(data)
FileUnlock(f)
if err != nil {
f.Close()
return err
}
return f.Close()
}
// HTTPListen allocates HTTP port and updates persistent configuration
func (state *DevState) HTTPListen() (net.Listener, error) {
port := state.HTTPPort
// Check that preallocated port is within the configured range
if !(Conf.HTTPMinPort <= port && port <= Conf.HTTPMaxPort) {
port = 0
}
// Try to allocate port used before
if port != 0 {
listener, err := NewListener(port)
if err == nil {
return listener, nil
}
}
// Allocate a port. Don't reuse ports allocated by other
// devices.
ports := LoadUsedPorts()
for port = Conf.HTTPMinPort; port <= Conf.HTTPMaxPort; port++ {
used := ports[port]
if used != "" {
Log.Info(' ', "HTTP port %d used by %s", port, used)
continue
}
listener, err := NewListener(port)
if err == nil {
state.HTTPPort = port
state.Save()
return listener, nil
}
}
// No success so far. Repeat allocation attempt, ignoring
// existent allocations
for port = Conf.HTTPMinPort; port <= Conf.HTTPMaxPort; port++ {
listener, err := NewListener(port)
if err == nil {
state.HTTPPort = port
state.Save()
return listener, nil
}
}
// Give up and return an error
err := state.error("failed to allocate HTTP port", state.Ident)
Log.Error('!', "STATE PORT: %s", err)
return nil, err
}
// devStatePath returns a path to the DevState file
func (state *DevState) devStatePath() string {
return filepath.Join(PathProgStateDev, state.Ident+".state")
}
// error creates a state-related error
func (state *DevState) error(format string, args ...interface{}) error {
return fmt.Errorf(state.Ident+": "+format, args...)
}