forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
1066 lines (933 loc) · 26.1 KB
/
session.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"sync"
"syscall"
"time"
"github.com/godbus/dbus/v5"
"github.com/linuxdeepin/dde-api/soundutils"
sysbt "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.bluetooth1"
daemon "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.daemon1"
powermanager "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.powermanager1"
ofdbus "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.dbus"
login1 "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.login1"
systemd1 "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.systemd1"
"github.com/linuxdeepin/go-lib/appinfo/desktopappinfo"
"github.com/linuxdeepin/go-lib/cgroup"
"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/keyfile"
"github.com/linuxdeepin/go-lib/log"
"github.com/linuxdeepin/go-lib/procfs"
"github.com/linuxdeepin/go-lib/xdg/basedir"
x "github.com/linuxdeepin/go-x11-client"
"github.com/linuxdeepin/go-x11-client/ext/dpms"
"github.com/linuxdeepin/startdde/memchecker"
"github.com/linuxdeepin/startdde/swapsched"
)
const (
sectionTheme = "Theme"
keyIconThemeName = "IconThemeName"
keyFont = "Font"
keyMonoFont = "MonFont"
keyFontSize = "FontSize"
xsKeyQtFontName = "Qt/FontName"
xsKeyQtMonoFontName = "Qt/MonoFontName"
ddeLockDesktopFile = "/usr/share/applications/dde-lock.desktop"
)
type SessionManager struct {
service *dbusutil.Service
mu sync.Mutex
Locked bool
CurrentUid string
cookieLocker sync.Mutex
cookies map[string]chan time.Time
Stage int32
allowSessionDaemonRun bool
loginSession login1.Session
dbusDaemon ofdbus.DBus // session bus daemon
sigLoop *dbusutil.SignalLoop // session bus signal loop
powerManager powermanager.PowerManager
sysBt sysbt.Bluetooth
CurrentSessionPath dbus.ObjectPath
objLogin login1.Manager
objLoginSessionSelf login1.Session
daemon daemon.Daemon
inCallRequestLock bool
inCallRequestLockMu sync.Mutex
//nolint
signals *struct {
Unlock struct{}
InhibitorAdded, InhibitorRemoved struct {
path dbus.ObjectPath
}
}
}
const (
cmdShutdown = "/usr/bin/dde-shutdown"
lockFrontDest = "org.deepin.dde.LockFront1"
lockFrontIfc = lockFrontDest
lockFrontObjPath = "/org/deepin/dde/LockFront1"
)
const (
SessionStageInitBegin int32 = iota
SessionStageInitEnd
SessionStageCoreBegin
SessionStageCoreEnd // nolint
SessionStageAppsBegin
SessionStageAppsEnd
)
var (
swapSchedDispatcher *swapsched.Dispatcher
)
func (m *SessionManager) CanLogout() (bool, *dbus.Error) {
return true, nil
}
func (m *SessionManager) Logout() *dbus.Error {
m.launch(cmdShutdown, false)
return nil
}
func stopBAMFDaemon() {
// NOTE: Proactively stop the bamfdaemon service.
// If you don't do this, it will exit in the failed state because X Server exits,
// causing the restart to be too frequent and not being started properly.
// This is a temporary workaround.
bus, err := dbus.SessionBus()
if err == nil {
systemdUser := bus.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
var jobPath dbus.ObjectPath
err = systemdUser.Call("org.freedesktop.systemd1.Manager.StopUnit",
dbus.FlagNoAutoStart, "bamfdaemon.service", "replace").Store(&jobPath)
if err != nil {
logger.Warning("failed to stop bamfdaemon.service:", err)
}
} else {
logger.Warning(err)
}
}
func stopRedshift() {
bus, err := dbus.SessionBus()
if err != nil {
logger.Warning(err)
return
}
systemdManger := systemd1.NewManager(bus)
_, err = systemdManger.StopUnit(0, "redshift.service", "replace")
if err != nil {
logger.Warning("failed to stop redshift.service:", err)
}
}
func (m *SessionManager) prepareLogout(force bool) {
// if !force {
// err := autostop.LaunchAutostopScripts(logger)
// if err != nil {
// logger.Warning("failed to run auto script:", err)
// }
// }
killSogouImeWatchdog()
// kill process LangSelector ,because LangSelector will not be kill by common logout
killLangSelector()
// quit at-spi-dbus-bus.service
quitAtSpiService()
stopBAMFDaemon()
stopRedshift()
quitObexService()
//注销系统断开所有蓝牙连接
m.sysBt.DisconnectAudioDevices(0)
if !force && soundutils.CanPlayEvent(soundutils.EventDesktopLogout) {
// playLogoutSound 内部会退出 pulseaudio
playLogoutSound()
} else {
quitPulseAudio()
}
}
// kill process LangSelector by cmd "pkill -ef -u $UID /usr/lib/deepin-daemon/langselector"
func killLangSelector() {
u, err := user.Current()
if err != nil {
logger.Debug("failed to get current user:", err)
}
out, err := exec.Command("pkill", "-ef", "-u", u.Uid, "/usr/lib/deepin-daemon/langselector").Output()
if err != nil {
logger.Debug("failed to kill langselector:", err)
} else {
logger.Infof("kill langselector out:%s", out)
}
}
func (m *SessionManager) RequestLogout() *dbus.Error {
logger.Info("RequestLogout")
m.logout(false)
return nil
}
func (m *SessionManager) ForceLogout() *dbus.Error {
logger.Info("ForceLogout")
m.logout(true)
return nil
}
func (m *SessionManager) clearCurrentTty() {
if m.loginSession == nil {
return
}
vTNr, err := m.loginSession.VTNr().Get(0)
if err != nil {
logger.Warning("clearCurrentTty:", err)
return
}
err = m.daemon.ClearTty(0, vTNr)
if err != nil {
logger.Warning("clearCurrentTty:", err)
}
return
}
func (m *SessionManager) logout(force bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.prepareLogout(force)
m.clearCurrentTty()
m.doLogout(force)
}
func (m *SessionManager) CanShutdown() (bool, *dbus.Error) {
str, err := m.objLogin.CanPowerOff(0) // 当前能否关机
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
return str == "yes", nil
}
func (m *SessionManager) Shutdown() *dbus.Error {
m.launch(cmdShutdown, false)
return nil
}
func (m *SessionManager) prepareShutdown(force bool) {
killSogouImeWatchdog()
stopBAMFDaemon()
if !force {
preparePlayShutdownSound()
}
quitPulseAudio()
}
func killSogouImeWatchdog() {
out, err := exec.Command("pkill", "-ef", "sogouImeService").Output()
if err != nil {
logger.Debug("failed to kill sogouIme watchdog:", err)
} else {
logger.Infof("kill sogouIme out:%s", out)
}
}
func (m *SessionManager) RequestShutdown() *dbus.Error {
logger.Info("RequestShutdown")
m.shutdown(false)
return nil
}
func (m *SessionManager) ForceShutdown() *dbus.Error {
logger.Info("ForceShutdown")
m.shutdown(true)
return nil
}
func (m *SessionManager) shutdown(force bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.prepareShutdown(force)
m.clearCurrentTty()
err := m.objLogin.PowerOff(0, false)
if err != nil {
logger.Warning("failed to call login PowerOff:", err)
}
if _gSettingsConfig.needQuickBlackScreen {
setDPMSMode(false)
}
if m.objLoginSessionSelf != nil {
err = m.objLoginSessionSelf.Terminate(0)
if err != nil {
logger.Warning("failed to terminate session self:", err)
}
}
os.Exit(0)
}
func (m *SessionManager) CanReboot() (bool, *dbus.Error) {
str, err := m.objLogin.CanReboot(0) // 当前能否重启
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
return str == "yes", nil
}
func (m *SessionManager) Reboot() *dbus.Error {
m.launch(cmdShutdown, false)
return nil
}
func (m *SessionManager) RequestReboot() *dbus.Error {
logger.Info("RequestReboot")
m.reboot(false)
return nil
}
func (m *SessionManager) ForceReboot() *dbus.Error {
logger.Info("ForceReboot")
m.reboot(true)
return nil
}
func (m *SessionManager) reboot(force bool) {
m.mu.Lock()
defer m.mu.Unlock()
m.prepareShutdown(force)
m.clearCurrentTty()
err := m.objLogin.Reboot(0, false)
if err != nil {
logger.Warning("failed to call login Reboot:", err)
}
if _gSettingsConfig.needQuickBlackScreen {
setDPMSMode(false)
}
if m.objLoginSessionSelf != nil {
err = m.objLoginSessionSelf.Terminate(0)
if err != nil {
logger.Warning("failed to terminate session self:", err)
}
}
os.Exit(0)
}
func (m *SessionManager) CanSuspend() (bool, *dbus.Error) {
if os.Getenv("POWER_CAN_SLEEP") == "0" {
logger.Info("can not Suspend, env POWER_CAN_SLEEP == 0")
return false, nil
}
can, err := m.powerManager.CanSuspend(0) // 是否支持待机
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
if can {
str, err := m.objLogin.CanSuspend(0) // 当前能否待机
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
return str == "yes", nil
}
return false, nil
}
func (m *SessionManager) RequestSuspend() *dbus.Error {
_, err := os.Stat("/etc/deepin/no_suspend")
if err == nil {
// no suspend
time.Sleep(time.Second)
setDPMSMode(false)
return nil
}
// 使用窗管接口进行黑屏处理
if _gSettingsConfig.needQuickBlackScreen {
logger.Info("request wm blackscreen effect")
cmd := exec.Command("/bin/bash", "-c", "dbus-send --print-reply --dest=org.kde.KWin /BlackScreen org.kde.kwin.BlackScreen.setActive boolean:true")
error := cmd.Run()
if error != nil {
logger.Warning("wm blackscreen failed")
}
}
logger.Info("login1 start suspend")
err = m.objLogin.Suspend(0, false)
if err != nil {
logger.Warning("failed to suspend:", err)
}
return nil
}
func (m *SessionManager) CanHibernate() (bool, *dbus.Error) {
if os.Getenv("POWER_CAN_SLEEP") == "0" {
logger.Info("can not Hibernate, env POWER_CAN_SLEEP == 0")
return false, nil
}
can, err := m.powerManager.CanHibernate(0) // 是否支持休眠
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
if can {
str, err := m.objLogin.CanHibernate(0) // 当前能否休眠
if err != nil {
logger.Warning(err)
return false, dbusutil.ToError(err)
}
return str == "yes", nil
}
return false, nil
}
func (m *SessionManager) RequestHibernate() *dbus.Error {
err := m.objLogin.Hibernate(0, false)
if err != nil {
logger.Warning("failed to Hibernate:", err)
}
if _gSettingsConfig.needQuickBlackScreen {
setDPMSMode(false)
}
return nil
}
func (m *SessionManager) RequestLock() *dbus.Error {
conn, err := dbus.SessionBus()
if err != nil {
return dbusutil.ToError(err)
}
err = conn.Object(lockFrontDest, lockFrontObjPath).Call(lockFrontIfc+".Show", 0).Err
return dbusutil.ToError(err)
}
func (m *SessionManager) PowerOffChoose() *dbus.Error {
m.launch(cmdShutdown, false)
return nil
}
func (m *SessionManager) ToggleDebug() *dbus.Error {
if logger.GetLogLevel() == log.LevelDebug {
doSetLogLevel(log.LevelInfo)
logger.Debug("Debug mode disabled")
} else {
doSetLogLevel(log.LevelDebug)
logger.Debug("Debug mode enabled")
}
return nil
}
func (m *SessionManager) SetLocked(sender dbus.Sender, value bool) *dbus.Error {
pid, err := m.service.GetConnPID(string(sender))
if err != nil {
return dbusutil.ToError(err)
}
process := procfs.Process(pid)
exe, err := process.Exe()
if err != nil {
return dbusutil.ToError(err)
}
if exe == "/usr/bin/dde-lock" {
m.setLocked(value)
return nil
}
cmd, err := process.Cmdline()
if err != nil {
return dbusutil.ToError(err)
}
desktopFile := cmd[0]
if desktopFile != ddeLockDesktopFile {
return dbusutil.ToError(fmt.Errorf("exe %q is invalid", exe))
}
info, err := desktopappinfo.NewDesktopAppInfoFromFile(desktopFile)
if err != nil {
return dbusutil.ToError(fmt.Errorf("desktop file %q is invalid", desktopFile))
}
exe = info.GetExecutable()
if exe != "/usr/bin/dde-lock" {
return dbusutil.ToError(fmt.Errorf("exe %q of desktop file %q is invalid", exe, desktopFile))
}
m.setLocked(value)
return nil
}
func (m *SessionManager) setLocked(value bool) {
logger.Debug("call setLocked", value)
m.mu.Lock()
if m.Locked != value {
m.Locked = value
err := m.service.EmitPropertyChanged(m, "Locked", value)
if err != nil {
logger.Warning(err)
}
}
m.mu.Unlock()
}
func (m *SessionManager) getLocked() bool {
m.mu.Lock()
v := m.Locked
m.mu.Unlock()
return v
}
func callSwapSchedHelperPrepare(sessionID string) error {
sysBus, err := dbus.SystemBus()
if err != nil {
return err
}
const dest = "org.deepin.dde.SwapSchedHelper1"
obj := sysBus.Object(dest, "/org/deepin/dde/SwapSchedHelper1")
return obj.Call(dest+".Prepare", 0, sessionID).Store()
}
func (m *SessionManager) initSession() {
var err error
sysBus, err := dbus.SystemBus()
if err != nil {
logger.Warning(err)
return
}
sysSigLoop := dbusutil.NewSignalLoop(sysBus, 10)
sysSigLoop.Start()
if m.objLoginSessionSelf != nil {
m.objLoginSessionSelf.InitSignalExt(sysSigLoop, true)
err = m.objLoginSessionSelf.Active().ConnectChanged(func(hasValue bool, active bool) {
logger.Debugf("session status changed hasValue: %v, active: %v", hasValue, active)
if !hasValue {
return
}
if active {
// 变活跃
} else {
// 变不活跃
isPreparingForSleep, _ := m.objLogin.PreparingForSleep().Get(0)
// 待机时不在这里锁屏
if !isPreparingForSleep {
logger.Debug("call objLoginSessionSelf.Lock")
err = m.objLoginSessionSelf.Lock(0)
if err != nil {
logger.Warning("failed to Lock current session:", err)
}
}
}
})
if err != nil {
logger.Warning("failed to connect Active changed:", err)
}
}
if _gSettingsConfig.swapSchedEnabled {
m.initSwapSched()
} else {
logger.Info("swap sched disabled")
}
}
func (m *SessionManager) initSwapSched() {
err := cgroup.Init()
if err != nil {
logger.Warning(err)
return
}
globalCgExecBin, err = exec.LookPath("cgexec")
if err != nil {
logger.Warning("cgexec not found:", err)
return
}
if m.objLoginSessionSelf == nil {
return
}
sessionID, err := m.objLoginSessionSelf.Id().Get(0)
if err != nil {
logger.Warning(err)
}
err = callSwapSchedHelperPrepare(sessionID)
if err != nil {
logger.Warning("call SwapSchedHelper.Prepare error:", err)
}
swapsched.SetLogger(logger)
memCheckerCfg := memchecker.GetConfig()
logger.Debugf("mem checker cfg min mem avail: %d KB", memCheckerCfg.MinMemAvail)
var enableMemAvailMax int64 // unit is byte
enableMemAvailMax = int64(memCheckerCfg.MinMemAvail*1024) - 100*swapsched.MB
if enableMemAvailMax < 0 {
enableMemAvailMax = 100 * swapsched.MB
}
swapSchedCfg := swapsched.Config{
UIAppsCGroup: sessionID + "@dde/uiapps",
DECGroup: sessionID + "@dde/DE",
EnableMemAvailMax: uint64(enableMemAvailMax),
DisableMemAvailMin: uint64(enableMemAvailMax) + 200*swapsched.MB,
}
swapSchedDispatcher, err = swapsched.NewDispatcher(swapSchedCfg)
logger.Debugf("swap sched config: %+v", swapSchedCfg)
if err == nil {
// add self to DE cgroup
deCg := cgroup.NewCgroup(swapSchedDispatcher.GetDECGroup())
deCg.AddController(cgroup.Memory)
err = deCg.AttachCurrentProcess()
if err != nil {
logger.Warning("failed to add self to DE cgroup:", err)
}
go func() {
err = swapsched.ActiveWindowHandler(swapSchedDispatcher.ActiveWindowHandler).Monitor()
if err != nil {
logger.Warning(err)
}
}()
go swapSchedDispatcher.Balance()
} else {
logger.Warning("failed to new swap sched dispatcher:", err)
}
}
func newSessionManager(service *dbusutil.Service) *SessionManager {
defer func() {
if err := recover(); err != nil {
logger.Error("newSessionManager recover:", err)
return
}
}()
var err error
sysBus, err := dbus.SystemBus()
if err != nil {
logger.Warning(err)
return nil
}
sessionBus := service.Conn()
sigLoop := dbusutil.NewSignalLoop(sessionBus, 10)
sigLoop.Start()
dbusDaemon := ofdbus.NewDBus(sessionBus)
dbusDaemon.InitSignalExt(sigLoop, true)
objLogin := login1.NewManager(sysBus)
powerManager := powermanager.NewPowerManager(sysBus)
sysBt := sysbt.NewBluetooth(sysBus)
var objLoginSessionSelf login1.Session
sessionPath, err := getCurSessionPath()
if err != nil {
logger.Warning("failed to get current session path:", err)
} else {
logger.Info("session path:", sessionPath)
objLoginSessionSelf, err = login1.NewSession(sysBus, dbus.ObjectPath(sessionPath))
if err != nil {
logger.Warning("login1.NewSession err:", err)
}
}
m := &SessionManager{
service: service,
cookies: make(map[string]chan time.Time),
sigLoop: sigLoop,
objLogin: objLogin,
objLoginSessionSelf: objLoginSessionSelf,
powerManager: powerManager,
sysBt: sysBt,
dbusDaemon: dbusDaemon,
daemon: daemon.NewDaemon(sysBus),
}
// 此处将init的操作提前,避免SessionManager对象被创建了,相关属性值还没有被初始化
// 如果其它进程读取了非法的属性值, dbus连接会被关闭,导致startdde启动失败
m.initSession()
m.init()
return m
}
func (m *SessionManager) init() {
m.setPropName("CurrentUid")
var err error
m.loginSession, err = getLoginSession()
if err != nil {
logger.Warning("failed to get current login session:", err)
}
m.CurrentSessionPath, err = getCurSessionPath()
if err != nil {
logger.Warning("failed to get current session path:", err)
// 要保证 CurrentSessionPath 属性值的合法性
m.CurrentSessionPath = "/"
}
}
func (m *SessionManager) launchDDE() {
groups, err := loadGroupFile()
if err != nil {
logger.Error("Failed to load launch group file:", err)
return
}
sort.Sort(groups)
for idx, group := range groups {
logger.Debugf("[%d] group p%d start", idx, group.Priority)
var waitGroup sync.WaitGroup
waitGroup.Add(len(group.Group))
for _, cmd := range group.Group {
cmd := cmd
go func() {
logger.Debug("run cmd:", cmd.Command, cmd.Args, cmd.Wait)
m.launch(cmd.Command, cmd.Wait, cmd.Args...)
waitGroup.Done()
}()
}
waitGroup.Wait()
logger.Debugf("[%d] group p%d end", idx, group.Priority)
}
}
func (m *SessionManager) launchAutostart() {
m.setPropStage(SessionStageAppsBegin)
delay := _gSettingsConfig.autoStartDelay
logger.Debug("autostart delay seconds:", delay)
if delay > 0 {
time.AfterFunc(time.Second*time.Duration(delay), func() {
startAutostartProgram()
})
} else {
startAutostartProgram()
}
m.setPropStage(SessionStageAppsEnd)
}
func setEnvWithMap(envVars map[string]string) {
for key, value := range envVars {
logger.Debugf("set env %s = %q", key, value)
err := os.Setenv(key, value)
if err != nil {
logger.Warning(err)
}
}
}
func (m *SessionManager) start(xConn *x.Conn, sysSignalLoop *dbusutil.SignalLoop, service *dbusutil.Service) *SessionManager {
defer func() {
if err := recover(); err != nil {
logger.Error("StartSession recover:", err)
return
}
}()
m.setPropStage(SessionStageCoreBegin)
startStartManager(xConn, service)
m.launchDDE()
time.AfterFunc(3*time.Second, _startManager.listenAutostartFileEvents)
go m.launchAutostart()
if m.loginSession != nil {
m.loginSession.InitSignalExt(sysSignalLoop, true)
_, err := m.loginSession.ConnectLock(m.handleLoginSessionLock)
if err != nil {
logger.Warning("failed to connect signal Lock:", err)
}
_, err = m.loginSession.ConnectUnlock(m.handleLoginSessionUnlock)
if err != nil {
logger.Warning("failled to connect signal Unlock:", err)
}
}
return m
}
func isDeepinVersionChanged() (changed bool, err error) {
kfDeepinVersion := keyfile.NewKeyFile()
err = kfDeepinVersion.LoadFromFile("/etc/deepin-version")
if err != nil {
return
}
v0, err := kfDeepinVersion.GetString("Release", "Version")
if err != nil {
return
}
kfDDEWelcome := keyfile.NewKeyFile()
ddeWelcomeFile := filepath.Join(basedir.GetUserConfigDir(), "deepin/dde-welcome.conf")
saveConfig := func() {
kfDDEWelcome.SetString("General", "Version", v0)
err := os.MkdirAll(filepath.Dir(ddeWelcomeFile), 0755)
if err != nil {
logger.Warning(err)
return
}
tmpFile := ddeWelcomeFile + ".tmp"
err = kfDDEWelcome.SaveToFile(tmpFile)
if err != nil {
logger.Warning("failed to save dde-welcome.conf:", err)
return
}
err = syncFile(tmpFile)
if err != nil {
logger.Warning("failed to sync temp file:", err)
return
}
err = os.Rename(tmpFile, ddeWelcomeFile)
if err != nil {
logger.Warning(err)
}
}
err = kfDDEWelcome.LoadFromFile(ddeWelcomeFile)
if err != nil {
if os.IsNotExist(err) {
// new user first login
saveConfig()
return false, nil
}
return false, err
}
v1, _ := kfDDEWelcome.GetString("General", "Version")
if v0 != v1 {
if v1 != "" {
changed = true
}
saveConfig()
}
return
}
func getLoginSession() (login1.Session, error) {
sysBus, err := dbus.SystemBus()
if err != nil {
return nil, err
}
sessionPath, err := getCurSessionPath()
if err != nil {
return nil, err
}
session, err := login1.NewSession(sysBus, dbus.ObjectPath(sessionPath))
if err != nil {
return nil, err
}
return session, nil
}
func (m *SessionManager) handleLoginSessionLock() {
logger.Debug("login session lock")
go func() {
// 在特殊情况下,比如用 dde-switchtogreeter 命令切换到 greeter, 即切换到其他 tty,
// RequestLock 方法不能立即返回。
// 如果已经锁定,则立即返回
locked := m.getLocked()
if locked {
logger.Debug("handleLoginSessionLock locked is true, return")
return
}
// 使用 m.inCallRequestLock 防止同时多次调用 RequestLock,以简化状况。
m.inCallRequestLockMu.Lock()
if m.inCallRequestLock {
m.inCallRequestLockMu.Unlock()
// 如果已经发出一个锁屏请求,则立即返回。
logger.Debug("handleLoginSessionLock inCall is true, return")
return
}
m.inCallRequestLock = true
m.inCallRequestLockMu.Unlock()
t0 := time.Now()
logger.Debug("handleLoginSessionLock call RequestLock begin")
err := m.RequestLock()
elapsed := time.Since(t0)
logger.Debugf("handleLoginSessionLock call RequestLock end, cost: %v", elapsed)
if err != nil {
logger.Warning("failed to request lock:", err)
}
m.inCallRequestLockMu.Lock()
m.inCallRequestLock = false
m.inCallRequestLockMu.Unlock()
}()
}
func (m *SessionManager) handleLoginSessionUnlock() {
if m.loginSession == nil {
return
}
logger.Debug("login session unlock")
isActive, err := m.loginSession.Active().Get(0)
if err != nil {
logger.Warning("failed to get session property Active:", err)
return
}
if isActive {
// 图形界面 session 活跃
m.emitSignalUnlock()
} else {
// 图形界面 session 不活跃,此时采用 kill 掉锁屏前端的特殊操作,
// 如果不 kill 掉它,则一旦 session 变活跃,锁屏前端会在很近的时间内执行锁屏和解锁,
// 会使用系统通知报告锁屏失败。 而在此种特殊情况下 kill 掉它,并不会造成明显问题。
err = m.killLockFront()
if err != nil {
logger.Warning("failed to kill lock front:", err)
m.emitSignalUnlock()
} else {
m.setLocked(false)
}
}
}
// 发送 Unlock 信号,前端锁屏程序收到后会立即解锁。
func (m *SessionManager) emitSignalUnlock() {
logger.Debug("emit signal Unlock")
err := m.service.Emit(m, "Unlock")
if err != nil {
logger.Warning("failed to emit signal Unlock:", err)
}
}
// kill 掉前端锁屏程序
func (m *SessionManager) killLockFront() error {
sessionBus := m.service.Conn()
dbusDaemon := ofdbus.NewDBus(sessionBus)
owner, err := dbusDaemon.GetNameOwner(0, "org.deepin.dde.LockFront1")
if err != nil {
return err
}
pid, err := m.service.GetConnPID(owner)
if err != nil {
return err
}
logger.Debugf("kill lock front owner: %v, pid: %v", owner, pid)
p, err := os.FindProcess(int(pid))
// linux 系统下这里不会失败
if err != nil {
return err
}
// 发送 SIGTERM 礼貌的退出信号
err = p.Signal(syscall.SIGTERM)
if err != nil {
return err
}
return nil
}
func setDPMSMode(on bool) {
var err error
if _useWayland {
if !on {
_, err = exec.Command("dde_wldpms", "-s", "Off").Output()
} else {
_, err = exec.Command("dde_wldpms", "-s", "On").Output()
}
} else {
var mode = uint16(dpms.DPMSModeOn)
if !on {
mode = uint16(dpms.DPMSModeOff)
}
err = dpms.ForceLevelChecked(_xConn, mode).Check(_xConn)
}
if err != nil {
logger.Warning("Failed to set dpms mode:", on, err)
}
}
func (m *SessionManager) doLogout(force bool) {
if m.objLoginSessionSelf != nil {
err := m.objLoginSessionSelf.Terminate(0)
if err != nil {
logger.Warning("LoginSessionSelf Terminate failed:", err)
}
}
os.Exit(0)
}