github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/configstate/configcore/journal.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  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/osutil"
    29  	"github.com/snapcore/snapd/osutil/sys"
    30  	"github.com/snapcore/snapd/overlord/configstate/config"
    31  	"github.com/snapcore/snapd/sysconfig"
    32  	"github.com/snapcore/snapd/systemd"
    33  )
    34  
    35  var osutilFindGid = osutil.FindGid
    36  var sysChownPath = sys.ChownPath
    37  
    38  func init() {
    39  	supportedConfigurations["core.journal.persistent"] = true
    40  }
    41  
    42  func validateJournalSettings(tr config.ConfGetter) error {
    43  	return validateBoolFlag(tr, "journal.persistent")
    44  }
    45  
    46  func handleJournalConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
    47  	output, err := coreCfg(tr, "journal.persistent")
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	if output == "" {
    53  		return nil
    54  	}
    55  
    56  	rootDir := dirs.GlobalRootDir
    57  	if opts != nil {
    58  		rootDir = opts.RootDir
    59  	}
    60  
    61  	tempLogPath := filepath.Join(rootDir, "/var/log/journal-snapd")
    62  	logPath := filepath.Join(rootDir, "/var/log/journal")
    63  	marker := ".snapd-created"
    64  
    65  	logDirExists, _, _ := osutil.DirExists(logPath)
    66  
    67  	switch output {
    68  	case "true":
    69  		if logDirExists {
    70  			// don't check marker; if the directory exists then logging is
    71  			// already enabled (although possibly not controlled by us), but
    72  			// don't error out. In such case we will error out if setting
    73  			// to false is attempted.
    74  			return nil
    75  		}
    76  		if err := os.MkdirAll(tempLogPath, 0755); err != nil {
    77  			return err
    78  		}
    79  		if err := osutil.AtomicWriteFile(filepath.Join(tempLogPath, marker), nil, 0700, 0); err != nil {
    80  			return nil
    81  		}
    82  		if err := os.Rename(tempLogPath, logPath); err != nil {
    83  			return err
    84  		}
    85  	case "false":
    86  		if !logDirExists {
    87  			return nil
    88  		}
    89  		// only remove journal log dir if our marker file is there
    90  		if !osutil.FileExists(filepath.Join(logPath, marker)) {
    91  			return fmt.Errorf("the %s directory was not created by snapd, journal logs will not be removed nor disabled", logPath)
    92  		}
    93  		// This removes all logs from /var/log/journal when journal.persistent
    94  		// is set to false.
    95  		// It's assumed that core device is fully controlled by snapd.
    96  		if err := os.RemoveAll(logPath); err != nil {
    97  			return err
    98  		}
    99  	default:
   100  		return fmt.Errorf("unsupported journal.persistent option: %q", output)
   101  	}
   102  
   103  	if opts == nil {
   104  		ver, err := systemd.Version()
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		// old systemd-journal (e.g. on core16) closes the pipes on SIGUSR1,
   110  		// causing SIGPIPE and restart of snapd and other services.
   111  		// upstream bug: https://bugs.freedesktop.org/show_bug.cgi?id=84923,
   112  		// therefore only tell journald to reload if it's new enough.
   113  		if ver >= 236 {
   114  			sysd := systemd.NewUnderRoot(dirs.GlobalRootDir, systemd.SystemMode, nil)
   115  			if err := sysd.Kill("systemd-journald", "USR1", ""); err != nil {
   116  				return err
   117  			}
   118  		}
   119  	}
   120  
   121  	return nil
   122  }