Skip to content

Commit b08a173

Browse files
authored
Merge pull request #4387 from AkihiroSuda/fix-4321
Make errors less scary
2 parents 0872e1f + a6b5c35 commit b08a173

File tree

7 files changed

+66
-15
lines changed

7 files changed

+66
-15
lines changed

hack/test-templates.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ if [[ -n ${CHECKS["ssh-over-vsock"]} ]]; then
328328
INFO "Testing .ssh.overVsock=true configuration"
329329
limactl stop "${NAME}"
330330
# Detection of the SSH server on VSOCK may fail; however, a failing log indicates that controlling detection via the environment variable works as expected.
331-
if ! limactl start --set '.ssh.overVsock=true' "${NAME}" 2>&1 | grep -i -E "(started vsock forwarder|Failed to detect SSH server on vsock)"; then
331+
if ! limactl start --set '.ssh.overVsock=true' "${NAME}" 2>&1 | grep -i -E "(started vsock forwarder|SSH server does not seem to be running on vsock port)"; then
332332
set +x
333333
diagnose "${NAME}"
334334
ERROR ".ssh.overVsock=true did not enable vsock forwarder"
@@ -337,7 +337,7 @@ if [[ -n ${CHECKS["ssh-over-vsock"]} ]]; then
337337
INFO 'Testing .ssh.overVsock=null configuration'
338338
limactl stop "${NAME}"
339339
# Detection of the SSH server on VSOCK may fail; however, a failing log indicates that controlling detection via the environment variable works as expected.
340-
if ! limactl start --set '.ssh.overVsock=null' "${NAME}" 2>&1 | grep -i -E "(started vsock forwarder|Failed to detect SSH server on vsock)"; then
340+
if ! limactl start --set '.ssh.overVsock=null' "${NAME}" 2>&1 | grep -i -E "(started vsock forwarder|SSH server does not seem to be running on vsock port)"; then
341341
set +x
342342
diagnose "${NAME}"
343343
ERROR ".ssh.overVsock=null did not enable vsock forwarder"

