Skip to content

Commit

Permalink
[FAB-4899] don't use syscall for writing out peer pid
Browse files Browse the repository at this point in the history
Its probably OK to not have to lock the peer PID file while
writing the PID to it. Using `syscall.Flock` etc to do this
causes failure on Windows platforms.

patch 1
. removed the file removal in case it belongs to a running server
  (was in two minds about it... we will overwrite but why remove
   unnecessarily...)

Change-Id: I6347ac744eeaa8114728d5357bfaa2292525db15
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Jun 22, 2017
1 parent cefe788 commit 864b9a1
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ package node

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"

Expand Down Expand Up @@ -370,33 +372,11 @@ func writePid(fileName string, pid int) error {
return err
}

fd, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer fd.Close()
if err := syscall.Flock(int(fd.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
return fmt.Errorf("can't lock '%s', lock is held", fd.Name())
}

if _, err := fd.Seek(0, 0); err != nil {
return err
}

if err := fd.Truncate(0); err != nil {
buf := strconv.Itoa(pid)
if err = ioutil.WriteFile(fileName, []byte(buf), 0644); err != nil {
logger.Errorf("Cannot write pid to %s (err:%s)", fileName, err)
return err
}

if _, err := fmt.Fprintf(fd, "%d", pid); err != nil {
return err
}

if err := fd.Sync(); err != nil {
return err
}

if err := syscall.Flock(int(fd.Fd()), syscall.LOCK_UN); err != nil {
return fmt.Errorf("can't release lock '%s', lock is held", fd.Name())
}
return nil
}

0 comments on commit 864b9a1

Please sign in to comment.