ipc

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ipc hands the command line of a second launch over to the running instance through a Unix socket (SPEC §10.11).

The first Emetos process of a user listens on a socket (Locations); a later launch connects, sends one versioned JSON message that holds its working directory and what it was asked to open, and exits with the exit code of the reply, which with --wait comes only when the files are closed. Join decides which of the two a process is, under a lock file, so that two launches at once end as one instance and one client. This is the only package that may import net (SPEC §12), and it opens Unix sockets only.

Index

Constants

View Source
const (
	// DefaultIOTimeout is how long a message may take to be read or written (a request has to arrive
	// at once; the wait of --wait has no limit).
	DefaultIOTimeout = 5 * time.Second
	// DefaultLockTimeout is how long a launch waits for the lock that another launch holds while it decides.
	DefaultLockTimeout = 10 * time.Second
)

Default timeouts.

View Source
const (
	// Version is the version of the protocol. A server answers a message of another version with an error.
	Version = 1
	// MaxMessageSize is the largest message, in bytes, a server or a client accepts (without the line end).
	MaxMessageSize = 1 << 20
)
View Source
const (
	// TypeOpen is the request of a launch: open what the arguments say.
	TypeOpen = "open"
	// TypeOK is the reply to a request that is carried out and not waited for.
	TypeOK = "ok"
	// TypeDone is the reply to a request that waited (--wait): what it opened is closed.
	TypeDone = "done"
	// TypeError is the reply to a request that failed; Message says why.
	TypeError = "error"
)

The types of messages.

Variables

This section is empty.

Functions

func Join

func Join(ctx context.Context, opts Options) (*Server, *Client, error)

Join decides what this process is. Under the lock file, so that two launches at once cannot both decide the same, it tries the socket: when an instance answers (accepts the connection), the result is a Client for it and no Server; when nobody does it removes a stale socket file (the file of an instance that died), listens, and the result is a Server and no Client. The lock is released before Join returns, and as soon as the server listens a launch that connects is queued until Serve accepts it, so there is no window in which the second launch finds nothing. The caller closes the Server (which removes the socket) or the Client.

