maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1//go:build linux
  2// +build linux
  3
  4/*
  5Maddy Mail Server - Composable all-in-one email server.
  6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  7
  8This program is free software: you can redistribute it and/or modify
  9it under the terms of the GNU General Public License as published by
 10the Free Software Foundation, either version 3 of the License, or
 11(at your option) any later version.
 12
 13This program is distributed in the hope that it will be useful,
 14but WITHOUT ANY WARRANTY; without even the implied warranty of
 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16GNU General Public License for more details.
 17
 18You should have received a copy of the GNU General Public License
 19along with this program.  If not, see <https://www.gnu.org/licenses/>.
 20*/
 21
 22package maddy
 23
 24import (
 25	"errors"
 26	"fmt"
 27	"io"
 28	"net"
 29	"os"
 30	"strings"
 31	"syscall"
 32
 33	"github.com/foxcpp/maddy/framework/log"
 34)
 35
 36type SDStatus string
 37
 38const (
 39	SDReady     = "READY=1"
 40	SDReloading = "RELOADING=1"
 41	SDStopping  = "STOPPING=1"
 42)
 43
 44var ErrNoNotifySock = errors.New("no systemd socket")
 45
 46func sdNotifySock() (*net.UnixConn, error) {
 47	sockAddr := os.Getenv("NOTIFY_SOCKET")
 48	if sockAddr == "" {
 49		return nil, ErrNoNotifySock
 50	}
 51	if strings.HasPrefix(sockAddr, "@") {
 52		sockAddr = "\x00" + sockAddr[1:]
 53	}
 54
 55	return net.DialUnix("unixgram", nil, &net.UnixAddr{
 56		Name: sockAddr,
 57		Net:  "unixgram",
 58	})
 59}
 60
 61func setScmPassCred(sock *net.UnixConn) error {
 62	sConn, err := sock.SyscallConn()
 63	if err != nil {
 64		return err
 65	}
 66
 67	var sockoptErr error
 68	if err := sConn.Control(func(fd uintptr) {
 69		sockoptErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
 70	}); err != nil {
 71		return err
 72	}
 73	if sockoptErr != nil {
 74		return sockoptErr
 75	}
 76	return nil
 77}
 78
 79func systemdStatus(status SDStatus, desc string) {
 80	sock, err := sdNotifySock()
 81	if err != nil {
 82		if !errors.Is(err, ErrNoNotifySock) {
 83			log.Println("systemd: failed to acquire notify socket:", err)
 84		}
 85		return
 86	}
 87	defer sock.Close()
 88
 89	if err := setScmPassCred(sock); err != nil {
 90		log.Println("systemd: failed to set SCM_PASSCRED on the socket:", err)
 91	}
 92
 93	if desc != "" {
 94		if _, err := io.WriteString(sock, fmt.Sprintf("%s\nSTATUS=%s", status, desc)); err != nil {
 95			log.Println("systemd: I/O error:", err)
 96		}
 97		log.Debugf(`systemd: %s STATUS="%s"`, status, desc)
 98	} else {
 99		if _, err := io.WriteString(sock, string(status)); err != nil {
100			log.Println("systemd: I/O error:", err)
101		}
102		log.Debugf(`systemd: %s`, status)
103	}
104}
105
106func systemdStatusErr(reportedErr error) {
107	sock, err := sdNotifySock()
108	if err != nil {
109		if !errors.Is(err, ErrNoNotifySock) {
110			log.Println("systemd: failed to acquire notify socket:", err)
111		}
112		return
113	}
114	defer sock.Close()
115
116	if err := setScmPassCred(sock); err != nil {
117		log.Println("systemd: failed to set SCM_PASSCRED on the socket:", err)
118	}
119
120	var errno syscall.Errno
121	if errors.As(reportedErr, &errno) {
122		log.Debugf(`systemd: ERRNO=%d STATUS="%v"`, errno, reportedErr)
123		if _, err := io.WriteString(sock, fmt.Sprintf("ERRNO=%d\nSTATUS=%v", errno, reportedErr)); err != nil {
124			log.Println("systemd: I/O error:", err)
125		}
126		return
127	}
128
129	if _, err := io.WriteString(sock, fmt.Sprintf("STATUS=%v\n", reportedErr)); err != nil {
130		log.Println("systemd: I/O error:", err)
131	}
132	log.Debugf(`systemd: STATUS="%v"`, reportedErr)
133}