Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make exclusive access optional #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type Mode struct {
Parity Parity // Parity (see Parity type for more info)
StopBits StopBits // Stop bits (see StopBits type for more info)
InitialStatusBits *ModemOutputBits // Initial output modem bits status (if nil defaults to DTR=true and RTS=true)
AccessMode AccessMode // Access mode on unix systems (Default: Exclusive)
}

// Parity describes a serial port parity setting
Expand Down Expand Up @@ -131,6 +132,14 @@ const (
TwoStopBits
)

// AccessMode whether to access the port exclusively
type AccessMode int

const (
Exclusive = 0
Shared = 1
)

// PortError is a platform independent error type for serial ports
type PortError struct {
code PortErrorCode
Expand Down
44 changes: 44 additions & 0 deletions serial_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package serial
import (
"context"
"os/exec"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -62,3 +63,46 @@ func TestDoubleCloseIsNoop(t *testing.T) {
require.NoError(t, port.Close())
require.NoError(t, port.Close())
}

func TestAccessModeDefault(t *testing.T) {
AccessModeExclusive(t, &Mode{})
}

func TestAccessModeExclusive(t *testing.T) {
mode := &Mode{
AccessMode: Exclusive,
}

AccessModeExclusive(t, mode)
}

func AccessModeExclusive(t *testing.T, mode *Mode) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := startSocatAndWaitForPort(t, ctx)
go cmd.Wait()

port, err := Open("/tmp/faketty", mode)
require.NoError(t, err)
_, err2 := Open("/tmp/faketty", mode)
require.Error(t, err2, syscall.ENOENT)
require.NoError(t, port.Close())
}

func TestAccessModeShared(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := startSocatAndWaitForPort(t, ctx)
go cmd.Wait()

mode := &Mode{
AccessMode: Shared,
}

port, err := Open("/tmp/faketty", mode)
require.NoError(t, err)
port2, err2 := Open("/tmp/faketty", mode)
require.NoError(t, err2)
require.NoError(t, port.Close())
require.NoError(t, port2.Close())
}
4 changes: 3 additions & 1 deletion serial_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {

unix.SetNonblock(h, false)

port.acquireExclusiveAccess()
if mode.AccessMode == Exclusive {
port.acquireExclusiveAccess()
}

// This pipe is used as a signal to cancel blocking Read
pipe := &unixutils.Pipe{}
Expand Down