Skip to content

Commit

Permalink
journal: add StdoutIsJournalStream function
Browse files Browse the repository at this point in the history
See discussion in #410
  • Loading branch information
WGH- committed Nov 4, 2022
1 parent fe4a250 commit 78aa072
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
29 changes: 28 additions & 1 deletion journal/journal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ 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 stderr'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 (the default is StandardError=inherit, which results in
// both file descriptors getting the same duplicated socket, and doesn't
// suffer from this problem):
//
// StandardOutput=journal
// StandardError=journal
//
// 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 +121,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 78aa072

Please sign in to comment.