Re: ping(8) / ping6(8) source address

2015-11-29 Thread Sebastian Benoit

ok

Florian Obser(flor...@openbsd.org) on 2015.11.29 14:07:24 +:
> Inspired by the traceroute / traceroute6 merge.
> At least reduces the diff in the option parser :)
> OK?
> 
> diff --git ping/ping.c ping/ping.c
> index f5ccaca..4944f77 100644
> --- ping/ping.c
> +++ ping/ping.c
> @@ -110,7 +110,7 @@ int options;
>  #define  F_SO_DEBUG  0x0040
>  /*   0x0080 */
>  #define  F_VERBOSE   0x0100
> -#define  F_SADDR 0x0200
> +/*   0x0200 */
>  #define  F_HDRINCL   0x0400
>  #define  F_TTL   0x0800
>  #define  F_AUD_RECV  0x2000
> @@ -132,7 +132,6 @@ int mx_dup_ck = MAX_DUP_CHK;
>  char rcvd_tbl[MAX_DUP_CHK / 8];
>  
>  struct sockaddr_in whereto;  /* who to ping */
> -struct sockaddr_in whence;   /* Which interface we come from */
>  unsigned int datalen = DEFDATALEN;
>  int s;   /* socket file descriptor */
>  u_char outpackhdr[IP_MAXPACKET]; /* Max packet size = 65535 */
> @@ -186,11 +185,13 @@ int
>  main(int argc, char *argv[])
>  {
>   struct hostent *hp;
> + struct addrinfo hints, *res;
> + struct sockaddr_in  from4;
>   struct sockaddr_in *to;
> - struct in_addr saddr;
>   int ch, i, optval = 1, packlen, preload, maxsize, df = 0, tos = 0;
> + int error;
>   u_char *datap, *packet, ttl = MAXTTL, loop = 1;
> - char *target, hnamebuf[HOST_NAME_MAX+1];
> + char *target, hnamebuf[HOST_NAME_MAX+1], *source = NULL;
>   char rspace[3 + 4 * NROUTES + 1];   /* record route space */
>   socklen_t maxsizelen;
>   const char *errstr;
> @@ -238,13 +239,7 @@ main(int argc, char *argv[])
>   break;
>   case 'I':
>   case 'S':   /* deprecated */
> - if (inet_aton(optarg, ) == 0) {
> - if ((hp = gethostbyname(optarg)) == NULL)
> - errx(1, "bad interface address: %s",
> - optarg);
> - memcpy(, hp->h_addr, sizeof(saddr));
> - }
> - options |= F_SADDR;
> + source = optarg;
>   break;
>   case 'i':   /* wait between sending packets */
>   interval = strtod(optarg, NULL);
> @@ -360,16 +355,27 @@ main(int argc, char *argv[])
>   hostname = hnamebuf;
>   }
>  
> - if (options & F_SADDR) {
> + if (source) {
>   if (IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
>   moptions |= MULTICAST_IF;
>   else {
> - memset(, 0, sizeof(whence));
> - whence.sin_len = sizeof(whence);
> - whence.sin_family = AF_INET;
> - memcpy(_addr.s_addr, , sizeof(saddr));
> - if (bind(s, (struct sockaddr *),
> - sizeof(whence)) < 0)
> + memset(, 0, sizeof(from4));
> + from4.sin_family = AF_INET;
> + if (inet_aton(source, _addr) == 0) {
> + memset(, 0, sizeof(hints));
> + hints.ai_family = AF_INET;
> + hints.ai_socktype = SOCK_DGRAM; /*dummy*/
> + if ((error = getaddrinfo(source, NULL, ,
> + )))
> + errx(1, "%s: %s", source,
> + gai_strerror(error));
> + if (res->ai_addrlen != sizeof(from4))
> + errx(1, "size of sockaddr mismatch");
> + memcpy(, res->ai_addr, res->ai_addrlen);
> + freeaddrinfo(res);
> + }
> + if (bind(s, (struct sockaddr *), sizeof(from4))
> + < 0)
>   err(1, "bind");
>   }
>   }
> @@ -426,8 +432,8 @@ main(int argc, char *argv[])
>   ip->ip_off = htons(df ? IP_DF : 0);
>   ip->ip_ttl = ttl;
>   ip->ip_p = IPPROTO_ICMP;
> - if (options & F_SADDR)
> - ip->ip_src = saddr;
> + if (source)
> + ip->ip_src = from4.sin_addr;
>   else
>   ip->ip_src.s_addr = INADDR_ANY;
>   ip->ip_dst = to->sin_addr;
> @@ -457,8 +463,8 @@ main(int argc, char *argv[])
>   sizeof(ttl)) < 0)
>   err(1, "setsockopt IP_MULTICAST_TTL");
>   if ((moptions & MULTICAST_IF) &&
> - setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, ,
> - sizeof(saddr)) < 0)
> + setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, _addr,
> + sizeof(from4.sin_addr)) < 0)
>   err(1, "setsockopt IP_MULTICAST_IF");
>  
>   

Re: Make bge(4) detachable

2015-11-29 Thread Bob Beck
Jeez Mark,  at this rate I'm going to have to buy you jeans and a
black turtleneck to go with your Jesus computer...

