Skip to content

Commit

Permalink
journal: add StdoutIsJournalStream function
Browse files Browse the repository at this point in the history
See discussion in coreos#410
  • Loading branch information
WGH- committed Nov 7, 2022
1 parent 04e77f7 commit 4ff5a6f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 26 additions & 1 deletion journal/journal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func Enabled() bool {
//
// [Journal Native Protocol]: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/#automatic-protocol-upgrading
func StderrIsJournalStream() (bool, error) {
return fdIsJournalStream(syscall.Stderr)
}

// StdoutIsJournalStream returns whether the process stdout is connected
// to the Journal's stream transport.
//
// Returns true if JOURNAL_STREAM environment variable is present,
// and stdout's device and inode numbers match it.
//
// Error is returned if unexpected error occurs: e.g. if JOURNAL_STREAM environment variable
// is present, but malformed, fstat syscall fails, etc.
//
// This might report false negatives in certain configurations,
// like when both stdout and stderr are independently connected to the
// journal:
//
// StandardOutput=journal
// StandardError=journal+console
//
// Most users should probably use [StderrIsJournalStream].
func StdoutIsJournalStream() (bool, error) {
return fdIsJournalStream(syscall.Stdout)
}

func fdIsJournalStream(fd int) (bool, error) {
journalStream := os.Getenv("JOURNAL_STREAM")
if journalStream == "" {
return false, nil
Expand All @@ -94,7 +119,7 @@ func StderrIsJournalStream() (bool, error) {
}

var stat syscall.Stat_t
err = syscall.Fstat(syscall.Stderr, &stat)
err = syscall.Fstat(fd, &stat)
if err != nil {
return false, err
}
Expand Down
4 changes: 4 additions & 0 deletions journal/journal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func Send(message string, priority Priority, vars map[string]string) error {
func StderrIsJournalStream() (bool, error) {
return false, nil
}

func StdoutIsJournalStream() (bool, error) {
return false, nil
}

0 comments on commit 4ff5a6f

Please sign in to comment.