github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/configstate/configcore/backlight.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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  
    25  	"github.com/snapcore/snapd/dirs"
    26  	"github.com/snapcore/snapd/overlord/configstate/config"
    27  	"github.com/snapcore/snapd/sysconfig"
    28  	"github.com/snapcore/snapd/systemd"
    29  )
    30  
    31  func init() {
    32  	// add supported configuration of this module
    33  	supportedConfigurations["core.system.disable-backlight-service"] = true
    34  }
    35  
    36  func validateBacklightServiceSettings(tr config.ConfGetter) error {
    37  	return validateBoolFlag(tr, "system.disable-backlight-service")
    38  }
    39  
    40  type backlightSysdLogger struct{}
    41  
    42  func (l *backlightSysdLogger) Notify(status string) {
    43  	fmt.Fprintf(Stderr, "sysd: %s\n", status)
    44  }
    45  
    46  // systemd-backlight service has no installation config. It's started when needed via udev.
    47  // So, systemctl enable/disable/start/stop are invalid commands. After masking/unmasking
    48  // the service, rebooting is required for making new setting work
    49  func handleBacklightServiceConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
    50  	var sysd systemd.Systemd
    51  	const serviceName = "systemd-backlight@.service"
    52  	if opts != nil {
    53  		sysd = systemd.NewEmulationMode(opts.RootDir)
    54  	} else {
    55  		sysd = systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.SystemMode, &backlightSysdLogger{})
    56  	}
    57  	output, err := coreCfg(tr, "system.disable-backlight-service")
    58  	if err != nil {
    59  		return nil
    60  	}
    61  	if output != "" {
    62  		switch output {
    63  		case "true":
    64  			return sysd.Mask(serviceName)
    65  		case "false":
    66  			return sysd.Unmask(serviceName)
    67  		default:
    68  			return fmt.Errorf("unsupported disable-backlight-service option: %q", output)
    69  		}
    70  	}
    71  	return nil
    72  }