forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logfile.go
71 lines (63 loc) · 1.79 KB
/
logfile.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build !windows
package paths
import (
"os"
"os/user"
"strconv"
"github.com/juju/errors"
jujuos "github.com/juju/os"
)
// LogfilePermission is the file mode to use for log files.
const LogfilePermission = os.FileMode(0640)
// SetSyslogOwner sets the owner and group of the file to be the appropriate
// syslog users as defined by the SyslogUserGroup method.
func SetSyslogOwner(filename string) error {
user, group := SyslogUserGroup()
return SetOwnership(filename, user, group)
}
// SetOwnership sets the ownership of a given file from a path.
// Searches for the corresponding id's from user, group and uses them to chown.
func SetOwnership(filePath string, wantedUser string, wantedGroup string) error {
group, err := user.LookupGroup(wantedGroup)
if err != nil {
return errors.Trace(err)
}
gid, err := strconv.Atoi(group.Gid)
if err != nil {
return errors.Trace(err)
}
usr, err := user.Lookup(wantedUser)
if err != nil {
return errors.Trace(err)
}
uid, err := strconv.Atoi(usr.Uid)
if err != nil {
return errors.Trace(err)
}
return Chown(filePath, uid, gid)
}
// PrimeLogFile ensures that the given log file is created with the
// correct mode and ownership.
func PrimeLogFile(path string) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, LogfilePermission)
if err != nil {
return errors.Trace(err)
}
if err := f.Close(); err != nil {
return errors.Trace(err)
}
return SetSyslogOwner(path)
}
// SyslogUserGroup returns the names of the user and group that own the log files.
func SyslogUserGroup() (string, string) {
switch jujuos.HostOS() {
case jujuos.CentOS:
return "root", "adm"
case jujuos.OpenSUSE:
return "root", "root"
default:
return "syslog", "adm"
}
}