Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2: Cleanup temp files #8

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 55 additions & 24 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strconv"
Expand Down Expand Up @@ -56,6 +57,23 @@ var (
clockIDRegEx = regexp.MustCompile(`\/dev\/ptp\d+`)
)

var configPrefix = "/var/run"

var ptpProcesses = []string{
ts2phcProcessName, // there can be only one ts2phc process in the system
syncEProcessName, // there can be only one synce Process per profile
ptp4lProcessName, // there could be more than one ptp4l in the system
phc2sysProcessName, // there can be only one phc2sys process in the system
}

var ptpTmpFiles = []string{
ts2phcProcessName,
syncEProcessName,
ptp4lProcessName,
phc2sysProcessName,
pmcSocketName,
}

// ProcessManager manages a set of ptpProcess
// which could be ptp4l, phc2sys or timemaster.
// Processes in ProcessManager will be started
Expand All @@ -81,14 +99,14 @@ func NewProcessManager() *ProcessManager {

// SetTestProfileProcess ...
func (p *ProcessManager) SetTestProfileProcess(name string, ifaces config.IFaces, socketPath,
ptp4lConfigPath string, nodeProfile ptpv1.PtpProfile) {
processConfigPath string, nodeProfile ptpv1.PtpProfile) {
p.process = append(p.process, &ptpProcess{
name: name,
ifaces: ifaces,
ptp4lSocketPath: socketPath,
ptp4lConfigPath: ptp4lConfigPath,
execMutex: sync.Mutex{},
nodeProfile: nodeProfile,
name: name,
ifaces: ifaces,
processSocketPath: socketPath,
processConfigPath: processConfigPath,
execMutex: sync.Mutex{},
nodeProfile: nodeProfile,
})
}

Expand Down Expand Up @@ -135,8 +153,8 @@ func (p *ProcessManager) UpdateSynceConfig(config *synce.Relations) {
type ptpProcess struct {
name string
ifaces config.IFaces
ptp4lSocketPath string
ptp4lConfigPath string
processSocketPath string
processConfigPath string
configName string
messageTag string
eventCh chan event.EventChannel
Expand Down Expand Up @@ -292,6 +310,24 @@ func (dn *Daemon) SetProcessManager(p *ProcessManager) {
dn.processManager = p
}

// Delete all socket and config files
func (dn *Daemon) cleanupTempFiles() error {
glog.Infof("Cleaning up temporary files")
var err error
for _, p := range ptpTmpFiles {
processWildcard := fmt.Sprintf("%s/%s*", configPrefix, p)
files, _ := filepath.Glob(processWildcard)
for _, file := range files {
err = os.Remove(file)
if err != nil {
glog.Infof("Failed deleting %s", file)
}
}

}
return nil
}

func (dn *Daemon) applyNodePTPProfiles() error {
glog.Infof("in applyNodePTPProfiles")
for _, p := range dn.processManager.process {
Expand Down Expand Up @@ -324,6 +360,9 @@ func (dn *Daemon) applyNodePTPProfiles() error {
// references).
dn.processManager.process = nil

// All configs will be rebuild, and sockets recreated, so they can all be deleted
dn.cleanupTempFiles()

// TODO:
// compare nodeProfile with previous config,
// only apply when nodeProfile changes
Expand Down Expand Up @@ -433,19 +472,11 @@ ptpconfig profiles for ptp4l
*/
func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile) error {
testDir, test := nodeProfile.PtpSettings["unitTest"]
var configPrefix = "/var/run"
if test {
configPrefix = testDir
}
dn.pluginManager.OnPTPConfigChange(nodeProfile)

ptpProcesses := []string{
ts2phcProcessName, // there can be only one ts2phc process in the system
syncEProcessName, // there can be only one synce Process per profile
ptp4lProcessName, // there could be more than one ptp4l in the system
phc2sysProcessName, // there can be only one phc2sys process in the system
}

var err error
var cmdLine string
var configPath string
Expand Down Expand Up @@ -594,8 +625,8 @@ func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile)
dprocess := ptpProcess{
name: p,
ifaces: ifaces,
ptp4lConfigPath: configPath,
ptp4lSocketPath: socketPath,
processConfigPath: configPath,
processSocketPath: socketPath,
configName: configFile,
messageTag: messageTag,
exitCh: make(chan bool),
Expand Down Expand Up @@ -1018,11 +1049,11 @@ func (p *ptpProcess) cmdStop() {
return
}
}
glog.Infof("removing config path %s for %s ", p.ptp4lConfigPath, p.name)
if p.ptp4lConfigPath != "" {
err := os.Remove(p.ptp4lConfigPath)
glog.Infof("removing config path %s for %s ", p.processConfigPath, p.name)
if p.processConfigPath != "" {
err := os.Remove(p.processConfigPath)
if err != nil {
glog.Errorf("failed to remove ptp4l config path %s: %v", p.ptp4lConfigPath, err)
glog.Errorf("failed to remove ptp4l config path %s: %v", p.processConfigPath, err)
}
}
<-p.exitCh
Expand Down Expand Up @@ -1109,7 +1140,7 @@ func (dn *Daemon) ApplyHaProfiles(nodeProfile *ptpv1.PtpProfile, cmdLine string)
for _, profileName := range lsProfiles {
for _, dmProcess := range dn.processManager.process {
if dmProcess.nodeProfile.Name != nil && *dmProcess.nodeProfile.Name == profileName {
updateHaProfileToSocketPath = append(updateHaProfileToSocketPath, "-z "+dmProcess.ptp4lSocketPath)
updateHaProfileToSocketPath = append(updateHaProfileToSocketPath, "-z "+dmProcess.processSocketPath)
var ifaces []string
for _, iface := range dmProcess.ifaces {
ifaces = append(ifaces, iface.Name)
Expand Down
1 change: 1 addition & 0 deletions pkg/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
syncEProcessName = "synce4l"
clockRealTime = "CLOCK_REALTIME"
master = "master"
pmcSocketName = "pmc"

faultyOffset = 999999

Expand Down