mirror of
https://github.com/siderolabs/talos.git
synced 2025-10-17 10:31:19 +02:00
In UNIX, any zombies without parent process get re-parented to process with PID 1 (usually running init), and PID 1 process should take care of them (usually simply clean them up). Cleaning up zombies is important, as they still take kerner resources, and having enormous amount of zombie processes signifcantly degrades system performance. For Talos, PID 1 process is machined, and machined itself forks to run other processes in process runner and `pkg/cmd` one-time commands. Naive solution of running `wait()` loop doesn't work as it might race with `Process.Wait()` and clean up zombie which wasn't re-parented which leads to process execution false failure. After considering other solutions, we decided to go with the simple approach: machined runs global zombie process reaper which publishes information about reaped zombies. Any call to `Process.Wait()` (or `Command.Wait()` which calls it) should be replaced with listening to reaper's channel for notifications to catch info about the process which was created in this call. There are several changes in this PR: 1. Reaper implementation itself, started from machined. 2. Process runner and `pkg/cmd` can either use regular `Command.Wait()` or use reaper notifications depending on reaper status (running/not running). This allows using this code outside of machined. 3. Small bug fixes with process log which was affecting the tests. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package log
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
filechunker "github.com/talos-systems/talos/pkg/chunker/file"
|
|
)
|
|
|
|
var instance = map[string]*Log{}
|
|
var mu sync.Mutex
|
|
|
|
// Log represents the log of a service. It supports streaming of the contents of
|
|
// the log file by way of implementing the chunker.Chunker interface.
|
|
type Log struct {
|
|
Name string
|
|
Path string
|
|
source filechunker.Source
|
|
}
|
|
|
|
// New initializes and registers a log for a service.
|
|
func New(name, rootPath string) (*Log, error) {
|
|
logpath := FormatLogPath(name, rootPath)
|
|
|
|
mu.Lock()
|
|
if l, ok := instance[logpath]; ok {
|
|
mu.Unlock()
|
|
return l, nil
|
|
}
|
|
mu.Unlock()
|
|
|
|
w, err := os.Create(logpath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create log file: %s", err.Error())
|
|
}
|
|
|
|
l := &Log{
|
|
Name: name,
|
|
Path: logpath,
|
|
source: w,
|
|
}
|
|
|
|
mu.Lock()
|
|
instance[l.Path] = l
|
|
mu.Unlock()
|
|
|
|
return l, nil
|
|
}
|
|
|
|
// Write implements io.WriteCloser.
|
|
func (l *Log) Write(p []byte) (n int, err error) {
|
|
return l.source.Write(p)
|
|
}
|
|
|
|
// Close implements io.WriteCloser.
|
|
func (l *Log) Close() error {
|
|
mu.Lock()
|
|
delete(instance, l.Path)
|
|
mu.Unlock()
|
|
|
|
return l.source.Close()
|
|
}
|
|
|
|
// Read implements chunker.Chunker.
|
|
func (l *Log) Read(ctx context.Context) <-chan []byte {
|
|
c := filechunker.NewChunker(l.source)
|
|
return c.Read(ctx)
|
|
}
|
|
|
|
// FormatLogPath formats the path the log file.
|
|
func FormatLogPath(p, rootPath string) string {
|
|
return filepath.Join(rootPath, p+".log")
|
|
}
|