Debian Codegraph @

client/service/common.go

raw
1 /*
2 Copyright 2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // package service implements the dbus-level service with which client
18 // applications are expected to interact.
19 package service
20
21 import (
22 "errors"
23 "fmt"
24 "strings"
25 "sync"
26
27 "github.com/godbus/dbus/v5"
28
29 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
30 "gitlab.com/ubports/development/core/lomiri-push-service/click"
31 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
32 "gitlab.com/ubports/development/core/lomiri-push-service/nih"
33 )
34
35 type DBusService struct {
36 lock sync.RWMutex
37 state ServiceState
38 installedChecker click.InstalledChecker
39 Log logger.Logger
40 Bus bus.Endpoint
41 }
42
43 // the service can be in a numnber of states
44 type ServiceState uint8
45
46 const (
47 StateUnknown ServiceState = iota
48 StateRunning // Start() has been successfully called
49 StateFinished // Stop() has been successfully called
50 )
51
52 var (
53 ErrNotConfigured = errors.New("not configured")
54 ErrAlreadyStarted = errors.New("already started")
55 ErrBadArgCount = errors.New("wrong number of arguments")
56 ErrBadArgType = errors.New("bad argument type")
57 ErrBadJSON = errors.New("bad json data")
58 ErrAppIdMismatch = errors.New("package must be prefix of app id")
59 )
60
61 func MakeDBusNoObjectErrorWithReason(path string, err error) *dbus.Error {
62 return dbus.NewError(
63 "org.freedesktop.DBus.Error.NoSuchObject",
64 []interface{}{fmt.Sprintf("No such object '%s' ('%s')",
65 path, err.Error())},
66 )
67 }
68
69 // IsRunning() returns whether the service's state is StateRunning
70 func (svc *DBusService) IsRunning() bool {
71 svc.lock.RLock()
72 defer svc.lock.RUnlock()
73 return svc.state == StateRunning
74 }
75
76 // Start() dials the bus, grab the name, and listens for method calls.
77 func (svc *DBusService) Start(dispatchMap bus.DispatchMap, busAddr bus.Address, init func() error) error {
78 svc.lock.Lock()
79 defer svc.lock.Unlock()
80 if svc.state != StateUnknown {
81 return ErrAlreadyStarted
82 }
83 if svc.Log == nil || svc.Bus == nil {
84 return ErrNotConfigured
85 }
86 err := svc.Bus.Dial()
87 if err != nil {
88 return err
89 }
90 ch := svc.Bus.GrabName(true)
91 log := svc.Log
92 go func() {
93 for err := range ch {
94 if !svc.IsRunning() {
95 break
96 }
97 if err != nil {
98 log.Fatalf("name channel for %s got: %v",
99 busAddr.Name, err)
100 }
101 }
102 }()
103 if init != nil {
104 if err = init(); err != nil {
105 return err
106 }
107 }
108 if err := svc.Bus.WatchMethod(dispatchMap, "/*"); err != nil {
109 return err
110 }
111 svc.state = StateRunning
112 return nil
113 }
114
115 // Stop() closes the bus and sets the state to StateFinished
116 func (svc *DBusService) Stop() {
117 svc.lock.Lock()
118 defer svc.lock.Unlock()
119 if svc.Bus != nil {
120 svc.Bus.Close()
121 }
122 svc.state = StateFinished
123 }
124
125 // grabDBusPackageAndAppId() checks the appID against the package in the last
126 // element of the dbus path.
127 func (svc *DBusService) grabDBusPackageAndAppId(msg *dbus.Message, id string) (app *click.AppId, err error) {
128 var path string
129 if err := msg.Headers[dbus.FieldPath].Store(&path); err != nil {
130 panic(err)
131 }
132 pkgname := string(nih.Unquote([]byte(path[strings.LastIndex(path, "/")+1:])))
133 app, err = click.ParseAndVerifyAppId(id, svc.installedChecker)
134 if err != nil {
135 return nil, err
136 }
137 if !app.InPackage(pkgname) {
138 return nil, ErrAppIdMismatch
139 }
140 return
141 }