github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/systemd/journal.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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 systemd
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"log/syslog"
    26  	"net"
    27  	"os"
    28  )
    29  
    30  var journalStdoutPath = "/run/systemd/journal/stdout"
    31  
    32  // NewJournalStreamFile creates log stream file descriptor to the journal. The
    33  // semantics is identical to that of sd_journal_stream_fd(3) call.
    34  func NewJournalStreamFile(identifier string, priority syslog.Priority, levelPrefix bool) (*os.File, error) {
    35  	conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: journalStdoutPath})
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	// does not affect *os.File created through conn.File() later on
    40  	defer conn.Close()
    41  
    42  	if err := conn.CloseRead(); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	// header contents taken from the original systemd code:
    47  	// https://github.com/systemd/systemd/blob/97a33b126c845327a3a19d6e66f05684823868fb/src/journal/journal-send.c#L395
    48  	header := bytes.Buffer{}
    49  	header.WriteString(identifier)
    50  	header.WriteByte('\n')
    51  	header.WriteByte('\n')
    52  	header.WriteByte(byte('0') + byte(priority))
    53  	header.WriteByte('\n')
    54  	var prefix int
    55  	if levelPrefix {
    56  		prefix = 1
    57  	}
    58  	header.WriteByte(byte('0') + byte(prefix))
    59  	header.WriteByte('\n')
    60  	header.WriteByte('0')
    61  	header.WriteByte('\n')
    62  	header.WriteByte('0')
    63  	header.WriteByte('\n')
    64  	header.WriteByte('0')
    65  	header.WriteByte('\n')
    66  
    67  	if _, err := conn.Write(header.Bytes()); err != nil {
    68  		return nil, fmt.Errorf("failed to write header: %v", err)
    69  	}
    70  
    71  	return conn.File()
    72  }