Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/bta2dpd/bta2dpd Add option -D to run in the backgro...



details:   https://anonhg.NetBSD.org/src/rev/b9c27feb6de4
branches:  trunk
changeset: 452917:b9c27feb6de4
user:      nakayama <nakayama%NetBSD.org@localhost>
date:      Sat Jul 27 20:10:29 2019 +0000

description:
Add option -D to run in the background using daemon(3).
Also changed the message output to using syslog(3).

diffstat:

 usr.sbin/bta2dpd/bta2dpd/Makefile  |    6 +-
 usr.sbin/bta2dpd/bta2dpd/bta2dpd.8 |   10 ++-
 usr.sbin/bta2dpd/bta2dpd/bta2dpd.c |  111 ++++++++++++++++++++++---------------
 3 files changed, 75 insertions(+), 52 deletions(-)

diffs (truncated from 433 to 300 lines):

diff -r 06853d52aae4 -r b9c27feb6de4 usr.sbin/bta2dpd/bta2dpd/Makefile
--- a/usr.sbin/bta2dpd/bta2dpd/Makefile Sat Jul 27 16:15:58 2019 +0000
+++ b/usr.sbin/bta2dpd/bta2dpd/Makefile Sat Jul 27 20:10:29 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.2 2017/01/28 23:19:20 christos Exp $
+# $NetBSD: Makefile,v 1.3 2019/07/27 20:10:29 nakayama Exp $
 
 .include <bsd.own.mk>
 
@@ -9,8 +9,8 @@
 MAN=           bta2dpd.8
 
 CFLAGS+=       -I.
-DPADD+=                ${LIBBLUETOOTH} ${LIBEVENT}
-LDADD+=                -lbluetooth -levent
+DPADD+=                ${LIBBLUETOOTH} ${LIBEVENT} ${LIBUTIL}
+LDADD+=                -lbluetooth -levent -lutil
 CLEANFILES+=   sbc_crc.h sbc_coeffs.h
 
 sbc_encode.c:  sbc_coeffs.h sbc_crc.h
diff -r 06853d52aae4 -r b9c27feb6de4 usr.sbin/bta2dpd/bta2dpd/bta2dpd.8
--- a/usr.sbin/bta2dpd/bta2dpd/bta2dpd.8        Sat Jul 27 16:15:58 2019 +0000
+++ b/usr.sbin/bta2dpd/bta2dpd/bta2dpd.8        Sat Jul 27 20:10:29 2019 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: bta2dpd.8,v 1.3 2018/05/15 04:25:25 nat Exp $
+.\"    $NetBSD: bta2dpd.8,v 1.4 2019/07/27 20:10:29 nakayama Exp $
 .\"
 .\" Copyright (c) 2015 - 2016  Nathanial Sloss <nathanialsloss%yahoo.com.au@localhost>
 .\" All rights reserved.
@@ -29,7 +29,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd March 2, 2016
+.Dd July 27, 2019
 .Dt BTA2DPD 8
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd Bluetooth Advanced Audio Distribution Profile daemon
 .Sh SYNOPSIS
 .Nm
-.Op Fl v
+.Op Fl \&Dv
 .Op Fl A Ar bitpool_allocation
 .Op Fl B Ar bitpool
 .Op Fl b Ar blocks
@@ -52,7 +52,7 @@
 .Ar files ...
 .Nm
 .Fl K
-.Op Fl Iv
+.Op Fl DIv
 .Op Fl A Ar bitpool_allocation
 .Op Fl B Ar bitpool
 .Op Fl b Ar blocks
@@ -105,6 +105,8 @@
 with only the
 .Fl v
 option and the maximum bitpool for your device will be printed to stdout.
+.It Fl D
+Run in the background.
 .It Fl d Ar device
 Local device address.
 May be given as BDADDR or device name.
diff -r 06853d52aae4 -r b9c27feb6de4 usr.sbin/bta2dpd/bta2dpd/bta2dpd.c
--- a/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c        Sat Jul 27 16:15:58 2019 +0000
+++ b/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c        Sat Jul 27 20:10:29 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bta2dpd.c,v 1.5 2018/01/13 10:20:45 nat Exp $ */
+/* $NetBSD: bta2dpd.c,v 1.6 2019/07/27 20:10:29 nakayama Exp $ */
 
 /*-
  * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsloss%yahoo.com.au@localhost>
@@ -82,7 +82,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 #include <unistd.h>
+#include <util.h>
 #include <errno.h>
 
 #include "avdtp_signal.h"
@@ -216,6 +218,7 @@
 static struct event ctl_ev;                    /* avdtp ctl event */
 
 struct l2cap_info      info;
