@@ -20,8 +20,11 @@ import (
2020 cniTypes "github.com/containernetworking/cni/pkg/types"
2121 cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
2222 cniVers "github.com/containernetworking/cni/pkg/version"
23+ "github.com/pkg/errors"
2324)
2425
26+ var errEmptyContent = errors .New ("read content is zero bytes" )
27+
2528// Plugin is the parent class for CNI plugins.
2629type Plugin struct {
2730 * common.Plugin
@@ -197,26 +200,33 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) (bool, error) {
197200 return false , cmdErr
198201 }
199202
200- // Read pid from lockfile
201203 lockFileName := plugin .Store .GetLockFileName ()
202- content , err := ioutil .ReadFile (lockFileName )
204+ // Read pid from lockfile
205+ lockFilePid , err := plugin .readLockFile (lockFileName )
203206 if err != nil {
204- log .Errorf ("Failed to read lock file :%v, " , err )
205- return false , err
206- }
207-
208- if len (content ) <= 0 {
209- log .Errorf ("Num bytes read from lock file is 0" )
210- return false , fmt .Errorf ("Num bytes read from lock file is 0" )
207+ return false , errors .Wrap (err , "IsSafeToRemoveLock lockfile read failed" )
211208 }
212209
213- log .Printf ("Read from Lock file :%s" , content )
210+ log .Printf ("Read from lockfile :%s" , lockFilePid )
214211 // Get the process name if running and
215212 // check if that matches with our expected process
216- pName , err := platform .GetProcessNameByID (string (content ))
213+ // if it returns non-nil error then process is not running
214+ pName , err := platform .GetProcessNameByID (lockFilePid )
217215 if err != nil {
216+ var content string
217+ content , err = plugin .readLockFile (lockFileName )
218+ if err != nil {
219+ return false , errors .Wrap (err , "IsSafeToRemoveLock lockfile 2nd read failed" )
220+ }
221+
222+ // pid in lockfile changed after getprocessnamebyid call. so some other process acquired lockfile in between.
223+ // so its not safe to remove lockfile
224+ if string (content ) != lockFilePid {
225+ log .Printf ("Lockfile content changed from %s to %s. So not safe to remove lockfile" , lockFilePid , content )
226+ return false , nil
227+ }
228+
218229 return true , nil
219- // if process id changed. read lockfile?
220230 }
221231
222232 log .Printf ("[CNI] Process name is %s" , pName )
@@ -229,3 +239,18 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) (bool, error) {
229239 log .Errorf ("Plugin store is nil" )
230240 return false , fmt .Errorf ("plugin store nil" )
231241}
242+
243+ func (plugin * Plugin ) readLockFile (filename string ) (string , error ) {
244+ content , err := ioutil .ReadFile (filename )
245+ if err != nil {
246+ log .Errorf ("Failed to read lockfile :%v" , err )
247+ return "" , fmt .Errorf ("readLockFile error:%w" , err )
248+ }
249+
250+ if len (content ) == 0 {
251+ log .Errorf ("Num bytes read from lockfile is 0" )
252+ return "" , errEmptyContent
253+ }
254+
255+ return string (content ), nil
256+ }
0 commit comments