diff --git a/client/connection.go b/client/connection.go index 9e6b09c14a4..acb66a145b3 100644 --- a/client/connection.go +++ b/client/connection.go @@ -171,20 +171,8 @@ func ConnectIncusUnixWithContext(ctx context.Context, path string, args *Connect ctxConnected, ctxConnectedCancel := context.WithCancel(context.Background()) - // Initialize the client struct - server := ProtocolIncus{ - ctx: ctx, - httpBaseURL: *httpBaseURL, - httpUnixPath: path, - httpProtocol: "unix", - httpUserAgent: args.UserAgent, - ctxConnected: ctxConnected, - ctxConnectedCancel: ctxConnectedCancel, - eventConns: make(map[string]*websocket.Conn), - eventListeners: make(map[string][]*EventListener), - } - // Determine the socket path + var projectName string if path == "" { path = os.Getenv("INCUS_SOCKET") if path == "" { @@ -194,9 +182,32 @@ func ConnectIncusUnixWithContext(ctx context.Context, path string, args *Connect } path = filepath.Join(incusDir, "unix.socket") + userPath := filepath.Join(incusDir, "unix.socket.user") + if !util.PathIsWritable(path) && util.PathIsWritable(userPath) { + // Handle the use of incus-user. + path = userPath + + // When using incus-user, the project list is typically restricted. + // So let's try to be smart about the project we're using. + projectName = fmt.Sprintf("user-%d", os.Geteuid()) + } } } + // Initialize the client struct + server := ProtocolIncus{ + ctx: ctx, + httpBaseURL: *httpBaseURL, + httpUnixPath: path, + httpProtocol: "unix", + httpUserAgent: args.UserAgent, + ctxConnected: ctxConnected, + ctxConnectedCancel: ctxConnectedCancel, + eventConns: make(map[string]*websocket.Conn), + eventListeners: make(map[string][]*EventListener), + project: projectName, + } + // Setup the HTTP client httpClient, err := unixHTTPClient(args, path) if err != nil {