Re: Open master pty (/dev/ptmx) non blocking

2022-09-24 Thread Christos Zoulas
In article <25389.33419.131713.821...@gargle.gargle.howl>,
Anthony Mallet   wrote:
>-=-=-=-=-=-
>
>Hi,
>
>I have a piece of software that opens a master pty non-blocking:
>fd = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
>
>The intent is to make further read(2) on the master non blocking. But
>the O_NONBLOCK flag seems to be ignored. Attached is a minimal sample
>C program showing the issue.
>
>Several remarks:
>* open(2) manual does not mention a master pty as special regarding the
>  O_NONBLOCK flag, and even says "this flag also has the effect of making
>  all subsequent I/O on the open file non-blocking",
>* explicitly setting the file descriptor as non-blocking with fcntl(2)
>  works fine,
>* a slave pty has no such issue (O_NONBLOCK in open(2) is honoured),
>* FWIW, linux has no surprise here, the flag behaves as one would expect,
>* POSIX does not mention the flag as supported in posix_openpt(3) (it
>  does not says it's not supported either :).
>
>So, I'm not sure if something should be changed here, and if someone
>is willing to check that? I tried to track down where in the kernel
>this happens, maybe in pty_alloc_master in kern/tty_ptm.c? I really
>don't master those kernel aspects so I'm not confident in providing a
>patch.
>
>I guess the principle of least surprise would say that the flag should
>be supported (e.g. for maximum portability). OTOH, if the current
>behaviour is deemed correct or sufficient, maybe at least some manual
>(posix_openpt, open, ...) should mention this is not supported?
>
>Best,
>Anthony

Fixed in HEAD.

christos



Re: Open master pty (/dev/ptmx) non blocking

2022-09-24 Thread Anthony Mallet
On Friday 23 Sep 2022, at 17:34, David H. Gutteridge wrote:
> On Fri, 23 Sep 2022 at 20:14:23 +, David Holland wrote:
> > On Fri, Sep 23, 2022 at 01:39:16PM +0200, Martin Husemann wrote:
> > >  Note that unlike implementations on some other operating
> > >  systems, posix_openpt() does not return EINVAL if the value
> > >  of oflag would be deemed invalid
> >
> > That is, however, kind of a feeble excuse :-)
>
> Agreed. But I inferred this was perhaps done this way on purpose, and
> could be controversial to change.

I noticed that this might also affect Linux emulation as
pty_alloc_master() is used for both native and emualted software.

Linux emulated applications may not be prepared deal with EINVAL if
they pass e.g. O_NONBLOCK.


Re: Open master pty (/dev/ptmx) non blocking

2022-09-24 Thread Anthony Mallet
On Fri, 23 Sep 2022, David Holland wrote:
> While my inclination would be to make it work, until someone wants to
> figure out how to do that it seems straightforward to make O_NONBLOCK
> fail:

Mostly out of curiosity and for the records, I tested the attached
patch. It enables the EINVAL error for unsupported flags and sets
O_NONBLOCK if present. This seems to work after a quick test.

Regarding the O_CLOEXEC, it may require a bit more work. I feel like
the fd_set_exclose() function in kern_descrip.c could be used.


Index: sys/kern/tty_ptm.c
===
RCS file: /cvsroot/src/sys/kern/tty_ptm.c,v
retrieving revision 1.43
diff -u -r1.43 tty_ptm.c
--- sys/kern/tty_ptm.c  29 Jun 2021 22:40:53 -  1.43
+++ sys/kern/tty_ptm.c  24 Sep 2022 15:41:44 -
@@ -87,7 +87,7 @@
 int pts_major, ptc_major;
 
 static dev_t pty_getfree(void);
-static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *);
+static int pty_alloc_master(struct lwp *, int, int *, dev_t *, struct mount *);
 static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
 static int pty_vn_open(struct vnode *, struct lwp *);
 
@@ -155,13 +155,16 @@
 }
 
 static int
-pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp)
+pty_alloc_master(struct lwp *l, int flag, int *fd, dev_t *dev, struct mount 
*mp)
 {
int error;
struct file *fp;
struct vnode *vp;
int md;
 
+   if (flag & ~(O_ACCMODE | O_NOCTTY | O_NONBLOCK))
+   return EINVAL;
+
if ((error = fd_allocfile(, fd)) != 0) {
DPRINTF(("fd_allocfile %d\n", error));
return error;
@@ -199,7 +202,7 @@
else
goto bad;
}
-   fp->f_flag = FREAD|FWRITE;
+   fp->f_flag = FREAD|FWRITE | (flag & O_NONBLOCK);
fp->f_type = DTYPE_VNODE;
fp->f_ops = 
fp->f_vnode = vp;
@@ -343,7 +346,7 @@
case 2: /* /emul/linux/dev/ptmx */
if ((error = pty_getmp(l, )) != 0)
return error;
-   if ((error = pty_alloc_master(l, , , mp)) != 0)
+   if ((error = pty_alloc_master(l, flag, , , mp)) != 0)
return error;
if (minor(dev) == 2) {
/*
@@ -392,7 +395,7 @@
if ((error = pty_getmp(l, )) != 0)
return error;
 
-   if ((error = pty_alloc_master(l, , , mp)) != 0)
+   if ((error = pty_alloc_master(l, 0, , , mp)) != 0)
return error;
 
if ((error = pty_grant_slave(l, newdev, mp)) != 0)