-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
357 lines (309 loc) · 7.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
"github.com/godbus/dbus"
"golang.org/x/sys/unix"
"github.com/urfave/cli/v2"
)
type DBUS_TYPE int
const (
SESSION DBUS_TYPE = iota
SYSTEM
)
type ProcessInfo struct {
pid uint32
cmd string
sender string
uid uint32
child *ProcessInfo
}
func (p *ProcessInfo) Display() string {
prefix := "↳"
tab := ""
output := ""
current := p
for {
output = fmt.Sprintf("%s%s%s[%d][%d][%s] %s\n", output, tab, prefix, current.pid, current.uid, current.sender, current.cmd)
tab = tab + " "
if current.child == nil {
break
}
current = current.child
}
return output
}
func main() {
app := &cli.App{
Name: "dbus-tool",
Version: "2.0.0",
Usage: "dbus connection debug tool!",
Action: func(c *cli.Context) error {
cli.ShowAppHelpAndExit(c, 0)
return nil
},
Commands: []*cli.Command{
{
Name: "monitor",
Aliases: []string{"m"},
Usage: "monitor dbus connection",
Action: func(c *cli.Context) error {
address := c.String("address")
progress := c.String("progress")
var conn *dbus.Conn
var err error
if address == "system" {
conn, err = dbus.SystemBus()
if err != nil {
return err
}
} else {
conn, err = dbus.SessionBus()
if err != nil {
return err
}
}
dbusNamesInfo(conn, progress)
dbusMonitor(conn, progress)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Value: "session",
Usage: "set dbus address(session/system)",
},
&cli.StringFlag{
Name: "progress",
Aliases: []string{"p"},
Usage: "set which progess to filter",
},
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list dbus connection",
Action: func(c *cli.Context) error {
address := c.String("address")
progress := c.String("progress")
sender := c.String("sender")
var conn *dbus.Conn
var err error
if address == "system" {
conn, err = dbus.SystemBus()
if err != nil {
return err
}
} else {
conn, err = dbus.SessionBus()
if err != nil {
return err
}
}
defer conn.Close()
if sender != "" {
p, err := GetPidTreeBySender(conn, sender, "")
if err != nil {
return err
}
fmt.Println(p.Display())
} else if progress != "" {
dbusNamesInfo(conn, progress)
} else if progress == "" && sender == "" {
dbusNamesInfo(conn, "")
}
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Value: "session",
Usage: "set dbus address(session/system)",
},
&cli.StringFlag{
Name: "progress",
Aliases: []string{"p"},
Usage: "set which progess to filter",
},
&cli.StringFlag{
Name: "sender",
Aliases: []string{"s"},
Usage: "set which sender to filter",
},
},
},
},
}
app.EnableBashCompletion = true
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func dbusNamesInfo(conn *dbus.Conn, rule string) {
names := listNames(conn)
if len(names) == 0 {
return
}
for _, name := range names {
//fmt.Println("name:", name)
p, err := GetPidTreeBySender(conn, name, rule)
if err != nil {
continue
}
fmt.Println(time.Now(), "=======================")
fmt.Println(p.Display())
}
}
func dbusMonitor(conn *dbus.Conn, rule string) {
obj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus").(*dbus.Object)
err := obj.AddMatchSignal("org.freedesktop.DBus", "NameOwnerChanged").Err
if err != nil {
return
}
ch := make(chan *dbus.Signal, 100)
conn.Signal(ch)
for signal := range ch {
signalProcess(conn, signal, rule)
}
}
func getConnectionUnixProcessID(conn *dbus.Conn, name string) (uint32, error) {
var pid uint32
obj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
err := obj.Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
if err != nil {
return 0, err
}
return pid, nil
}
func getConnectionUnixUser(conn *dbus.Conn, name string) (uint32, error) {
var uid uint32
obj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
err := obj.Call("org.freedesktop.DBus.GetConnectionUnixUser", 0, name).Store(&uid)
if err != nil {
return 0, err
}
return uid, nil
}
func listNames(conn *dbus.Conn) []string {
names := make([]string, 0)
obj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
err := obj.Call("org.freedesktop.DBus.ListNames", 0).Store(&names)
if err != nil {
return nil
}
return names
}
func signalProcess(conn *dbus.Conn, sig *dbus.Signal, rule string) error {
if len(sig.Body) > 1 {
name := sig.Body[0].(string)
oldName := sig.Body[1].(string)
newName := sig.Body[2].(string)
if name == "" {
return fmt.Errorf("name is nil")
}
if oldName == "" && newName != "" {
p, err := GetPidTreeBySender(conn, newName, rule)
if err != nil {
if err != fmt.Errorf("match error.") {
return err
}
return nil
}
fmt.Println(p.Display())
}
}
return nil
}
func GetPidInfo(pid uint32) (string, *linuxproc.ProcessStat, uint32, error) {
pidFile := fmt.Sprintf("/proc/%d", pid)
var fileStat unix.Stat_t
//获取/proc/$pid文件夹stat,该文件夹stat的uid权限可以代表进程uid
err := unix.Stat(pidFile, &fileStat)
if err != nil {
return "", nil, 0, err
}
fileName := fmt.Sprintf("/proc/%d/cmdline", pid)
fileData, err := ioutil.ReadFile(fileName)
if err != nil {
log.Println("read file error =", err)
return "", nil, 0, err
}
//通过hexdump发现,读出来的数据使用\x0分割,为了正常显示,需要使用\x20替换一下。
cmdline := bytes.ReplaceAll(fileData, []byte{0x00}, []byte{0x20})
statName := fmt.Sprintf("/proc/%d/stat", pid)
stat, err := linuxproc.ReadProcessStat(statName)
if err != nil {
return "", nil, 0, err
}
return string(cmdline), stat, fileStat.Uid, nil
}
func GetPidTreeBySender(conn *dbus.Conn, name string, progress string) (*ProcessInfo, error) {
pid, err := getConnectionUnixProcessID(conn, name)
if err != nil {
return nil, err
}
/*uid, err := getConnectionUnixUser(conn, name)
if err != nil {
return nil, err
}*/
var node *ProcessInfo
hasMatch := false
for pid != 0 {
cmdline, stat, uid, err := GetPidInfo(pid)
if err != nil {
break
}
if progress != "" && !hasMatch && strings.Contains(cmdline, progress) && name != "" {
hasMatch = true
}
if node != nil {
parent := &ProcessInfo{
pid: pid,
uid: uid,
cmd: cmdline,
sender: name,
child: node,
}
node = parent
} else {
node = &ProcessInfo{
pid: pid,
uid: uid,
cmd: cmdline,
sender: name,
}
}
name = ""
pid = uint32(stat.Ppid)
}
if !hasMatch && progress != "" {
return nil, fmt.Errorf("match error.")
}
if node == nil {
return nil, fmt.Errorf("find node info error")
}
return node, nil
}
func displayPidTree(pid uint32, prefix string) {
for pid != 0 {
cmdline, stat, _, err := GetPidInfo(pid)
if err != nil {
break
}
fmt.Printf("%sprocess:%d -- %s\n", prefix, pid, cmdline)
//log.Println("ppid=", stat.Ppid)
pid = uint32(stat.Ppid)
prefix = " " + prefix
}
fmt.Println("")
}