usensord-1.0+14.04.20140404/0000755000015301777760000000000012317524560015615 5ustar pbusernogroup00000000000000usensord-1.0+14.04.20140404/dbus/0000755000015301777760000000000012317524560016552 5ustar pbusernogroup00000000000000usensord-1.0+14.04.20140404/dbus/matchrule.go0000644000015301777760000000307612317524417021074 0ustar pbusernogroup00000000000000package dbus import "fmt" import "strings" // Matches all messages with equal type, interface, member, or path. // Any missing/invalid fields are not matched against. type MatchRule struct { Type MessageType Sender string Path ObjectPath Interface string Member string Arg0 string senderNameOwner string } // A string representation af the MatchRule (D-Bus variant map). func (p *MatchRule) String() string { params := make([]string, 0, 6) if p.Type != TypeInvalid { params = append(params, fmt.Sprintf("type='%s'", p.Type)) } if p.Sender != "" { params = append(params, fmt.Sprintf("sender='%s'", p.Sender)) } if p.Path != "" { params = append(params, fmt.Sprintf("path='%s'", p.Path)) } if p.Interface != "" { params = append(params, fmt.Sprintf("interface='%s'", p.Interface)) } if p.Member != "" { params = append(params, fmt.Sprintf("member='%s'", p.Member)) } if p.Arg0 != "" { params = append(params, fmt.Sprintf("arg0='%s'", p.Arg0)) } return strings.Join(params, ",") } func (p *MatchRule) Match(msg *Message) bool { if p.Type != TypeInvalid && p.Type != msg.Type { return false } if p.Sender != "" { if !(p.Sender == msg.Sender || p.senderNameOwner == msg.Sender) { return false } } if p.Path != "" && p.Path != msg.Path { return false } if p.Interface != "" && p.Interface != msg.Interface { return false } if p.Member != "" && p.Member != msg.Member { return false } if p.Arg0 != "" { var arg0 string if err := msg.Args(&arg0); err != nil || arg0 != p.Arg0 { return false } } return true } usensord-1.0+14.04.20140404/dbus/decoder.go0000644000015301777760000002724312317524417020517 0ustar pbusernogroup00000000000000package dbus import ( "encoding/binary" "errors" "math" "reflect" ) type decoder struct { signature Signature data []byte order binary.ByteOrder dataOffset, sigOffset int } var ( bufferOverrunError = errors.New("Buffer too small") signatureOverrunError = errors.New("Signature too small") ) func newDecoder(signature Signature, data []byte, order binary.ByteOrder) *decoder { return &decoder{signature: signature, data: data, order: order} } func (self *decoder) align(alignment int) { inc := -self.dataOffset % alignment if inc < 0 { inc += alignment } self.dataOffset += inc } func (self *decoder) Decode(args ...interface{}) error { for _, arg := range args { v := reflect.ValueOf(arg) // We expect to be given pointers here, so the caller // can see the decoded values. if v.Kind() != reflect.Ptr { return errors.New("arguments to Decode should be pointers") } if err := self.decodeValue(v.Elem()); err != nil { return err } } return nil } func (self *decoder) HasMore() bool { return self.sigOffset < len(self.signature) } func (self *decoder) Remainder() []byte { return self.data[self.dataOffset:] } func (self *decoder) readByte() (byte, error) { if len(self.data) < self.dataOffset+1 { return 0, bufferOverrunError } value := self.data[self.dataOffset] self.dataOffset += 1 return value, nil } func (self *decoder) readInt16() (int16, error) { self.align(2) if len(self.data) < self.dataOffset+2 { return 0, bufferOverrunError } value := int16(self.order.Uint16(self.data[self.dataOffset:])) self.dataOffset += 2 return value, nil } func (self *decoder) readUint16() (uint16, error) { self.align(2) if len(self.data) < self.dataOffset+2 { return 0, bufferOverrunError } value := self.order.Uint16(self.data[self.dataOffset:]) self.dataOffset += 2 return value, nil } func (self *decoder) readInt32() (int32, error) { self.align(4) if len(self.data) < self.dataOffset+4 { return 0, bufferOverrunError } value := int32(self.order.Uint32(self.data[self.dataOffset:])) self.dataOffset += 4 return value, nil } func (self *decoder) readUint32() (uint32, error) { self.align(4) if len(self.data) < self.dataOffset+4 { return 0, bufferOverrunError } value := self.order.Uint32(self.data[self.dataOffset:]) self.dataOffset += 4 return value, nil } func (self *decoder) readInt64() (int64, error) { self.align(8) if len(self.data) < self.dataOffset+8 { return 0, bufferOverrunError } value := int64(self.order.Uint64(self.data[self.dataOffset:])) self.dataOffset += 8 return value, nil } func (self *decoder) readUint64() (uint64, error) { self.align(8) if len(self.data) < self.dataOffset+8 { return 0, bufferOverrunError } value := self.order.Uint64(self.data[self.dataOffset:]) self.dataOffset += 8 return value, nil } func (self *decoder) readFloat64() (float64, error) { value, err := self.readUint64() return math.Float64frombits(value), err } func (self *decoder) readString() (string, error) { length, err := self.readUint32() if err != nil { return "", err } // One extra byte for null termination if len(self.data) < self.dataOffset+int(length)+1 { return "", bufferOverrunError } value := string(self.data[self.dataOffset : self.dataOffset+int(length)]) self.dataOffset += int(length) + 1 return value, nil } func (self *decoder) readSignature() (Signature, error) { length, err := self.readByte() if err != nil { return "", err } // One extra byte for null termination if len(self.data) < self.dataOffset+int(length)+1 { return "", bufferOverrunError } value := Signature(self.data[self.dataOffset : self.dataOffset+int(length)]) self.dataOffset += int(length) + 1 return value, nil } func (self *decoder) decodeValue(v reflect.Value) error { if len(self.signature) < self.sigOffset { return signatureOverrunError } sigCode := self.signature[self.sigOffset] self.sigOffset += 1 switch sigCode { case 'y': value, err := self.readByte() if err != nil { return err } switch { case v.Kind() == reflect.Uint8: v.SetUint(uint64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'b': value, err := self.readUint32() if err != nil { return err } switch { case v.Kind() == reflect.Bool: v.SetBool(value != 0) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value != 0)) return nil } case 'n': value, err := self.readInt16() if err != nil { return err } switch { case v.Kind() == reflect.Int16: v.SetInt(int64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'q': value, err := self.readUint16() if err != nil { return err } switch { case v.Kind() == reflect.Uint16: v.SetUint(uint64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'i': value, err := self.readInt32() if err != nil { return err } switch { case v.Kind() == reflect.Int32: v.SetInt(int64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'u': value, err := self.readUint32() if err != nil { return err } switch { case v.Kind() == reflect.Uint32: v.SetUint(uint64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'x': value, err := self.readInt64() if err != nil { return err } switch { case v.Kind() == reflect.Int64: v.SetInt(int64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 't': value, err := self.readUint64() if err != nil { return err } switch { case v.Kind() == reflect.Uint64: v.SetUint(uint64(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'd': value, err := self.readFloat64() if err != nil { return err } switch { case v.Kind() == reflect.Float64: v.SetFloat(value) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 's': value, err := self.readString() if err != nil { return err } switch { case v.Kind() == reflect.String: v.SetString(value) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'o': value, err := self.readString() if err != nil { return err } switch { case v.Kind() == reflect.String: v.SetString(value) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(ObjectPath(value))) return nil } case 'g': value, err := self.readSignature() if err != nil { return err } switch { case v.Kind() == reflect.String: v.SetString(string(value)) return nil case typeBlankInterface.AssignableTo(v.Type()): v.Set(reflect.ValueOf(value)) return nil } case 'a': length, err := self.readUint32() if err != nil { return err } elemSigOffset := self.sigOffset afterElemOffset, err := self.signature.NextType(elemSigOffset) if err != nil { return err } // Adjust data offset so we are aligned to read array // elements. Anything with an alignment of 4 or less // will already be aligned due to reading the length. switch self.signature[self.sigOffset] { case 'x', 't', 'd', '(', '{': self.align(8) } arrayEnd := self.dataOffset + int(length) if len(self.data) < arrayEnd { return bufferOverrunError } switch { case v.Kind() == reflect.Array: for i := 0; self.dataOffset < arrayEnd; i++ { // Reset signature offset to the array element. self.sigOffset = elemSigOffset if err := self.decodeValue(v.Index(i)); err != nil { return err } } self.sigOffset = afterElemOffset return nil case v.Kind() == reflect.Slice: if v.IsNil() { v.Set(reflect.MakeSlice(v.Type(), 0, 0)) } v.SetLen(0) for self.dataOffset < arrayEnd { // Reset signature offset to the array element. self.sigOffset = elemSigOffset elem := reflect.New(v.Type().Elem()).Elem() if err := self.decodeValue(elem); err != nil { return err } v.Set(reflect.Append(v, elem)) } self.sigOffset = afterElemOffset return nil case v.Kind() == reflect.Map: if self.signature[elemSigOffset] != '{' { return errors.New("Expected type code '{' but got " + string(self.signature[elemSigOffset]) + " when decoding to map") } v.Set(reflect.MakeMap(v.Type())) for self.dataOffset < arrayEnd { self.align(8) // Reset signature offset to first // item in dictionary entry: self.sigOffset = elemSigOffset + 1 key := reflect.New(v.Type().Key()).Elem() value := reflect.New(v.Type().Elem()).Elem() if err := self.decodeValue(key); err != nil { return err } if err := self.decodeValue(value); err != nil { return err } v.SetMapIndex(key, value) } self.sigOffset = afterElemOffset return nil case typeBlankInterface.AssignableTo(v.Type()): // XXX: Need to support maps here (i.e. next // signature char is '{') array := make([]interface{}, 0) for self.dataOffset < arrayEnd { // Reset signature offset to the array element. self.sigOffset = elemSigOffset var elem interface{} if err := self.decodeValue(reflect.ValueOf(&elem).Elem()); err != nil { return err } array = append(array, elem) } v.Set(reflect.ValueOf(array)) return nil } case '(': self.align(8) // Do we have a pointer to a struct? if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct { if v.IsNil() { v.Set(reflect.New(v.Type().Elem())) } v = v.Elem() } switch { case v.Kind() == reflect.Struct: for i := 0; i < v.NumField() && self.sigOffset < len(self.signature) && self.signature[self.sigOffset] != ')'; i++ { if err := self.decodeValue(v.Field(i)); err != nil { return err } } if self.sigOffset >= len(self.signature) || self.signature[self.sigOffset] != ')' { return signatureOverrunError } // move past the closing parentheses self.sigOffset += 1 return nil case typeBlankInterface.AssignableTo(v.Type()): // Decode as a slice of interface{} values. s := make([]interface{}, 0) for self.sigOffset < len(self.signature) && self.signature[self.sigOffset] != ')' { var field interface{} if err := self.decodeValue(reflect.ValueOf(&field).Elem()); err != nil { return err } s = append(s, field) } v.Set(reflect.ValueOf(s)) return nil } case 'v': var variant *Variant switch { case v.Kind() == reflect.Ptr && v.Type().Elem() == typeVariant: if v.IsNil() { variant = &Variant{} v.Set(reflect.ValueOf(variant)) } else { variant = v.Interface().(*Variant) } case v.Type() == typeVariant: variant = v.Addr().Interface().(*Variant) case typeBlankInterface.AssignableTo(v.Type()): variant = &Variant{} v.Set(reflect.ValueOf(variant)) } if variant != nil { signature, err := self.readSignature() if err != nil { return err } // Decode the variant value through a sub-decoder. variantDec := decoder{ signature: signature, data: self.data, order: self.order, dataOffset: self.dataOffset, sigOffset: 0} if err := variantDec.decodeValue(reflect.ValueOf(&variant.Value).Elem()); err != nil { return err } // Decoding continues after the variant value. self.dataOffset = variantDec.dataOffset return nil } } return errors.New("Could not decode " + string(sigCode) + " to " + v.Type().String()) } usensord-1.0+14.04.20140404/dbus/encoder.go0000644000015301777760000001176212317524417020530 0ustar pbusernogroup00000000000000package dbus import ( "bytes" "encoding/binary" "errors" "reflect" ) type encoder struct { signature Signature data bytes.Buffer order binary.ByteOrder } func newEncoder(signature Signature, data []byte, order binary.ByteOrder) *encoder { enc := &encoder{signature: signature, order: order} if data != nil { enc.data.Write(data) } return enc } func (self *encoder) align(alignment int) { for self.data.Len()%alignment != 0 { self.data.WriteByte(0) } } func (self *encoder) Append(args ...interface{}) error { for _, arg := range args { if err := self.appendValue(reflect.ValueOf(arg)); err != nil { return err } } return nil } func (self *encoder) alignForType(t reflect.Type) error { // If type matches the ObjectPather interface, treat like an // ObjectPath if t.AssignableTo(typeObjectPather) { t = reflect.TypeOf(ObjectPath("")) } // dereference pointers for t.Kind() == reflect.Ptr { t = t.Elem() } switch t.Kind() { case reflect.Uint8: self.align(1) case reflect.Int16, reflect.Uint16: self.align(2) case reflect.Bool, reflect.Int32, reflect.Uint32, reflect.Array, reflect.Slice, reflect.Map: self.align(4) case reflect.Int64, reflect.Uint64, reflect.Float64: self.align(8) case reflect.String: if t == typeSignature { self.align(1) } else { self.align(4) } case reflect.Struct: if t == typeVariant { self.align(1) } else { self.align(8) } default: return errors.New("Don't know how to align " + t.String()) } return nil } func (self *encoder) appendValue(v reflect.Value) error { signature, err := SignatureOf(v.Type()) if err != nil { return err } self.signature += signature // Convert ObjectPather values to ObjectPath strings if v.Type().AssignableTo(typeObjectPather) { path := v.Interface().(ObjectPather).ObjectPath() v = reflect.ValueOf(path) } // We want pointer values here, rather than the pointers themselves. for v.Kind() == reflect.Ptr { v = v.Elem() } self.alignForType(v.Type()) switch v.Kind() { case reflect.Uint8: self.data.WriteByte(byte(v.Uint())) return nil case reflect.Bool: var uintval uint32 if v.Bool() { uintval = 1 } else { uintval = 0 } binary.Write(&self.data, self.order, uintval) return nil case reflect.Int16: binary.Write(&self.data, self.order, int16(v.Int())) return nil case reflect.Uint16: binary.Write(&self.data, self.order, uint16(v.Uint())) return nil case reflect.Int32: binary.Write(&self.data, self.order, int32(v.Int())) return nil case reflect.Uint32: binary.Write(&self.data, self.order, uint32(v.Uint())) return nil case reflect.Int64: binary.Write(&self.data, self.order, int64(v.Int())) return nil case reflect.Uint64: binary.Write(&self.data, self.order, uint64(v.Uint())) return nil case reflect.Float64: binary.Write(&self.data, self.order, float64(v.Float())) return nil case reflect.String: s := v.String() // Signatures only use a single byte for the length. if v.Type() == typeSignature { self.data.WriteByte(byte(len(s))) } else { binary.Write(&self.data, self.order, uint32(len(s))) } self.data.Write([]byte(s)) self.data.WriteByte(0) return nil case reflect.Array, reflect.Slice: // Marshal array contents to a separate buffer so we // can find its length. var content encoder content.order = self.order for i := 0; i < v.Len(); i++ { if err := content.appendValue(v.Index(i)); err != nil { return err } } binary.Write(&self.data, self.order, uint32(content.data.Len())) self.alignForType(v.Type().Elem()) self.data.Write(content.data.Bytes()) return nil case reflect.Map: // Marshal array contents to a separate buffer so we // can find its length. var content encoder content.order = self.order for _, key := range v.MapKeys() { content.align(8) if err := content.appendValue(key); err != nil { return err } if err := content.appendValue(v.MapIndex(key)); err != nil { return err } } binary.Write(&self.data, self.order, uint32(content.data.Len())) self.align(8) // alignment of DICT_ENTRY self.data.Write(content.data.Bytes()) return nil case reflect.Struct: if v.Type() == typeVariant { variant := v.Interface().(Variant) variantSig, err := variant.GetVariantSignature() if err != nil { return err } // Save the signature, so we don't add the // typecodes for the variant value to the // signature. savedSig := self.signature if err := self.appendValue(reflect.ValueOf(variantSig)); err != nil { return err } if err := self.appendValue(reflect.ValueOf(variant.Value)); err != nil { return err } self.signature = savedSig return nil } // XXX: save and restore the signature, since we wrote // out the entire struct signature previously. savedSig := self.signature for i := 0; i != v.NumField(); i++ { if err := self.appendValue(v.Field(i)); err != nil { return err } } self.signature = savedSig return nil } return errors.New("Could not marshal " + v.Type().String()) } usensord-1.0+14.04.20140404/dbus/message.go0000644000015301777760000002441312317524417020532 0ustar pbusernogroup00000000000000package dbus import ( "encoding/binary" "errors" "io" ) // See the D-Bus tutorial for information about message types. // http://dbus.freedesktop.org/doc/dbus-tutorial.html#messages type MessageType uint8 const ( TypeInvalid MessageType = iota TypeMethodCall TypeMethodReturn TypeError TypeSignal ) var messageTypeString = map[MessageType]string{ TypeInvalid: "invalid", TypeMethodCall: "method_call", TypeMethodReturn: "method_return", TypeSignal: "signal", TypeError: "error", } func (t MessageType) String() string { return messageTypeString[t] } type MessageFlag uint8 const ( // When applied to method call messages, indicates that no // method return or error message is expected. FlagNoReplyExpected MessageFlag = 1 << iota // Indicates that the message should not autostart the service // if the destination service is not currently running. FlagNoAutoStart ) // Message represents a D-Bus message. // // It is used to both construct messages for sending on the bus, and // to represent messages received from the bus. // // There type does not use locks to protect its internal members. // Instead, it is expected that users either (a) only modify a message // from a single thread (usually the case when constructing a message // to send), or (b) treat the message as read only (usually the case // when processing a received message). type Message struct { order binary.ByteOrder Type MessageType Flags MessageFlag Protocol uint8 serial uint32 // header fields Path ObjectPath Interface string Member string ErrorName string replySerial uint32 Dest string Sender string sig Signature body []byte } // Create a new message with Flags == 0 and Protocol == 1. func newMessage() *Message { msg := new(Message) msg.order = binary.LittleEndian msg.serial = 0 msg.replySerial = 0 msg.Flags = 0 msg.Protocol = 1 msg.body = []byte{} return msg } // NewMethodCallMessage creates a method call message. // // Method arguments can be appended to the message via AppendArgs. func NewMethodCallMessage(destination string, path ObjectPath, iface string, member string) *Message { msg := newMessage() msg.Type = TypeMethodCall msg.Dest = destination msg.Path = path msg.Interface = iface msg.Member = member return msg } // NewMethodReturnMessage creates a method return message. // // This message type represents a successful reply to the method call // message passed as an argument. // // Return arguments should be appended to the message via AppendArgs. func NewMethodReturnMessage(methodCall *Message) *Message { if methodCall.serial == 0 { panic("methodCall.serial == 0") } if methodCall.Type != TypeMethodCall { panic("replies should be sent in response to method calls") } msg := newMessage() msg.Type = TypeMethodReturn msg.replySerial = methodCall.serial msg.Dest = methodCall.Sender return msg } // NewSignalMessage creates a signal message. // // Signal messages are used to broadcast messages to interested // listeners. // // Arguments can be appended to the signal with AppendArgs. func NewSignalMessage(path ObjectPath, iface string, member string) *Message { msg := newMessage() msg.Type = TypeSignal msg.Path = path msg.Interface = iface msg.Member = member return msg } // NewErrorMessage creates an error message. // // This message type should be sent in response to a method call // message in the case of a failure. func NewErrorMessage(methodCall *Message, errorName string, message string) *Message { if methodCall.serial == 0 { panic("methodCall.serial == 0") } if methodCall.Type != TypeMethodCall { panic("errors should be sent in response to method calls") } msg := newMessage() msg.Type = TypeError msg.replySerial = methodCall.serial msg.Dest = methodCall.Sender msg.ErrorName = errorName if err := msg.AppendArgs(message); err != nil { panic(err) } return msg } func (p *Message) setSerial(serial uint32) { if p.serial != 0 { panic("Message already has a serial number") } p.serial = serial } // AppendArgs appends arguments to a message. // // Native Go types are converted to equivalent D-Bus types: // - uint8 represents a byte. // - bool represents a boolean value. // - int16, uint16, int32, uint32, int64 and uint64 represent the // equivalent integer types. // - string represents a string. // - The dbus.ObjectPath type or any type conforming to the // dbus.ObjectPather interface represents an object path. // - arrays and slices represent arrays of the element type. // - maps represent equivalent D-Bus dictionaries. // - structures represent a structure comprising the public members. // - the dbus.Variant type represents a variant. // // If an argument can not be serialised in the message, an error is // returned. When multiple arguments are being appended, it is // possible for some arguments to be successfully appended before the // error is generated. func (p *Message) AppendArgs(args ...interface{}) error { enc := newEncoder(p.sig, p.body, p.order) if err := enc.Append(args...); err != nil { return err } p.sig = enc.signature p.body = enc.data.Bytes() return nil } // Args decodes one or more arguments from the message. // // The arguments should be pointers to variables used to hold the // arguments. If the type of the argument does not match the // corresponding argument in the message, then an error will be // raised. // // As a special case, arguments may be decoded into a blank interface // value. This may result in a less useful decoded version though // (e.g. an "ai" message argument would be decoded as []interface{} // instead of []int32). func (p *Message) Args(args ...interface{}) error { dec := newDecoder(p.sig, p.body, p.order) return dec.Decode(args...) } // AllArgs returns all arguments in the message. // // This method is equivalent to calling Args and passing pointers // to blank interface values for each message argument. func (p *Message) AllArgs() []interface{} { dec := newDecoder(p.sig, p.body, p.order) args := make([]interface{}, 0) for dec.HasMore() { var arg interface{} if err := dec.Decode(&arg); err != nil { panic(err) } args = append(args, arg) } return args } // AsError creates a Go error value corresponding to a message. // // This method should only be called on messages of the error type. func (p *Message) AsError() error { if p.Type != TypeError { panic("Only messages of type 'error' can be converted to an error") } var errorMessage string if err := p.Args(&errorMessage); err != nil { // Ignore error errorMessage = "" } return &Error{p.ErrorName, errorMessage} } type headerField struct { Code byte Value Variant } func readMessage(r io.Reader) (*Message, error) { header := make([]byte, 16) if n, err := r.Read(header); n < len(header) { if err == nil { err = errors.New("Could not read message header") } return nil, err } msg := newMessage() switch header[0] { case 'l': msg.order = binary.LittleEndian case 'B': msg.order = binary.BigEndian default: return nil, errors.New("Unknown message endianness: " + string(header[0])) } dec := newDecoder("yyyyuuu", header, msg.order) var msgOrder byte var msgBodyLength, headerFieldsLength uint32 if err := dec.Decode(&msgOrder, &msg.Type, &msg.Flags, &msg.Protocol, &msgBodyLength, &msg.serial, &headerFieldsLength); err != nil { return nil, err } // Read out and decode the header fields, plus the padding to // 8 bytes. padding := -(len(header) + int(headerFieldsLength)) % 8 if padding < 0 { padding += 8 } headerFields := make([]byte, 16+int(headerFieldsLength)+padding) copy(headerFields[:16], header) if n, err := r.Read(headerFields[16:]); n < len(headerFields)-16 { if err == nil { err = errors.New("Could not read message header fields") } return nil, err } dec = newDecoder("a(yv)", headerFields, msg.order) dec.dataOffset += 12 fields := make([]headerField, 0, 10) if err := dec.Decode(&fields); err != nil { return nil, err } for _, field := range fields { switch field.Code { case 1: msg.Path = field.Value.Value.(ObjectPath) case 2: msg.Interface = field.Value.Value.(string) case 3: msg.Member = field.Value.Value.(string) case 4: msg.ErrorName = field.Value.Value.(string) case 5: msg.replySerial = field.Value.Value.(uint32) case 6: msg.Dest = field.Value.Value.(string) case 7: msg.Sender = field.Value.Value.(string) case 8: msg.sig = field.Value.Value.(Signature) } } msg.body = make([]byte, msgBodyLength) if n, err := r.Read(msg.body); n < len(msg.body) { if err == nil { err = errors.New("Could not read message body") } return nil, err } return msg, nil } // WriteTo serialises the message and writes it to the given writer. func (p *Message) WriteTo(w io.Writer) (int64, error) { fields := make([]headerField, 0, 10) if p.Path != "" { fields = append(fields, headerField{1, Variant{p.Path}}) } if p.Interface != "" { fields = append(fields, headerField{2, Variant{p.Interface}}) } if p.Member != "" { fields = append(fields, headerField{3, Variant{p.Member}}) } if p.ErrorName != "" { fields = append(fields, headerField{4, Variant{p.ErrorName}}) } if p.replySerial != 0 { fields = append(fields, headerField{5, Variant{p.replySerial}}) } if p.Dest != "" { fields = append(fields, headerField{6, Variant{p.Dest}}) } if p.Sender != "" { fields = append(fields, headerField{7, Variant{p.Sender}}) } if p.sig != "" { fields = append(fields, headerField{8, Variant{p.sig}}) } var orderTag byte switch p.order { case binary.LittleEndian: orderTag = 'l' case binary.BigEndian: orderTag = 'B' default: return 0, errors.New("Unknown byte order: " + p.order.String()) } header := newEncoder("", nil, p.order) if err := header.Append(orderTag, byte(p.Type), byte(p.Flags), byte(p.Protocol), uint32(len(p.body)), p.serial, fields); err != nil { return 0, err } // Add alignment bytes for body header.align(8) m, err := w.Write(header.data.Bytes()) if err != nil { return int64(m), err } else if m != header.data.Len() { return int64(m), errors.New("Failed to write complete message header") } n, err := w.Write(p.body) if err != nil { return int64(m + n), err } else if n != len(p.body) { return int64(m + n), errors.New("Failed to write complete message body") } return int64(m + n), nil } usensord-1.0+14.04.20140404/dbus/introspect_test.go0000644000015301777760000000400112317524417022326 0ustar pbusernogroup00000000000000// +build ignore package dbus import ( . "launchpad.net/gocheck" ) var introStr = ` ` func (s *S) TestIntrospect(c *C) { intro, err := NewIntrospect(introStr) c.Assert(err, Equals, nil) c.Assert(intro, Not(Equals), nil) intf := intro.GetInterfaceData("org.freedesktop.SampleInterface") c.Assert(intf, Not(Equals), nil) c.Check(intf.GetName(), Equals, "org.freedesktop.SampleInterface") meth := intf.GetMethodData("Frobate") c.Assert(meth, Not(Equals), nil) c.Check(meth.GetOutSignature(), Equals, Signature("sa{us}")) nilmeth := intf.GetMethodData("Hoo") // unknown method name c.Check(nilmeth, Equals, nil) signal := intf.GetSignalData("Changed") c.Assert(signal, Not(Equals), nil) c.Check(signal.GetSignature(), Equals, Signature("b")) nilsignal := intf.GetSignalData("Hoo") // unknown signal name c.Check(nilsignal, Equals, nil) } usensord-1.0+14.04.20140404/dbus/encoder_test.go0000644000015301777760000002015012317524417021556 0ustar pbusernogroup00000000000000package dbus import ( "encoding/binary" . "launchpad.net/gocheck" ) func (s *S) TestEncoderAlign(c *C) { enc := newEncoder("", nil, binary.LittleEndian) enc.data.WriteByte(1) enc.align(1) c.Check(enc.data.Bytes(), DeepEquals, []byte{1}) enc.align(2) c.Check(enc.data.Bytes(), DeepEquals, []byte{1, 0}) enc.align(4) c.Check(enc.data.Bytes(), DeepEquals, []byte{1, 0, 0, 0}) enc.align(8) c.Check(enc.data.Bytes(), DeepEquals, []byte{1, 0, 0, 0, 0, 0, 0, 0}) } func (s *S) TestEncoderAppendByte(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(byte(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("y")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42}) } func (s *S) TestEncoderAppendBoolean(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(true); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("b")) c.Check(enc.data.Bytes(), DeepEquals, []byte{1, 0, 0, 0}) } func (s *S) TestEncoderAppendInt16(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(int16(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("n")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0}) } func (s *S) TestEncoderAppendUint16(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(uint16(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("q")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0}) } func (s *S) TestEncoderAppendInt32(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(int32(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("i")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0, 0, 0}) } func (s *S) TestEncoderAppendUint32(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(uint32(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("u")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0, 0, 0}) } func (s *S) TestEncoderAppendInt64(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(int64(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("x")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0, 0, 0, 0, 0, 0, 0}) } func (s *S) TestEncoderAppendUint64(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(uint64(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("t")) c.Check(enc.data.Bytes(), DeepEquals, []byte{42, 0, 0, 0, 0, 0, 0, 0}) } func (s *S) TestEncoderAppendFloat64(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(float64(42.0)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("d")) c.Check(enc.data.Bytes(), DeepEquals, []byte{0, 0, 0, 0, 0, 0, 69, 64}) } func (s *S) TestEncoderAppendString(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append("hello"); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("s")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 5, 0, 0, 0, // Length 'h', 'e', 'l', 'l', 'o', // "hello" 0}) // nul termination } func (s *S) TestEncoderAppendObjectPath(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(ObjectPath("/foo")); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("o")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 4, 0, 0, 0, // Length '/', 'f', 'o', 'o', // ObjectPath("/foo") 0}) // nul termination } type testObject struct{} func (f *testObject) ObjectPath() ObjectPath { return ObjectPath("/foo") } func (s *S) TestEncoderAppendObject(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(&testObject{}); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("o")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 4, 0, 0, 0, // Length '/', 'f', 'o', 'o', // ObjectPath("/foo") 0}) // nul termination } func (s *S) TestEncoderAppendSignature(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(Signature("a{si}")); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("g")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 5, // Length 'a', '{', 's', 'i', '}', // Signature("a{si}") 0}) // nul termination } func (s *S) TestEncoderAppendArray(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append([]int32{42, 420}); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("ai")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 8, 0, 0, 0, // Length 42, 0, 0, 0, // int32(42) 164, 1, 0, 0}) // int32(420) } func (s *S) TestEncoderAppendArrayLengthAlignment(c *C) { enc := newEncoder("", nil, binary.LittleEndian) // append a byte, which means we are no longer aligned. c.Assert(enc.Append(byte(1)), Equals, nil) // Now create an array. c.Check(enc.Append([]uint32{42}), Equals, nil) c.Check(enc.signature, Equals, Signature("yau")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 1, // byte(1) 0, 0, 0, // padding 4, 0, 0, 0, // array length 42, 0, 0, 0}) // uint32(42) } func (s *S) TestEncoderAppendArrayPaddingAfterLength(c *C) { enc := newEncoder("", nil, binary.LittleEndian) // Now create an array with alignment 8 values. c.Check(enc.Append([]int64{42}), Equals, nil) c.Check(enc.signature, Equals, Signature("ax")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 8, 0, 0, 0, // array length (not including padding) 0, 0, 0, 0, // padding 42, 0, 0, 0, 0, 0, 0, 0}) // int64(42) // The padding is needed, even if there are no elements in the array. enc = newEncoder("", nil, binary.LittleEndian) c.Check(enc.Append([]int64{}), Equals, nil) c.Check(enc.signature, Equals, Signature("ax")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 0, 0, 0, 0, // array length (not including padding) 0, 0, 0, 0}) // padding } func (s *S) TestEncoderAppendMap(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(map[string]bool{"true": true}); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("a{sb}")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 16, 0, 0, 0, // array content length 0, 0, 0, 0, // padding to 8 bytes 4, 0, 0, 0, 't', 'r', 'u', 'e', 0, // "true" 0, 0, 0, // padding to 4 bytes 1, 0, 0, 0}) // true } func (s *S) TestEncoderAppendMapAlignment(c *C) { enc := newEncoder("", nil, binary.LittleEndian) // append a byte, which means we are no longer aligned. c.Assert(enc.Append(byte(1)), Equals, nil) c.Check(enc.Append(map[string]bool{"true": true}), Equals, nil) c.Check(enc.signature, Equals, Signature("ya{sb}")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 1, // byte(1) 0, 0, 0, // padding 16, 0, 0, 0, // array content length 4, 0, 0, 0, 't', 'r', 'u', 'e', 0, // "true" 0, 0, 0, // padding to 4 bytes 1, 0, 0, 0}) // true } func (s *S) TestEncoderAppendStruct(c *C) { enc := newEncoder("", nil, binary.LittleEndian) type sample struct { one int32 two string } if err := enc.Append(&sample{42, "hello"}); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("(is)")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 42, 0, 0, 0, 5, 0, 0, 0, 'h', 'e', 'l', 'l', 'o', 0}) } func (s *S) TestEncoderAppendVariant(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(&Variant{int32(42)}); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("v")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 1, 'i', 0, // Signature("i") 0, // padding to 4 bytes 42, 0, 0, 0}) // int32(42) } func (s *S) TestEncoderAppendAlignment(c *C) { enc := newEncoder("", nil, binary.LittleEndian) if err := enc.Append(byte(42), int16(42), true, int32(42), int64(42)); err != nil { c.Error(err) } c.Check(enc.signature, Equals, Signature("ynbix")) c.Check(enc.data.Bytes(), DeepEquals, []byte{ 42, // byte(42) 0, // padding to 2 bytes 42, 0, // int16(42) 1, 0, 0, 0, // true 42, 0, 0, 0, // int32(42) 0, 0, 0, 0, // padding to 8 bytes 42, 0, 0, 0, 0, 0, 0, 0}) // int64(42) } usensord-1.0+14.04.20140404/dbus/transport_test.go0000644000015301777760000001005312317524417022174 0ustar pbusernogroup00000000000000package dbus import ( "bytes" "errors" "fmt" "io/ioutil" . "launchpad.net/gocheck" "net" "path" ) func (s *S) TestNewTransportUnix(c *C) { trans, err := newTransport("unix:path=/tmp/dbus%3dsock") c.Check(err, Equals, nil) unixTrans, ok := trans.(*unixTransport) c.Check(ok, Equals, true) c.Check(unixTrans.Address, Equals, "/tmp/dbus=sock") // And for abstract namespace sockets: trans, err = newTransport("unix:abstract=/tmp/dbus%3dsock") c.Check(err, Equals, nil) unixTrans, ok = trans.(*unixTransport) c.Check(ok, Equals, true) c.Check(unixTrans.Address, Equals, "@/tmp/dbus=sock") } func (s *S) TestUnixTransportDial(c *C) { socketFile := path.Join(c.MkDir(), "bus.sock") listener, err := net.Listen("unix", socketFile) c.Assert(err, IsNil) trans, err := newTransport(fmt.Sprintf("unix:path=%s", socketFile)) c.Assert(err, IsNil) errChan := make(chan error, 1) go func() { conn, err := listener.Accept() if err == nil { conn.Close() } errChan <- err }() conn, err := trans.Dial() c.Assert(err, IsNil) conn.Close() // Was the other end of the connection established correctly? err = <-errChan c.Check(err, IsNil) listener.Close() } func (s *S) TestNewTransportTcp(c *C) { trans, err := newTransport("tcp:host=localhost,port=4444") c.Check(err, Equals, nil) tcpTrans, ok := trans.(*tcpTransport) c.Check(ok, Equals, true) c.Check(tcpTrans.Address, Equals, "localhost:4444") c.Check(tcpTrans.Family, Equals, "tcp4") // And with explicit family: trans, err = newTransport("tcp:host=localhost,port=4444,family=ipv4") c.Check(err, Equals, nil) tcpTrans, ok = trans.(*tcpTransport) c.Check(ok, Equals, true) c.Check(tcpTrans.Address, Equals, "localhost:4444") c.Check(tcpTrans.Family, Equals, "tcp4") trans, err = newTransport("tcp:host=localhost,port=4444,family=ipv6") c.Check(err, Equals, nil) tcpTrans, ok = trans.(*tcpTransport) c.Check(ok, Equals, true) c.Check(tcpTrans.Address, Equals, "localhost:4444") c.Check(tcpTrans.Family, Equals, "tcp6") } func (s *S) TestTcpTransportDial(c *C) { listener, err := net.Listen("tcp", "127.0.0.1:0") c.Assert(err, IsNil) addr := listener.Addr().(*net.TCPAddr) address := fmt.Sprintf("tcp:host=%s,port=%d", addr.IP.String(), addr.Port) trans, err := newTransport(address) c.Assert(err, IsNil) errChan := make(chan error, 1) go func() { conn, err := listener.Accept() if err == nil { conn.Close() } errChan <- err }() conn, err := trans.Dial() c.Assert(err, IsNil) conn.Close() // Was the other end of the connection established correctly? err = <-errChan c.Check(err, IsNil) listener.Close() } func (s *S) TestNewTransportNonceTcp(c *C) { trans, err := newTransport("nonce-tcp:host=localhost,port=4444,noncefile=/tmp/foo") c.Check(err, Equals, nil) nonceTcpTrans, ok := trans.(*nonceTcpTransport) c.Check(ok, Equals, true) c.Check(nonceTcpTrans.Address, Equals, "localhost:4444") c.Check(nonceTcpTrans.Family, Equals, "tcp4") c.Check(nonceTcpTrans.NonceFile, Equals, "/tmp/foo") } func (s *S) TestNonceTcpTransportDial(c *C) { nonceFile := path.Join(c.MkDir(), "nonce-file") nonceData := []byte("nonce-data") c.Assert(ioutil.WriteFile(nonceFile, nonceData, 0600), IsNil) listener, err := net.Listen("tcp", "127.0.0.1:0") c.Assert(err, IsNil) addr := listener.Addr().(*net.TCPAddr) address := fmt.Sprintf("nonce-tcp:host=%s,port=%d,noncefile=%s", addr.IP.String(), addr.Port, nonceFile) trans, err := newTransport(address) c.Assert(err, IsNil) errChan := make(chan error, 1) go func() { conn, err := listener.Accept() if err != nil { errChan <- err return } // The client starts by writing the nonce data to the socket. data := make([]byte, 4096) n, err := conn.Read(data) if err != nil { conn.Close() errChan <- err return } if !bytes.Equal(data[:n], nonceData) { err = errors.New("Did not receive nonce data") } conn.Close() errChan <- err }() conn, err := trans.Dial() c.Assert(err, IsNil) conn.Close() // Was the other end of the connection established correctly? err = <-errChan c.Check(err, IsNil) listener.Close() } usensord-1.0+14.04.20140404/dbus/transport.go0000644000015301777760000000520012317524417021133 0ustar pbusernogroup00000000000000package dbus import ( "errors" "io/ioutil" "net" "net/url" "strings" ) type transport interface { Dial() (net.Conn, error) } func newTransport(address string) (transport, error) { if len(address) == 0 { return nil, errors.New("Unknown address type") } // Split the address into transport type and options. transportType := address[:strings.Index(address, ":")] options := make(map[string]string) for _, option := range strings.Split(address[len(transportType)+1:], ",") { pair := strings.SplitN(option, "=", 2) key, err := url.QueryUnescape(pair[0]) if err != nil { return nil, err } value, err := url.QueryUnescape(pair[1]) if err != nil { return nil, err } options[key] = value } switch transportType { case "unix": if abstract, ok := options["abstract"]; ok { return &unixTransport{"@" + abstract}, nil } else if path, ok := options["path"]; ok { return &unixTransport{path}, nil } else { return nil, errors.New("unix transport requires 'path' or 'abstract' options") } case "tcp", "nonce-tcp": address := options["host"] + ":" + options["port"] var family string switch options["family"] { case "", "ipv4": family = "tcp4" case "ipv6": family = "tcp6" default: return nil, errors.New("Unknown family for tcp transport: " + options["family"]) } if transportType == "tcp" { return &tcpTransport{address, family}, nil } else { nonceFile := options["noncefile"] return &nonceTcpTransport{address, family, nonceFile}, nil } // These can be implemented later as needed case "launchd": // Perform newTransport() on contents of // options["env"] environment variable case "systemd": // Only used when systemd is starting the message bus, // so probably not needed in a client library. case "unixexec": // exec a process with a socket hooked to stdin/stdout } return nil, errors.New("Unhandled transport type " + transportType) } type unixTransport struct { Address string } func (trans *unixTransport) Dial() (net.Conn, error) { return net.Dial("unix", trans.Address) } type tcpTransport struct { Address, Family string } func (trans *tcpTransport) Dial() (net.Conn, error) { return net.Dial(trans.Family, trans.Address) } type nonceTcpTransport struct { Address, Family, NonceFile string } func (trans *nonceTcpTransport) Dial() (net.Conn, error) { data, err := ioutil.ReadFile(trans.NonceFile) if err != nil { return nil, err } conn, err := net.Dial(trans.Family, trans.Address) if err != nil { return nil, err } // Write the nonce data to the socket if _, err := conn.Write(data); err != nil { conn.Close() return nil, err } return conn, nil } usensord-1.0+14.04.20140404/dbus/signal.go0000644000015301777760000000755512317524417020373 0ustar pbusernogroup00000000000000package dbus import ( "errors" ) // A structure to store the set of signal watches, keyed by object // path, interface and member. type signalWatchSet map[ObjectPath]map[string]map[string][]*SignalWatch func (self signalWatchSet) Add(watch *SignalWatch) { byInterface, ok := self[watch.rule.Path] if !ok { byInterface = make(map[string]map[string][]*SignalWatch) self[watch.rule.Path] = byInterface } byMember, ok := byInterface[watch.rule.Interface] if !ok { byMember = make(map[string][]*SignalWatch) byInterface[watch.rule.Interface] = byMember } watches, ok := byMember[watch.rule.Member] if !ok { watches = make([]*SignalWatch, 0, 1) } byMember[watch.rule.Member] = append(watches, watch) } func (self signalWatchSet) Remove(watch *SignalWatch) bool { byInterface, ok := self[watch.rule.Path] if !ok { return false } byMember, ok := byInterface[watch.rule.Interface] if !ok { return false } watches, ok := byMember[watch.rule.Member] if !ok { return false } for i, other := range watches { if other == watch { // Truncate the watch slice, moving the item // at the end to the new location. watches[i] = watches[len(watches)-1] byMember[watch.rule.Member] = watches[:len(watches)-1] return true } } return false } func (self signalWatchSet) FindMatches(msg *Message) (matches []*SignalWatch) { pathKeys := []ObjectPath{""} if msg.Path != ObjectPath("") { pathKeys = append(pathKeys, msg.Path) } ifaceKeys := []string{""} if msg.Interface != "" { ifaceKeys = append(ifaceKeys, msg.Interface) } memberKeys := []string{""} if msg.Member != "" { memberKeys = append(memberKeys, msg.Member) } for _, path := range pathKeys { byInterface, ok := self[path] if !ok { continue } for _, iface := range ifaceKeys { byMember, ok := byInterface[iface] if !ok { continue } for _, member := range memberKeys { watches, ok := byMember[member] if !ok { continue } for _, watch := range watches { if watch.rule.Match(msg) { matches = append(matches, watch) } } } } } return } type SignalWatch struct { bus *Connection rule MatchRule C chan *Message // If the rule tries to match against a bus name as the // sender, we need to track the current owner of that name. nameWatch *NameWatch cancelled bool } // Handle received signals. func (p *Connection) WatchSignal(rule *MatchRule) (*SignalWatch, error) { if rule.Type != TypeSignal { return nil, errors.New("Match rule is not for signals") } watch := &SignalWatch{ bus: p, rule: *rule, C: make(chan *Message)} // Does the rule match a bus name other than the daemon? if rule.Sender != "" && rule.Sender != BUS_DAEMON_NAME { nameWatch, err := p.WatchName(rule.Sender) if err != nil { return nil, err } watch.nameWatch = nameWatch if rule.Sender[0] == ':' { // For unique names, cancel the signal watch // when the name is lost. go func() { for newOwner := range nameWatch.C { if newOwner == "" { watch.Cancel() } } }() } else { // Otherwise, update the sender owner. go func() { for newOwner := range nameWatch.C { watch.rule.senderNameOwner = newOwner } }() } } if err := p.busProxy.AddMatch(rule.String()); err != nil { watch.nameWatch.Cancel() return nil, err } p.handlerMutex.Lock() p.signalMatchRules.Add(watch) p.handlerMutex.Unlock() return watch, nil } func (watch *SignalWatch) Cancel() error { if watch.cancelled { return nil } watch.cancelled = true close(watch.C) watch.bus.handlerMutex.Lock() foundMatch := watch.bus.signalMatchRules.Remove(watch) watch.bus.handlerMutex.Unlock() if foundMatch { if err := watch.bus.busProxy.RemoveMatch(watch.rule.String()); err != nil { return err } if watch.nameWatch != nil { if err := watch.nameWatch.Cancel(); err != nil { return err } } } return nil } usensord-1.0+14.04.20140404/dbus/types.go0000644000015301777760000000715312317524417020254 0ustar pbusernogroup00000000000000package dbus import ( "errors" "fmt" "reflect" ) var ( typeObjectPather = reflect.TypeOf((*ObjectPather)(nil)).Elem() typeVariant = reflect.TypeOf(Variant{}) typeSignature = reflect.TypeOf(Signature("")) typeBlankInterface = reflect.TypeOf((*interface{})(nil)).Elem() ) type Signature string func SignatureOf(t reflect.Type) (Signature, error) { if t.AssignableTo(typeObjectPather) { return Signature("o"), nil } switch t.Kind() { case reflect.Uint8: return Signature("y"), nil case reflect.Bool: return Signature("b"), nil case reflect.Int16: return Signature("n"), nil case reflect.Uint16: return Signature("q"), nil case reflect.Int32: return Signature("i"), nil case reflect.Uint32: return Signature("u"), nil case reflect.Int64: return Signature("x"), nil case reflect.Uint64: return Signature("t"), nil case reflect.Float64: return Signature("d"), nil case reflect.String: if t == typeSignature { return Signature("g"), nil } return Signature("s"), nil case reflect.Array, reflect.Slice: valueSig, err := SignatureOf(t.Elem()) if err != nil { return Signature(""), err } return Signature("a") + valueSig, nil case reflect.Map: keySig, err := SignatureOf(t.Key()) if err != nil { return Signature(""), err } valueSig, err := SignatureOf(t.Elem()) if err != nil { return Signature(""), err } return Signature("a{") + keySig + valueSig + Signature("}"), nil case reflect.Struct: // Special case the variant structure if t == typeVariant { return Signature("v"), nil } sig := Signature("(") for i := 0; i != t.NumField(); i++ { fieldSig, err := SignatureOf(t.Field(i).Type) if err != nil { return Signature(""), err } sig += fieldSig } sig += Signature(")") return sig, nil case reflect.Ptr: // dereference pointers sig, err := SignatureOf(t.Elem()) return sig, err } return Signature(""), errors.New("Can not determine signature for " + t.String()) } func (sig Signature) NextType(offset int) (next int, err error) { if offset >= len(sig) { err = errors.New("No more types codes in signature") return } switch sig[offset] { case 'y', 'b', 'n', 'q', 'i', 'u', 'x', 't', 'd', 's', 'o', 'g', 'v', 'h': // A basic type code. next = offset + 1 case 'a': // An array: consume the embedded type code next, err = sig.NextType(offset + 1) case '{': // A pair used in maps: consume the two contained types next, err = sig.NextType(offset + 1) if err != nil { return } next, err = sig.NextType(next) if err != nil { return } if next >= len(sig) || sig[next] != '}' { err = errors.New("Pair does not end with '}'") return } next += 1 case '(': // A struct: consume types until we next = offset + 1 for { if next < len(sig) && sig[next] == ')' { next += 1 return } next, err = sig.NextType(next) if err != nil { return } } default: err = errors.New("Unknown type code " + string(sig[offset])) } return } // Validate that the signature is a valid string of type codes func (sig Signature) Validate() (err error) { offset := 0 for offset < len(sig) { offset, err = sig.NextType(offset) if err != nil { break } } return } type ObjectPath string type ObjectPather interface { ObjectPath() ObjectPath } func (o ObjectPath) ObjectPath() ObjectPath { return o } type Variant struct { Value interface{} } func (v *Variant) GetVariantSignature() (Signature, error) { return SignatureOf(reflect.TypeOf(v.Value)) } type Error struct { Name string Message string } func (e *Error) Error() string { return fmt.Sprint(e.Name, ": ", e.Message) } usensord-1.0+14.04.20140404/dbus/decoder_test.go0000644000015301777760000002275012317524417021554 0ustar pbusernogroup00000000000000package dbus import "encoding/binary" import . "launchpad.net/gocheck" func (s *S) TestDecoderDecodeByte(c *C) { dec := newDecoder("yy", []byte{42, 100}, binary.LittleEndian) var value1 byte var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, byte(42)) c.Check(value2, Equals, byte(100)) c.Check(dec.dataOffset, Equals, 2) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeBool(c *C) { dec := newDecoder("bb", []byte{0, 0, 0, 0, 1, 0, 0, 0}, binary.LittleEndian) var value1 bool var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, false) c.Check(value2, Equals, true) c.Check(dec.dataOffset, Equals, 8) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeInt16(c *C) { dec := newDecoder("nn", []byte{42, 0, 100, 0}, binary.LittleEndian) var value1 int16 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, int16(42)) c.Check(value2, Equals, int16(100)) c.Check(dec.dataOffset, Equals, 4) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeUint16(c *C) { dec := newDecoder("qq", []byte{42, 0, 100, 0}, binary.LittleEndian) var value1 uint16 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, uint16(42)) c.Check(value2, Equals, uint16(100)) c.Check(dec.dataOffset, Equals, 4) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeInt32(c *C) { dec := newDecoder("ii", []byte{42, 0, 0, 0, 100, 0, 0, 0}, binary.LittleEndian) var value1 int32 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, int32(42)) c.Check(value2, Equals, int32(100)) c.Check(dec.dataOffset, Equals, 8) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeUint32(c *C) { dec := newDecoder("uu", []byte{42, 0, 0, 0, 100, 0, 0, 0}, binary.LittleEndian) var value1 uint32 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, uint32(42)) c.Check(value2, Equals, uint32(100)) c.Check(dec.dataOffset, Equals, 8) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeInt64(c *C) { dec := newDecoder("xx", []byte{42, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0}, binary.LittleEndian) var value1 int64 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, int64(42)) c.Check(value2, Equals, int64(100)) c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeUint64(c *C) { dec := newDecoder("tt", []byte{42, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0}, binary.LittleEndian) var value1 uint64 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, uint64(42)) c.Check(value2, Equals, uint64(100)) c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeFloat64(c *C) { dec := newDecoder("dd", []byte{0, 0, 0, 0, 0, 0, 69, 64, 0, 0, 0, 0, 0, 0, 89, 64}, binary.LittleEndian) var value1 float64 var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, float64(42)) c.Check(value2, Equals, float64(100)) c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeString(c *C) { dec := newDecoder("ss", []byte{ 5, 0, 0, 0, // len("hello") 'h', 'e', 'l', 'l', 'o', 0, // "hello" 0, 0, // padding 5, 0, 0, 0, // len("world") 'w', 'o', 'r', 'l', 'd', 0}, // "world" binary.LittleEndian) var value1 string var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, "hello") c.Check(value2, Equals, "world") c.Check(dec.dataOffset, Equals, 22) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeObjectPath(c *C) { dec := newDecoder("oo", []byte{ 4, 0, 0, 0, // len("/foo") '/', 'f', 'o', 'o', 0, // ObjectPath("/foo") 0, 0, 0, // padding 4, 0, 0, 0, // len("/bar") '/', 'b', 'a', 'r', 0}, // ObjectPath("/bar") binary.LittleEndian) var value1 ObjectPath var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, ObjectPath("/foo")) c.Check(value2, Equals, ObjectPath("/bar")) c.Check(dec.dataOffset, Equals, 21) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeSignature(c *C) { dec := newDecoder("gg", []byte{ 8, // len("a{s(iv)}") 'a', '{', 's', '(', 'i', 'v', ')', '}', 0, // Signature("a{s(iv)}") 4, // len("asvi") 'a', 's', 'v', 'i', 0}, // Signature("asvi") binary.LittleEndian) var value1 Signature var value2 interface{} if err := dec.Decode(&value1, &value2); err != nil { c.Error(err) } c.Check(value1, Equals, Signature("a{s(iv)}")) c.Check(value2, Equals, Signature("asvi")) c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 2) } func (s *S) TestDecoderDecodeArray(c *C) { dec := newDecoder("ai", []byte{ 8, 0, 0, 0, // array length 42, 0, 0, 0, // int32(42) 100, 0, 0, 0}, // int32(100) binary.LittleEndian) // Decode as an array var value1 [2]int32 if err := dec.Decode(&value1); err != nil { c.Error("Decode as array:", err) } c.Check(dec.dataOffset, Equals, 12) c.Check(dec.sigOffset, Equals, 2) c.Check(value1[0], Equals, int32(42)) c.Check(value1[1], Equals, int32(100)) // Decode as a slice dec.dataOffset = 0 dec.sigOffset = 0 var value2 []int32 if err := dec.Decode(&value2); err != nil { c.Error("Decode as slice:", err) } c.Check(value2, DeepEquals, []int32{42, 100}) // Decode as blank interface dec.dataOffset = 0 dec.sigOffset = 0 var value3 interface{} if err := dec.Decode(&value3); err != nil { c.Error("Decode as interface:", err) } c.Check(value3, DeepEquals, []interface{}{int32(42), int32(100)}) } func (s *S) TestDecoderDecodeEmptyArray(c *C) { dec := newDecoder("ai", []byte{ 0, 0, 0, 0}, // array length binary.LittleEndian) var value []int32 c.Check(dec.Decode(&value), Equals, nil) c.Check(dec.dataOffset, Equals, 4) c.Check(dec.sigOffset, Equals, 2) c.Check(value, DeepEquals, []int32{}) } func (s *S) TestDecoderDecodeArrayPaddingAfterLength(c *C) { dec := newDecoder("ax", []byte{ 8, 0, 0, 0, // array length 0, 0, 0, 0, // padding 42, 0, 0, 0, 0, 0, 0, 0}, // uint64(42) binary.LittleEndian) var value []int64 c.Check(dec.Decode(&value), Equals, nil) c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 2) c.Check(value, DeepEquals, []int64{42}) // This padding exists even for empty arays dec = newDecoder("ax", []byte{ 0, 0, 0, 0, // array length 0, 0, 0, 0}, // padding binary.LittleEndian) c.Check(dec.Decode(&value), Equals, nil) c.Check(dec.dataOffset, Equals, 8) c.Check(dec.sigOffset, Equals, 2) c.Check(value, DeepEquals, []int64{}) } func (s *S) TestDecoderDecodeMap(c *C) { dec := newDecoder("a{si}", []byte{ 36, 0, 0, 0, // array length 0, 0, 0, 0, // padding 3, 0, 0, 0, // len("one") 'o', 'n', 'e', 0, // "one" 1, 0, 0, 0, // int32(1) 0, 0, 0, 0, // padding 9, 0, 0, 0, // len("forty two") 'f', 'o', 'r', 't', 'y', ' ', 't', 'w', 'o', 0, 0, 0, // padding 42, 0, 0, 0}, // int32(42) binary.LittleEndian) var value map[string]int32 c.Check(dec.Decode(&value), Equals, nil) c.Check(len(value), Equals, 2) c.Check(value["one"], Equals, int32(1)) c.Check(value["forty two"], Equals, int32(42)) } func (s *S) TestDecoderDecodeStruct(c *C) { dec := newDecoder("(si)", []byte{ 5, 0, 0, 0, // len("hello") 'h', 'e', 'l', 'l', 'o', 0, // "hello" 0, 0, // padding 42, 0, 0, 0}, // int32(42) binary.LittleEndian) type Dummy struct { S string I int32 } // Decode as structure var value1 Dummy if err := dec.Decode(&value1); err != nil { c.Error("Decode as structure:", err) } c.Check(dec.dataOffset, Equals, 16) c.Check(dec.sigOffset, Equals, 4) c.Check(value1, DeepEquals, Dummy{"hello", 42}) // Decode as pointer to structure dec.dataOffset = 0 dec.sigOffset = 0 var value2 *Dummy if err := dec.Decode(&value2); err != nil { c.Error("Decode as structure pointer:", err) } c.Check(value2, DeepEquals, &Dummy{"hello", 42}) // Decode as blank interface dec.dataOffset = 0 dec.sigOffset = 0 var value3 interface{} if err := dec.Decode(&value3); err != nil { c.Error("Decode as interface:", err) } c.Check(value3, DeepEquals, []interface{}{"hello", int32(42)}) } func (s *S) TestDecoderDecodeVariant(c *C) { dec := newDecoder("v", []byte{ 1, // len("i") 'i', 0, // Signature("i") 0, // padding 42, 0, 0, 0}, // int32(42) binary.LittleEndian) var value1 Variant if err := dec.Decode(&value1); err != nil { c.Error("Decode as Variant:", err) } c.Check(dec.dataOffset, Equals, 8) c.Check(dec.sigOffset, Equals, 1) c.Check(value1, DeepEquals, Variant{int32(42)}) // Decode as pointer to Variant dec.dataOffset = 0 dec.sigOffset = 0 var value2 *Variant if err := dec.Decode(&value2); err != nil { c.Error("Decode as *Variant:", err) } c.Check(value2, DeepEquals, &Variant{int32(42)}) // Decode as pointer to blank interface dec.dataOffset = 0 dec.sigOffset = 0 var value3 interface{} if err := dec.Decode(&value3); err != nil { c.Error("Decode as interface:", err) } c.Check(value3, DeepEquals, &Variant{int32(42)}) } usensord-1.0+14.04.20140404/dbus/auth_test.go0000644000015301777760000000144612317524417021107 0ustar pbusernogroup00000000000000package dbus import ( "bufio" . "launchpad.net/gocheck" "net" ) func (s *S) TestAuthenticate(c *C) { server, client := net.Pipe() clientWrites := []string{} complete := make(chan int) go func() { r := bufio.NewReader(server) // Read the nul byte that marks the start of the protocol zero := []byte{0} r.Read(zero) clientWrites = append(clientWrites, string(zero)) line, _, _ := r.ReadLine() clientWrites = append(clientWrites, string(line)) server.Write([]byte("OK\r\n")) line, _, _ = r.ReadLine() clientWrites = append(clientWrites, string(line)) complete <- 1 }() c.Check(authenticate(client, nil), Equals, nil) <-complete c.Check(clientWrites[0], Equals, "\x00") c.Check(clientWrites[1][:13], Equals, "AUTH EXTERNAL") c.Check(clientWrites[2], Equals, "BEGIN") } usensord-1.0+14.04.20140404/dbus/auth.go0000644000015301777760000001055112317524417020045 0ustar pbusernogroup00000000000000package dbus import ( "bufio" "bytes" "crypto/rand" "crypto/sha1" "encoding/hex" "errors" "io" "net" "os" "strconv" ) type authenticator interface { Mechanism() []byte InitialResponse() []byte ProcessData(challenge []byte) (response []byte, err error) } type authExternal struct { } func (p *authExternal) Mechanism() []byte { return []byte("EXTERNAL") } func (p *authExternal) InitialResponse() []byte { uid := []byte(strconv.Itoa(os.Geteuid())) uidHex := make([]byte, hex.EncodedLen(len(uid))) hex.Encode(uidHex, uid) return uidHex } func (p *authExternal) ProcessData([]byte) ([]byte, error) { return nil, errors.New("Unexpected Response") } type authDbusCookieSha1 struct { } func (p *authDbusCookieSha1) Mechanism() []byte { return []byte("DBUS_COOKIE_SHA1") } func (p *authDbusCookieSha1) InitialResponse() []byte { user := []byte(os.Getenv("USER")) userHex := make([]byte, hex.EncodedLen(len(user))) hex.Encode(userHex, user) return userHex } func (p *authDbusCookieSha1) ProcessData(mesg []byte) ([]byte, error) { decodedLen, err := hex.Decode(mesg, mesg) if err != nil { return nil, err } mesgTokens := bytes.SplitN(mesg[:decodedLen], []byte(" "), 3) file, err := os.Open(os.Getenv("HOME") + "/.dbus-keyrings/" + string(mesgTokens[0])) if err != nil { return nil, err } defer file.Close() fileStream := bufio.NewReader(file) var cookie []byte for { line, _, err := fileStream.ReadLine() if err == io.EOF { return nil, errors.New("SHA1 Cookie not found") } else if err != nil { return nil, err } cookieTokens := bytes.SplitN(line, []byte(" "), 3) if bytes.Compare(cookieTokens[0], mesgTokens[1]) == 0 { cookie = cookieTokens[2] break } } challenge := make([]byte, len(mesgTokens[2])) if _, err = rand.Read(challenge); err != nil { return nil, err } for temp := challenge; ; { if index := bytes.IndexAny(temp, " \t"); index == -1 { break } else if _, err := rand.Read(temp[index : index+1]); err != nil { return nil, err } else { temp = temp[index:] } } hash := sha1.New() if _, err := hash.Write(bytes.Join([][]byte{mesgTokens[2], challenge, cookie}, []byte(":"))); err != nil { return nil, err } resp := bytes.Join([][]byte{challenge, []byte(hex.EncodeToString(hash.Sum(nil)))}, []byte(" ")) respHex := make([]byte, hex.EncodedLen(len(resp))) hex.Encode(respHex, resp) return respHex, nil } func authenticate(conn net.Conn, authenticators []authenticator) error { // If no authenticators are provided, try them all if authenticators == nil { authenticators = []authenticator{ new(authExternal), new(authDbusCookieSha1)} } // The authentication process starts by writing a nul byte if _, err := conn.Write([]byte{0}); err != nil { return err } inStream := bufio.NewReader(conn) send := func(command ...[]byte) ([][]byte, error) { msg := bytes.Join(command, []byte(" ")) _, err := conn.Write(append(msg, []byte("\r\n")...)) if err != nil { return nil, err } line, isPrefix, err := inStream.ReadLine() if err != nil { return nil, err } if isPrefix { return nil, errors.New("Received line is too long") } return bytes.Split(line, []byte(" ")), err } success := false for _, auth := range authenticators { reply, err := send([]byte("AUTH"), auth.Mechanism(), auth.InitialResponse()) StatementLoop: for { if err != nil { return err } if len(reply) < 1 { return errors.New("No response command from server") } switch string(reply[0]) { case "OK": success = true break StatementLoop case "REJECTED": // XXX: should note the list of // supported mechanisms break StatementLoop case "ERROR": return errors.New("Received error from server: " + string(bytes.Join(reply, []byte(" ")))) case "DATA": var response []byte response, err = auth.ProcessData(reply[1]) if err == nil { reply, err = send([]byte("DATA"), response) } else { // Cancel so we can move on to // the next mechanism. reply, err = send([]byte("CANCEL")) } default: return errors.New("Unknown response from server: " + string(bytes.Join(reply, []byte(" ")))) } } if success { break } } if !success { return errors.New("Could not authenticate with any mechanism") } // XXX: UNIX FD negotiation would go here. if _, err := conn.Write([]byte("BEGIN\r\n")); err != nil { return err } return nil } usensord-1.0+14.04.20140404/dbus/types_test.go0000644000015301777760000000370612317524417021313 0ustar pbusernogroup00000000000000package dbus import . "launchpad.net/gocheck" func (s *S) TestSignatureNextType(c *C) { // NextType() works for basic types for _, sig := range []Signature{"y", "b", "n", "q", "i", "u", "x", "t", "d", "s", "o", "g", "v", "h"} { next, err := sig.NextType(0) c.Check(next, Equals, 1) c.Check(err, Equals, nil) } // Unknown type code gives error next, err := Signature("_").NextType(0) c.Check(err, Not(Equals), nil) // Offset inside signature next, err = Signature("ii").NextType(1) c.Check(next, Equals, 2) c.Check(err, Equals, nil) // Error if there is no more type codes in signature next, err = Signature("i").NextType(1) c.Check(err, Not(Equals), nil) // Arrays consume their element type code next, err = Signature("ai").NextType(0) c.Check(next, Equals, 2) c.Check(err, Equals, nil) // Array without element type code gives error next, err = Signature("a").NextType(0) c.Check(err, Not(Equals), nil) // Structs are consumed entirely next, err = Signature("(isv)").NextType(0) c.Check(next, Equals, 5) c.Check(err, Equals, nil) // Incomplete struct gives error next, err = Signature("(isv").NextType(0) c.Check(err, Not(Equals), nil) // Dict entries have two contained type codes next, err = Signature("{ii}").NextType(0) c.Check(next, Equals, 4) c.Check(err, Equals, nil) next, err = Signature("{}").NextType(0) c.Check(err, Not(Equals), nil) next, err = Signature("{i}").NextType(0) c.Check(err, Not(Equals), nil) next, err = Signature("{iii}").NextType(0) c.Check(err, Not(Equals), nil) next, err = Signature("{ii").NextType(0) c.Check(err, Not(Equals), nil) // Now a recursive type combining the above. next, err = Signature("a{s(saax)}").NextType(0) c.Check(next, Equals, 10) c.Check(err, Equals, nil) } func (s *S) TestSignatureValidate(c *C) { c.Check(Signature("a{s(sax)}aav").Validate(), Equals, nil) c.Check(Signature("a").Validate(), Not(Equals), nil) c.Check(Signature("a(ii").Validate(), Not(Equals), nil) } usensord-1.0+14.04.20140404/dbus/names.go0000644000015301777760000001430712317524417020212 0ustar pbusernogroup00000000000000package dbus import ( "errors" "log" ) type nameInfo struct { bus *Connection busName string currentOwner string signalWatch *SignalWatch watches []*NameWatch } type NameWatch struct { info *nameInfo C chan string cancelled bool } func newNameInfo(bus *Connection, busName string) (*nameInfo, error) { info := &nameInfo{ bus: bus, busName: busName, watches: []*NameWatch{}} watch, err := bus.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: BUS_DAEMON_NAME, Path: BUS_DAEMON_PATH, Interface: BUS_DAEMON_IFACE, Member: "NameOwnerChanged", Arg0: busName}) if err != nil { return nil, err } go func() { for msg := range watch.C { var busName, oldOwner, newOwner string if err := msg.Args(&busName, &oldOwner, &newOwner); err != nil { log.Println("Could not decode NameOwnerChanged message:", err) continue } info.handleOwnerChange(newOwner) } }() info.signalWatch = watch // spawn a goroutine to find the current name owner go info.checkCurrentOwner() return info, nil } func (self *nameInfo) checkCurrentOwner() { currentOwner, err := self.bus.busProxy.GetNameOwner(self.busName) if err != nil { if dbusErr, ok := err.(*Error); !ok || dbusErr.Name != "org.freedesktop.DBus.Error.NameHasNoOwner" { log.Println("Unexpected error from GetNameOwner:", err) } } if self.currentOwner == "" { // Simulate an ownership change message. self.handleOwnerChange(currentOwner) } } func (self *nameInfo) handleOwnerChange(newOwner string) { for _, watch := range self.watches { watch.C <- newOwner } self.currentOwner = newOwner } func (p *Connection) WatchName(busName string) (watch *NameWatch, err error) { p.nameInfoMutex.Lock() defer p.nameInfoMutex.Unlock() info, ok := p.nameInfo[busName] if !ok { if info, err = newNameInfo(p, busName); err != nil { return } p.nameInfo[busName] = info } watch = &NameWatch{info: info, C: make(chan string, 1)} info.watches = append(info.watches, watch) // If we're hooking up to an existing nameOwner and it already // knows the current name owner, tell our callback. if ok && info.currentOwner != "" { watch.C <- info.currentOwner } return } func (watch *NameWatch) Cancel() error { if watch.cancelled { return nil } watch.cancelled = true close(watch.C) info := watch.info bus := info.bus bus.nameInfoMutex.Lock() defer bus.nameInfoMutex.Unlock() found := false for i, other := range info.watches { if other == watch { info.watches[i] = info.watches[len(info.watches)-1] info.watches = info.watches[:len(info.watches)-1] found = true break } } if !found { return errors.New("NameOwnerWatch already cancelled") } if len(info.watches) != 0 { // There are other watches interested in this name, so // leave the nameOwner in place. return nil } delete(bus.nameInfo, info.busName) return info.signalWatch.Cancel() } type BusName struct { bus *Connection Name string Flags NameFlags cancelled bool needsRelease bool acquiredCallback func(*BusName) lostCallback func(*BusName) acquiredWatch *SignalWatch lostWatch *SignalWatch } type NameFlags uint32 const ( NameFlagAllowReplacement NameFlags = 1 << iota NameFlagReplaceExisting NameFlagDoNotQueue ) func (p *Connection) RequestName(busName string, flags NameFlags, nameAcquired func(*BusName), nameLost func(*BusName)) *BusName { name := &BusName{ bus: p, Name: busName, Flags: flags, acquiredCallback: nameAcquired, lostCallback: nameLost} go name.request() return name } func (name *BusName) request() { if name.cancelled { return } result, err := name.bus.busProxy.RequestName(name.Name, uint32(name.Flags)) if err != nil { log.Println("Error requesting bus name", name.Name, "err =", err) return } subscribe := false switch result { case 1: // DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER if name.acquiredCallback != nil { name.acquiredCallback(name) } subscribe = true name.needsRelease = true case 2: // DBUS_REQUEST_NAME_REPLY_IN_QUEUE if name.lostCallback != nil { name.lostCallback(name) } subscribe = true name.needsRelease = true case 3: // DBUS_REQUEST_NAME_REPLY_EXISTS fallthrough case 4: // DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER fallthrough default: // assume that other responses mean we couldn't own // the name if name.lostCallback != nil { name.lostCallback(name) } } if subscribe && !name.cancelled { watch, err := name.bus.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: BUS_DAEMON_NAME, Path: BUS_DAEMON_PATH, Interface: BUS_DAEMON_IFACE, Member: "NameLost", Arg0: name.Name}) if err != nil { log.Println("Could not set up NameLost signal watch") name.Release() return } name.lostWatch = watch go func() { for _ = range name.lostWatch.C { if !name.cancelled && name.lostCallback != nil { name.lostCallback(name) } } }() watch, err = name.bus.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: BUS_DAEMON_NAME, Path: BUS_DAEMON_PATH, Interface: BUS_DAEMON_IFACE, Member: "NameAcquired", Arg0: name.Name}) if err != nil { log.Println("Could not set up NameLost signal watch") name.Release() return } name.acquiredWatch = watch go func() { for _ = range name.acquiredWatch.C { if !name.cancelled && name.acquiredCallback != nil { name.acquiredCallback(name) } } }() // XXX: if we disconnect from the bus, we should // report the name being lost. } } func (name *BusName) Release() error { if name.cancelled { return nil } name.cancelled = true if name.acquiredWatch != nil { if err := name.acquiredWatch.Cancel(); err != nil { return err } name.acquiredWatch = nil } if name.lostWatch != nil { if err := name.lostWatch.Cancel(); err != nil { return err } name.lostWatch = nil } if name.needsRelease { result, err := name.bus.busProxy.ReleaseName(name.Name) if err != nil { return err } if result != 1 { // DBUS_RELEASE_NAME_REPLY_RELEASED log.Println("Unexpected result when releasing name", name.Name, "result =", result) } name.needsRelease = false } return nil } usensord-1.0+14.04.20140404/dbus/names_test.go0000644000015301777760000001062112317524417021244 0ustar pbusernogroup00000000000000package dbus import ( . "launchpad.net/gocheck" ) func (s *S) TestConnectionWatchName(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus.Close() // Set up the name watch nameChanged := make(chan int, 1) owners := []string{} watch, err := bus.WatchName("com.example.GoDbus") c.Assert(err, IsNil) defer watch.Cancel() go func() { for newOwner := range watch.C { owners = append(owners, newOwner) nameChanged <- 0 } }() // Our handler will be called once with the initial name owner <-nameChanged c.Check(owners, DeepEquals, []string{""}) // Acquire the name, and wait for the process to complete. nameAcquired := make(chan int, 1) name := bus.RequestName("com.example.GoDbus", NameFlagDoNotQueue, func(*BusName) { nameAcquired <- 0 }, nil) <-nameAcquired <-nameChanged c.Check(owners, DeepEquals, []string{"", bus.UniqueName}) err = name.Release() c.Assert(err, IsNil) <-nameChanged c.Check(owners, DeepEquals, []string{"", bus.UniqueName, ""}) } func (s *S) TestConnectionRequestName(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus.Close() nameAcquired := make(chan int, 1) name := bus.RequestName("com.example.GoDbus", 0, func(*BusName) { nameAcquired <- 0 }, nil) c.Check(name, NotNil) <-nameAcquired owner, err := bus.busProxy.GetNameOwner("com.example.GoDbus") c.Check(err, IsNil) c.Check(owner, Equals, bus.UniqueName) c.Check(name.Release(), IsNil) } func (s *S) TestConnectionRequestNameQueued(c *C) { // Acquire the name on a second connection bus1, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus1.Close() bus2, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus2.Close() ready := make(chan int, 1) name1 := bus1.RequestName("com.example.GoDbus", 0, func(*BusName) { ready <- 0 }, nil) <-ready c.Check(name1.needsRelease, Equals, true) callLog := []string{} called := make(chan int, 1) name2 := bus2.RequestName("com.example.GoDbus", 0, func(*BusName) { callLog = append(callLog, "acquired") called <- 0 }, func(*BusName) { callLog = append(callLog, "lost") called <- 0 }) <-called c.Check(name2.needsRelease, Equals, true) c.Check(callLog, DeepEquals, []string{"lost"}) // Release the name on the first connection c.Check(name1.Release(), IsNil) <-called c.Check(callLog, DeepEquals, []string{"lost", "acquired"}) c.Check(name2.Release(), IsNil) } func (s *S) TestConnectionRequestNameDoNotQueue(c *C) { // Acquire the name on a second connection bus1, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus1.Close() bus2, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus2.Close() ready := make(chan int, 1) name1 := bus1.RequestName("com.example.GoDbus", 0, func(*BusName) { ready <- 0 }, nil) defer name1.Release() <-ready c.Check(name1.needsRelease, Equals, true) callLog := []string{} called := make(chan int, 1) name2 := bus2.RequestName("com.example.GoDbus", NameFlagDoNotQueue, func(*BusName) { callLog = append(callLog, "acquired") called <- 0 }, func(*BusName) { callLog = append(callLog, "lost") called <- 0 }) <-called c.Check(name2.needsRelease, Equals, false) c.Check(callLog, DeepEquals, []string{"lost"}) c.Check(name2.Release(), IsNil) } func (s *S) TestConnectionRequestNameAllowReplacement(c *C) { // Acquire the name on a second connection bus1, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus1.Close() bus2, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus2.Close() callLog1 := []string{} called1 := make(chan int, 1) name1 := bus1.RequestName("com.example.GoDbus", NameFlagAllowReplacement, func(*BusName) { callLog1 = append(callLog1, "acquired") called1 <- 0 }, func(*BusName) { callLog1 = append(callLog1, "lost") called1 <- 0 }) defer name1.Release() <-called1 c.Check(name1.needsRelease, Equals, true) c.Check(callLog1, DeepEquals, []string{"acquired"}) callLog2 := []string{} called2 := make(chan int, 1) name2 := bus2.RequestName("com.example.GoDbus", NameFlagReplaceExisting, func(*BusName) { callLog2 = append(callLog2, "acquired") called2 <- 0 }, func(*BusName) { callLog2 = append(callLog2, "lost") called2 <- 0 }) defer name2.Release() <-called2 c.Check(name2.needsRelease, Equals, true) c.Check(callLog2, DeepEquals, []string{"acquired"}) // The first name owner loses possession. <-called1 c.Check(callLog1, DeepEquals, []string{"acquired", "lost"}) } usensord-1.0+14.04.20140404/dbus/message_test.go0000644000015301777760000001024512317524417021567 0ustar pbusernogroup00000000000000package dbus import ( "bytes" "io" . "launchpad.net/gocheck" ) var testMessage = []byte{ 'l', // Byte order 1, // Message type 0, // Flags 1, // Protocol 8, 0, 0, 0, // Body length 1, 0, 0, 0, // Serial 127, 0, 0, 0, // Header fields array length 1, 1, 'o', 0, // Path, type OBJECT_PATH 21, 0, 0, 0, '/', 'o', 'r', 'g', '/', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '/', 'D', 'B', 'u', 's', 0, 0, 0, 2, 1, 's', 0, // Interface, type STRING 20, 0, 0, 0, 'o', 'r', 'g', '.', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '.', 'D', 'B', 'u', 's', 0, 0, 0, 0, 3, 1, 's', 0, // Member, type STRING 12, 0, 0, 0, 'N', 'a', 'm', 'e', 'H', 'a', 's', 'O', 'w', 'n', 'e', 'r', 0, 0, 0, 0, 6, 1, 's', 0, // Destination, type STRING 20, 0, 0, 0, 'o', 'r', 'g', '.', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '.', 'D', 'B', 'u', 's', 0, 0, 0, 0, 8, 1, 'g', 0, // Signature, type SIGNATURE 1, 's', 0, 0, // Message body 3, 0, 0, 0, 'x', 'y', 'z', 0} func (s *S) TestReadMessage(c *C) { r := bytes.NewReader(testMessage) msg, err := readMessage(r) if nil != err { c.Error(err) } c.Check(msg.Type, Equals, TypeMethodCall) c.Check(msg.Path, Equals, ObjectPath("/org/freedesktop/DBus")) c.Check(msg.Dest, Equals, "org.freedesktop.DBus") c.Check(msg.Interface, Equals, "org.freedesktop.DBus") c.Check(msg.Member, Equals, "NameHasOwner") c.Check(msg.sig, Equals, Signature("s")) var arg string if err := msg.Args(&arg); err != nil { c.Error(err) } c.Check(arg, Equals, "xyz") // Try reading a second message from the reader msg, err = readMessage(r) if err == nil { c.Error("Should not have been able to read a second message.") } else if err != io.EOF { c.Error(err) } } func (s *S) TestWriteMessage(c *C) { msg := newMessage() msg.Type = TypeMethodCall msg.Flags = MessageFlag(0) msg.serial = 1 msg.Path = "/org/freedesktop/DBus" msg.Dest = "org.freedesktop.DBus" msg.Interface = "org.freedesktop.DBus" msg.Member = "NameHasOwner" if err := msg.AppendArgs("xyz"); err != nil { c.Error(err) } buff := new(bytes.Buffer) n, err := msg.WriteTo(buff) c.Check(err, Equals, nil) c.Check(n, Equals, int64(len(testMessage))) c.Check(buff.Bytes(), DeepEquals, testMessage) } func (s *S) TestNewMethodCallMessage(c *C) { msg := NewMethodCallMessage("com.destination", "/path", "com.interface", "method") c.Check(msg.Type, Equals, TypeMethodCall) c.Check(msg.Dest, Equals, "com.destination") c.Check(msg.Path, Equals, ObjectPath("/path")) c.Check(msg.Interface, Equals, "com.interface") c.Check(msg.Member, Equals, "method") // No signature or data c.Check(msg.sig, Equals, Signature("")) c.Check(msg.body, DeepEquals, []byte{}) } func (s *S) TestNewMethodReturnMessage(c *C) { call := NewMethodCallMessage("com.destination", "/path", "com.interface", "method") call.serial = 42 call.Sender = ":1.2" reply := NewMethodReturnMessage(call) c.Check(reply.Type, Equals, TypeMethodReturn) c.Check(reply.Dest, Equals, ":1.2") c.Check(reply.replySerial, Equals, uint32(42)) // No signature or data c.Check(reply.sig, Equals, Signature("")) c.Check(reply.body, DeepEquals, []byte{}) } func (s *S) TestNewSignalMessage(c *C) { msg := NewSignalMessage("/path", "com.interface", "signal") c.Check(msg.Type, Equals, TypeSignal) c.Check(msg.Dest, Equals, "") c.Check(msg.Path, Equals, ObjectPath("/path")) c.Check(msg.Interface, Equals, "com.interface") c.Check(msg.Member, Equals, "signal") // No signature or data c.Check(msg.sig, Equals, Signature("")) c.Check(msg.body, DeepEquals, []byte{}) } func (s *S) TestNewErrorMessage(c *C) { call := NewMethodCallMessage("com.destination", "/path", "com.interface", "method") call.serial = 42 call.Sender = ":1.2" reply := NewErrorMessage(call, "com.interface.Error", "message") c.Check(reply.Type, Equals, TypeError) c.Check(reply.Dest, Equals, ":1.2") c.Check(reply.replySerial, Equals, uint32(42)) c.Check(reply.ErrorName, Equals, "com.interface.Error") // No signature or data c.Check(reply.sig, Equals, Signature("s")) var errorMessage string if err := reply.Args(&errorMessage); err != nil { c.Error(err) } c.Check(errorMessage, Equals, "message") } usensord-1.0+14.04.20140404/dbus/signal_test.go0000644000015301777760000001037112317524417021420 0ustar pbusernogroup00000000000000package dbus import ( . "launchpad.net/gocheck" ) func (s *S) TestConnectionWatchSignal(c *C) { bus1, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus1.Close() // Set up a second bus connection to receive a signal. watchReady := make(chan int) complete := make(chan *Message) go func(sender string, watchReady chan<- int, complete chan<- *Message) { bus2, err := Connect(SessionBus) if err != nil { c.Error(err) watchReady <- 0 complete <- nil return } defer bus2.Close() watch, err := bus2.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: sender, Path: "/go/dbus/test", Interface: "com.example.GoDbus", Member: "TestSignal"}) watchReady <- 0 if err != nil { c.Error(err) bus2.Close() complete <- nil return } msg := <-watch.C if err := watch.Cancel(); err != nil { c.Error(err) } complete <- msg }(bus1.UniqueName, watchReady, complete) // Wait for the goroutine to configure the signal watch <-watchReady // Send the signal and wait for it to be received at the other end. signal := NewSignalMessage("/go/dbus/test", "com.example.GoDbus", "TestSignal") if err := bus1.Send(signal); err != nil { c.Fatal(err) } signal2 := <-complete c.Check(signal2, NotNil) } func (s *S) TestConnectionWatchSignalWithBusName(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus.Close() // Request a bus name result, err := bus.busProxy.RequestName("com.example.GoDbus", 0x4) c.Assert(err, IsNil) c.Assert(result, Equals, uint32(1)) // We are Primary Owner // Set up a signal watch received := make(chan *Message, 1) watch, err := bus.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: "com.example.GoDbus", Interface: "com.example.GoDbus", Member: "TestSignal"}) c.Assert(err, IsNil) defer watch.Cancel() // pump received signals messages into our bufferred channel go func() { for msg := range watch.C { received <- msg } }() // Send the signal, and wait to receive it. signal := NewSignalMessage("/go/dbus/test", "com.example.GoDbus", "TestSignal") if err := bus.Send(signal); err != nil { c.Fatal(err) } signal2 := <-received c.Check(signal2, NotNil) } func (s *S) TestSignalWatchSetAdd(c *C) { set := make(signalWatchSet) watch := SignalWatch{rule: MatchRule{ Type: TypeSignal, Sender: ":1.42", Path: "/foo", Interface: "com.example.Foo", Member: "Bar"}} set.Add(&watch) byInterface, ok := set["/foo"] c.Assert(ok, Equals, true) byMember, ok := byInterface["com.example.Foo"] c.Assert(ok, Equals, true) watches, ok := byMember["Bar"] c.Assert(ok, Equals, true) c.Check(watches, DeepEquals, []*SignalWatch{&watch}) } func (s *S) TestSignalWatchSetRemove(c *C) { set := make(signalWatchSet) watch1 := SignalWatch{rule: MatchRule{ Type: TypeSignal, Sender: ":1.42", Path: "/foo", Interface: "com.example.Foo", Member: "Bar"}} set.Add(&watch1) watch2 := SignalWatch{rule: MatchRule{ Type: TypeSignal, Sender: ":1.43", Path: "/foo", Interface: "com.example.Foo", Member: "Bar"}} set.Add(&watch2) c.Check(set.Remove(&watch1), Equals, true) c.Check(set["/foo"]["com.example.Foo"]["Bar"], DeepEquals, []*SignalWatch{&watch2}) // A second attempt at removal fails c.Check(set.Remove(&watch1), Equals, false) } func (s *S) TestSignalWatchSetFindMatches(c *C) { msg := NewSignalMessage("/foo", "com.example.Foo", "Bar") msg.Sender = ":1.42" set := make(signalWatchSet) watch := SignalWatch{rule: MatchRule{ Type: TypeSignal, Sender: ":1.42", Path: "/foo", Interface: "com.example.Foo", Member: "Bar"}} set.Add(&watch) c.Check(set.FindMatches(msg), DeepEquals, []*SignalWatch{&watch}) set.Remove(&watch) // An empty path also matches watch.rule.Path = "" set.Add(&watch) c.Check(set.FindMatches(msg), DeepEquals, []*SignalWatch{&watch}) set.Remove(&watch) // Or an empty interface watch.rule.Path = "/foo" watch.rule.Interface = "" set.Add(&watch) c.Check(set.FindMatches(msg), DeepEquals, []*SignalWatch{&watch}) set.Remove(&watch) // Or an empty member watch.rule.Interface = "com.example.Foo" watch.rule.Member = "" set.Add(&watch) c.Check(set.FindMatches(msg), DeepEquals, []*SignalWatch{&watch}) set.Remove(&watch) } usensord-1.0+14.04.20140404/dbus/LICENSE0000644000015301777760000000204012317524417017554 0ustar pbusernogroup00000000000000Copyright (c) 2009 papamitra Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. usensord-1.0+14.04.20140404/dbus/dbus.go0000644000015301777760000001703612317524417020046 0ustar pbusernogroup00000000000000// Package dbus provides a client interface to the D-Bus IPC system. // It can be used to talk to system services (via the "system bus") or // services within the user's session (via the "session bus"). package dbus import ( "errors" "fmt" "io" "log" "net" "os" "sync" "sync/atomic" ) type StandardBus int const ( SessionBus StandardBus = iota SystemBus ) const ( BUS_DAEMON_NAME = "org.freedesktop.DBus" BUS_DAEMON_PATH = ObjectPath("/org/freedesktop/DBus") BUS_DAEMON_IFACE = "org.freedesktop.DBus" ) type MessageFilter struct { filter func(*Message) *Message } // Connection represents a connection to a message bus. type Connection struct { // The unique name of this connection on the message bus. UniqueName string conn net.Conn busProxy BusDaemon lastSerial uint32 handlerMutex sync.Mutex // covers the next three messageFilters []*MessageFilter methodCallReplies map[uint32]chan<- *Message objectPathHandlers map[ObjectPath]chan<- *Message signalMatchRules signalWatchSet nameInfoMutex sync.Mutex nameInfo map[string]*nameInfo } // ObjectProxy represents a remote object on the bus. It can be used // to simplify constructing method calls, and acts as a basis for // D-Bus interface client stubs. type ObjectProxy struct { bus *Connection destination string path ObjectPath } func (o *ObjectProxy) ObjectPath() ObjectPath { return o.path } // Call the given method on the remote object. // // On success, the reply message will be returned, whose arguments can // be unpacked with its Args() method. // // On failure (both network failures and D-Bus level errors), an error // will be returned. func (o *ObjectProxy) Call(iface, method string, args ...interface{}) (*Message, error) { msg := NewMethodCallMessage(o.destination, o.path, iface, method) if err := msg.AppendArgs(args...); err != nil { return nil, err } reply, err := o.bus.SendWithReply(msg) if err != nil { return nil, err } if reply.Type == TypeError { return nil, reply.AsError() } return reply, nil } func (o *ObjectProxy) WatchSignal(iface, member string) (*SignalWatch, error) { return o.bus.WatchSignal(&MatchRule{ Type: TypeSignal, Sender: o.destination, Path: o.path, Interface: iface, Member: member}) } // Connect returns a connection to the message bus identified by busType. func Connect(busType StandardBus) (*Connection, error) { var address string switch busType { case SessionBus: address = os.Getenv("DBUS_SESSION_BUS_ADDRESS") case SystemBus: if address = os.Getenv("DBUS_SYSTEM_BUS_ADDRESS"); len(address) == 0 { address = "unix:path=/var/run/dbus/system_bus_socket" } default: return nil, errors.New("Unknown bus") } trans, err := newTransport(address) if err != nil { return nil, err } bus := new(Connection) if bus.conn, err = trans.Dial(); err != nil { return nil, err } if err = authenticate(bus.conn, nil); err != nil { bus.conn.Close() return nil, err } bus.busProxy = BusDaemon{bus.Object(BUS_DAEMON_NAME, BUS_DAEMON_PATH)} bus.messageFilters = []*MessageFilter{} bus.methodCallReplies = make(map[uint32]chan<- *Message) bus.objectPathHandlers = make(map[ObjectPath]chan<- *Message) bus.signalMatchRules = make(signalWatchSet) bus.nameInfo = make(map[string]*nameInfo) go bus.receiveLoop() if bus.UniqueName, err = bus.busProxy.Hello(); err != nil { bus.Close() return nil, err } return bus, nil } func (p *Connection) Authenticate() error { log.Println("dbus.Connection.Authenticate() is deprecated. This call can be removed") return nil } func (p *Connection) receiveLoop() { for { msg, err := readMessage(p.conn) if err != nil { if err != io.EOF { log.Println("Failed to read message:", err) } break } if err = p.dispatchMessage(msg); err != nil { log.Println("Error dispatching message:", err) break } } } func (p *Connection) dispatchMessage(msg *Message) error { // Run the message through the registered filters, stopping // processing if a filter returns nil. for _, filter := range p.messageFilters { msg := filter.filter(msg) if msg == nil { return nil } } switch msg.Type { case TypeMethodCall: switch { case msg.Interface == "org.freedesktop.DBus.Peer" && msg.Member == "Ping": reply := NewMethodReturnMessage(msg) if err := p.Send(reply); err != nil { return err } case msg.Interface == "org.freedesktop.DBus.Peer" && msg.Member == "GetMachineId": // Should be returning the UUID found in /var/lib/dbus/machine-id fmt.Println("XXX: handle GetMachineId") reply := NewMethodReturnMessage(msg) if err := reply.AppendArgs("machine-id"); err != nil { return err } if err := p.Send(reply); err != nil { return err } default: p.handlerMutex.Lock() handler, ok := p.objectPathHandlers[msg.Path] p.handlerMutex.Unlock() if ok { handler <- msg } else { reply := NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object path "+string(msg.Path)) if err := p.Send(reply); err != nil { return err } } } case TypeMethodReturn, TypeError: p.handlerMutex.Lock() rs := msg.replySerial replyChan, ok := p.methodCallReplies[rs] if ok { delete(p.methodCallReplies, rs) } p.handlerMutex.Unlock() if ok { replyChan <- msg } case TypeSignal: p.handlerMutex.Lock() watches := p.signalMatchRules.FindMatches(msg) p.handlerMutex.Unlock() for _, watch := range watches { watch.C <- msg } } return nil } func (p *Connection) Close() error { return p.conn.Close() } func (p *Connection) nextSerial() uint32 { return atomic.AddUint32(&p.lastSerial, 1) } func (p *Connection) Send(msg *Message) error { msg.setSerial(p.nextSerial()) if _, err := msg.WriteTo(p.conn); err != nil { return err } return nil } func (p *Connection) SendWithReply(msg *Message) (*Message, error) { // XXX: also check for "no reply" flag. if msg.Type != TypeMethodCall { panic("Only method calls have replies") } serial := p.nextSerial() msg.setSerial(serial) replyChan := make(chan *Message, 1) p.handlerMutex.Lock() p.methodCallReplies[serial] = replyChan p.handlerMutex.Unlock() if _, err := msg.WriteTo(p.conn); err != nil { p.handlerMutex.Lock() delete(p.methodCallReplies, serial) p.handlerMutex.Unlock() return nil, err } reply := <-replyChan return reply, nil } func (p *Connection) RegisterMessageFilter(filter func(*Message) *Message) *MessageFilter { msgFilter := &MessageFilter{filter} p.messageFilters = append(p.messageFilters, msgFilter) return msgFilter } func (p *Connection) UnregisterMessageFilter(filter *MessageFilter) { for i, other := range p.messageFilters { if other == filter { p.messageFilters = append(p.messageFilters[:i], p.messageFilters[i+1:]...) return } } panic("Message filter not registered to this bus") } func (p *Connection) RegisterObjectPath(path ObjectPath, handler chan<- *Message) { p.handlerMutex.Lock() defer p.handlerMutex.Unlock() if _, ok := p.objectPathHandlers[path]; ok { panic("A handler has already been registered for " + string(path)) } p.objectPathHandlers[path] = handler } func (p *Connection) UnregisterObjectPath(path ObjectPath) { p.handlerMutex.Lock() defer p.handlerMutex.Unlock() if _, ok := p.objectPathHandlers[path]; !ok { panic("No handler registered for " + string(path)) } delete(p.objectPathHandlers, path) } // Object returns a proxy for the object identified by the given // destination address and path func (p *Connection) Object(dest string, path ObjectPath) *ObjectProxy { return &ObjectProxy{p, dest, path} } usensord-1.0+14.04.20140404/dbus/matchrule_test.go0000644000015301777760000000243612317524417022132 0ustar pbusernogroup00000000000000package dbus import . "launchpad.net/gocheck" func (s *S) TestMatchRuleToString(c *C) { mr := MatchRule{ Type: TypeSignal, Interface: "org.freedesktop.DBus", Member: "Foo", Path: "/bar/foo"} c.Check(mr.String(), Equals, "type='signal',path='/bar/foo',interface='org.freedesktop.DBus',member='Foo'") // A rule that doesn't match the member mr = MatchRule{ Type: TypeSignal, Interface: "com.example.Foo", Member: "Bar"} c.Check(mr.String(), Equals, "type='signal',interface='com.example.Foo',member='Bar'") } func (s *S) TestMatchRuleMatch(c *C) { msg := NewSignalMessage("", "org.freedesktop.DBus", "NameOwnerChanged") _ = msg.AppendArgs("com.example.Foo", "", ":2.0") mr := MatchRule{ Type: TypeSignal, Interface: "org.freedesktop.DBus", Member: "NameOwnerChanged"} c.Check(mr.Match(msg), Equals, true) mr = MatchRule{ Type: TypeSignal, Interface: "org.freedesktop.DBus", Member: "NameAcquired"} c.Check(mr.Match(msg), Equals, false) // Check matching against first argument. mr = MatchRule{ Type: TypeSignal, Interface: "org.freedesktop.DBus", Member: "NameOwnerChanged", Arg0: "com.example.Foo"} c.Check(mr.Match(msg), Equals, true) mr.Arg0 = "com.example.Bar" c.Check(mr.Match(msg), Equals, false) } usensord-1.0+14.04.20140404/dbus/README.markdown0000644000015301777760000000301112317524417021247 0ustar pbusernogroup00000000000000Documentation ============= Look at the API on [GoPkgDoc](http://gopkgdoc.appspot.com/pkg/github.com/norisatir/go-dbus). Installation ============ go get launchpad.net/~jamesh/go-dbus/trunk Usage ===== An example ---------- ```go // Issue OSD notifications according to the Desktop Notifications Specification 1.1 // http://people.canonical.com/~agateau/notifications-1.1/spec/index.html // See also // https://wiki.ubuntu.com/NotifyOSD#org.freedesktop.Notifications.Notify package main import "launchpad.net/~jamesh/go-dbus/trunk" import "log" func main() { var ( err error conn *dbus.Connection ) // Connect to Session or System buses. if conn, err = dbus.Connect(dbus.SessionBus); err != nil { log.Fatal("Connection error:", err) } if err = conn.Authenticate(); err != nil { log.Fatal("Authentication error:", err) } // Create an object proxy obj := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications") // Call object methods. reply, err := obj.Call("org.freedesktop.Notifications", "Notify", "dbus-tutorial", uint32(0), "", "dbus-tutorial", "You've been notified!", []string{}, map[string]dbus.Variant{}, int32(-1)) if err != nil { log.Fatal("Notification error:", err) } // Parse the reply message var notification_id uint32 if err := reply.GetArgs(¬ification_id); err != nil { log.Fatal(err) } log.Print("Notification id:", notification_id) } ``` usensord-1.0+14.04.20140404/dbus/introspect.go0000644000015301777760000000527512317524417021305 0ustar pbusernogroup00000000000000// +build ignore // // XXX: The method call logic no longer needs introspection data, so // this code is currently unused. I've kept it around since it may be // useful stub generation. package dbus import ( "bytes" "encoding/xml" "strings" ) type annotationData struct { Name string `xml:"name,attr"` Value string `xml:"value,attr"` } type argData struct { Name string `xml:"name,attr"` Type string `xml:"type,attr"` Direction string `xml:"direction,attr"` } type methodData struct { Name string `xml:"name,attr"` Arg []argData `xml:"arg"` Annotation annotationData `xml:"annotation"` } type signalData struct { Name string `xml:"name,attr"` Arg []argData `xml:"arg"` } type interfaceData struct { Name string `xml:"name,attr"` Method []methodData `xml:"method"` Signal []signalData `xml:"signal"` } type introspect struct { Name string `xml:"name,attr"` Interface []interfaceData `xml:"interface"` Node []*Introspect `xml:"node"` } type Introspect interface { GetInterfaceData(name string) InterfaceData } type InterfaceData interface { GetMethodData(name string) MethodData GetSignalData(name string) SignalData GetName() string } type MethodData interface { GetName() string GetInSignature() Signature GetOutSignature() Signature } type SignalData interface { GetName() string GetSignature() Signature } func NewIntrospect(xmlIntro string) (Introspect, error) { intro := new(introspect) buff := bytes.NewBufferString(xmlIntro) err := xml.Unmarshal(buff.Bytes(), intro) if err != nil { return nil, err } return intro, nil } func (p introspect) GetInterfaceData(name string) InterfaceData { for _, v := range p.Interface { if v.Name == name { return v } } return nil } func (p interfaceData) GetMethodData(name string) MethodData { for _, v := range p.Method { if v.GetName() == name { return v } } return nil } func (p interfaceData) GetSignalData(name string) SignalData { for _, v := range p.Signal { if v.GetName() == name { return v } } return nil } func (p interfaceData) GetName() string { return p.Name } func (p methodData) GetInSignature() (sig Signature) { for _, v := range p.Arg { if strings.ToUpper(v.Direction) == "IN" { sig += Signature(v.Type) } } return } func (p methodData) GetOutSignature() (sig Signature) { for _, v := range p.Arg { if strings.ToUpper(v.Direction) == "OUT" { sig += Signature(v.Type) } } return } func (p methodData) GetName() string { return p.Name } func (p signalData) GetSignature() (sig Signature) { for _, v := range p.Arg { sig += Signature(v.Type) } return } func (p signalData) GetName() string { return p.Name } usensord-1.0+14.04.20140404/dbus/proxy.go0000644000015301777760000001001712317524417020262 0ustar pbusernogroup00000000000000package dbus // This is not yet finished: it is an idea for what statically generated object bindings could look like. type Introspectable struct { *ObjectProxy } func (o *Introspectable) Introspect() (data string, err error) { reply, err := o.Call("org.freedesktop.DBus.Introspectable", "Introspect") if err != nil { return } err = reply.Args(&data) return } type Properties struct { *ObjectProxy } func (o *Properties) Get(interfaceName string, propertyName string) (value interface{}, err error) { reply, err := o.Call("org.freedesktop.DBus.Properties", "Get", interfaceName, propertyName) if err != nil { return } var variant Variant err = reply.Args(&variant) value = variant.Value return } func (o *Properties) Set(interfaceName string, propertyName string, value interface{}) (err error) { _, err = o.Call("org.freedesktop.DBus.Properties", "Set", interfaceName, propertyName, Variant{value}) return } func (o *Properties) GetAll(interfaceName string) (props map[string]Variant, err error) { reply, err := o.Call("org.freedesktop.DBus.Properties", "GetAll", interfaceName) if err != nil { return } err = reply.Args(&props) return } type BusDaemon struct { *ObjectProxy } func (o *BusDaemon) Hello() (uniqueName string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "Hello") if err != nil { return } err = reply.Args(&uniqueName) return } func (o *BusDaemon) RequestName(name string, flags uint32) (result uint32, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "RequestName", name, flags) if err != nil { return } err = reply.Args(&result) return } func (o *BusDaemon) ReleaseName(name string) (result uint32, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "ReleaseName", name) if err != nil { return } err = reply.Args(&result) return } func (o *BusDaemon) ListQueuedOwners(name string) (owners []string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "ListQueuedOwners", name) if err != nil { return } err = reply.Args(&owners) return } func (o *BusDaemon) ListNames() (names []string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "ListNames") if err != nil { return } err = reply.Args(&names) return } func (o *BusDaemon) ListActivatableNames() (names []string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "ListActivatableNames") if err != nil { return } err = reply.Args(&names) return } func (o *BusDaemon) NameHasOwner(name string) (hasOwner bool, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "NameHasOwner", name) if err != nil { return } err = reply.Args(&hasOwner) return } func (o *BusDaemon) StartServiceByName(name string, flags uint32) (result uint32, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "StartServiceByName", name, flags) if err != nil { return } err = reply.Args(&result) return } func (o *BusDaemon) UpdateActivationEnvironment(env map[string]string) (err error) { _, err = o.Call(BUS_DAEMON_IFACE, "UpdateActivationEnvironment", env) return } func (o *BusDaemon) GetNameOwner(name string) (owner string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "GetNameOwner", name) if err != nil { return } err = reply.Args(&owner) return } func (o *BusDaemon) GetConnectionUnixUser(busName string) (user uint32, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "GetConnectionUnixUser", busName) if err != nil { return } err = reply.Args(&user) return } func (o *BusDaemon) GetConnectionUnixProcessID(busName string) (process uint32, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "GetConnectionUnixProcessID", busName) if err != nil { return } err = reply.Args(&process) return } func (o *BusDaemon) AddMatch(rule string) (err error) { _, err = o.Call(BUS_DAEMON_IFACE, "AddMatch", rule) return } func (o *BusDaemon) RemoveMatch(rule string) (err error) { _, err = o.Call(BUS_DAEMON_IFACE, "RemoveMatch", rule) return } func (o *BusDaemon) GetId() (busId string, err error) { reply, err := o.Call(BUS_DAEMON_IFACE, "GetId") if err != nil { return } err = reply.Args(&busId) return } usensord-1.0+14.04.20140404/dbus/suite_test.go0000644000015301777760000000022212317524417021266 0ustar pbusernogroup00000000000000package dbus import ( . "launchpad.net/gocheck" "testing" ) func TestAll(t *testing.T) { TestingT(t) } type S struct{} var _ = Suite(&S{}) usensord-1.0+14.04.20140404/dbus/dbus_test.go0000644000015301777760000000410012317524417021071 0ustar pbusernogroup00000000000000package dbus import ( "fmt" . "launchpad.net/gocheck" ) type callTest struct { dest string path ObjectPath iface, method string args []interface{} validate func(*Message) error } var callTests = []callTest{ {"org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "Notify", []interface{}{ "go-dbus", uint32(0), "info", "testing go-dbus", "test_body", []string{}, map[string]Variant{}, int32(2000)}, func(*Message) error { return nil }}, } func (test callTest) Call(c *Connection) error { proxy := c.Object(test.dest, test.path) reply, err := proxy.Call(test.iface, test.method, test.args...) if err != nil { return fmt.Errorf("failed Method.Call: %v", err) } if err = test.validate(reply); err != nil { err = fmt.Errorf("failed validation: %v", err) } return err } func (s *S) TestDBus(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus.Close() for i, test := range callTests { err = test.Call(bus) if err != nil { c.Errorf("callTest %d: %v", i, err) } } } func (s *S) TestConnectionConnectSessionBus(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) c.Check(bus.Close(), IsNil) } func (s *S) TestConnectionConnectSystemBus(c *C) { bus, err := Connect(SystemBus) c.Assert(err, IsNil) c.Check(bus.Close(), IsNil) } func (s *S) TestConnectionRegisterMessageFilter(c *C) { bus, err := Connect(SessionBus) c.Assert(err, IsNil) defer bus.Close() filter := bus.RegisterMessageFilter(func(msg *Message) *Message { // Make a change that shows the filter ran. if msg.Type == TypeMethodReturn { if err := msg.AppendArgs("Added by filter"); err != nil { c.Error(err) } } return msg }) c.Check(filter, NotNil) defer bus.UnregisterMessageFilter(filter) msg := NewMethodCallMessage(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, "GetId") reply, err := bus.SendWithReply(msg) c.Assert(err, IsNil) var busId, extra string c.Assert(reply.Args(&busId, &extra), IsNil) c.Assert(extra, Equals, "Added by filter") } usensord-1.0+14.04.20140404/usensord.go0000644000015301777760000000401112317524417020003 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * Authors: * Michael Frey: michael.frey@canonical.com * * This file is part of usensord. * * usensord is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * usensord is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package main import ( "launchpad.net/usensord/haptic" "log" "os" "os/signal" "syscall" ) var logger *log.Logger var done = false type Mainloop struct { sigchan chan os.Signal termchan chan int Bindings map[os.Signal]func() } /* Start the mainloop. This method will block its current thread. The best spot for calling this method is right near the bottom of your application's main() function. */ func (m *Mainloop) Start() { sigs := make([]os.Signal, len(m.Bindings)) for s, _ := range m.Bindings { sigs = append(sigs, s) } signal.Notify(m.sigchan, sigs...) for { select { case sig := <-m.sigchan: m.Bindings[sig]() case _ = <-m.termchan: break } } return } /* Stops the mainloop. */ func (m *Mainloop) Stop() { go func() { m.termchan <- 1 }() return } func HupHandler() { syscall.Exit(1) } func IntHandler() { syscall.Exit(1) } func init() { logger = log.New(os.Stderr, "uSensord: ", log.Ldate|log.Ltime|log.Lshortfile) } func main() { err := haptic.Init(logger) if err != nil { logger.Println("Error starting haptic service") } logger.Println("uSensord starting...") m := Mainloop{ sigchan: make(chan os.Signal), termchan: make(chan int), Bindings: make(map[os.Signal]func())} m.Bindings[syscall.SIGHUP] = HupHandler m.Bindings[syscall.SIGINT] = IntHandler m.Start() } usensord-1.0+14.04.20140404/COPYING0000644000015301777760000007724612317524417016671 0ustar pbusernogroup00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. usensord-1.0+14.04.20140404/haptic/0000755000015301777760000000000012317524560017065 5ustar pbusernogroup00000000000000usensord-1.0+14.04.20140404/haptic/haptic_test.go0000644000015301777760000000344212317524417021727 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * Authors: * Michael Frey: michael.frey@canonical.com * * This file is part of usensord. * * usensord is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * usensord is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package haptic import ( "launchpad.net/usensord/dbus" "log" "os" "testing" ) func init() { logger = log.New(os.Stderr, "uSensord: ", log.Ldate|log.Ltime|log.Lshortfile) var err error conn, err = dbus.Connect(dbus.SessionBus) if err != nil { logger.Fatal("Connection error:", err) } err = Init(logger) if err != nil { logger.Fatal("Error: %s\n", err) } } func TestHapticDBUS(t *testing.T) { obj := conn.Object("com.canonical.usensord.haptic", "/com/canonical/usensord/haptic") reply, err := obj.Call("com.canonical.usensord.haptic", "Vibrate", uint32(10)) if err != nil || reply.Type == dbus.TypeError { logger.Println("FAILED") t.Errorf("Notification error: %s", err) } } func TestPatternHapticDBUS(t *testing.T) { pattern := []uint32{uint32(10), uint32(100), uint32(200), uint32(10)} obj := conn.Object("com.canonical.usensord.haptic", "/com/canonical/usensord/haptic") reply, err := obj.Call("com.canonical.usensord.haptic", "VibratePattern", pattern) if err != nil || reply.Type == dbus.TypeError { logger.Println("FAILED") t.Errorf("Notification error: %s", err) } } usensord-1.0+14.04.20140404/haptic/haptic.go0000644000015301777760000000644412317524417020675 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * Authors: * Michael Frey: michael.frey@canonical.com * * This file is part of usensord. * * usensord is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * usensord is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package haptic import ( "fmt" "launchpad.net/usensord/dbus" "log" "os" "time" ) var ( conn *dbus.Connection logger *log.Logger ) const ( HAPTIC_DBUS_IFACE = "com.canonical.usensord.haptic" HAPTIC_DEVICE = "/sys/class/timed_output/vibrator/enable" ) func watchDBusMethodCalls(msgChan <-chan *dbus.Message) { var reply *dbus.Message for msg := range msgChan { switch { case msg.Interface == HAPTIC_DBUS_IFACE && msg.Member == "Vibrate": var duration uint32 msg.Args(&duration) logger.Printf("Received Vibrate() method call %d", duration) if err := Vibrate(duration); err != nil { reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error()) } else { reply = dbus.NewMethodReturnMessage(msg) } case msg.Interface == HAPTIC_DBUS_IFACE && msg.Member == "VibratePattern": var pattern []uint32 msg.Args(&pattern) logger.Print("Received VibratePattern() method call", pattern) if err := VibratePattern(pattern); err != nil { reply = dbus.NewErrorMessage(msg, "com.canonical.usensord.Error", err.Error()) } else { reply = dbus.NewMethodReturnMessage(msg) } default: logger.Println("Received unkown method call on", msg.Interface, msg.Member) reply = dbus.NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownMethod", "Unknown method") } if err := conn.Send(reply); err != nil { logger.Println("Could not send reply:", err) } } } func Vibrate(duration uint32) error { return VibratePattern([]uint32{duration}) } func VibratePattern(duration []uint32) (err error) { fi, err := os.Create(HAPTIC_DEVICE) if err != nil { logger.Println("Error opening haptic device") return err } x := true go func() { defer fi.Close() for _, t := range duration { if x { if _, err := fi.WriteString(fmt.Sprintf("%d", t)); err != nil { logger.Println(err) } x = false } else { x = true } time.Sleep(time.Duration(t) * time.Millisecond) } }() return nil } /*Initialize Haptic service and register on the bus*/ func Init(log *log.Logger) (err error) { logger = log if conn, err = dbus.Connect(dbus.SessionBus); err != nil { logger.Fatal("Connection error:", err) return err } nameAcquired := make(chan int, 1) name := conn.RequestName("com.canonical.usensord.haptic", dbus.NameFlagDoNotQueue, func(*dbus.BusName) { nameAcquired <- 0 }, nil) <-nameAcquired logger.Printf("Successfully registerd %s on the bus", name) ch := make(chan *dbus.Message) go watchDBusMethodCalls(ch) conn.RegisterObjectPath("/com/canonical/usensord/haptic", ch) logger.Println("Connected to DBUS") return nil }