On Sun, Nov 29, 2015 at 10:37 AM, Mark Kettenis  wrote:
> Just in case I accidentally unplug he Thiunderbolt gadget...  The
> bge_stop() function gains a "softonly" argumentlike we introduced in
> other network drivers.
>
> ok?
>
>
> Index: if_bge.c
> ===
> RCS file: /cvs/src/sys/dev/pci/if_bge.c,v
> retrieving revision 1.379
> diff -u -p -r1.379 if_bge.c
> --- if_bge.c25 Nov 2015 03:09:59 -  1.379
> +++ if_bge.c29 Nov 2015 17:35:00 -
> @@ -118,10 +118,12 @@ const struct bge_revision * bge_lookup_r
>  int bge_can_use_msi(struct bge_softc *);
>  int bge_probe(struct device *, void *, void *);
>  void bge_attach(struct device *, struct device *, void *);
> +int bge_detach(struct device *, int);
>  int bge_activate(struct device *, int);
>
>  struct cfattach bge_ca = {
> -   sizeof(struct bge_softc), bge_probe, bge_attach, NULL, bge_activate
> +   sizeof(struct bge_softc), bge_probe, bge_attach, bge_detach,
> +   bge_activate
>  };
>
>  struct cfdriver bge_cd = {
> @@ -145,7 +147,7 @@ int bge_ioctl(struct ifnet *, u_long, ca
>  int bge_rxrinfo(struct bge_softc *, struct if_rxrinfo *);
>  void bge_init(void *);
>  void bge_stop_block(struct bge_softc *, bus_size_t, u_int32_t);
> -void bge_stop(struct bge_softc *);
> +void bge_stop(struct bge_softc *, int);
>  void bge_watchdog(struct ifnet *);
>  int bge_ifmedia_upd(struct ifnet *);
>  void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
> @@ -2536,9 +2538,7 @@ bge_attach(struct device *parent, struct
> pcireg_tpm_ctl, memtype, subid, reg;
> pci_intr_handle_t   ih;
> const char  *intrstr = NULL;
> -   bus_size_t  size, apesize;
> -   bus_dma_segment_t   seg;
> -   int rseg, gotenaddr = 0;
> +   int gotenaddr = 0;
> u_int32_t   hwcfg = 0;
> u_int32_t   mac_addr = 0;
> u_int32_t   misccfg;
> @@ -2560,7 +2560,7 @@ bge_attach(struct device *parent, struct
> DPRINTFN(5, ("pci_mapreg_map\n"));
> memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BGE_PCI_BAR0);
> if (pci_mapreg_map(pa, BGE_PCI_BAR0, memtype, 0, >bge_btag,
> -   >bge_bhandle, NULL, , 0)) {
> +   >bge_bhandle, NULL, >bge_bsize, 0)) {
> printf(": can't find mem space\n");
> return;
> }
> @@ -2827,7 +2827,8 @@ bge_attach(struct device *parent, struct
> if ((sc->bge_flags & BGE_APE) != 0) {
> memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 
> BGE_PCI_BAR2);
> if (pci_mapreg_map(pa, BGE_PCI_BAR2, memtype, 0,
> -   >bge_apetag, >bge_apehandle, NULL, , 0)) {
> +   >bge_apetag, >bge_apehandle, NULL,
> +   >bge_apesize, 0)) {
> printf(": couldn't map BAR2 memory\n");
> goto fail_1;
> }
> @@ -2944,14 +2945,15 @@ bge_attach(struct device *parent, struct
> sc->bge_dmatag = pa->pa_dmat;
> DPRINTFN(5, ("bus_dmamem_alloc\n"));
> if (bus_dmamem_alloc(sc->bge_dmatag, sizeof(struct bge_ring_data),
> -PAGE_SIZE, 0, , 1, , BUS_DMA_NOWAIT)) {
> +   PAGE_SIZE, 0, >bge_ring_seg, 1, >bge_ring_nseg,
> +   BUS_DMA_NOWAIT)) {
> printf(": can't alloc rx buffers\n");
> goto fail_2;
> }
> DPRINTFN(5, ("bus_dmamem_map\n"));
> -   if (bus_dmamem_map(sc->bge_dmatag, , rseg,
> -  sizeof(struct bge_ring_data), ,
> -  BUS_DMA_NOWAIT)) {
> +   if (bus_dmamem_map(sc->bge_dmatag, >bge_ring_seg,
> +   sc->bge_ring_nseg, sizeof(struct bge_ring_data), ,
> +   BUS_DMA_NOWAIT)) {
> printf(": can't map dma buffers (%lu bytes)\n",
> sizeof(struct bge_ring_data));
> goto fail_3;
> @@ -3134,18 +3136,54 @@ fail_5:
> bus_dmamap_destroy(sc->bge_dmatag, sc->bge_ring_map);
>
>  fail_4:
> -   bus_dmamem_unmap(sc->bge_dmatag, kva,
> +   bus_dmamem_unmap(sc->bge_dmatag, (caddr_t)sc->bge_rdata,
> sizeof(struct bge_ring_data));
>
>  fail_3:
> -   bus_dmamem_free(sc->bge_dmatag, , rseg);
> +   bus_dmamem_free(sc->bge_dmatag, >bge_ring_seg, sc->bge_ring_nseg);
>
>  fail_2:
> if ((sc->bge_flags & BGE_APE) != 0)
> -   bus_space_unmap(sc->bge_apetag, sc->bge_apehandle, apesize);
> +   bus_space_unmap(sc->bge_apetag, sc->bge_apehandle,
> +   sc->bge_apesize);
>
>  fail_1:
> -   bus_space_unmap(sc->bge_btag, sc->bge_bhandle, size);
> +   bus_space_unmap(sc->bge_btag, sc->bge_bhandle, 

Re: Make bge(4) detachable

2015-11-29 Thread Bryan Vyhmeister
On Sun, Nov 29, 2015 at 06:37:45PM +0100, Mark Kettenis wrote:
> Just in case I accidentally unplug he Thiunderbolt gadget...  The
> bge_stop() function gains a "softonly" argumentlike we introduced in
> other network drivers.

This along with the ppb.c patch allows the Apple Thunderbolt Ethernet
adapter to work just fine and I can now unplug it without a panic on my
MacBookAir7,2. Thanks!

Bryan



UTF-8 support for wc(1)

2015-11-29 Thread Ingo Schwarze
Hi,

our wc(1) utility currently violates POSIX in two ways:

 1. The -m option counts bytes instead of characters.
The patch given below fixes that.

 2. Word counting with -w only treats ASCII whitespace as word
boundaries and regards two words joined by non-ASCII whitespace
as one single word.

The second issue is not related to UTF-8, but a matter of full
Unicode support.  It would not be hard to fix that by using
mbtowc(3) and iswblank(3) instead of mblen(3).  However, i don't
think we want to pollute our base system tools with functions
requiring full Unicode support, not even to the extent available
in our own C library.  So i consider iswblank(3) taboo for now.

A few notes about the patch:

 * As usual, reduce the ridiculous setlocale(LC_CTYPE, "")
   to what is actually needed, setlocale(LC_ALL, "").

 * As usual, -m only differs from -c if LC_CTYPE is set
   to a multibyte encoding.

 * In the case  /* Do it the hard way... */,
   we need to switch from read(2) to getline(3)
   because read(2) might chop multibyte characters to pieces.
   That doesn't affect memory consumption of "wc -l" or "wc -c",
   not even for huge binary files without newline characters.
   It does increase memory consumption for files with very long
   lines when -w or -m is requested - but that's not a problem
   because both only make sense with real text, and real text
   does not have lines of a length that getline(3) is unable
   to handle.

OK?
  Ingo


Index: wc.1
===
RCS file: /cvs/src/usr.bin/wc/wc.1,v
retrieving revision 1.25
diff -u -p -r1.25 wc.1
--- wc.121 Apr 2015 10:46:48 -  1.25
+++ wc.129 Nov 2015 16:34:28 -
@@ -72,9 +72,7 @@ using powers of 2 for sizes (K=1024, M=1
 The number of lines in each input file
 is written to the standard output.
 .It Fl m
-Intended to count characters instead of bytes;
-currently an alias for
-.Fl c .
+Count characters instead of bytes.
 .It Fl w
 The number of words in each input file
 is written to the standard output.
@@ -111,7 +109,8 @@ The
 .Nm
 utility is compliant with the
 .St -p1003.1-2008
-specification, except that it ignores the locale.
+specification, except that it recognizes word boundaries only at ASCII
+whitespace.
 .Pp
 The flag
 .Op Fl h
@@ -121,7 +120,16 @@ A
 .Nm
 utility appeared in
 .At v1 .
-.Sh BUGS
+.Sh CAVEATS
 The
 .Fl m
-option counts bytes instead of characters.
+option depends on the character set
+.Xr locale 1 .
+If
+.Ev LC_CTYPE
+is set to
+.Qq C
+or
+.Qq POSIX ,
+it has the same effect as
+.Fl c .
Index: wc.c
===
RCS file: /cvs/src/usr.bin/wc/wc.c,v
retrieving revision 1.19
diff -u -p -r1.19 wc.c
--- wc.c9 Oct 2015 01:37:09 -   1.19
+++ wc.c29 Nov 2015 16:34:28 -
@@ -42,7 +42,7 @@
 #include 
 
 int64_ttlinect, twordct, tcharct;
-intdoline, doword, dochar, humanchar;
+intdoline, doword, dochar, humanchar, multibyte;
 intrval;
 extern char *__progname;
 
@@ -55,7 +55,7 @@ main(int argc, char *argv[])
 {
int ch;
 
-   setlocale(LC_ALL, "");
+   setlocale(LC_CTYPE, "");
 
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
@@ -68,8 +68,11 @@ main(int argc, char *argv[])
case 'w':
doword = 1;
break;
-   case 'c':
case 'm':
+   if (MB_CUR_MAX > 1)
+   multibyte = 1;
+   /* FALLTHROUGH */
+   case 'c':
dochar = 1;
break;
case 'h':
@@ -112,15 +115,19 @@ main(int argc, char *argv[])
 void
 cnt(char *file)
 {
+   static char *buf;
+   static ssize_t bufsz;
+
+   FILE *stream;
u_char *C;
short gotsp;
-   int len;
+   ssize_t len;
int64_t linect, wordct, charct;
struct stat sbuf;
int fd;
-   u_char buf[MAXBSIZE];
 
linect = wordct = charct = 0;
+   stream = NULL;
if (file) {
if ((fd = open(file, O_RDONLY, 0)) < 0) {
warn("%s", file);
@@ -131,7 +138,10 @@ cnt(char *file)
fd = STDIN_FILENO;
}
 
-   if (!doword) {
+   if (!doword && !multibyte) {
+   if (bufsz < MAXBSIZE &&
+   (buf = realloc(buf, MAXBSIZE)) == NULL)
+   err(1, NULL);
/*
 * Line counting is split out because it's a lot
 * faster to get lines than to get words, since
@@ -178,16 +188,25 @@ cnt(char *file)
}
}
} else {
+   if (file == NULL)
+   stream = stdin;
+   else if ((stream = fdopen(fd, "r")) == NULL) {
+   warn("%s", file);
+ 

pledge for cribbage(6)

2015-11-29 Thread Theo Buehler
Fairly straightforward pledge for cribbage:

It might fork-exec a pager ($PAGER or more(1)) in instructions()
afterwards only "stdio tty" is needed.

Index: cribbage/crib.c
===
RCS file: /var/cvs/src/games/cribbage/crib.c,v
retrieving revision 1.18
diff -u -p -r1.18 crib.c
--- cribbage/crib.c 12 Mar 2015 02:19:10 -  1.18
+++ cribbage/crib.c 29 Nov 2015 12:42:30 -
@@ -49,6 +49,9 @@ main(int argc, char *argv[])
bool playing;
int ch;
 
+   if(pledge("stdio rpath tty proc exec", NULL) == -1)
+   err(1, "pledge");
+
while ((ch = getopt(argc, argv, "emqr")) != -1)
switch (ch) {
case 'e':
@@ -92,6 +95,8 @@ main(int argc, char *argv[])
mvcur(0, COLS - 1, LINES - 1, 0);
fflush(stdout);
instructions();
+   if (pledge("stdio tty", NULL) == -1)
+   err(1, "pledge");
cbreak();
noecho();
clear();
Index: cribbage/instr.c
===
RCS file: /var/cvs/src/games/cribbage/instr.c,v
retrieving revision 1.11
diff -u -p -r1.11 instr.c
--- cribbage/instr.c27 Oct 2009 23:59:24 -  1.11
+++ cribbage/instr.c29 Nov 2015 12:13:21 -
@@ -64,7 +64,6 @@ instructions(void)
err(1, "vfork");
/* NOTREACHED */
case 0:
-   setgid(getgid());
if (!isatty(1))
pager = "/bin/cat";
else {



move scorefile + pledge for robots(6)

2015-11-29 Thread Theo Buehler
Robots has a nicely implemented score file, so I decided not to touch
it, except for moving to $HOME by default.  Needs pledge("getpw")
because of that.

Index: robots/main.c
===
RCS file: /cvs/src/games/robots/main.c,v
retrieving revision 1.20
diff -u -p -r1.20 main.c
--- robots/main.c   26 Aug 2015 00:29:24 -  1.20
+++ robots/main.c   29 Nov 2015 13:39:57 -
@@ -43,22 +43,31 @@ int
 main(int ac, char *av[])
 {
boolshow_only;
-   extern char *Scorefile;
+   extern char Scorefile[PATH_MAX];
int score_wfd; /* high score writable file descriptor */
int score_err = 0; /* hold errno from score file open */
int ch;
+   int ret;
extern int  optind;
-   gid_t   gid;
+   char*home;
 #ifdef FANCY
char*sp;
 #endif
 
-   if ((score_wfd = open(Scorefile, O_RDWR)) < 0)
-   score_err = errno;
+   if (pledge("stdio rpath wpath cpath getpw tty", NULL) == -1)
+   err(1, "pledge");
+
+   home = getenv("HOME");
+   if (home == NULL || *home == '\0')
+   err(1, "getenv");
 
-   /* revoke privs */
-   gid = getgid();
-   setresgid(gid, gid, gid);
+   ret = snprintf(Scorefile, sizeof(Scorefile), "%s/%s", home,
+   ".robots.scores");
+   if (ret < 0 || ret >= PATH_MAX)
+   errc(1, ENAMETOOLONG, "%s/%s", home, ".robots.scores");
+
+   if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
+   score_err = errno;
 
show_only = FALSE;
while ((ch = getopt(ac, av, "srajt")) != -1)
@@ -90,11 +99,11 @@ main(int ac, char *av[])
if (ac > 1)
usage();
if (ac == 1) {
-   Scorefile = av[0];
+   strlcpy(Scorefile, av[0], sizeof(Scorefile));
if (score_wfd >= 0)
close(score_wfd);
/* This file requires no special privileges. */
-   if ((score_wfd = open(Scorefile, O_RDWR)) < 0)
+   if ((score_wfd = open(Scorefile, O_RDWR | O_CREAT, 0666)) < 0)
score_err = errno;
 #ifdef FANCY
sp = strrchr(Scorefile, '/');
Index: robots/pathnames.h
===
RCS file: robots/pathnames.h
diff -N robots/pathnames.h
--- robots/pathnames.h  3 Jun 2003 03:01:41 -   1.3
+++ /dev/null   1 Jan 1970 00:00:00 -
@@ -1,35 +0,0 @@
-/* $OpenBSD: pathnames.h,v 1.3 2003/06/03 03:01:41 millert Exp $   */
-/* $NetBSD: pathnames.h,v 1.3 1995/04/22 10:09:01 cgd Exp $*/
-
-/*-
- * Copyright (c) 1990, 1993
- * The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *notice, this list of conditions and the following disclaimer in the
- *documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *may be used to endorse or promote products derived from this software
- *without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)pathnames.h 8.1 (Berkeley) 5/31/93
- */
-
-#define_PATH_SCORE "/var/games/robots_roll"
Index: robots/robots.6
===
RCS file: /cvs/src/games/robots/robots.6,v
retrieving revision 1.13
diff -u -p -r1.13 robots.6
--- robots/robots.6 8 Sep 2014 01:27:54 -   1.13
+++ robots/robots.6 29 Nov 2015 13:39:57 -
@@ -137,8 +137,8 @@ This is a little disconcerting until you
 very nice.
 .El
 .Sh FILES
-.Bl -tag -width /var/games/robots_roll -compact
-.It Pa /var/games/robots_roll
+.Bl -tag 

Prefer LOGNAME over USER in snake(6) and tetris(6)

2015-11-29 Thread Theo Buehler
Prefer $LOGNAME over $USER, since the latter is deprecated,
see environ(7).  Pointed out by millert@
Fall back to getlogin() before defaulting to "  ???".

Index: snake/snake.6
===
RCS file: /cvs/src/games/snake/snake.6,v
retrieving revision 1.12
diff -u -p -r1.12 snake.6
--- snake/snake.6   27 Nov 2015 09:37:56 -  1.12
+++ snake/snake.6   29 Nov 2015 13:39:21 -
@@ -116,7 +116,7 @@ To see who wastes time playing snake, ru
 .Fl s .
 .Sh ENVIRONMENT
 .Bl -tag -width Ds
-.It Ev USER
+.It Ev LOGNAME
 Name displayed in high score file.
 .El
 .Sh FILES
Index: snake/snake.c
===
RCS file: /cvs/src/games/snake/snake.c,v
retrieving revision 1.17
diff -u -p -r1.17 snake.c
--- snake/snake.c   27 Nov 2015 09:37:56 -  1.17
+++ snake/snake.c   29 Nov 2015 13:39:21 -
@@ -985,7 +985,7 @@ int
 readscores(int create)
 {
const char  *home;
-   const char  *user;
+   const char  *name;
const char  *modstr;
int  modint;
int  ret;
@@ -1019,13 +1019,17 @@ readscores(int create)
if (ferror(sf))
err(1, "error reading %s", scorepath);
 
-   user = getenv("USER");
-   if (user == NULL || *user == '\0')
-   user = "???";
+   name = getenv("LOGNAME");
+   if (name == NULL || *name == '\0')
+   name = getenv("USER");
+   if (name == NULL || *name == '\0')
+   name = getlogin();
+   if (name == NULL || *name == '\0')
+   name = "  ???";
 
if (nscores > TOPN)
nscores = TOPN;
-   strlcpy(scores[nscores].name, user, sizeof(scores[nscores].name));
+   strlcpy(scores[nscores].name, name, sizeof(scores[nscores].name));
scores[nscores].score = 0;
 
return 1;
Index: tetris/scores.c
===
RCS file: /cvs/src/games/tetris/scores.c,v
retrieving revision 1.15
diff -u -p -r1.15 scores.c
--- tetris/scores.c 26 Nov 2015 14:43:18 -  1.15
+++ tetris/scores.c 29 Nov 2015 13:39:21 -
@@ -217,10 +217,13 @@ thisuser(void)
 
if (u[0])
return (u);
-   p = getlogin();
-   if (p == NULL || *p == '\0') {
+   p = getenv("LOGNAME");
+   if (p == NULL || *p == '\0')
+   p = getenv("USER");
+   if (p == NULL || *p == '\0')
+   p = getlogin();
+   if (p == NULL || *p == '\0')
p = "  ???";
-   }
strlcpy(u, p, sizeof(u));
return (u);
 }
Index: tetris/tetris.6
===
RCS file: /cvs/src/games/tetris/tetris.6,v
retrieving revision 1.18
diff -u -p -r1.18 tetris.6
--- tetris/tetris.6 25 Nov 2015 16:15:06 -  1.18
+++ tetris/tetris.6 29 Nov 2015 13:39:21 -
@@ -145,6 +145,11 @@ name, score, and how many points were sc
 Scores which are the highest on a given level
 are marked with asterisks
 .Dq * .
+.Sh ENVIRONMENT
+.Bl -tag -width Ds
+.It Ev LOGNAME
+Name displayed in high score file.
+.El
 .Sh FILES
 .Bl -tag -width $HOME/.tetris.scores
 .It Pa $HOME/.tetris.scores



ping(8) / ping6(8) source address

2015-11-29 Thread Florian Obser
Inspired by the traceroute / traceroute6 merge.
At least reduces the diff in the option parser :)
OK?

diff --git ping/ping.c ping/ping.c
index f5ccaca..4944f77 100644
--- ping/ping.c
+++ ping/ping.c
@@ -110,7 +110,7 @@ int options;
 #defineF_SO_DEBUG  0x0040
 /* 0x0080 */
 #defineF_VERBOSE   0x0100
-#defineF_SADDR 0x0200
+/* 0x0200 */
 #defineF_HDRINCL   0x0400
 #defineF_TTL   0x0800
 #defineF_AUD_RECV  0x2000
@@ -132,7 +132,6 @@ int mx_dup_ck = MAX_DUP_CHK;
 char rcvd_tbl[MAX_DUP_CHK / 8];
 
 struct sockaddr_in whereto;/* who to ping */
-struct sockaddr_in whence; /* Which interface we come from */
 unsigned int datalen = DEFDATALEN;
 int s; /* socket file descriptor */
 u_char outpackhdr[IP_MAXPACKET]; /* Max packet size = 65535 */
@@ -186,11 +185,13 @@ int
 main(int argc, char *argv[])
 {
struct hostent *hp;
+   struct addrinfo hints, *res;
+   struct sockaddr_in  from4;
struct sockaddr_in *to;
-   struct in_addr saddr;
int ch, i, optval = 1, packlen, preload, maxsize, df = 0, tos = 0;
+   int error;
u_char *datap, *packet, ttl = MAXTTL, loop = 1;
-   char *target, hnamebuf[HOST_NAME_MAX+1];
+   char *target, hnamebuf[HOST_NAME_MAX+1], *source = NULL;
char rspace[3 + 4 * NROUTES + 1];   /* record route space */
socklen_t maxsizelen;
const char *errstr;
@@ -238,13 +239,7 @@ main(int argc, char *argv[])
break;
case 'I':
case 'S':   /* deprecated */
-   if (inet_aton(optarg, ) == 0) {
-   if ((hp = gethostbyname(optarg)) == NULL)
-   errx(1, "bad interface address: %s",
-   optarg);
-   memcpy(, hp->h_addr, sizeof(saddr));
-   }
-   options |= F_SADDR;
+   source = optarg;
break;
case 'i':   /* wait between sending packets */
interval = strtod(optarg, NULL);
@@ -360,16 +355,27 @@ main(int argc, char *argv[])
hostname = hnamebuf;
}
 
-   if (options & F_SADDR) {
+   if (source) {
if (IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
moptions |= MULTICAST_IF;
else {
-   memset(, 0, sizeof(whence));
-   whence.sin_len = sizeof(whence);
-   whence.sin_family = AF_INET;
-   memcpy(_addr.s_addr, , sizeof(saddr));
-   if (bind(s, (struct sockaddr *),
-   sizeof(whence)) < 0)
+   memset(, 0, sizeof(from4));
+   from4.sin_family = AF_INET;
+   if (inet_aton(source, _addr) == 0) {
+   memset(, 0, sizeof(hints));
+   hints.ai_family = AF_INET;
+   hints.ai_socktype = SOCK_DGRAM; /*dummy*/
+   if ((error = getaddrinfo(source, NULL, ,
+   )))
+   errx(1, "%s: %s", source,
+   gai_strerror(error));
+   if (res->ai_addrlen != sizeof(from4))
+   errx(1, "size of sockaddr mismatch");
+   memcpy(, res->ai_addr, res->ai_addrlen);
+   freeaddrinfo(res);
+   }
+   if (bind(s, (struct sockaddr *), sizeof(from4))
+   < 0)
err(1, "bind");
}
}
@@ -426,8 +432,8 @@ main(int argc, char *argv[])
ip->ip_off = htons(df ? IP_DF : 0);
ip->ip_ttl = ttl;
ip->ip_p = IPPROTO_ICMP;
-   if (options & F_SADDR)
-   ip->ip_src = saddr;
+   if (source)
+   ip->ip_src = from4.sin_addr;
else
ip->ip_src.s_addr = INADDR_ANY;
ip->ip_dst = to->sin_addr;
@@ -457,8 +463,8 @@ main(int argc, char *argv[])
sizeof(ttl)) < 0)
err(1, "setsockopt IP_MULTICAST_TTL");
if ((moptions & MULTICAST_IF) &&
-   setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, ,
-   sizeof(saddr)) < 0)
+   setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, _addr,
+   sizeof(from4.sin_addr)) < 0)
err(1, "setsockopt IP_MULTICAST_IF");
 
/*
diff --git ping6/ping6.c ping6/ping6.c
index bedd559..9bc696f 100644
--- ping6/ping6.c
+++ ping6/ping6.c
@@ -137,7 

Re: pledge for cribbage(6)

2015-11-29 Thread Theo Buehler
On Sun, Nov 29, 2015 at 03:00:13PM +0100, Theo Buehler wrote:
> Fairly straightforward pledge for cribbage:
> 
> It might fork-exec a pager ($PAGER or more(1)) in instructions()
> afterwards only "stdio tty" is needed.

I sent a slightly older version of the patch...  Here's what I actually
wanted to do: move the second pledge call a bit down, but call it in
all code paths.

Note: I'll stop the spam for now, but I have updates for battlestar(6)
and atc(6) in the pipeline.

Index: games/cribbage/crib.c
===
RCS file: /cvs/src/games/cribbage/crib.c,v
retrieving revision 1.18
diff -u -p -r1.18 crib.c
--- games/cribbage/crib.c   12 Mar 2015 02:19:10 -  1.18
+++ games/cribbage/crib.c   29 Nov 2015 14:13:25 -
@@ -49,6 +49,9 @@ main(int argc, char *argv[])
bool playing;
int ch;
 
+   if(pledge("stdio rpath tty proc exec", NULL) == -1)
+   err(1, "pledge");
+
while ((ch = getopt(argc, argv, "emqr")) != -1)
switch (ch) {
case 'e':
@@ -99,6 +102,10 @@ main(int argc, char *argv[])
msg("For cribbage rules, use \"man cribbage\"");
}
}
+
+   if (pledge("stdio tty", NULL) == -1)
+   err(1, "pledge");
+
playing = TRUE;
do {
wclrtobot(Msgwin);
Index: games/cribbage/instr.c
===
RCS file: /cvs/src/games/cribbage/instr.c,v
retrieving revision 1.11
diff -u -p -r1.11 instr.c
--- games/cribbage/instr.c  27 Oct 2009 23:59:24 -  1.11
+++ games/cribbage/instr.c  29 Nov 2015 14:13:25 -
@@ -64,7 +64,6 @@ instructions(void)
err(1, "vfork");
/* NOTREACHED */
case 0:
-   setgid(getgid());
if (!isatty(1))
pager = "/bin/cat";
else {



Re: ppb diff for testing

2015-11-29 Thread Ossi Herrala
On Sat, Nov 28, 2015 at 02:02:57PM +0100, Mark Kettenis wrote:

> The diff below teaches ppb(4) how to configure PCI bridges that are
> left unconfigured by firmware.  This happens for example on Apple
> hardware, where devices connected through Thunderbolt are left
> unconfigured.  And since Thunderbolt is basically a bunch of PCI
> bridges (or more correctly PCIe switches), this includes a couple of
> ppb(4) devices.
> 
> With this diff, an Apple Thunderbolt Gigabit Ethernet adapter that is
> plugged in when my MacBookPro12,1 is booted starts working:
> 
> bge0 at pci9 dev 0 function 0 "Broadcom BCM57762" rev 0x00, unknown BCM57766 
> (0x57766000): msi, address 98:5a:eb:c7:30:e4
> brgphy0 at bge0 phy 1: BCM57765 10/100/1000baseT PHY, rev. 0
> 

I have MacBookPro9,2 (mid-2012) and no problems with your ppb(4)
patch. I have the machine running via Thunderbolt ethernet.

However, dmesg has

  10:0:0: mem address conflict 0x/0x1

that is not there without Thunderbolt ethernet adapter.

dmesg and pcidump after the patch and with Thunderbolt Ethernet
connected below.


OpenBSD 5.8-current (GENERIC.MP) #0: Sat Nov 28 20:51:44 UTC 2015
r...@cn0022.my.domain:/usr/src/sys/arch/amd64/compile/GENERIC.MP
RTC BIOS diagnostic error 
7f
real mem = 8473620480 (8081MB)
avail mem = 8212688896 (7832MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.4 @ 0x8ad14000 (63 entries)
bios0: vendor Apple Inc. version "MBP91.88Z.00D3.B0B.1506081214" date 06/08/2015
bios0: Apple Inc. MacBookPro9,2
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP HPET APIC SBST ECDT SSDT SSDT SSDT SSDT SSDT SSDT SSDT 
SSDT SSDT SSDT SSDT DMAR MCFG
acpi0: wakeup devices P0P2(S3) PEG1(S3) EC__(S4) GMUX(S3) HDEF(S3) RP01(S3) 
GIGE(S3) SDXC(S3) RP02(S3) ARPT(S3) RP03(S3) EHC1(S3) EHC2(S3) XHC1(S3) 
ADP1(S4) LID0(S4)
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpihpet0 at acpi0: 14318179 Hz
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz, 2494.72 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,SENSOR,ARAT
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu0: mwait min=64, max=64, C-substates=0.2.1.1.2, IBE
cpu1 at mainbus0: apid 2 (application processor)
cpu1: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz, 2494.33 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,SENSOR,ARAT
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 0, core 1, package 0
cpu2 at mainbus0: apid 1 (application processor)
cpu2: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz, 2494.33 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,SENSOR,ARAT
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 1, core 0, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz, 2494.33 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,SENSOR,ARAT
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 1, core 1, package 0
ioapic0 at mainbus0: apid 2 pa 0xfec0, version 20, 24 pins
ioapic0: misconfigured as apic 0, remapped to apid 2
acpiec0 at acpi0
acpimcfg0 at acpi0 addr 0xe000, bus 0-154
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus 4 (P0P2)
acpiprt2 at acpi0: bus -1 (PEG1)
acpiprt3 at acpi0: bus 1 (RP01)
acpiprt4 at acpi0: bus 2 (RP02)
acpiprt5 at acpi0: bus 3 (RP03)
acpicpu0 at acpi0: C3(200@198 mwait.1@0x30), C2(500@148 mwait.1@0x10), 
C1(1000@1 mwait.1), PSS
acpicpu1 at acpi0: C3(200@198 mwait.1@0x30), C2(500@148 mwait.1@0x10), 
C1(1000@1 mwait.1), PSS
acpicpu2 at acpi0: C3(200@198 mwait.1@0x30), C2(500@148 mwait.1@0x10), 
C1(1000@1 mwait.1), PSS
acpicpu3 at acpi0: C3(200@198 mwait.1@0x30), C2(500@148 mwait.1@0x10), 
C1(1000@1 mwait.1), PSS
acpibat0 at acpi0: BAT0 model "3545797981023400290" type 

Re: Slow wsmouse down in console

2015-11-29 Thread Ulf Brosziewski

This looks nice. Maybe someone else who is more familiar with wsdisplay
could also have a look at it?

I made a test with a USB mouse and an ALPS Glidepoint, which worked
well.  Without the patch wsmoused is completely useless for that
touchpad. Out of curiosity, I made another test with an Elantech-v4
clickpad, and the cursor control also worked well (of course, wsmoused
remains useless for clickpads because you cannot paste).

Both tracking the remainders when scaling and taking the size of the
cells into account looks reasonable. There might still be no garantuee
that this works everywhere - pms, for example, applies somewhat
arbitrary scale factors in compat mode, there is no stable correlation
to the touchpad resolutions. But if this works in standard environments,
it is certainly more useful than the current version.

Because I saw no complaints up to now, I have been thinking that hardly
anyone uses touchpads at the console. Am I wrong here?


On 11/27/2015 12:35 AM, Thierry Deval wrote:

Hi,

I hope I'm not the only one being annoyed by the fast movement of wsmouse when
in text mode, and this one can be useful.

Here is a patch that slows down the cursor by reducing the mouse coordinates
proportionally to the console font size.
I confess I have only tried it on this laptop with its Synaptics touchpad and
some other USB mouses.

Anyone else interested ?

Thierry

Index: sys/dev/wscons/wsdisplay.c
===
RCS file: /cvs/src/sys/dev/wscons/wsdisplay.c,v
retrieving revision 1.124
diff -u -p -r1.124 wsdisplay.c
--- dev/wscons/wsdisplay.c  8 Sep 2015 11:13:20 -   1.124
+++ dev/wscons/wsdisplay.c  26 Nov 2015 20:28:48 -
@@ -2463,27 +2463,39 @@ ctrl_event(struct wsdisplay_softc *sc, u
  void
  mouse_moverel(struct wsscreen *scr, int dx, int dy)
  {
+   static int acc_dx, acc_dy;
struct wsscreen_internal *dconf = scr->scr_dconf;
+   const struct wsscreen_descr *descr = dconf->scrdata;
u_int old_mouse = scr->mouse;
int mouse_col = scr->mouse % N_COLS(dconf);
int mouse_row = scr->mouse / N_COLS(dconf);
+   int norm_dx, norm_dy;
+
+   /* accumulate movement and calculate character equivalent */
+   acc_dx += dx;
+   if ((norm_dx = acc_dx / descr->fontwidth )) acc_dx -= descr->fontwidth  
* norm_dx;
+   acc_dy += dy;
+   if ((norm_dy = acc_dy / descr->fontheight)) acc_dy -= descr->fontheight 
* norm_dy;
+
+   /* bail out if mouse hasn't virtually moved */
+   if (norm_dx == 0 && norm_dy == 0) return;

/* update position */
-   if (mouse_col + dx >= MAXCOL(dconf))
+   if (mouse_col + norm_dx >= MAXCOL(dconf))
mouse_col = MAXCOL(dconf);
else {
-   if (mouse_col + dx <= 0)
+   if (mouse_col + norm_dx <= 0)
mouse_col = 0;
else
-   mouse_col += dx;
+   mouse_col += norm_dx;
}
-   if (mouse_row + dy >= MAXROW(dconf))
+   if (mouse_row + norm_dy >= MAXROW(dconf))
mouse_row = MAXROW(dconf);
else {
-   if (mouse_row + dy <= 0)
+   if (mouse_row + norm_dy <= 0)
mouse_row = 0;
else
-   mouse_row += dy;
+   mouse_row += norm_dy;
}
scr->mouse = mouse_row * N_COLS(dconf) + mouse_col;







Re: taskq_barrier(9)

2015-11-29 Thread Mark Kettenis
> Date: Mon, 30 Nov 2015 16:28:55 +1000
> From: David Gwynne 
> 
> while playing with some options around running an interfaces send
> queue, i came up with the following. if you've deferred work to a
> taskq and you're not reference counting, this can help you guarantee
> that any work before a point has completed before proceeding.
> 
> think of it like intr_barrier.
> 
> i dont currently have a need for this right now, so there's no real
> pressure to put this in. it's more a backup.
> 
> if someone oks it i will happily commit it though.

Never been a big fan of adding interfaces before we have an actual use
case.  I might have one though in inteldrm(4).  But I'll need to look
a little bit deeper into that.

One problem with the interface is that it doesn't really work for a
taskq that has multiple worker threads isn't it?

> Index: share/man/man9/task_add.9
> ===
> RCS file: /cvs/src/share/man/man9/task_add.9,v
> retrieving revision 1.16
> diff -u -p -r1.16 task_add.9
> --- share/man/man9/task_add.9 14 Sep 2015 15:14:55 -  1.16
> +++ share/man/man9/task_add.9 30 Nov 2015 06:24:29 -
> @@ -20,6 +20,7 @@
>  .Sh NAME
>  .Nm taskq_create ,
>  .Nm taskq_destroy ,
> +.Nm taskq_barrier ,
>  .Nm task_set ,
>  .Nm task_add ,
>  .Nm task_del ,
> @@ -37,6 +38,8 @@
>  .Ft void
>  .Fn taskq_destroy "struct taskq *tq"
>  .Ft void
> +.Fn taskq_barrier "struct taskq *tq"
> +.Ft void
>  .Fn task_set "struct task *t" "void (*fn)(void *)" "void *arg"
>  .Ft int
>  .Fn task_add "struct taskq *tq" "struct task *t"
> @@ -88,6 +91,15 @@ Calling
>  against the system taskq is an error and will lead to undefined
>  behaviour or a system fault.
>  .Pp
> +.Fn taskq_barrier
> +guarantees that any task that was running on the
> +.Fa tq
> +taskq when the barrier was called has finished by the time the barrier
> +returns.
> +.Fn taskq_barrier
> +is only supported on taskqs serviced by 1 thread,
> +and may not be called by a task running in the specified taskq.
> +.Pp
>  It is the responsibility of the caller to provide the
>  .Fn task_set ,
>  .Fn task_add ,
> @@ -163,6 +175,8 @@ argument given in
>  and
>  .Fn taskq_destroy
>  can be called during autoconf, or from process context.
> +.Fn taskq_barrier
> +can be called from process context.
>  .Fn task_set ,
>  .Fn task_add ,
>  and
> Index: share/man/man9/Makefile
> ===
> RCS file: /cvs/src/share/man/man9/Makefile,v
> retrieving revision 1.262
> diff -u -p -r1.262 Makefile
> --- share/man/man9/Makefile   25 Nov 2015 03:09:57 -  1.262
> +++ share/man/man9/Makefile   30 Nov 2015 06:24:29 -
> @@ -397,6 +397,7 @@ MLINKS+=systrace.9 systrace_redirect.9 \
>   systrace.9 systrace_fork.9 systrace.9 systrace_exit.9
>  MLINKS+=task_add.9 taskq_create.9 \
>   task_add.9 taskq_destroy.9 \
> + task_add.9 taskq_barrier.9 \
>   task_add.9 task_set.9 \
>   task_add.9 task_del.9 \
>   task_add.9 TASK_INITIALIZER.9
> Index: sys/sys/task.h
> ===
> RCS file: /cvs/src/sys/sys/task.h,v
> retrieving revision 1.8
> diff -u -p -r1.8 task.h
> --- sys/sys/task.h9 Feb 2015 03:15:41 -   1.8
> +++ sys/sys/task.h30 Nov 2015 06:24:29 -
> @@ -39,6 +39,7 @@ extern struct taskq *const systqmp;
>  
>  struct taskq *taskq_create(const char *, unsigned int, int, unsigned int);
>  void  taskq_destroy(struct taskq *);
> +void  taskq_barrier(struct taskq *);
>  
>  void  task_set(struct task *, void (*)(void *), void *);
>  int   task_add(struct taskq *, struct task *);
> Index: sys/kern/kern_task.c
> ===
> RCS file: /cvs/src/sys/kern/kern_task.c,v
> retrieving revision 1.15
> diff -u -p -r1.15 kern_task.c
> --- sys/kern/kern_task.c  19 Nov 2015 13:19:24 -  1.15
> +++ sys/kern/kern_task.c  30 Nov 2015 06:24:29 -
> @@ -22,6 +22,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  
>  #define TASK_ONQUEUE 1
>  
> @@ -68,6 +69,7 @@ struct taskq *const systqmp = _sys
>  
>  void taskq_init(void); /* called in init_main.c */
>  void taskq_create_thread(void *);
> +void taskq_barrier_task(void *);
>  int  taskq_sleep(const volatile void *, struct mutex *, int,
>   const char *, int);
>  int  taskq_next_work(struct taskq *, struct task *, sleepfn);
> @@ -176,6 +178,30 @@ taskq_create_thread(void *arg)
>   } while (tq->tq_running < tq->tq_nthreads);
>  
>   mtx_leave(>tq_mtx);
> +}
> +
> +void
> +taskq_barrier(struct taskq *tq)
> +{
> + struct sleep_state sls;
> + unsigned int notdone = 1;
> + struct task t = TASK_INITIALIZER(taskq_barrier_task, );
> +
> + task_add(tq, );
> +
> + while (notdone) {
> + sleep_setup(, , PWAIT, "tqbar");
> + sleep_finish(, notdone);
> + }
> 

what approach for TRNG?

2015-11-29 Thread Devin Reade

A while ago a posted a dmesg for a TRNG USB device (the MoonBase Otago
OneRNG) per
.

I'm looking at adding support for this device but haven't splunked too
much into the OpenBSD kernel before, and I'm looking for advice.

The OneRNG was manufactured to present itself as a modem and in the Linux
sample source code the (user space) process to first initialize the device
by echoing some commands to the device and then cat its output to rngd.

For the OpenBSD side, I found /usr/src/sys/dev/usb/ualea.c which is
another USB-based TRNG.  The model there would be to usbd_transfer(9)
from the device and then forward it via add_true_randomness(9).

However, because the OneRNG was manufactured to look like a modem,
the other possibility is to have it recognised in
/usr/src/sys/dev/usb/umodem.c.  If that's the case, though, it's
unclear to me what the mechanism should be to pull the data from the
OneRNG and give it to the kernel; presumably we don't want any
userspace code here.

Am I correct in assuming that the ualea mechanism is probabably the
right way to go?

Devin



taskq_barrier(9)

2015-11-29 Thread David Gwynne
while playing with some options around running an interfaces send
queue, i came up with the following. if you've deferred work to a
taskq and you're not reference counting, this can help you guarantee
that any work before a point has completed before proceeding.

think of it like intr_barrier.

i dont currently have a need for this right now, so there's no real
pressure to put this in. it's more a backup.

if someone oks it i will happily commit it though.

Index: share/man/man9/task_add.9
===
RCS file: /cvs/src/share/man/man9/task_add.9,v
retrieving revision 1.16
diff -u -p -r1.16 task_add.9
--- share/man/man9/task_add.9   14 Sep 2015 15:14:55 -  1.16
+++ share/man/man9/task_add.9   30 Nov 2015 06:24:29 -
@@ -20,6 +20,7 @@
 .Sh NAME
 .Nm taskq_create ,
 .Nm taskq_destroy ,
+.Nm taskq_barrier ,
 .Nm task_set ,
 .Nm task_add ,
 .Nm task_del ,
@@ -37,6 +38,8 @@
 .Ft void
 .Fn taskq_destroy "struct taskq *tq"
 .Ft void
+.Fn taskq_barrier "struct taskq *tq"
+.Ft void
 .Fn task_set "struct task *t" "void (*fn)(void *)" "void *arg"
 .Ft int
 .Fn task_add "struct taskq *tq" "struct task *t"
@@ -88,6 +91,15 @@ Calling
 against the system taskq is an error and will lead to undefined
 behaviour or a system fault.
 .Pp
+.Fn taskq_barrier
+guarantees that any task that was running on the
+.Fa tq
+taskq when the barrier was called has finished by the time the barrier
+returns.
+.Fn taskq_barrier
+is only supported on taskqs serviced by 1 thread,
+and may not be called by a task running in the specified taskq.
+.Pp
 It is the responsibility of the caller to provide the
 .Fn task_set ,
 .Fn task_add ,
@@ -163,6 +175,8 @@ argument given in
 and
 .Fn taskq_destroy
 can be called during autoconf, or from process context.
+.Fn taskq_barrier
+can be called from process context.
 .Fn task_set ,
 .Fn task_add ,
 and
Index: share/man/man9/Makefile
===
RCS file: /cvs/src/share/man/man9/Makefile,v
retrieving revision 1.262
diff -u -p -r1.262 Makefile
--- share/man/man9/Makefile 25 Nov 2015 03:09:57 -  1.262
+++ share/man/man9/Makefile 30 Nov 2015 06:24:29 -
@@ -397,6 +397,7 @@ MLINKS+=systrace.9 systrace_redirect.9 \
systrace.9 systrace_fork.9 systrace.9 systrace_exit.9
 MLINKS+=task_add.9 taskq_create.9 \
task_add.9 taskq_destroy.9 \
+   task_add.9 taskq_barrier.9 \
task_add.9 task_set.9 \
task_add.9 task_del.9 \
task_add.9 TASK_INITIALIZER.9
Index: sys/sys/task.h
===
RCS file: /cvs/src/sys/sys/task.h,v
retrieving revision 1.8
diff -u -p -r1.8 task.h
--- sys/sys/task.h  9 Feb 2015 03:15:41 -   1.8
+++ sys/sys/task.h  30 Nov 2015 06:24:29 -
@@ -39,6 +39,7 @@ extern struct taskq *const systqmp;
 
 struct taskq   *taskq_create(const char *, unsigned int, int, unsigned int);
 voidtaskq_destroy(struct taskq *);
+voidtaskq_barrier(struct taskq *);
 
 voidtask_set(struct task *, void (*)(void *), void *);
 int task_add(struct taskq *, struct task *);
Index: sys/kern/kern_task.c
===
RCS file: /cvs/src/sys/kern/kern_task.c,v
retrieving revision 1.15
diff -u -p -r1.15 kern_task.c
--- sys/kern/kern_task.c19 Nov 2015 13:19:24 -  1.15
+++ sys/kern/kern_task.c30 Nov 2015 06:24:29 -
@@ -22,6 +22,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #define TASK_ONQUEUE   1
 
@@ -68,6 +69,7 @@ struct taskq *const systqmp = _sys
 
 void   taskq_init(void); /* called in init_main.c */
 void   taskq_create_thread(void *);
+void   taskq_barrier_task(void *);
 inttaskq_sleep(const volatile void *, struct mutex *, int,
const char *, int);
 inttaskq_next_work(struct taskq *, struct task *, sleepfn);
@@ -176,6 +178,30 @@ taskq_create_thread(void *arg)
} while (tq->tq_running < tq->tq_nthreads);
 
mtx_leave(>tq_mtx);
+}
+
+void
+taskq_barrier(struct taskq *tq)
+{
+   struct sleep_state sls;
+   unsigned int notdone = 1;
+   struct task t = TASK_INITIALIZER(taskq_barrier_task, );
+
+   task_add(tq, );
+
+   while (notdone) {
+   sleep_setup(, , PWAIT, "tqbar");
+   sleep_finish(, notdone);
+   }
+}
+
+void
+taskq_barrier_task(void *p)
+{
+   unsigned int *notdone = p;
+
+   *notdone = 0;
+   wakeup_one(notdone);
 }
 
 void



Re: use ping6(8)'s engine in ping(8)

2015-11-29 Thread Sebastian Benoit
ok

Florian Obser(flor...@openbsd.org) on 2015.11.29 15:57:34 +:
> This shoves a round peg into a square hole with considerable force...
> I was only concerned with moving the functionality over from ping6,
> further cleanup will happen on top of this.
> 
> OK?
> 
> diff --git ping.c ping.c
> index 4944f77..a3a6fe3 100644
> --- ping.c
> +++ ping.c
> @@ -147,8 +147,7 @@ unsigned long nreceived;  /* # of packets we got back */
>  unsigned long nrepeats;  /* number of duplicates */
>  unsigned long ntransmitted;  /* sequence # for outbound packets = #sent */
>  unsigned long nmissedmax = 1;/* max value of ntransmitted - 
> nreceived - 1 */
> -double interval = 1; /* interval between packets */
> -struct itimerval interstr;   /* interval structure for use with setitimer */
> +struct timeval interval = {1, 0}; /* interval between packets */
>  
>  /* timing */
>  int timing;  /* flag to do timing */
> @@ -163,17 +162,21 @@ int bufspace = IP_MAXPACKET;
>  struct tv64 tv64_offset;
>  SIPHASH_KEY mac_key;
>  
> +volatile sig_atomic_t seenalrm;
> +volatile sig_atomic_t seenint;
> +volatile sig_atomic_t seeninfo;
> +
>  void fill(char *, char *);
> -void catcher(int signo);
> -void prtsig(int signo);
> -__dead void finish(int signo);
> -void summary(int, int);
> +void summary(int);
>  int in_cksum(u_short *, int);
> -void pinger(void);
> +void onsignal(int);
> +void retransmit(void);
> +void onint(int);
> +int pinger(void);
>  char *pr_addr(in_addr_t);
>  int check_icmph(struct ip *);
>  void pr_icmph(struct icmp *);
> -void pr_pack(char *, int, struct sockaddr_in *);
> +void pr_pack(char *, int, struct msghdr *);
>  void pr_retip(struct ip *);
>  void pr_iph(struct ip *);
>  #ifndef SMALL
> @@ -186,15 +189,17 @@ main(int argc, char *argv[])
>  {
>   struct hostent *hp;
>   struct addrinfo hints, *res;
> - struct sockaddr_in  from4;
> + struct itimerval itimer;
> + struct sockaddr_in  from, from4;
>   struct sockaddr_in *to;
>   int ch, i, optval = 1, packlen, preload, maxsize, df = 0, tos = 0;
>   int error;
>   u_char *datap, *packet, ttl = MAXTTL, loop = 1;
> - char *target, hnamebuf[HOST_NAME_MAX+1], *source = NULL;
> + char *e, *target, hnamebuf[HOST_NAME_MAX+1], *source = NULL;
>   char rspace[3 + 4 * NROUTES + 1];   /* record route space */
>   socklen_t maxsizelen;
>   const char *errstr;
> + double intval;
>   uid_t uid;
>   u_int rtableid = 0;
>  
> @@ -242,19 +247,23 @@ main(int argc, char *argv[])
>   source = optarg;
>   break;
>   case 'i':   /* wait between sending packets */
> - interval = strtod(optarg, NULL);
> -
> - if (interval <= 0 || interval >= INT_MAX)
> - errx(1, "bad timing interval: %s", optarg);
> -
> - if (interval < 1)
> - if (getuid())
> - errx(1, "%s: only root may use interval 
> < 1s",
> - strerror(EPERM));
> -
> - if (interval < 0.01)
> - interval = 0.01;
> -
> + intval = strtod(optarg, );
> + if (*optarg == '\0' || *e != '\0')
> + errx(1, "illegal timing interval %s", optarg);
> + if (intval < 1 && getuid()) {
> + errx(1, "%s: only root may use interval < 1s",
> + strerror(EPERM));
> + }
> + interval.tv_sec = (time_t)intval;
> + interval.tv_usec =
> + (long)((intval - interval.tv_sec) * 100);
> + if (interval.tv_sec < 0)
> + errx(1, "illegal timing interval %s", optarg);
> + /* less than 1/Hz does not make sense */
> + if (interval.tv_sec == 0 && interval.tv_usec < 1) {
> + warnx("too small interval, raised to 0.01");
> + interval.tv_usec = 1;
> + }
>   options |= F_INTERVAL;
>   break;
>   case 'L':
> @@ -387,12 +396,6 @@ main(int argc, char *argv[])
>   arc4random_buf(_offset, sizeof(tv64_offset));
>   arc4random_buf(_key, sizeof(mac_key));
>  
> - memset(, 0, sizeof(interstr));
> -
> - interstr.it_value.tv_sec = interval;
> - interstr.it_value.tv_usec =
> - (long) ((interval - interstr.it_value.tv_sec) * 100);
> -
>   if (options & F_FLOOD && options & F_INTERVAL)
>   errx(1, "-f and -i options are incompatible");
>  
> @@ -508,26 +511,54 @@ main(int argc, char *argv[])
>   err(1, "pledge");
>   }
>  
> - (void)signal(SIGINT,