Skip to content

Commit

Permalink
journal: remove implicit initialization
Browse files Browse the repository at this point in the history
some application could log to journald conditionally, the implicit
initialization will always create a socket on init.

We now create the socket only if necessary

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
  • Loading branch information
drakkan committed Feb 1, 2022
1 parent 9ed442a commit 75f33b0
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions journal/journal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows
// +build !windows

// Package journal provides write bindings to the local systemd journal.
Expand Down Expand Up @@ -53,15 +54,9 @@ var (
onceConn sync.Once
)

func init() {
onceConn.Do(initConn)
}

// Enabled checks whether the local systemd journal is available for logging.
func Enabled() bool {
onceConn.Do(initConn)

if (*net.UnixConn)(atomic.LoadPointer(&unixConnPtr)) == nil {
if c := getOrInitConn(); c == nil {
return false
}

Expand All @@ -82,7 +77,7 @@ func Enabled() bool {
// (http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html)
// for more details. vars may be nil.
func Send(message string, priority Priority, vars map[string]string) error {
conn := (*net.UnixConn)(atomic.LoadPointer(&unixConnPtr))
conn := getOrInitConn()
if conn == nil {
return errors.New("could not initialize socket to journald")
}
Expand Down Expand Up @@ -126,6 +121,16 @@ func Send(message string, priority Priority, vars map[string]string) error {
return nil
}

// getOrInitConn attempts to get the global `unixConnPtr` socket, initializing if necessary
func getOrInitConn() *net.UnixConn {
conn := (*net.UnixConn)(atomic.LoadPointer(&unixConnPtr))
if conn != nil {
return conn
}
onceConn.Do(initConn)
return (*net.UnixConn)(atomic.LoadPointer(&unixConnPtr))
}

func appendVariable(w io.Writer, name, value string) {
if err := validVarName(name); err != nil {
fmt.Fprintf(os.Stderr, "variable name %s contains invalid character, ignoring\n", name)
Expand Down Expand Up @@ -194,7 +199,7 @@ func tempFd() (*os.File, error) {
}

// initConn initializes the global `unixConnPtr` socket.
// It is meant to be called exactly once, at program startup.
// It is automatically called when needed.
func initConn() {
autobind, err := net.ResolveUnixAddr("unixgram", "")
if err != nil {
Expand Down

0 comments on commit 75f33b0

Please sign in to comment.