1 Star 0 Fork 1

mysnapcore/mysnapd

forked from tupelo-shen/mysnapd 
Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
powerbtn.go 2.38 KB
Copy Edit Raw Blame History
tupelo-shen authored 2022-11-08 15:12 +08:00 . fix: overlord commit
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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 configcore
import (
"fmt"
"os"
"path/filepath"
"gitee.com/mysnapcore/mysnapd/dirs"
"gitee.com/mysnapcore/mysnapd/osutil"
"gitee.com/mysnapcore/mysnapd/overlord/configstate/config"
"gitee.com/mysnapcore/mysnapd/sysconfig"
)
func init() {
supportedConfigurations["core.system.power-key-action"] = true
}
func powerBtnCfg(opts *fsOnlyContext) string {
rootDir := dirs.GlobalRootDir
if opts != nil {
rootDir = opts.RootDir
}
return filepath.Join(rootDir, "/etc/systemd/logind.conf.d/00-snap-core.conf")
}
// switchHandlePowerKey changes the behavior when the power key is pressed
func switchHandlePowerKey(action string, opts *fsOnlyContext) error {
validActions := map[string]bool{
"ignore": true,
"poweroff": true,
"reboot": true,
"halt": true,
"kexec": true,
"suspend": true,
"hibernate": true,
"hybrid-sleep": true,
"lock": true,
}
cfgDir := filepath.Dir(powerBtnCfg(opts))
if !osutil.IsDirectory(cfgDir) {
if err := os.MkdirAll(cfgDir, 0755); err != nil {
return err
}
}
if !validActions[action] {
return fmt.Errorf("invalid action %q supplied for system.power-key-action option", action)
}
content := fmt.Sprintf(`[Login]
HandlePowerKey=%s
`, action)
return osutil.AtomicWriteFile(powerBtnCfg(opts), []byte(content), 0644, 0)
}
func handlePowerButtonConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
output, err := coreCfg(tr, "system.power-key-action")
if err != nil {
return err
}
if output == "" {
if err := os.Remove(powerBtnCfg(opts)); err != nil && !os.IsNotExist(err) {
return err
}
} else {
if err := switchHandlePowerKey(output, opts); err != nil {
return err
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.1.0

Search