aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ringlogger/ringlogger.go
diff options
context:
space:
mode:
Diffstat (limited to 'ringlogger/ringlogger.go')
-rw-r--r--ringlogger/ringlogger.go61
1 files changed, 36 insertions, 25 deletions
diff --git a/ringlogger/ringlogger.go b/ringlogger/ringlogger.go
index fe88fdc7..e932f88c 100644
--- a/ringlogger/ringlogger.go
+++ b/ringlogger/ringlogger.go
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: MIT
*
- * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
*/
package ringlogger
@@ -21,6 +21,7 @@ import (
const (
maxLogLineLength = 512
+ maxTagLength = 5
maxLines = 2048
magic = 0xbadbabe
)
@@ -44,8 +45,11 @@ type Ringlogger struct {
readOnly bool
}
-func NewRinglogger(filename string, tag string) (*Ringlogger, error) {
- file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
+func NewRinglogger(filename, tag string) (*Ringlogger, error) {
+ if len(tag) > maxTagLength {
+ return nil, windows.ERROR_LABEL_TOO_LONG
+ }
+ file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return nil, err
}
@@ -54,18 +58,19 @@ func NewRinglogger(filename string, tag string) (*Ringlogger, error) {
return nil, err
}
mapping, err := windows.CreateFileMapping(windows.Handle(file.Fd()), nil, windows.PAGE_READWRITE, 0, 0, nil)
- if err != nil {
+ if err != nil && err != windows.ERROR_ALREADY_EXISTS {
return nil, err
}
rl, err := newRingloggerFromMappingHandle(mapping, tag, windows.FILE_MAP_WRITE)
if err != nil {
+ windows.CloseHandle(mapping)
return nil, err
}
rl.file = file
return rl, nil
}
-func NewRingloggerFromInheritedMappingHandle(handleStr string, tag string) (*Ringlogger, error) {
+func NewRingloggerFromInheritedMappingHandle(handleStr, tag string) (*Ringlogger, error) {
handle, err := strconv.ParseUint(handleStr, 10, 64)
if err != nil {
return nil, err
@@ -78,10 +83,6 @@ func newRingloggerFromMappingHandle(mappingHandle windows.Handle, tag string, ac
if err != nil {
return nil, err
}
- if err != nil {
- windows.CloseHandle(mappingHandle)
- return nil, err
- }
log := (*logMem)(unsafe.Pointer(view))
if log.magic != magic {
bytes := (*[unsafe.Sizeof(logMem{})]byte)(unsafe.Pointer(log))
@@ -103,12 +104,20 @@ func newRingloggerFromMappingHandle(mappingHandle windows.Handle, tag string, ac
}
func (rl *Ringlogger) Write(p []byte) (n int, err error) {
+ // Race: This isn't synchronized with the fetch_add below, so items might be slightly out of order.
+ ts := time.Now().UnixNano()
+ return rl.WriteWithTimestamp(p, ts)
+}
+
+func (rl *Ringlogger) WriteWithTimestamp(p []byte, ts int64) (n int, err error) {
if rl.readOnly {
return 0, io.ErrShortWrite
}
-
- // Race: This isn't synchronized with the fetch_add below, so items might be slightly out of order.
- ts := time.Now().UnixNano()
+ ret := len(p)
+ p = bytes.TrimSpace(p)
+ if len(p) == 0 {
+ return ret, nil
+ }
if rl.log == nil {
return 0, io.EOF
@@ -124,18 +133,21 @@ func (rl *Ringlogger) Write(p []byte) (n int, err error) {
line.line[i] = 0
}
- text := []byte(fmt.Sprintf("[%s] %s", rl.tag, bytes.TrimSpace(p)))
- if len(text) > maxLogLineLength-1 {
- text = text[:maxLogLineLength-1]
+ textLen := 3 + len(p) + len(rl.tag)
+ if textLen > maxLogLineLength-1 {
+ p = p[:maxLogLineLength-1-3-len(rl.tag)]
+ textLen = maxLogLineLength - 1
}
- line.line[len(text)] = 0
- copy(line.line[:], text[:])
+ line.line[textLen] = 0
+ line.line[0] = 0 // Null out the beginning and only let it extend after the other writes have completed
+ copy(line.line[1:], rl.tag)
+ line.line[1+len(rl.tag)] = ']'
+ line.line[2+len(rl.tag)] = ' '
+ copy(line.line[3+len(rl.tag):], p[:])
+ line.line[0] = '['
atomic.StoreInt64(&line.timeNs, ts)
- windows.FlushViewOfFile((uintptr)(unsafe.Pointer(&rl.log.nextIndex)), unsafe.Sizeof(rl.log.nextIndex))
- windows.FlushViewOfFile((uintptr)(unsafe.Pointer(line)), unsafe.Sizeof(*line))
-
- return len(p), nil
+ return ret, nil
}
func (rl *Ringlogger) WriteTo(out io.Writer) (n int64, err error) {
@@ -223,17 +235,16 @@ func (rl *Ringlogger) Close() error {
return nil
}
-func (rl *Ringlogger) ExportInheritableMappingHandleStr() (str string, handleToClose windows.Handle, err error) {
+func (rl *Ringlogger) ExportInheritableMappingHandle() (handleToClose windows.Handle, err error) {
handleToClose, err = windows.CreateFileMapping(windows.Handle(rl.file.Fd()), nil, windows.PAGE_READONLY, 0, 0, nil)
- if err != nil {
+ if err != nil && err != windows.ERROR_ALREADY_EXISTS {
return
}
err = windows.SetHandleInformation(handleToClose, windows.HANDLE_FLAG_INHERIT, windows.HANDLE_FLAG_INHERIT)
if err != nil {
- windows.Close(handleToClose)
+ windows.CloseHandle(handleToClose)
handleToClose = 0
return
}
- str = strconv.FormatUint(uint64(handleToClose), 10)
return
}