forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_process.go
167 lines (142 loc) · 4.12 KB
/
session_process.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
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"fmt"
"crypto/rand"
"io"
"os"
"os/exec"
"time"
dbus "github.com/godbus/dbus/v5"
)
var launchTimeout = 30 * time.Second
func genUuid() string {
uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid)
if n != len(uuid) || err != nil {
panic("This can failed?")
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
func (m *SessionManager) startSessionDaemonPart2() bool {
sessionBus, err := dbus.SessionBus()
if err != nil {
logger.Warning(err)
return false
}
timeStart := time.Now()
sessionDaemonObj := sessionBus.Object("org.deepin.dde.Daemon1", "/org/deepin/dde/Daemon1")
err = sessionDaemonObj.Call("org.deepin.dde.Daemon1.StartPart2",
dbus.FlagNoAutoStart).Err
logger.Info("start dde-session-daemon part2 cost:", time.Since(timeStart))
m.allowSessionDaemonRun = true
if err != nil {
logger.Warning(err)
return false
}
return true
}
// 如果 endFn 为 nil,则等待命令完成或结束;如果 endFn 不为 nil,则不等待,命令行启动后就返回,命令完成或结束后调用 endFn。
func (m *SessionManager) launchWaitAux(cookie, program string, args []string, cmdWaitDelay time.Duration, endFn func(bool)) (launchOk bool) {
cmd := exec.Command(program, args...)
cmd.Env = append(os.Environ(), "DDE_SESSION_PROCESS_COOKIE_ID="+cookie)
ch := make(chan time.Time, 1)
m.cookieLocker.Lock()
m.cookies[cookie] = ch
m.cookieLocker.Unlock()
cmdStr := fmt.Sprintf("%s %v", program, args)
timeStart := time.Now()
err := cmd.Start()
if err != nil {
logger.Warningf("start command %s failed: %v", cmdStr, err)
if endFn != nil {
endFn(launchOk)
}
return false
}
logger.Infof("command %s started, pid: %v", cmdStr, cmd.Process.Pid)
time.AfterFunc(cmdWaitDelay, func() {
err := cmd.Wait()
if err != nil {
logger.Warningf("command %s exit with error: %v", cmdStr, err)
}
m.cookieLocker.Lock()
ch := m.cookies[cookie]
if ch != nil {
delete(m.cookies, cookie)
ch <- time.Now()
}
m.cookieLocker.Unlock()
})
waitCh := func() {
select {
case timeEnd := <-ch:
logger.Info(cmdStr, "startup duration:", timeEnd.Sub(timeStart))
launchOk = true
case timeEnd := <-time.After(launchTimeout):
logger.Info(cmdStr, "startup timed out!", timeEnd.Sub(timeStart))
}
}
if endFn != nil {
go func() {
waitCh()
endFn(launchOk)
}()
} else {
waitCh()
}
return
}
func (m *SessionManager) launchWaitCore(name string, program string, args []string, cmdWaitDelay time.Duration, endFn func(bool)) {
m.launchWaitAux(name, program, args, cmdWaitDelay, endFn)
}
func (m *SessionManager) launchWait(program string, args ...string) bool {
cookie := genUuid()
return m.launchWaitAux(cookie, program, args, 0, nil)
}
func (m *SessionManager) launchWithoutWait(bin string, args ...string) {
cmd := exec.Command(bin, args...)
go func() {
err := cmd.Run()
if err != nil {
logger.Warningf("launchWithoutWait %v %v exit with error: %v", bin, args, err)
}
}()
}
func (m *SessionManager) launch(bin string, wait bool, args ...string) bool {
if bin == "dde-session-daemon-part2" {
return m.startSessionDaemonPart2()
}
if swapSchedDispatcher != nil {
cgroupPath := swapSchedDispatcher.GetDECGroup()
argsTemp := []string{"-g", "memory:" + cgroupPath, bin}
args = append(argsTemp, args...)
bin = globalCgExecBin
}
logger.Debugf("sessionManager.launch %q %v", bin, args)
if wait {
return m.launchWait(bin, args...)
}
m.launchWithoutWait(bin, args...)
return true
}
func (m *SessionManager) AllowSessionDaemonRun() (bool, *dbus.Error) {
return m.allowSessionDaemonRun, nil
}
func (m *SessionManager) Register(id string) (bool, *dbus.Error) {
m.cookieLocker.Lock()
defer m.cookieLocker.Unlock()
timeCh := m.cookies[id]
if timeCh == nil {
return false, nil
}
delete(m.cookies, id)
timeCh <- time.Now()
return true, nil
}