pkg/driver/vz/vm_darwin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ func startVM(ctx context.Context, inst *limatype.Instance, sshLocalPort int) (vm
116116
logrus.Infof("Detected SSH server is listening on the vsock port; changed %s to proxy for the vsock port", hostAddress)
117117
usernetSSHLocalPort = 0 // disable gvisor ssh port forwarding
118118
} else {
119-
logrus.WithError(err).Warn("Failed to detect SSH server on vsock port, falling back to usernet forwarder")
119+
logrus.WithError(err).WithField("hostAddress", hostAddress).
120+
Debugf("Failed to start vsock forwarder (systemd is older than v256?)")
121+
logrus.Info("SSH server does not seem to be running on vsock port, using usernet forwarder")
120122
}
121123
} else {
122124
logrus.WithError(err).Warn("Failed to wait for the guest SSH server to become available, falling back to usernet forwarder")

pkg/osutil/osutil_unix.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package osutil
88
import (
99
"bytes"
1010
"context"
11+
"errors"
1112
"fmt"
1213
"os"
1314
"os/exec"
@@ -36,3 +37,7 @@ func Sysctl(ctx context.Context, name string) (string, error) {
3637
}
3738
return strings.TrimSuffix(string(stdout), "\n"), nil
3839
}
40+
41+
func IsEACCES(err error) bool {
42+
return errors.Is(err, unix.EACCES)
43+
}

pkg/osutil/osutil_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ func SignalName(sig os.Signal) string {
5757
func Sysctl(_ context.Context, _ string) (string, error) {
5858
return "", errors.New("sysctl: unimplemented on Windows")
5959
}
60+
61+
func IsEACCES(err error) bool {
62+
return errors.Is(err, syscall.ERROR_ACCESS_DENIED) || errors.Is(err, syscall.WSAEACCES)
63+
}

pkg/portfwd/listener.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"sync"
1515

1616
"github.com/sirupsen/logrus"
17+
18+
"github.com/lima-vm/lima/v2/pkg/osutil"
1719
)
1820

1921
type ClosableListeners struct {
@@ -102,7 +104,7 @@ func (p *ClosableListeners) forwardTCP(ctx context.Context, dialContext func(ctx
102104
}
103105
tcpLis, err := Listen(ctx, p.listenConfig, hostAddress)
104106
if err != nil {
105-
logrus.Errorf("failed to listen to TCP connection: %v", err)
107+
logListenError(err, "tcp", hostAddress)
106108
p.listenersRW.Unlock()
107109
return
108110
}
@@ -139,7 +141,7 @@ func (p *ClosableListeners) forwardUDP(ctx context.Context, dialContext func(ctx
139141

140142
udpConn, err := ListenPacket(ctx, p.listenConfig, hostAddress)
141143
if err != nil {
142-
logrus.Errorf("failed to listen udp: %v", err)
144+
logListenError(err, "udp", hostAddress)
143145
p.udpListenersRW.Unlock()
144146
return
145147
}
@@ -162,3 +164,41 @@ func prepareUnixSocket(hostSocket string) error {
162164
}
163165
return nil
164166
}
167+
168+
func logListenError(err error, proto, hostAddress string) {
169+
var negligibleReason string
170+
if proto != "unix" {
171+
if _, portStr, err := net.SplitHostPort(hostAddress); err == nil {
172+
switch proto {
173+
case "tcp":
174+
//nolint:gocritic // singleCaseSwitch: should rewrite switch statement to if statement
175+
switch portStr {
176+
case "53":
177+
negligibleReason = "DNS port (often conflicts with the system resolver)"
178+
}
179+
case "udp":
180+
switch portStr {
181+
case "53":
182+
negligibleReason = "DNS port (often conflicts with the system resolver)"
183+
case "323":
184+
negligibleReason = "NTP port (often conflicts with the system NTP server)"
185+
case "5353":
186+
negligibleReason = "mDNS port (often conflicts with the system mDNS responder)"
187+
case "5355":
188+
negligibleReason = "LLMNR port (often conflicts with the system LLMNR responder)"
189+
}
190+
}
191+
}
192+
if osutil.IsEACCES(err) {
193+
negligibleReason = "privileged port"
194+
}
195+
}
196+
197+
if negligibleReason != "" {
198+
logrus.WithError(err).WithField("negligible-reason", negligibleReason).Debugf("failed to listen %s: %s", proto, hostAddress)
199+
logrus.Infof("Not forwarding %s %s", strings.ToUpper(proto), hostAddress)
200+
return
201+
}
202+
203+
logrus.WithError(err).Warnf("failed to listen %s: %s", proto, hostAddress)
204+
}

pkg/portfwd/listener_darwin.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress stri
2222
var lc net.ListenConfig
2323
unixLis, err := lc.Listen(ctx, "unix", hostAddress)
2424
if err != nil {
25-
logrus.WithError(err).Errorf("failed to listen unix: %v", hostAddress)
25+
logListenError(err, "unix", hostAddress)
2626
return nil, err
2727
}
2828
return unixLis, nil
@@ -34,14 +34,15 @@ func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress stri
3434
if !localIP.Equal(IPv4loopback1) || localPort >= 1024 {
3535
tcpLis, err := listenConfig.Listen(ctx, "tcp", hostAddress)
3636
if err != nil {
37-
logrus.Errorf("failed to listen tcp: %v", err)
37+
logListenError(err, "tcp", hostAddress)
3838
return nil, err
3939
}
4040
return tcpLis, nil
4141
}
42-
tcpLis, err := listenConfig.Listen(ctx, "tcp", fmt.Sprintf("0.0.0.0:%d", localPort))
42+
hostAddressPseudo := net.JoinHostPort("0.0.0.0", localPortStr)
43+
tcpLis, err := listenConfig.Listen(ctx, "tcp", hostAddressPseudo)
4344
if err != nil {
44-
logrus.Errorf("failed to listen tcp: %v", err)
45+
logListenError(err, "tcp", hostAddressPseudo)
4546
return nil, err
4647
}
4748
return &pseudoLoopbackListener{tcpLis}, nil
@@ -55,14 +56,15 @@ func ListenPacket(ctx context.Context, listenConfig net.ListenConfig, hostAddres
5556
if !localIP.Equal(IPv4loopback1) || localPort >= 1024 {
5657
udpConn, err := listenConfig.ListenPacket(ctx, "udp", hostAddress)
5758
if err != nil {
58-
logrus.Errorf("failed to listen udp: %v", err)
59+
logListenError(err, "udp", hostAddress)
5960
return nil, err
6061
}
6162
return udpConn, nil
6263
}
63-
udpConn, err := listenConfig.ListenPacket(ctx, "udp", fmt.Sprintf("0.0.0.0:%d", localPort))
64+
hostAddressPseudo := net.JoinHostPort("0.0.0.0", localPortStr)
65+
udpConn, err := listenConfig.ListenPacket(ctx, "udp", hostAddressPseudo)
6466
if err != nil {
65-
logrus.Errorf("failed to listen udp: %v", err)
67+
logListenError(err, "udp", hostAddressPseudo)
6668
return nil, err
6769
}
6870
return &pseudoLoopbackPacketConn{udpConn}, nil

pkg/portfwd/listener_others.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"context"
1010
"net"
1111
"path/filepath"
12-
13-
"github.com/sirupsen/logrus"
1412
)
1513

1614
func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress string) (net.Listener, error) {
@@ -22,7 +20,7 @@ func Listen(ctx context.Context, listenConfig net.ListenConfig, hostAddress stri
2220
var lc net.ListenConfig
2321
unixLis, err := lc.Listen(ctx, "unix", hostAddress)
2422
if err != nil {
25-
logrus.WithError(err).Errorf("failed to listen unix: %v", hostAddress)
23+
logListenError(err, "unix", hostAddress)
2624
return nil, err
2725
}
2826
return unixLis, nil

0 commit comments

Comments
 (0)