2021-10-01 06:21:43 +08:00
|
|
|
// Package daemon implements a service for mediating access to the data store,
|
|
|
|
// and its client.
|
|
|
|
//
|
|
|
|
// Most RPCs exposed by the service correspond to the methods of Store in the
|
|
|
|
// store package and are not documented here.
|
2018-10-14 01:51:47 +08:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2021-09-12 05:04:54 +08:00
|
|
|
"net"
|
2018-10-14 01:51:47 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/daemon/internal/api"
|
2021-10-01 06:21:43 +08:00
|
|
|
"src.elv.sh/pkg/logutil"
|
|
|
|
"src.elv.sh/pkg/prog"
|
2022-01-27 05:17:29 +08:00
|
|
|
"src.elv.sh/pkg/rpc"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/store"
|
2018-10-14 01:51:47 +08:00
|
|
|
)
|
|
|
|
|
2021-10-01 06:21:43 +08:00
|
|
|
var logger = logutil.GetLogger("[daemon] ")
|
|
|
|
|
|
|
|
// Program is the daemon subprogram.
|
2022-02-05 22:48:55 +08:00
|
|
|
type Program struct {
|
|
|
|
run bool
|
|
|
|
paths *prog.DaemonPaths
|
|
|
|
// Used in tests.
|
|
|
|
serveOpts ServeOpts
|
|
|
|
}
|
2021-10-01 06:21:43 +08:00
|
|
|
|
2022-02-05 22:48:55 +08:00
|
|
|
func (p *Program) RegisterFlags(fs *prog.FlagSet) {
|
2022-03-03 08:29:49 +08:00
|
|
|
fs.BoolVar(&p.run, "daemon", false,
|
|
|
|
"[internal flag] Run the storage daemon instead of an Elvish shell")
|
2022-02-05 22:48:55 +08:00
|
|
|
p.paths = fs.DaemonPaths()
|
2021-10-01 06:21:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-05 22:48:55 +08:00
|
|
|
func (p *Program) Run(fds [3]*os.File, args []string) error {
|
|
|
|
if !p.run {
|
|
|
|
return prog.ErrNextProgram
|
2021-10-01 06:21:43 +08:00
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
return prog.BadUsage("arguments are not allowed with -daemon")
|
|
|
|
}
|
2022-02-05 22:48:55 +08:00
|
|
|
|
|
|
|
// The stdout is redirected to a unique log file (see the spawn function),
|
|
|
|
// so just use it for logging.
|
|
|
|
logutil.SetOutput(fds[1])
|
2021-10-01 06:21:43 +08:00
|
|
|
setUmaskForDaemon()
|
2022-02-05 22:48:55 +08:00
|
|
|
exit := Serve(p.paths.Sock, p.paths.DB, p.serveOpts)
|
2021-10-01 06:21:43 +08:00
|
|
|
return prog.Exit(exit)
|
|
|
|
}
|
|
|
|
|
2021-10-02 22:47:18 +08:00
|
|
|
// ServeOpts keeps options that can be passed to Serve.
|
|
|
|
type ServeOpts struct {
|
2021-09-24 03:54:12 +08:00
|
|
|
// If not nil, will be closed when the daemon is ready to serve requests.
|
|
|
|
Ready chan<- struct{}
|
2021-09-30 06:29:07 +08:00
|
|
|
// Causes the daemon to abort if closed or sent any date. If nil, Serve will
|
|
|
|
// set up its own signal channel by listening to SIGINT and SIGTERM.
|
2021-10-02 22:47:18 +08:00
|
|
|
Signals <-chan os.Signal
|
|
|
|
// If not nil, overrides the response of the Version RPC.
|
|
|
|
Version *int
|
2021-09-24 03:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 01:51:47 +08:00
|
|
|
// Serve runs the daemon service, listening on the socket specified by sockpath
|
2021-09-30 06:29:07 +08:00
|
|
|
// and serving data from dbpath until all clients have exited. See doc for
|
2022-06-20 02:06:24 +08:00
|
|
|
// ServeOpts for additional options.
|
2021-10-02 22:47:18 +08:00
|
|
|
func Serve(sockpath, dbpath string, opts ServeOpts) int {
|
2018-10-14 01:51:47 +08:00
|
|
|
logger.Println("pid is", syscall.Getpid())
|
|
|
|
logger.Println("going to listen", sockpath)
|
2021-09-12 05:04:54 +08:00
|
|
|
listener, err := net.Listen("unix", sockpath)
|
2018-10-14 01:51:47 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to listen on %s: %v", sockpath, err)
|
|
|
|
logger.Println("aborting")
|
2021-09-16 03:59:37 +08:00
|
|
|
return 2
|
2018-10-14 01:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
st, err := store.NewStore(dbpath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to create storage: %v", err)
|
|
|
|
logger.Printf("serving anyway")
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:54:12 +08:00
|
|
|
server := rpc.NewServer()
|
2021-10-02 22:47:18 +08:00
|
|
|
version := api.Version
|
|
|
|
if opts.Version != nil {
|
|
|
|
version = *opts.Version
|
|
|
|
}
|
|
|
|
server.RegisterName(api.ServiceName, &service{version, st, err})
|
2021-09-24 03:54:12 +08:00
|
|
|
|
|
|
|
connCh := make(chan net.Conn, 10)
|
|
|
|
listenErrCh := make(chan error, 1)
|
2018-10-14 01:51:47 +08:00
|
|
|
go func() {
|
2021-09-24 03:54:12 +08:00
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
2021-09-16 03:59:37 +08:00
|
|
|
if err != nil {
|
2021-09-24 03:54:12 +08:00
|
|
|
listenErrCh <- err
|
|
|
|
close(listenErrCh)
|
|
|
|
return
|
2021-09-16 03:59:37 +08:00
|
|
|
}
|
2021-09-24 03:54:12 +08:00
|
|
|
connCh <- conn
|
2018-10-14 01:51:47 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-02 22:47:18 +08:00
|
|
|
sigCh := opts.Signals
|
2021-09-30 06:29:07 +08:00
|
|
|
if sigCh == nil {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
sigCh = ch
|
|
|
|
}
|
2018-10-14 01:51:47 +08:00
|
|
|
|
2021-09-24 03:54:12 +08:00
|
|
|
conns := make(map[net.Conn]struct{})
|
|
|
|
connDoneCh := make(chan net.Conn, 10)
|
2018-10-14 01:51:47 +08:00
|
|
|
|
2021-10-24 01:39:55 +08:00
|
|
|
interrupt := func() {
|
2021-09-24 03:54:12 +08:00
|
|
|
if len(conns) == 0 {
|
|
|
|
logger.Println("exiting since there are no clients")
|
|
|
|
}
|
|
|
|
logger.Printf("going to close %v active connections", len(conns))
|
|
|
|
for conn := range conns {
|
2022-06-15 04:01:50 +08:00
|
|
|
// Ignore the error - if we can't close the connection it's because
|
|
|
|
// the client has closed it. There is nothing we can do anyway.
|
|
|
|
conn.Close()
|
2021-09-24 03:54:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 22:47:18 +08:00
|
|
|
if opts.Ready != nil {
|
|
|
|
close(opts.Ready)
|
2021-09-24 03:54:12 +08:00
|
|
|
}
|
2018-10-14 01:51:47 +08:00
|
|
|
|
2021-09-24 03:54:12 +08:00
|
|
|
loop:
|
2018-10-14 01:51:47 +08:00
|
|
|
for {
|
2021-09-24 03:54:12 +08:00
|
|
|
select {
|
|
|
|
case sig := <-sigCh:
|
2021-10-24 01:39:55 +08:00
|
|
|
logger.Printf("received signal %v", sig)
|
|
|
|
interrupt()
|
|
|
|
break loop
|
2021-09-24 03:54:12 +08:00
|
|
|
case err := <-listenErrCh:
|
|
|
|
logger.Println("could not listen:", err)
|
|
|
|
if len(conns) == 0 {
|
|
|
|
logger.Println("exiting since there are no clients")
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
logger.Println("continuing to serve until all existing clients exit")
|
|
|
|
case conn := <-connCh:
|
|
|
|
conns[conn] = struct{}{}
|
|
|
|
go func() {
|
|
|
|
server.ServeConn(conn)
|
|
|
|
connDoneCh <- conn
|
|
|
|
}()
|
|
|
|
case conn := <-connDoneCh:
|
|
|
|
delete(conns, conn)
|
|
|
|
if len(conns) == 0 {
|
|
|
|
logger.Println("all clients disconnected, exiting")
|
|
|
|
break loop
|
2021-06-28 06:57:01 +08:00
|
|
|
}
|
2018-10-14 01:51:47 +08:00
|
|
|
}
|
2021-09-24 03:54:12 +08:00
|
|
|
}
|
2018-10-14 01:51:47 +08:00
|
|
|
|
2021-09-24 03:54:12 +08:00
|
|
|
err = os.Remove(sockpath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to remove socket %s: %v", sockpath, err)
|
|
|
|
}
|
|
|
|
if st != nil {
|
|
|
|
err = st.Close()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to close storage: %v", err)
|
2018-10-14 01:51:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 03:54:12 +08:00
|
|
|
err = listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("failed to close listener: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure that the listener goroutine has exited before returning
|
|
|
|
<-listenErrCh
|
2021-09-16 03:59:37 +08:00
|
|
|
return 0
|
2018-10-14 01:51:47 +08:00
|
|
|
}
|