+static bool            runasDaemon;
 static bool            asSpeaker;
 static bool            initDiscover;   /* initiate avdtp discover */
 static bool            verbose;        /* copy to stdout */
@@ -250,10 +253,11 @@
 static void do_recv(int, short, void *);
 static void do_ctlreq(int, short, void *);
 
-#define log_err(x, ...)                if (verbose) { fprintf (stderr, x "\n",\
-                                               __VA_ARGS__); }
-#define log_info(x, ...)       if (verbose) { fprintf (stderr, x "\n",\
-                                               __VA_ARGS__); }
+#define log_err(st, fmt, args...)      \
+       do { syslog(LOG_ERR, fmt, ##args); exit(st); } while (0)
+#define log_warn(fmt, args...) syslog(LOG_WARNING, fmt, ##args)
+#define log_info(fmt, args...) syslog(LOG_INFO, fmt, ##args)
+#define log_debug(fmt, args...)        syslog(LOG_DEBUG, fmt, ##args)
 
 int
 main(int ac, char *av[])
@@ -263,7 +267,7 @@
        bdaddr_copy(&info.laddr, BDADDR_ANY);
 
        sc = hc = -1;
-       verbose = asSpeaker = test_mode = initDiscover = false;
+       verbose = asSpeaker = test_mode = initDiscover = runasDaemon = false;
        n = m = l = i = j = o = 0;
        freqs[0] = frequency;
        channel_config[0] = channel_mode;
@@ -272,7 +276,7 @@
        alloc_config[0] = alloc_method;
        channel_config[0] = channel_mode;
 
-       while ((ch = getopt(ac, av, "A:a:B:b:d:e:f:IKM:m:p:r:tV:v")) != EOF) {
+       while ((ch = getopt(ac, av, "A:a:B:b:Dd:e:f:IKM:m:p:r:tV:v")) != EOF) {
                switch (ch) {
                case 'A':
                        for (k = 0; k < (int)strlen(optarg); k++) {
@@ -331,6 +335,9 @@
                                usage();
                        }
                        break;
+               case 'D':
+                       runasDaemon = true;
+                       break;
                case 'd': /* local device address */
                        if (!bt_devaddr(optarg, &info.laddr))
                                usage();
@@ -446,6 +453,9 @@
        for (i = 0; i < numfiles; i++)
                files2open[i] = av[i];
 
+       if (runasDaemon && test_mode)
+               usage();
+
        if (bdaddr_any(&info.raddr) && (!asSpeaker && !test_mode))
                usage();
 
@@ -508,21 +518,31 @@
        if (bitpool == 0 || tmpbitpool < bitpool)
                bitpool = (uint8_t)tmpbitpool;
 
+       if (runasDaemon) {
+               if (daemon(0, 0) == -1)
+                       err(EXIT_FAILURE, "daemon");
+               pidfile(NULL);
+               openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
+       } else {
+               openlog(getprogname(), LOG_NLOG | LOG_PERROR | LOG_PTRIM,
+                   LOG_USER);
+       }
+
 again:
        base = event_init();
 
        if (asSpeaker == 0) {
                if (init_client(&info) < 0)
-                       err(EXIT_FAILURE, "init client");
+                       log_err(EXIT_FAILURE, "init client: %m");
        } else {
                if (init_server(&info) < 0)
-                       err(EXIT_FAILURE, "init server");
+                       log_err(EXIT_FAILURE, "init server: %m");
        }
 
        if (verbose) {
-               fprintf(stderr, "A2DP:\n");
-               fprintf(stderr, "\tladdr: %s\n", bt_ntoa(&info.laddr, NULL));
-               fprintf(stderr, "\traddr: %s\n", bt_ntoa(&info.raddr, NULL));
+               log_info("A2DP:");
+               log_info("\tladdr: %s", bt_ntoa(&info.laddr, NULL));
+               log_info("\traddr: %s", bt_ntoa(&info.raddr, NULL));
        }
 
        event_base_loop(base, 0);
@@ -546,10 +566,10 @@
 usage(void)
 {
        fprintf(stderr,
-           "usage:\t%s [-v] [-d device] [-m mode] [-r rate] [-M mtu]\n"
+           "usage:\t%s [-v] [-D] [-d device] [-m mode] [-r rate] [-M mtu]\n"
            "\t\t[-V volume] [-f mode] [-b blocks] [-e bands] [-A alloc]\n"
            "\t\t[-B bitpool] -a address files...\n"
-           "\t%s [-v] [-d device] [-m mode] [-p psm] [-B bitpool]\n"
+           "\t%s [-v] [-D] [-d device] [-m mode] [-p psm] [-B bitpool]\n"
            "\t\t[-a address] [-M mtu] [-r rate] [-I] -K file\n"
            "\t%s -t [-v] [-K] [-r rate] [-M mtu] [-V volume] [-f mode]\n"
            "\t\t[-b blocks] [-e bands] [-A alloc] [-B bitpool] files...\n"
@@ -558,6 +578,7 @@
            "\t-a address   Remote device address\n"
            "\tfiles...     Files to read from (Defaults to stdin/stdout)\n"
            "\t-v           Verbose output\n"
+           "\t-D           Run in the background\n"
            "\t-M mtu       MTU for transmission\n"
            "\t-B bitpool   Maximum bitpool value for encoding\n"
            "\t-V volume    Volume multiplier 0,1,2.\n"
@@ -681,7 +702,7 @@
                        avdtpSendReject(fd, fd, trans, signal);
                }
                if (verbose)
-                       fprintf(stderr, "Received command %d\n",signal);
+                       log_debug("Received command %d", signal);
        } else {
                switch (signal) {
                case AVDTP_DISCOVER:
@@ -704,7 +725,7 @@
                        }
 
                        if (!result && verbose)
-                               fprintf(stderr, "Bitpool value = %d\n",bitpool);
+                               log_debug("Bitpool value = %d", bitpool);
                        state = 3;
                        break;
                case AVDTP_SET_CONFIGURATION:
@@ -725,7 +746,7 @@
                        avdtpSendReject(fd, fd, trans, signal);
                }
                if (verbose)
-                       fprintf(stderr, "Responded to command %d\n",signal);
+                       log_debug("Responded to command %d", signal);
        }
 
 
@@ -735,7 +756,7 @@
        if (asSpeaker && state == 6) {
                len = sizeof(addr);
                if ((sc = accept(orighc,(struct sockaddr*)&addr, &len)) < 0)
-                       err(EXIT_FAILURE, "stream accept");
+                       log_err(EXIT_FAILURE, "stream accept: %m");
 
        }
        if (state == 6)
@@ -757,8 +778,8 @@
 
        if (setsockopt(sc, BTPROTO_L2CAP, SO_L2CAP_LM,
            &l2cap_mode, sizeof(l2cap_mode)) == -1) {
-               log_err("Could not set link mode (0x%4.4x)", l2cap_mode);
-               exit(EXIT_FAILURE);
+               log_err(EXIT_FAILURE, "Could not set link mode (0x%4.4x)",
+                   l2cap_mode);
        }
 
        bdaddr_copy(&addr.bt_bdaddr, &info.raddr);
@@ -774,13 +795,13 @@
        if (asSpeaker) {
                event_set(&recv_ev, sc, EV_READ | EV_PERSIST, do_recv, NULL);
                if (event_add(&recv_ev, NULL) < 0)
-                       err(EXIT_FAILURE, "recv_ev");
+                       log_err(EXIT_FAILURE, "recv_ev: %m");
                state = 7;
        } else {
                event_set(&interrupt_ev, audfile, EV_READ | EV_PERSIST,
                    do_interrupt, NULL);
                if (event_add(&interrupt_ev, NULL) < 0)
-                       err(EXIT_FAILURE, "interrupt_ev");
+                       log_err(EXIT_FAILURE, "interrupt_ev: %m");
                state = 7;
        }
 
@@ -800,7 +821,7 @@
        len = recvstream(fd, audfile);
 
        if (verbose)
-               fprintf(stderr, "Recving %zd bytes\n",len);
+               log_debug("Recving %zd bytes", len);
 
        if (len < 0) {
                event_del(&recv_ev);
@@ -834,7 +855,7 @@
                currentFileInd++;
                audfile = open(files2open[currentFileInd], O_RDONLY);
                if (audfile < 0) {
-                       warn("error opening file %s",
+                       log_warn("error opening file %s: %m",
                            files2open[currentFileInd]);
                        goto next_file;
                }
@@ -843,11 +864,11 @@
                event_set(&interrupt_ev, audfile, EV_READ |
                    EV_PERSIST, do_interrupt, NULL);
                if (event_add(&interrupt_ev, NULL) < 0)
-                       err(EXIT_FAILURE, "interrupt_ev");
+                       log_err(EXIT_FAILURE, "interrupt_ev: %m");
        }
 
        if (verbose)
-               fprintf(stderr, "Streaming %zd bytes\n",len);
+               log_debug("Streaming %zd bytes", len);
 }
 
 /*
@@ -862,7 +883,7 @@



Home | Main Index | Thread Index | Old Index