本文整理匯總了Golang中syscall.UnixRights函數的典型用法代碼示例。如果您正苦於以下問題:Golang UnixRights函數的具體用法?Golang UnixRights怎麽用?Golang UnixRights使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了UnixRights函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SendRequest
func SendRequest(c *net.UnixConn, req *server.Request) error {
payload, err := json.Marshal(req)
if err != nil {
return err
}
err = binary.Write(c, binary.BigEndian, uint32(len(payload)))
if err != nil {
return err
}
n, err := c.Write(payload)
if err != nil {
return err
} else if n != len(payload) {
return fmt.Errorf("Failed to write full payload, expected %v, wrote %v", len(payload), n)
}
if !req.HasFds {
return nil
}
// Send filedescriptors with a 1 byte message.
var oob []byte
oob = syscall.UnixRights(req.Fds...)
payload = make([]byte, 1)
n, oobn, err := c.WriteMsgUnix(payload, oob, nil)
if err != nil {
return err
} else if n != len(payload) || oobn != len(oob) {
return fmt.Errorf("Error writing to socket, expected n=%v got %v, oob=%v got %v", len(payload), n, len(oob), oobn)
}
return nil
}
示例2: writeData
func writeData(conn *net.UnixConn, files []*os.File, pid int, responseErr error) {
var errMsg string = ""
if responseErr != nil {
errMsg = responseErr.Error()
}
response := &Response{
Pid: pid,
ErrMessage: errMsg,
}
responseJson, _ := json.Marshal(response) // Ignore error
args := make([]int, len(files))
for i, f := range files {
args[i] = int(f.Fd())
}
resp := syscall.UnixRights(args...)
conn.WriteMsgUnix(responseJson, resp, nil) // Ignore error
// Close the files whose descriptors have been sent to the host to ensure that
// a close on the host takes effect in a timely fashion.
for _, file := range files {
file.Close() // Ignore error
}
}
示例3: SendMessage
func (t *unixTransport) SendMessage(msg *Message) error {
fds := make([]int, 0)
for i, v := range msg.Body {
if fd, ok := v.(UnixFD); ok {
msg.Body[i] = UnixFDIndex(len(fds))
fds = append(fds, int(fd))
}
}
if len(fds) != 0 {
if !t.hasUnixFDs {
return errors.New("dbus: unix fd passing not enabled")
}
msg.Headers[FieldUnixFDs] = MakeVariant(uint32(len(fds)))
oob := syscall.UnixRights(fds...)
buf := new(bytes.Buffer)
msg.EncodeTo(buf, binary.LittleEndian)
n, oobn, err := t.UnixConn.WriteMsgUnix(buf.Bytes(), oob, nil)
if err != nil {
return err
}
if n != buf.Len() || oobn != len(oob) {
return io.ErrShortWrite
}
} else {
if err := msg.EncodeTo(t, binary.LittleEndian); err != nil {
return nil
}
}
return nil
}
示例4: Notify
func Notify(state string, fds ...int) {
ns := os.Getenv("NOTIFY_SOCKET")
if ns == "" {
return
}
addr := &net.UnixAddr{
Name: ns,
Net: "unixgram",
}
conn, err := net.DialUnix("unixgram", nil, addr)
if err != nil {
return
}
defer conn.Close()
if len(fds) > 0 {
rights := syscall.UnixRights(fds...)
conn.WriteMsgUnix([]byte(state), rights, nil)
} else {
conn.Write([]byte(state))
}
}
示例5: Write
func (m *Message) Write(arg interface{}) error {
switch t := arg.(type) {
case Proxy:
return binary.Write(m.data, binary.LittleEndian, uint32(t.Id()))
case uint32, int32:
return binary.Write(m.data, binary.LittleEndian, t)
case float32:
f := float64ToFixed(float64(t))
return binary.Write(m.data, binary.LittleEndian, f)
case string:
str, _ := arg.(string)
tail := 4 - (len(str) & 0x3)
err := binary.Write(m.data, binary.LittleEndian, uint32(len(str)+tail))
if err != nil {
return err
}
err = binary.Write(m.data, binary.LittleEndian, []byte(str))
if err != nil {
return err
}
padding := make([]byte, tail)
return binary.Write(m.data, binary.LittleEndian, padding)
case uintptr:
rights := syscall.UnixRights(int(t))
return binary.Write(m.control, binary.LittleEndian, rights)
default:
panic("Invalid Wayland request parameter type.")
}
return nil
}
示例6: Write
func (w *FDWriter) Write(b []byte) (int, error) {
if len(w.fds) == 0 {
return w.conn.Write(b)
} else {
rights := syscall.UnixRights(w.fds...)
n, _, err := w.conn.WriteMsgUnix(b, rights, nil)
w.fds = nil
return n, err
}
}
示例7: WriteFile
func WriteFile(c *net.UnixConn, file *os.File, timeout time.Duration) error {
if timeout > 0 {
deadline := time.Now().Add(timeout)
if err := c.SetWriteDeadline(deadline); err != nil {
return err
}
}
oob := syscall.UnixRights(int(file.Fd()))
_, _, err := c.WriteMsgUnix(nil, oob, nil)
return err
}
示例8: writeTo
func (fd Fd) writeTo(c *net.UnixConn) error {
var b []byte
oob := syscall.UnixRights(int(fd))
_, oobn, err := c.WriteMsgUnix(b, oob, nil)
if err != nil {
return err
}
if oobn != len(oob) {
return fmt.Errorf("expected to write %d oob bytes, wrote %d", len(oob), oobn)
}
return nil
}
示例9: WriteFD
func (usock *Usock) WriteFD(fd int) error {
rights := syscall.UnixRights(fd)
dummyByte := []byte("\000")
n, oobn, err := usock.Conn.WriteMsgUnix(dummyByte, rights, nil)
if err != nil {
str := fmt.Sprintf("Usock#WriteFD:WriteMsgUnix: %v %v\n", err, syscall.EINVAL)
return errors.New(str)
}
if n != 1 || oobn != len(rights) {
str := fmt.Sprintf("Usock#WriteFD:WriteMsgUnix = %d, %d; want 1, %d\n", n, oobn, len(rights))
return errors.New(str)
}
return nil
}
示例10: Write
func (s *OOBUnixConn) Write(buf []byte) (int, error) {
var oob []byte
s.m.Lock()
fds := s.sendFDs
s.sendFDs = nil
s.m.Unlock()
if len(fds) > 0 {
oob = syscall.UnixRights(fds...)
}
n, oobn, err := s.WriteMsgUnix(buf, oob, nil)
if err == nil && oobn != len(oob) {
err = ErrOOBSendFailed
}
return n, err
}
示例11: SendFd
func SendFd(conn *net.UnixConn, file *os.File) error {
rights := syscall.UnixRights(int(file.Fd()))
dummy := []byte("x")
n, oobn, err := conn.WriteMsgUnix(dummy, rights, nil)
if err != nil {
return fmt.Errorf("sendfd: err %v", err)
}
if n != len(dummy) {
return fmt.Errorf("sendfd: short write %v", conn)
}
if oobn != len(rights) {
return fmt.Errorf("sendfd: short oob write %v", conn)
}
return nil
}
示例12: writeData
func writeData(conn *net.UnixConn, response *container_daemon.ResponseMessage) {
data, _ := json.Marshal(response) // Ignore error
args := make([]int, len(response.Files))
for i, f := range response.Files {
args[i] = int(f.Fd())
}
oobData := syscall.UnixRights(args...)
conn.WriteMsgUnix(data, oobData, nil) // Ignore error
// Close the files whose descriptors have been sent to the host to ensure that
// a close on the host takes effect in a timely fashion.
for _, file := range response.Files {
file.Close() // Ignore error
}
}
示例13: passFDChild
// passFDChild is the child process used by TestPassFD.
func passFDChild() {
defer os.Exit(0)
// Look for our fd. It should be fd 3, but we work around an fd leak
// bug here (http://golang.org/issue/2603) to let it be elsewhere.
var uc *net.UnixConn
for fd := uintptr(3); fd <= 10; fd++ {
f := os.NewFile(fd, "unix-conn")
var ok bool
netc, _ := net.FileConn(f)
uc, ok = netc.(*net.UnixConn)
if ok {
break
}
}
if uc == nil {
fmt.Println("failed to find unix fd")
return
}
// Make a file f to send to our parent process on uc.
// We make it in tempDir, which our parent will clean up.
flag.Parse()
tempDir := flag.Arg(0)
f, err := ioutil.TempFile(tempDir, "")
if err != nil {
fmt.Printf("TempFile: %v", err)
return
}
f.Write([]byte("Hello from child process!\n"))
f.Seek(0, 0)
rights := syscall.UnixRights(int(f.Fd()))
dummyByte := []byte("x")
n, oobn, err := uc.WriteMsgUnix(dummyByte, rights, nil)
if err != nil {
fmt.Printf("WriteMsgUnix: %v", err)
return
}
if n != 1 || oobn != len(rights) {
fmt.Printf("WriteMsgUnix = %d, %d; want 1, %d", n, oobn, len(rights))
return
}
}
示例14: WriteFD
func (u *Usock) WriteFD(fd int) error {
u.Lock()
defer u.Unlock()
rights := syscall.UnixRights(fd)
dummyByte := []byte{0}
n, oobn, err := u.reader.Conn.WriteMsgUnix(dummyByte, rights, nil)
if err != nil {
str := fmt.Sprintf("Usock#WriteFD:WriteMsgUnix: %v / %v\n", err, syscall.EINVAL)
return errors.New(str)
}
if n != 1 || oobn != len(rights) {
str := fmt.Sprintf("Usock#WriteFD:WriteMsgUnix = %d, %d; want 1, %d\n", n, oobn, len(rights))
return errors.New(str)
}
return nil
}
示例15: WriteMessage
func (c *Conn) WriteMessage(m *Message) (err error) {
var payload []byte
if m.p != nil {
payload = m.p.Bytes()
}
if err = c.writeHeader(m.object, m.opcode, uint16(len(payload))); err != nil {
return
}
if len(payload) == 0 {
// message without payload wouldn't contain fds, so we can return here
return nil
}
var oob []byte
if len(m.fds) != 0 {
oob = syscall.UnixRights(m.fds...)
}
_, _, err = c.c.WriteMsgUnix(payload, oob, nil)
return
}