On Sun, May 06, 2018 at 11:25:19AM +0200, Alexandre Ratchov wrote:
> On Fri, May 04, 2018 at 05:31:22PM +0200, Theo Buehler wrote:
> > 
> > While the last submitted patch looks correct to me, I wonder (rather
> > naively) if it would be possible to refactor in such a way that
> > slot_new() is called only after or from within dev_open(), so we can
> > drop the promises at least to "stdio rpath wpath cpath audio" at the
> > point where slot_new() is called for the -i or -o options.
> 
> slot_new() is used to determine device parameters, which are used to
> call dev_open(), so it must be called first.
> 
> The only way to do this is to save the list of files and then open the
> device, then parse file headers, then configure the device. This is
> not trivial.
> 

Hi again,

How about the following solution?  I've also moved the pledge calls back
to the main to make it easier to see what promises are set and where.
I've still not been able to test this using MIDI devices, but everything
else seems to work as far as I can tell.


Regards,
Jesper Wallin


Index: aucat.c
===================================================================
RCS file: /cvs/src/usr.bin/aucat/aucat.c,v
retrieving revision 1.172
diff -u -p -r1.172 aucat.c
--- aucat.c     7 Nov 2017 11:39:24 -0000       1.172
+++ aucat.c     7 May 2018 13:25:30 -0000
@@ -14,6 +14,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <err.h>
 #include <errno.h>
 #include <limits.h>
 #include <poll.h>
@@ -640,31 +641,12 @@ slot_sub_bcopy(struct slot *s, adata_t *
 }
 
 static int
-dev_open(char *dev, int mode, int bufsz, char *port)
+dev_config(int mode, int bufsz)
 {
        int rate, pmax, rmax;
        struct sio_par par;
        struct slot *s;
 
-       if (port) {
-               dev_port = port;
-               dev_mh = mio_open(dev_port, MIO_IN, 0);
-               if (dev_mh == NULL) {
-                       log_puts(port);
-                       log_puts(": couldn't open midi port\n");
-                       return 0;
-               }
-       } else
-               dev_mh = NULL;
-
-       dev_name = dev;
-       dev_sh = sio_open(dev, mode, 0);
-       if (dev_sh == NULL) {
-               log_puts(dev_name);
-               log_puts(": couldn't open audio device\n");
-               return 0;
-       }
-
        rate = pmax = rmax = 0;
        for (s = slot_list; s != NULL; s = s->next) {
                if (s->afile.rate > rate)
@@ -737,6 +719,30 @@ dev_open(char *dev, int mode, int bufsz,
        return 1;
 }
 
+static int
+dev_open(char *dev, int mode, char *port)
+{
+       if (port) {
+               dev_port = port;
+               dev_mh = mio_open(dev_port, MIO_IN, 0);
+               if (dev_mh == NULL) {
+                       log_puts(port);
+                       log_puts(": couldn't open midi port\n");
+                       return 0;
+               }
+       } else
+               dev_mh = NULL;
+
+       dev_name = dev;
+       dev_sh = sio_open(dev, mode, 0);
+       if (dev_sh == NULL) {
+               log_puts(dev_name);
+               log_puts(": couldn't open audio device\n");
+               return 0;
+       }
+       return 1;
+}
+
 static void
 dev_close(void)
 {
@@ -1149,7 +1155,7 @@ sigint(int s)
 }
 
 static int
-playrec(char *dev, int mode, int bufsz, char *port)
+playrec(char *dev, int mode, char *port)
 {
 #define MIDIBUFSZ 0x100
        unsigned char mbuf[MIDIBUFSZ];
@@ -1158,8 +1164,6 @@ playrec(char *dev, int mode, int bufsz, 
        struct slot *s;
        int n, ns, nm, ev;
 
-       if (!dev_open(dev, mode, bufsz, port))
-               return 0;
        n = sio_nfds(dev_sh);
        if (dev_mh)
                n += mio_nfds(dev_mh);
@@ -1359,11 +1363,14 @@ int
 main(int argc, char **argv)
 {
        int dup, cmin, cmax, rate, vol, bufsz, hdr, mode;
-       char *port, *dev;
+       char *port, *dev, *ppath, *rpath;
        struct aparams par;
        int n_flag, c;
        long long pos;
 
+       if (pledge("stdio rpath wpath cpath inet unix dns audio", NULL) == -1)
+               err(1, "pledge");
+
        vol = 127;
        dup = 0;
        bufsz = 0;
@@ -1375,6 +1382,8 @@ main(int argc, char **argv)
        n_flag = 0;
        port = NULL;
        dev = NULL;
+       ppath = NULL;
+       rpath = NULL;
        mode = 0;
        pos = 0;
 
@@ -1408,9 +1417,7 @@ main(int argc, char **argv)
                                return 1;
                        break;
                case 'i':
-                       if (!slot_new(optarg, SIO_PLAY,
-                               &par, hdr, cmin, cmax, rate, dup, vol, pos))
-                               return 1;
+                       ppath = optarg;
                        mode |= SIO_PLAY;
                        break;
                case 'j':
@@ -1421,9 +1428,7 @@ main(int argc, char **argv)
                        n_flag = 1;
                        break;
                case 'o':
-                       if (!slot_new(optarg, SIO_REC,
-                               &par, hdr, cmin, cmax, rate, dup, 0, pos))
-                               return 1;
+                       rpath = optarg;
                        mode |= SIO_REC;
                        break;
                case 'p':
@@ -1461,6 +1466,16 @@ main(int argc, char **argv)
                        log_puts("both -i and -o required\n");
                        return 1;
                }
+               if (pledge("stdio rpath wpath cpath", NULL) == -1)
+                       err(1, "pledge");
+               if (!slot_new(ppath, SIO_PLAY,
+                   &par, hdr, cmin, cmax, rate, dup, vol, pos))
+                       return 1;
+               if (!slot_new(rpath, SIO_REC,
+                   &par, hdr, cmin, cmax, rate, dup, 0, pos))
+                       return 1;
+               if (pledge("stdio", NULL) == -1)
+                       err(1, "pledge");
                if (!offline())
                        return 1;
        } else {
@@ -1470,7 +1485,25 @@ main(int argc, char **argv)
                        log_puts("at least -i or -o required\n");
                        return 1;
                }
-               if (!playrec(dev, mode, bufsz, port))
+               if (!dev_open(dev, mode, port))
+                       return 1;
+               if (pledge("stdio rpath wpath cpath audio", NULL) == -1)
+                       err(1, "pledge");
+               if (mode & SIO_PLAY) {
+                       if (!slot_new(ppath, SIO_PLAY,
+                           &par, hdr, cmin, cmax, rate, dup, vol, pos))
+                               return 1;
+               }
+               if (mode & SIO_REC) {
+                       if (!slot_new(rpath, SIO_REC,
+                           &par, hdr, cmin, cmax, rate, dup, 0, pos))
+                               return 1;
+               }
+               if (!dev_config(mode, bufsz))
+                       return 1;
+               if (pledge("stdio audio", NULL) == -1)
+                       err(1, "pledge");
+               if (!playrec(dev, mode, port))
                        return 1;
        }
        return 0;

Reply via email to