7
7
"net"
8
8
"os"
9
9
"os/exec"
10
+ "path/filepath"
10
11
"regexp"
11
12
"slices"
12
13
"strconv"
56
57
clockIDRegEx = regexp .MustCompile (`\/dev\/ptp\d+` )
57
58
)
58
59
60
+ var configPrefix = "/var/run"
61
+
62
+ var ptpProcesses = []string {
63
+ ts2phcProcessName , // there can be only one ts2phc process in the system
64
+ syncEProcessName , // there can be only one synce Process per profile
65
+ ptp4lProcessName , // there could be more than one ptp4l in the system
66
+ phc2sysProcessName , // there can be only one phc2sys process in the system
67
+ }
68
+
69
+ var ptpTmpFiles = []string {
70
+ ts2phcProcessName ,
71
+ syncEProcessName ,
72
+ ptp4lProcessName ,
73
+ phc2sysProcessName ,
74
+ pmcSocketName ,
75
+ }
76
+
59
77
// ProcessManager manages a set of ptpProcess
60
78
// which could be ptp4l, phc2sys or timemaster.
61
79
// Processes in ProcessManager will be started
@@ -81,14 +99,14 @@ func NewProcessManager() *ProcessManager {
81
99
82
100
// SetTestProfileProcess ...
83
101
func (p * ProcessManager ) SetTestProfileProcess (name string , ifaces config.IFaces , socketPath ,
84
- ptp4lConfigPath string , nodeProfile ptpv1.PtpProfile ) {
102
+ processConfigPath string , nodeProfile ptpv1.PtpProfile ) {
85
103
p .process = append (p .process , & ptpProcess {
86
- name : name ,
87
- ifaces : ifaces ,
88
- ptp4lSocketPath : socketPath ,
89
- ptp4lConfigPath : ptp4lConfigPath ,
90
- execMutex : sync.Mutex {},
91
- nodeProfile : nodeProfile ,
104
+ name : name ,
105
+ ifaces : ifaces ,
106
+ processSocketPath : socketPath ,
107
+ processConfigPath : processConfigPath ,
108
+ execMutex : sync.Mutex {},
109
+ nodeProfile : nodeProfile ,
92
110
})
93
111
}
94
112
@@ -135,8 +153,8 @@ func (p *ProcessManager) UpdateSynceConfig(config *synce.Relations) {
135
153
type ptpProcess struct {
136
154
name string
137
155
ifaces config.IFaces
138
- ptp4lSocketPath string
139
- ptp4lConfigPath string
156
+ processSocketPath string
157
+ processConfigPath string
140
158
configName string
141
159
messageTag string
142
160
eventCh chan event.EventChannel
@@ -292,6 +310,24 @@ func (dn *Daemon) SetProcessManager(p *ProcessManager) {
292
310
dn .processManager = p
293
311
}
294
312
313
+ // Delete all socket and config files
314
+ func (dn * Daemon ) cleanupTempFiles () error {
315
+ glog .Infof ("Cleaning up temporary files" )
316
+ var err error
317
+ for _ , p := range ptpTmpFiles {
318
+ processWildcard := fmt .Sprintf ("%s/%s*" , configPrefix , p )
319
+ files , _ := filepath .Glob (processWildcard )
320
+ for _ , file := range files {
321
+ err = os .Remove (file )
322
+ if err != nil {
323
+ glog .Infof ("Failed deleting %s" , file )
324
+ }
325
+ }
326
+
327
+ }
328
+ return nil
329
+ }
330
+
295
331
func (dn * Daemon ) applyNodePTPProfiles () error {
296
332
glog .Infof ("in applyNodePTPProfiles" )
297
333
for _ , p := range dn .processManager .process {
@@ -324,6 +360,9 @@ func (dn *Daemon) applyNodePTPProfiles() error {
324
360
// references).
325
361
dn .processManager .process = nil
326
362
363
+ // All configs will be rebuild, and sockets recreated, so they can all be deleted
364
+ dn .cleanupTempFiles ()
365
+
327
366
// TODO:
328
367
// compare nodeProfile with previous config,
329
368
// only apply when nodeProfile changes
@@ -433,19 +472,11 @@ ptpconfig profiles for ptp4l
433
472
*/
434
473
func (dn * Daemon ) applyNodePtpProfile (runID int , nodeProfile * ptpv1.PtpProfile ) error {
435
474
testDir , test := nodeProfile .PtpSettings ["unitTest" ]
436
- var configPrefix = "/var/run"
437
475
if test {
438
476
configPrefix = testDir
439
477
}
440
478
dn .pluginManager .OnPTPConfigChange (nodeProfile )
441
479
442
- ptpProcesses := []string {
443
- ts2phcProcessName , // there can be only one ts2phc process in the system
444
- syncEProcessName , // there can be only one synce Process per profile
445
- ptp4lProcessName , // there could be more than one ptp4l in the system
446
- phc2sysProcessName , // there can be only one phc2sys process in the system
447
- }
448
-
449
480
var err error
450
481
var cmdLine string
451
482
var configPath string
@@ -594,8 +625,8 @@ func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile)
594
625
dprocess := ptpProcess {
595
626
name : p ,
596
627
ifaces : ifaces ,
597
- ptp4lConfigPath : configPath ,
598
- ptp4lSocketPath : socketPath ,
628
+ processConfigPath : configPath ,
629
+ processSocketPath : socketPath ,
599
630
configName : configFile ,
600
631
messageTag : messageTag ,
601
632
exitCh : make (chan bool ),
@@ -1018,11 +1049,11 @@ func (p *ptpProcess) cmdStop() {
1018
1049
return
1019
1050
}
1020
1051
}
1021
- glog .Infof ("removing config path %s for %s " , p .ptp4lConfigPath , p .name )
1022
- if p .ptp4lConfigPath != "" {
1023
- err := os .Remove (p .ptp4lConfigPath )
1052
+ glog .Infof ("removing config path %s for %s " , p .processConfigPath , p .name )
1053
+ if p .processConfigPath != "" {
1054
+ err := os .Remove (p .processConfigPath )
1024
1055
if err != nil {
1025
- glog .Errorf ("failed to remove ptp4l config path %s: %v" , p .ptp4lConfigPath , err )
1056
+ glog .Errorf ("failed to remove ptp4l config path %s: %v" , p .processConfigPath , err )
1026
1057
}
1027
1058
}
1028
1059
<- p .exitCh
@@ -1109,7 +1140,7 @@ func (dn *Daemon) ApplyHaProfiles(nodeProfile *ptpv1.PtpProfile, cmdLine string)
1109
1140
for _ , profileName := range lsProfiles {
1110
1141
for _ , dmProcess := range dn .processManager .process {
1111
1142
if dmProcess .nodeProfile .Name != nil && * dmProcess .nodeProfile .Name == profileName {
1112
- updateHaProfileToSocketPath = append (updateHaProfileToSocketPath , "-z " + dmProcess .ptp4lSocketPath )
1143
+ updateHaProfileToSocketPath = append (updateHaProfileToSocketPath , "-z " + dmProcess .processSocketPath )
1113
1144
var ifaces []string
1114
1145
for _ , iface := range dmProcess .ifaces {
1115
1146
ifaces = append (ifaces , iface .Name )
0 commit comments