github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/configstate/configcore/powerbtn.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package configcore
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/osutil"
    29  	"github.com/snapcore/snapd/overlord/configstate/config"
    30  	"github.com/snapcore/snapd/sysconfig"
    31  )
    32  
    33  func init() {
    34  	supportedConfigurations["core.system.power-key-action"] = true
    35  }
    36  
    37  func powerBtnCfg(opts *fsOnlyContext) string {
    38  	rootDir := dirs.GlobalRootDir
    39  	if opts != nil {
    40  		rootDir = opts.RootDir
    41  	}
    42  	return filepath.Join(rootDir, "/etc/systemd/logind.conf.d/00-snap-core.conf")
    43  }
    44  
    45  // switchHandlePowerKey changes the behavior when the power key is pressed
    46  func switchHandlePowerKey(action string, opts *fsOnlyContext) error {
    47  	validActions := map[string]bool{
    48  		"ignore":       true,
    49  		"poweroff":     true,
    50  		"reboot":       true,
    51  		"halt":         true,
    52  		"kexec":        true,
    53  		"suspend":      true,
    54  		"hibernate":    true,
    55  		"hybrid-sleep": true,
    56  		"lock":         true,
    57  	}
    58  
    59  	cfgDir := filepath.Dir(powerBtnCfg(opts))
    60  	if !osutil.IsDirectory(cfgDir) {
    61  		if err := os.MkdirAll(cfgDir, 0755); err != nil {
    62  			return err
    63  		}
    64  	}
    65  	if !validActions[action] {
    66  		return fmt.Errorf("invalid action %q supplied for system.power-key-action option", action)
    67  	}
    68  
    69  	content := fmt.Sprintf(`[Login]
    70  HandlePowerKey=%s
    71  `, action)
    72  	return osutil.AtomicWriteFile(powerBtnCfg(opts), []byte(content), 0644, 0)
    73  }
    74  
    75  func handlePowerButtonConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
    76  	output, err := coreCfg(tr, "system.power-key-action")
    77  	if err != nil {
    78  		return err
    79  	}
    80  	if output == "" {
    81  		if err := os.Remove(powerBtnCfg(opts)); err != nil && !os.IsNotExist(err) {
    82  			return err
    83  		}
    84  
    85  	} else {
    86  		if err := switchHandlePowerKey(output, opts); err != nil {
    87  			return err
    88  		}
    89  	}
    90  	return nil
    91  }