Skip to content

Commit

Permalink
Cygwin: dsp: Fix incorrect openflags when opening multiple /dev/dsp
Browse files Browse the repository at this point in the history
Previously, the following steps failed with error:
  1) Open /dev/dsp with O_RDONLY
  2) Open /dev/dsp with O_WRONLY
  3) Issue SNDCTL_DSP_GETOSPACE ioctl() for 2)
This is because IS_WRITE() returns false for 2) due to incorrect
openflags handling in archetype instance. This patch fixes the
issue by adding open_setup() to fhandler_dev_dsp to set openflags
correctly for each instance.

Fixes: 92ddb74 ("* fhandler_dsp.cc (fhandler_dev_dsp::open): Remove archetype handling.")
Signed-off-by: Takashi Yano <[email protected]>
  • Loading branch information
tyan0 committed Jun 28, 2024
1 parent 36e398b commit eaa606c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
26 changes: 16 additions & 10 deletions winsup/cygwin/fhandler/dsp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,19 +1026,19 @@ fhandler_dev_dsp::fhandler_dev_dsp ():
ssize_t
fhandler_dev_dsp::write (const void *ptr, size_t len)
{
return base ()->_write (ptr, len);
return base ()->_write (ptr, len, this);
}

void
fhandler_dev_dsp::read (void *ptr, size_t& len)
{
base ()->_read (ptr, len);
base ()->_read (ptr, len, this);
}

int
fhandler_dev_dsp::ioctl (unsigned int cmd, void *buf)
{
return base ()->_ioctl (cmd, buf);
return base ()->_ioctl (cmd, buf, this);
}

int
Expand All @@ -1065,7 +1065,6 @@ fhandler_dev_dsp::open (int flags, mode_t mode)
{
int ret = -1, err = 0;
UINT num_in = 0, num_out = 0;
set_flags ((flags & ~O_TEXT) | O_BINARY);
// Work out initial sample format & frequency, /dev/dsp defaults
audioformat_ = AFMT_U8;
audiofreq_ = 8000;
Expand Down Expand Up @@ -1105,11 +1104,11 @@ fhandler_dev_dsp::open (int flags, mode_t mode)
return ret;
}

#define IS_WRITE() ((get_flags() & O_ACCMODE) != O_RDONLY)
#define IS_READ() ((get_flags() & O_ACCMODE) != O_WRONLY)
#define IS_WRITE() ((fh->get_flags() & O_ACCMODE) != O_RDONLY)
#define IS_READ() ((fh->get_flags() & O_ACCMODE) != O_WRONLY)

ssize_t
fhandler_dev_dsp::_write (const void *ptr, size_t len)
fhandler_dev_dsp::_write (const void *ptr, size_t len, fhandler_dev_dsp *fh)
{
debug_printf ("ptr=%p len=%ld", ptr, len);
int len_s = len;
Expand Down Expand Up @@ -1168,7 +1167,7 @@ fhandler_dev_dsp::_write (const void *ptr, size_t len)
}

void
fhandler_dev_dsp::_read (void *ptr, size_t& len)
fhandler_dev_dsp::_read (void *ptr, size_t& len, fhandler_dev_dsp *fh)
{
debug_printf ("ptr=%p len=%ld", ptr, len);

Expand Down Expand Up @@ -1244,7 +1243,7 @@ fhandler_dev_dsp::close ()
}

int
fhandler_dev_dsp::_ioctl (unsigned int cmd, void *buf)
fhandler_dev_dsp::_ioctl (unsigned int cmd, void *buf, fhandler_dev_dsp *fh)
{
debug_printf ("audio_in=%p audio_out=%p", audio_in_, audio_out_);
int *intbuf = (int *) buf;
Expand Down Expand Up @@ -1349,7 +1348,7 @@ fhandler_dev_dsp::_ioctl (unsigned int cmd, void *buf)
CASE (SNDCTL_DSP_STEREO)
{
int nChannels = *intbuf + 1;
int res = _ioctl (SNDCTL_DSP_CHANNELS, &nChannels);
int res = _ioctl (SNDCTL_DSP_CHANNELS, &nChannels, fh);
*intbuf = nChannels - 1;
return res;
}
Expand Down Expand Up @@ -1547,3 +1546,10 @@ fhandler_dev_dsp::read_ready ()
{
return base ()->_read_ready ();
}

bool
fhandler_dev_dsp::open_setup (int flags)
{
set_flags ((flags & ~O_TEXT) | O_BINARY);
return fhandler_base::open_setup (flags);
}
7 changes: 4 additions & 3 deletions winsup/cygwin/local_includes/fhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2881,11 +2881,12 @@ class fhandler_dev_dsp: public fhandler_base
int close ();
void fixup_after_fork (HANDLE);
void fixup_after_exec ();
bool open_setup (int);

private:
ssize_t _write (const void *, size_t);
void _read (void *, size_t&);
int _ioctl (unsigned int, void *);
ssize_t _write (const void *, size_t, fhandler_dev_dsp *);
void _read (void *, size_t&, fhandler_dev_dsp *);
int _ioctl (unsigned int, void *, fhandler_dev_dsp *);
int _fcntl (int cmd, intptr_t);
void _fixup_after_fork (HANDLE);
void _fixup_after_exec ();
Expand Down

0 comments on commit eaa606c

Please sign in to comment.