/
cmd_userd.go
134 lines (111 loc) · 2.96 KB
/
cmd_userd.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
// -*- Mode: Go; indent-tabs-mode: t -*-
// +build !darwin
/*
* Copyright (C) 2017-2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/snapdtool"
"github.com/snapcore/snapd/usersession/agent"
"github.com/snapcore/snapd/usersession/autostart"
"github.com/snapcore/snapd/usersession/userd"
)
type cmdUserd struct {
Autostart bool `long:"autostart"`
Agent bool `long:"agent"`
}
var shortUserdHelp = i18n.G("Start the userd service")
var longUserdHelp = i18n.G(`
The userd command starts the snap user session service.
`)
func init() {
cmd := addCommand("userd",
shortUserdHelp,
longUserdHelp,
func() flags.Commander {
return &cmdUserd{}
}, map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"autostart": i18n.G("Autostart user applications"),
// TRANSLATORS: This should not start with a lowercase letter.
"agent": i18n.G("Run the user session agent"),
}, nil)
cmd.hidden = true
}
func (x *cmdUserd) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if x.Autostart {
return x.runAutostart()
}
if x.Agent {
return x.runAgent()
}
return x.runUserd()
}
var signalNotify = signalNotifyImpl
func (x *cmdUserd) runUserd() error {
var userd userd.Userd
if err := userd.Init(); err != nil {
return err
}
userd.Start()
ch, stop := signalNotify(syscall.SIGINT, syscall.SIGTERM)
defer stop()
select {
case sig := <-ch:
fmt.Fprintf(Stdout, "Exiting on %s.\n", sig)
case <-userd.Dying():
// something called Stop()
}
return userd.Stop()
}
func (x *cmdUserd) runAgent() error {
agent, err := agent.New()
if err != nil {
return err
}
agent.Version = snapdtool.Version
agent.Start()
ch, stop := signalNotify(syscall.SIGINT, syscall.SIGTERM)
defer stop()
select {
case sig := <-ch:
fmt.Fprintf(Stdout, "Exiting on %s.\n", sig)
case <-agent.Dying():
// something called Stop()
}
return agent.Stop()
}
func (x *cmdUserd) runAutostart() error {
if err := autostart.AutostartSessionApps(); err != nil {
return fmt.Errorf("autostart failed for the following apps:\n%v", err)
}
return nil
}
func signalNotifyImpl(sig ...os.Signal) (ch chan os.Signal, stop func()) {
ch = make(chan os.Signal, len(sig))
signal.Notify(ch, sig...)
stop = func() { signal.Stop(ch) }
return ch, stop
}