The tags are those of Locations, IPCSocketPathInvalid, IPCSocketPathTooLong, IPCLockFailed, IPCLockTimeout, IPCDialFailed, IPCSocketBusy (the path exists and is not a socket, or not the user's), IPCStaleSocketRemoveFailed and IPCListenFailed.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a connection to the running instance.

func Dial

func Dial(ctx context.Context, opts Options) (*Client, error)

Dial connects to the instance that listens at loc, without the lock and without ever becoming a server (tests and tools; a launch uses Join). The tag is IPCDialFailed.

func (*Client) Close

func (c *Client) Close() error

Close ends the connection. It is safe to call more than once.

func (*Client) Send

func (c *Client) Send(ctx context.Context, req Request) (Reply, error)

Send sends the request and returns the reply of the instance. A request with no Version, Type or PID gets them (Version, TypeOpen, the process id). A request that does not wait (Open.Wait false) gets its reply within the I/O timeout; one that waits has no limit but ctx, whose end closes the connection (the instance then stops waiting). The reply can be an error reply (TypeError): that is a Reply, not an error here. The tags are IPCSendFailed and IPCReplyFailed (the deadline of the connection could not be set, or the context ended), those of writeMessage and readMessage (IPCWriteFailed, IPCMessageTooLarge, IPCReadFailed, IPCConnectionClosed, IPCMessageInvalid: the connection ended or the reply did not come in time), IPCReplyInvalid and IPCVersionUnsupported (the reply is of another version).

type FileTarget

type FileTarget struct {
	// Path is the absolute path of the file.
	Path string `json:"path"`
	// Line and Column count from 1; zero is no position.
	Line   int `json:"line,omitempty"`
	Column int `json:"column,omitempty"`
}

FileTarget is a file to open and the position to show in it.

type Handler

type Handler func(ctx context.Context, req Request) Reply

Handler carries out the request of a client and gives the reply. ctx ends when the server stops or when the client goes away (killed, or it closed the connection), so a handler that waits (--wait) stops waiting. It runs on a goroutine of its own, concurrently with the handlers of other clients, and must not touch what belongs to the UI thread directly.

type Location

type Location struct {
	// Socket is the path of the Unix socket.
	Socket string
	// Lock is the path of the lock file that is held while a launch decides whether it is the instance.
	Lock string
}

Location is where the socket of the instance and its lock file are.

func Locations

func Locations(paths storage.Paths, uid int) (Location, error)

Locations gives the socket and the lock file of the user uid: $XDG_RUNTIME_DIR/emetos-<uid>.sock and emetos-<uid>.lock (SPEC §10.11). Without a runtime folder (the variable is unset) they are in <temporary folder>/emetos-<uid>/, a folder that is made with mode 0700 and that must belong to the user and be private, as ssh does for its agent socket. The tags are IPCRuntimeDirInvalid (the folder cannot be made, is not the user's or not private) and IPCSocketPathTooLong (the path does not fit in the address of a Unix socket).

type OpenArgs

type OpenArgs struct {
	// Folder is the folder to open as the workspace, or empty.
	Folder string `json:"folder,omitempty"`
	// Files are the files to open, in order; the first is the one that is shown.
	Files []FileTarget `json:"files,omitempty"`
	// Diff holds the two paths of --diff, or none.
	Diff []string `json:"diff,omitempty"`
	// Merge holds the four paths of --merge (the two sides, the base and the result), or none.
	Merge []string `json:"merge,omitempty"`
	// Wait asks the reply to come only when the files that were opened are closed (--wait).
	Wait bool `json:"wait,omitempty"`
	// NewWindow is --new-window and ReuseWindow is --reuse-window.
	NewWindow   bool `json:"newWindow,omitempty"`
	ReuseWindow bool `json:"reuseWindow,omitempty"`
	// Profile is the name given to --profile, or empty.
	Profile string `json:"profile,omitempty"`
}

OpenArgs are the arguments of a launch that the instance carries out (SPEC §10.11). Every path is absolute.

type Options

type Options struct {
	// Location is the socket and the lock file (Locations).
	Location Location
	// UID is the user that owns the socket: only a peer of this user is served. It is os.Getuid() when zero
	// and UIDSet is false.
	UID    int
	UIDSet bool
	// Logger gets the debug and warning lines of the server; nothing when nil.
	Logger *slog.Logger
	// OnError is told of the failures of one connection (a refused peer, a bad message), which do not stop the
	// server. Optional.
	OnError func(err error)
	// Credentials gives the user id of the peer of a connection; the SO_PEERCRED of the socket when nil.
	// Tests replace it.
	Credentials func(conn *net.UnixConn) (uid int, err error)
	// IOTimeout is DefaultIOTimeout when zero, and LockTimeout is DefaultLockTimeout.
	IOTimeout, LockTimeout time.Duration
}

Options say where the instance listens and how a server and a client behave. The zero value of every field but Location is the default.

type Reply

type Reply struct {
	// Version is the protocol version (Version).
	Version int `json:"version"`
	// Type is TypeOK, TypeDone or TypeError.
	Type string `json:"type"`
	// ExitCode is the exit code the client ends with.
	ExitCode int `json:"exitCode"`
	// Message is the text for the user (an error, a notice), or empty.
	Message string `json:"message,omitempty"`
}

Reply is the message of the instance to a client.

func Done

func Done(exitCode int) Reply

Done is a reply for a request that waited and whose files are closed.

func Failed

func Failed(message string) Reply

Failed is the reply for a request that cannot be carried out; the client ends with exit code 1.

func OK

func OK() Reply

OK is a reply for a request that is carried out.

type Request

type Request struct {
	// Version is the protocol version (Version).
	Version int `json:"version"`
	// Type is TypeOpen.
	Type string `json:"type"`
	// PID is the process id of the client, for the log of the instance.
	PID int `json:"pid"`
	// Cwd is the working directory of the client. The paths in Open are made absolute against it already;
	// it is handed over for the instance's log and for what a later version resolves.
	Cwd string `json:"cwd"`
	// Open says what to open; it is required by TypeOpen.
	Open *OpenArgs `json:"open,omitempty"`
}

Request is the message of a client to the instance: one JSON object on one line.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the instance's end of the socket. Make one with Join.

func (*Server) Close

func (s *Server) Close() error

Close stops listening, ends the connections that are open and removes the socket file, but only if the file is still the one this server made (a later instance may have replaced it). It is safe to call more than once and from the goroutine of Serve's context; the error is IPCCloseFailed.

func (*Server) Path

func (s *Server) Path() string

Path is the path of the socket.

func (*Server) Serve

func (s *Server) Serve(parent context.Context, h Handler) error

Serve accepts clients and gives their requests to h until ctx ends or Close is called. It then closes the server (Close) and returns its result: the handlers in flight are told to stop through their context and may still give a reply, which is written when it comes in time. Each client is served on a goroutine of its own. The tag of a failure of the listener is IPCAcceptFailed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL