feat: soporte Android/Termux — battery_file + log_file (file-IPC sin exec), tail de logcat, workarounds DNS(1.1.1.1)+CA(SSL_CERT_FILE), procesos Android-safe
This commit is contained in:
@@ -2,16 +2,52 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fn-registry/functions/infra"
|
||||
)
|
||||
|
||||
// shipLogs picks the right log source for the platform and ships to Loki:
|
||||
// systemd journald on Linux servers, logcat on Android/Termux. If neither is
|
||||
// available it logs once and returns, leaving metrics shipping unaffected.
|
||||
//
|
||||
// Binaries are located with os.Stat (not exec.LookPath) and run by absolute
|
||||
// path: on Android the faccessat2 syscall that LookPath uses is blocked by
|
||||
// seccomp and crashes the process with SIGSYS.
|
||||
func shipLogs(ctx context.Context, cfg Config) {
|
||||
// Android/Termux: a shell helper writes `logcat` output to cfg.LogFile and
|
||||
// we tail it (no exec, which seccomp would kill via pidfd_open SIGSYS).
|
||||
if cfg.LogFile != "" {
|
||||
shipFileTail(ctx, cfg, cfg.LogFile, "logcat")
|
||||
return
|
||||
}
|
||||
// Linux servers: read the systemd journal directly.
|
||||
if p := findBin("/usr/bin/journalctl", "/bin/journalctl"); p != "" {
|
||||
shipJournald(ctx, cfg, p)
|
||||
return
|
||||
}
|
||||
log.Print("logs: no log source (no log_file nor journalctl), log shipping disabled")
|
||||
}
|
||||
|
||||
// findBin returns the first candidate path that exists, or "".
|
||||
func findBin(candidates ...string) string {
|
||||
for _, c := range candidates {
|
||||
if _, err := os.Stat(c); err == nil {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// journalEntry is the subset of fields we read from `journalctl -o json`.
|
||||
type journalEntry struct {
|
||||
Message json.RawMessage `json:"MESSAGE"`
|
||||
@@ -64,8 +100,8 @@ type logLine struct {
|
||||
// batches, grouped into one stream per unit. It returns when ctx is cancelled.
|
||||
// If journalctl is not available (e.g. on Android/Termux) it logs once and exits
|
||||
// without error, leaving metrics shipping unaffected.
|
||||
func shipJournald(ctx context.Context, cfg Config) {
|
||||
cmd := exec.CommandContext(ctx, "journalctl", "-f", "-o", "json", "-n", "0", "--no-pager")
|
||||
func shipJournald(ctx context.Context, cfg Config, binPath string) {
|
||||
cmd := exec.CommandContext(ctx, binPath, "-f", "-o", "json", "-n", "0", "--no-pager")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Printf("logs: cannot pipe journalctl: %v", err)
|
||||
@@ -146,3 +182,96 @@ func shipJournald(ctx context.Context, cfg Config) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// shipFileTail tails a growing log file (written by an external shell helper,
|
||||
// e.g. `logcat -v epoch` on Android/Termux) and pushes new lines to Loki under
|
||||
// one stream (job=<job>). It does NO exec — only file reads — so it is safe on
|
||||
// Android where exec from Go is blocked by seccomp. Handles truncation/rotation
|
||||
// by detecting a shrinking file and restarting from offset 0.
|
||||
func shipFileTail(ctx context.Context, cfg Config, path, job string) {
|
||||
log.Printf("logs: tailing %s for Loki (job=%s)", path, job)
|
||||
|
||||
var offset int64
|
||||
if fi, err := os.Stat(path); err == nil {
|
||||
offset = fi.Size() // skip pre-existing history on first start
|
||||
}
|
||||
labels := map[string]string{"instance": cfg.Node, "job": job}
|
||||
|
||||
ticker := time.NewTicker(3 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
continue
|
||||
}
|
||||
if fi.Size() < offset {
|
||||
offset = 0 // file was truncated or rotated
|
||||
}
|
||||
if fi.Size() == offset {
|
||||
f.Close()
|
||||
continue
|
||||
}
|
||||
if _, err := f.Seek(offset, io.SeekStart); err != nil {
|
||||
f.Close()
|
||||
continue
|
||||
}
|
||||
data, err := io.ReadAll(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// Only consume up to the last complete line; keep the remainder for
|
||||
// the next tick so we never ship a half-written line.
|
||||
lastNL := bytes.LastIndexByte(data, '\n')
|
||||
if lastNL < 0 {
|
||||
continue
|
||||
}
|
||||
offset += int64(lastNL + 1)
|
||||
|
||||
var ts []int64
|
||||
var ln []string
|
||||
now := time.Now().UnixNano()
|
||||
for _, raw := range strings.Split(string(data[:lastNL]), "\n") {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || strings.HasPrefix(raw, "---------") {
|
||||
continue
|
||||
}
|
||||
t, msg := parseLogcatEpoch(raw, now)
|
||||
ts = append(ts, t)
|
||||
ln = append(ln, msg)
|
||||
}
|
||||
if len(ln) == 0 {
|
||||
continue
|
||||
}
|
||||
if err := infra.PushLokiStream(cfg.LokiURL, cfg.User, cfg.Pass, labels, ts, ln); err != nil {
|
||||
log.Printf("logs: file push error (%d lines): %v", len(ln), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseLogcatEpoch splits a `-v epoch` logcat line into a nanosecond timestamp
|
||||
// and the remaining text. Lines look like: "1609459200.123 1234 1235 I Tag: msg".
|
||||
// On any parse failure it returns the fallback timestamp and the raw line.
|
||||
func parseLogcatEpoch(raw string, fallback int64) (int64, string) {
|
||||
sp := strings.IndexByte(raw, ' ')
|
||||
if sp <= 0 {
|
||||
return fallback, raw
|
||||
}
|
||||
secs, err := strconv.ParseFloat(raw[:sp], 64)
|
||||
if err != nil {
|
||||
return fallback, raw
|
||||
}
|
||||
rest := strings.TrimSpace(raw[sp:])
|
||||
return int64(secs * 1e9), rest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user