2014-07-12 02:02:55 +08:00
|
|
|
package syncpipe
|
2014-02-22 14:58:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-06-16 21:30:42 +08:00
|
|
|
"syscall"
|
2014-02-22 14:58:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// SyncPipe allows communication to and from the child processes
|
|
|
|
// to it's parent and allows the two independent processes to
|
|
|
|
// syncronize their state.
|
|
|
|
type SyncPipe struct {
|
|
|
|
parent, child *os.File
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:08:49 +08:00
|
|
|
func NewSyncPipeFromFd(parentFd, childFd uintptr) (*SyncPipe, error) {
|
2014-02-22 14:58:30 +08:00
|
|
|
s := &SyncPipe{}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-06-27 05:08:49 +08:00
|
|
|
if parentFd > 0 {
|
|
|
|
s.parent = os.NewFile(parentFd, "parentPipe")
|
2014-02-22 14:58:30 +08:00
|
|
|
} else if childFd > 0 {
|
|
|
|
s.child = os.NewFile(childFd, "childPipe")
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("no valid sync pipe fd specified")
|
|
|
|
}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:02:45 +08:00
|
|
|
func (s *SyncPipe) Child() *os.File {
|
|
|
|
return s.child
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncPipe) Parent() *os.File {
|
|
|
|
return s.parent
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
func (s *SyncPipe) SendToChild(v interface{}) error {
|
|
|
|
data, err := json.Marshal(v)
|
2014-02-22 14:58:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-27 05:18:39 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
s.parent.Write(data)
|
2014-06-27 05:18:39 +08:00
|
|
|
|
|
|
|
return syscall.Shutdown(int(s.parent.Fd()), syscall.SHUT_WR)
|
2014-06-16 21:30:42 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 02:38:53 +08:00
|
|
|
func (s *SyncPipe) ReadFromChild() error {
|
2014-06-16 21:30:42 +08:00
|
|
|
data, err := ioutil.ReadAll(s.parent)
|
|
|
|
if err != nil {
|
2014-06-27 02:38:53 +08:00
|
|
|
return err
|
2014-06-16 21:30:42 +08:00
|
|
|
}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-06-16 21:30:42 +08:00
|
|
|
if len(data) > 0 {
|
2014-06-27 02:50:22 +08:00
|
|
|
return fmt.Errorf("%s", data)
|
2014-06-16 21:30:42 +08:00
|
|
|
}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
func (s *SyncPipe) ReadFromParent(v interface{}) error {
|
2014-02-22 14:58:30 +08:00
|
|
|
data, err := ioutil.ReadAll(s.child)
|
|
|
|
if err != nil {
|
2014-08-07 09:00:52 +08:00
|
|
|
return fmt.Errorf("error reading from sync pipe %s", err)
|
2014-02-22 14:58:30 +08:00
|
|
|
}
|
2014-08-07 09:00:52 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
if len(data) > 0 {
|
2014-08-07 09:00:52 +08:00
|
|
|
if err := json.Unmarshal(data, v); err != nil {
|
|
|
|
return err
|
2014-02-22 14:58:30 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 09:00:52 +08:00
|
|
|
|
|
|
|
return nil
|
2014-02-22 14:58:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 02:38:53 +08:00
|
|
|
func (s *SyncPipe) ReportChildError(err error) {
|
2014-08-29 22:17:22 +08:00
|
|
|
// ensure that any data sent from the parent is consumed so it doesn't
|
|
|
|
// receive ECONNRESET when the child writes to the pipe.
|
|
|
|
ioutil.ReadAll(s.child)
|
|
|
|
|
2014-06-16 21:30:42 +08:00
|
|
|
s.child.Write([]byte(err.Error()))
|
|
|
|
s.CloseChild()
|
|
|
|
}
|
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
func (s *SyncPipe) Close() error {
|
|
|
|
if s.parent != nil {
|
|
|
|
s.parent.Close()
|
|
|
|
}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
if s.child != nil {
|
|
|
|
s.child.Close()
|
|
|
|
}
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-02-22 14:58:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-06-16 21:30:42 +08:00
|
|
|
|
|
|
|
func (s *SyncPipe) CloseChild() {
|
|
|
|
if s.child != nil {
|
|
|
|
s.child.Close()
|
|
|
|
s.child = nil
|
|
|
|
}
|
|
|
|
}
|