sparc64: delete old "traptrace" bits

2016-10-16 Thread Philip Guenther

In 2003(!) a pile of code was deleted from sparc64's locore.s:

revision 1.35
date: 2003/05/17 07:09:08;  author: art;  state: Exp;  lines: +1 -957;
Get rid of lots of hairy ifdefs that we'll most likely never use.
TRAPTRACE, TRAPSTATS, FLTTRACE and SCHED_DEBUG.

mdw@ henric@ ok.


That was the code that actually put data into the trap_trace[] array, 
rendering the remaining trap_trace* code useless.  How about we delete 
those bits?

This built and the box booted with the resulting kernel.

ok?

Philip Guenther

Index: sparc64/autoconf.c
===
RCS file: /cvs/src/sys/arch/sparc64/sparc64/autoconf.c,v
retrieving revision 1.126
diff -u -p -r1.126 autoconf.c
--- sparc64/autoconf.c  8 Jun 2016 17:24:44 -   1.126
+++ sparc64/autoconf.c  17 Oct 2016 06:25:36 -
@@ -586,11 +586,6 @@ bootpath_build(void)
 #else
printf("kernel has no debugger\n");
 #endif
-   } else if (*cp == 't') {
-   /* turn on traptrace w/o breaking into kdb */
-   extern int trap_trace_dis;
-
-   trap_trace_dis = 0;
}
}
 }
Index: sparc64/db_interface.c
===
RCS file: /cvs/src/sys/arch/sparc64/sparc64/db_interface.c,v
retrieving revision 1.45
diff -u -p -r1.45 db_interface.c
--- sparc64/db_interface.c  8 Oct 2016 05:49:09 -   1.45
+++ sparc64/db_interface.c  17 Oct 2016 06:25:37 -
@@ -73,17 +73,6 @@ db_regs_tddb_regs;   /* register state */
 
 extern void OF_enter(void);
 
-extern struct traptrace {
-   unsigned short tl:3,/* Trap level */
-   ns:4,   /* PCB nsaved */
-   tt:9;   /* Trap type */
-   unsigned short pid; /* PID */
-   u_int tstate;   /* tstate */
-   u_int tsp;  /* sp */
-   u_int tpc;  /* pc */
-   u_int tfault;   /* MMU tag access */
-} trap_trace[], trap_trace_end[];
-
 static long nil;
 
 static int
@@ -231,14 +220,12 @@ void db_pmap_kernel(db_expr_t, int, db_e
 void db_pload_cmd(db_expr_t, int, db_expr_t, char *);
 void db_pmap_cmd(db_expr_t, int, db_expr_t, char *);
 void db_lock(db_expr_t, int, db_expr_t, char *);
-void db_traptrace(db_expr_t, int, db_expr_t, char *);
 void db_dump_buf(db_expr_t, int, db_expr_t, char *);
 void db_dump_espcmd(db_expr_t, int, db_expr_t, char *);
 void db_watch(db_expr_t, int, db_expr_t, char *);
 void db_xir(db_expr_t, int, db_expr_t, char *);
 
 static void db_dump_pmap(struct pmap*);
-static void db_print_trace_entry(struct traptrace *, int);
 
 #ifdef MULTIPROCESSOR
 void db_cpuinfo_cmd(db_expr_t, int, db_expr_t, char *);
@@ -272,9 +259,6 @@ db_ktrap(type, tf)
struct trapstate *ts = &ddb_regs.ddb_ts[0];
extern int savetstate(struct trapstate *ts);
extern void restoretstate(int tl, struct trapstate *ts);
-   extern int trap_trace_dis;
-
-   trap_trace_dis++;
 
 #if NTDA > 0
tda_full_blast();
@@ -336,7 +320,6 @@ db_ktrap(type, tf)
*(struct frame *)tf->tf_out[6] = ddb_regs.ddb_fr;
 #endif
*tf = ddb_regs.ddb_tf;
-   trap_trace_dis--;
 
 #ifdef MULTIPROCESSOR
if (!db_switch_cpu)
@@ -1098,78 +1081,6 @@ db_setpcb(addr, have_addr, count, modif)
db_printf("PID %ld not found.\n", addr);
 }
 
-static void
-db_print_trace_entry(te, i)
-   struct traptrace *te;
-   int i;
-{
-   db_printf("%d:%d p:%d tt:%d:%llx:%llx %llx:%llx ", i,
- (int)te->tl, (int)te->pid,
- (int)te->tt, (unsigned long long)te->tstate,
- (unsigned long long)te->tfault, (unsigned long long)te->tsp,
- (unsigned long long)te->tpc);
-   db_printsym((u_long)te->tpc, DB_STGY_PROC, db_printf);
-   db_printf(": ");
-   if ((te->tpc && !(te->tpc&0x3)) &&
-   curproc &&
-   (curproc->p_pid == te->pid)) {
-   db_disasm((u_long)te->tpc, 0);
-   } else db_printf("\n");
-}
-
-void
-db_traptrace(addr, have_addr, count, modif)
-   db_expr_t addr;
-   int have_addr;
-   db_expr_t count;
-   char *modif;
-{
-   int i, start = 0, full = 0, reverse = 0;
-   struct traptrace *end;
-
-   start = 0;
-   end = &trap_trace_end[0];
-
-   {
-   register char c, *cp = modif;
-   if (modif)
-   while ((c = *cp++) != 0) {
-   if (c == 'f')
-   full = 1;
-   if (c == 'r')
-   reverse = 1;
-   }
-   }
-
-   if (have_addr) {
-   start = addr / (sizeof (struct traptrace));
-   if (&trap_trace[start] > &trap_trace_end[0]) {
-   db_printf("Addres

Re: vmd: simplify fatal/fatalx errno handling

2016-10-16 Thread Theo de Raadt
> On Sun, Oct 16, 2016 at 02:55:39PM -0700, Philip Guenther wrote:
> > 
> > Instead of using errno as a hidden argument to vfatal(), make it an 
> > _actual_ argument named 'code', ala the errc/warnc family, and rename it 
> > to vfatalc() to match the pattern set.
> > 
> > ok?
> > 
> > Philip Guenther
> > 
> 
> Isn't this code used elsewhere too?

benno is in the process of unifying all the log.c versions, as much
as possible.



Re: vmd: simplify fatal/fatalx errno handling

2016-10-16 Thread Mike Larkin
On Sun, Oct 16, 2016 at 02:55:39PM -0700, Philip Guenther wrote:
> 
> Instead of using errno as a hidden argument to vfatal(), make it an 
> _actual_ argument named 'code', ala the errc/warnc family, and rename it 
> to vfatalc() to match the pattern set.
> 
> ok?
> 
> Philip Guenther
> 

Isn't this code used elsewhere too?

Reyk should probably comment on this.

-ml

> Index: log.c
> ===
> RCS file: /data/src/openbsd/src/usr.sbin/vmd/log.c,v
> retrieving revision 1.3
> diff -u -p -r1.3 log.c
> --- log.c 12 Oct 2016 11:47:34 -  1.3
> +++ log.c 16 Oct 2016 21:17:40 -
> @@ -165,11 +165,10 @@ log_debug(const char *emsg, ...)
>  }
>  
>  static void
> -vfatal(const char *emsg, va_list ap)
> +vfatalc(int code, const char *emsg, va_list ap)
>  {
>   static char s[BUFSIZ];
>   const char  *sep;
> - int  saved_errno = errno;
>  
>   if (emsg != NULL) {
>   (void)vsnprintf(s, sizeof(s), emsg, ap);
> @@ -178,9 +177,9 @@ vfatal(const char *emsg, va_list ap)
>   s[0] = '\0';
>   sep = "";
>   }
> - if (saved_errno)
> + if (code)
>   logit(LOG_CRIT, "%s: %s%s%s",
> - log_procname, s, sep, strerror(saved_errno));
> + log_procname, s, sep, strerror(code));
>   else
>   logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
>  }
> @@ -191,7 +190,7 @@ fatal(const char *emsg, ...)
>   va_list ap;
>  
>   va_start(ap, emsg);
> - vfatal(emsg, ap);
> + vfatalc(errno, emsg, ap);
>   va_end(ap);
>   exit(1);
>  }
> @@ -201,9 +200,8 @@ fatalx(const char *emsg, ...)
>  {
>   va_list ap;
>  
> - errno = 0;
>   va_start(ap, emsg);
> - vfatal(emsg, ap);
> + vfatalc(0, emsg, ap);
>   va_end(ap);
>   exit(1);
>  }
> 



Re: use x2apic if it is enabled by BIOS

2016-10-16 Thread YASUOKA Masahiko
Hi,

Thanks your comments,

On Fri, 14 Oct 2016 16:23:40 +0200 (CEST)
s...@openbsd.org wrote:
> On Fri, 14 Oct 2016, YASUOKA Masahiko wrote:
>> I'm working on NEC Express5800/R110h-1 (dmesg is attached).  On this
>> machine, our kernel panics with following message.
>> 
>>   cpu0 at mainbus0panic: cpu at apic id 0 already attached?
>> 
>> This seems to happen since x2APIC on the machine is enabled by BIOS
>> and the kernel doesn't assume that.  The diff makes the kernel use
>> x2APIC if it is enabled by BIOS.
>> 
>> ok?
> 
> the code looks ok, but ...
(snip)
> ... I would leave the comment here why we don't enable it on all hardware 
> that supports it.

I reverted the comment and also tweaked white space.

Index: sys/arch/amd64/amd64/lapic.c
===
RCS file: /cvs/src/sys/arch/amd64/amd64/lapic.c,v
retrieving revision 1.44
diff -u -p -r1.44 lapic.c
--- sys/arch/amd64/amd64/lapic.c22 Jun 2016 01:12:38 -  1.44
+++ sys/arch/amd64/amd64/lapic.c17 Oct 2016 01:24:50 -
@@ -170,59 +170,57 @@ lapic_map(paddr_t lapic_base)
int s;
pt_entry_t *pte;
vaddr_t va;
+   u_int64_t msr;
 
-   /*
-* On real hardware, x2apic must only be enabled if interrupt remapping
-* is also enabled. See 10.12.7 of the SDM vol 3.
-* On hypervisors, this is not necessary. Hypervisors can implement
-* x2apic support even if the host CPU does not support it.
-* Until we support interrupt remapping, use x2apic only if the
-* hypervisor flag is also set.
-*/
-   if ((cpu_ecxfeature&CPUIDECX_X2APIC) && (cpu_ecxfeature&CPUIDECX_HV)) {
-   u_int64_t msr;
-
-   disable_intr();
-   s = lapic_tpr;
-
-   msr = rdmsr(MSR_APICBASE);
-   msr |= APICBASE_ENABLE_X2APIC;
-   wrmsr(MSR_APICBASE, msr);
+   disable_intr();
+   s = lapic_tpr;
+
+   msr = rdmsr(MSR_APICBASE);
 
+   if (ISSET(msr, APICBASE_ENABLE_X2APIC) ||
+   (ISSET(cpu_ecxfeature, CPUIDECX_HV) &&
+   ISSET(cpu_ecxfeature, CPUIDECX_X2APIC))) {
+/*
+ * On real hardware, x2apic must only be enabled if interrupt
+ * remapping is also enabled. See 10.12.7 of the SDM vol 3.
+ * On hypervisors, this is not necessary. Hypervisors can
+ * implement x2apic support even if the host CPU does not
+ * support it.  Until we support interrupt remapping, use
+ * x2apic only if the hypervisor flag is also set or it is
+ * enabled by BIOS.
+ */
+   if (!ISSET(msr, APICBASE_ENABLE_X2APIC)) {
+   msr |= APICBASE_ENABLE_X2APIC;
+   wrmsr(MSR_APICBASE, msr);
+   }
lapic_readreg = x2apic_readreg;
lapic_writereg = x2apic_writereg;
 #ifdef MULTIPROCESSOR
x86_ipi = x2apic_ipi;
 #endif
x2apic_enabled = 1;
-
codepatch_call(CPTAG_EOI, &x2apic_eoi);
 
lapic_writereg(LAPIC_TPRI, s);
-   enable_intr();
+   } else {
+   /*
+* Map local apic.  If we have a local apic, it's safe to
+* assume we're on a 486 or better and can use invlpg and
+* non-cacheable PTE's
+*
+* Whap the PTE "by hand" rather than calling pmap_kenter_pa
+* because the latter will attempt to invoke TLB shootdown
+* code just as we might have changed the value of
+* cpu_number()..
+*/
+   va = (vaddr_t)&local_apic;
+   pte = kvtopte(va);
+   *pte = lapic_base | PG_RW | PG_V | PG_N | PG_G | pg_nx;
+   invlpg(va);
 
-   return;
+   lapic_tpr = s;
}
 
-   va = (vaddr_t)&local_apic;
-
-   disable_intr();
-   s = lapic_tpr;
-
-   /*
-* Map local apic.  If we have a local apic, it's safe to assume
-* we're on a 486 or better and can use invlpg and non-cacheable PTE's
-*
-* Whap the PTE "by hand" rather than calling pmap_kenter_pa because
-* the latter will attempt to invoke TLB shootdown code just as we
-* might have changed the value of cpu_number()..
-*/
-
-   pte = kvtopte(va);
-   *pte = lapic_base | PG_RW | PG_V | PG_N | PG_G | pg_nx;
-   invlpg(va);
-
-   lapic_tpr = s;
enable_intr();
 }
 



minor diff for ldapd.conf.5

2016-10-16 Thread Rob Pierce
Fix a couple of grammar mistakes, remove a redundant word, and add a FILES
reference for the /etc/ldap/certs directory.

Rob

Index: ldapd.conf.5
===
RCS file: /cvs/src/usr.sbin/ldapd/ldapd.conf.5,v
retrieving revision 1.19
diff -u -p -r1.19 ldapd.conf.5
--- ldapd.conf.511 Jun 2014 18:00:40 -  1.19
+++ ldapd.conf.517 Oct 2016 00:43:43 -
@@ -108,7 +108,7 @@ the interface name with a .crt extension
 .Pa /etc/ldap/certs/fxp0.crt .
 .Pp
 If the certificate name is an absolute path, a .crt and .key extension
-is appended to form the certificate path and key path respectively.
+are appended to form the certificate path and key path respectively.
 .Pp
 Only secured connections accept plain text password authentication.
 Connections using TLS or unix domain sockets are always considered secured.
@@ -125,7 +125,7 @@ This option can be given multiple times,
 considered equal.
 Clients may choose to follow any of the referral URLs.
 .Pp
-The URL format has the following format:
+The URL has the following format:
 .Bd -literal -offset indent
 ldap://ldap.example.com
 ldaps://ldap.example.com:3890
@@ -148,7 +148,7 @@ below.
 .Sh NAMESPACES
 A namespace is a subtree of the global X.500 DIT (Directory Information Tree),
 also known as a naming context.
-All entries' distinguished names (DN) has the same suffix, which is used to
+All entries' distinguished names (DN) have the same suffix, which is used to
 identify the namespace.
 The suffix should consist of the domain components, in reverse order, of your
 domain name, as recommended by RFC 2247.
@@ -322,6 +322,8 @@ This would define MyOidAttributes as a s
 .El
 .Sh FILES
 .Bl -tag -width "/etc/ldap/ldapd.confXXX" -compact
+.It Pa /etc/ldap/certs/
+The directory where LDAP certificates are kept.
 .It Pa /etc/ldapd.conf
 Default
 .Xr ldapd 8



syslogd max unix sockets

2016-10-16 Thread Alexander Bluhm
Hi,

I would like to remove the artificial maximum number of unix domain
sockets in syslogd(8).  Just malloc(3) them dynamically which also
gives a more random address space layout.

ok?

bluhm

Index: usr.sbin/syslogd/syslogd.8
===
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.8,v
retrieving revision 1.51
diff -u -p -r1.51 syslogd.8
--- usr.sbin/syslogd/syslogd.8  4 Oct 2016 22:09:21 -   1.51
+++ usr.sbin/syslogd/syslogd.8  17 Oct 2016 00:06:57 -
@@ -75,7 +75,6 @@ to use only IPv6 addresses for UDP.
 Specify a location where
 .Nm
 should place an additional log socket.
-Up to 20 additional logging sockets can be specified.
 The primary use for this is to place additional log sockets in
 .Pa /dev/log
 of various chroot filespaces, though the need for these is
Index: usr.sbin/syslogd/syslogd.c
===
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.220
diff -u -p -r1.220 syslogd.c
--- usr.sbin/syslogd/syslogd.c  16 Oct 2016 22:12:50 -  1.220
+++ usr.sbin/syslogd/syslogd.c  17 Oct 2016 00:28:11 -
@@ -197,8 +197,8 @@ char*TypeNames[] = {
 SIMPLEQ_HEAD(filed_list, filed) Files;
 struct filed consfile;
 
-intnunix = 1;  /* Number of Unix domain sockets requested */
-char   *path_unix[MAXUNIX] = { _PATH_LOG }; /* Paths to Unix domain sockets */
+intnunix;  /* Number of Unix domain sockets requested */
+char   **path_unix;/* Paths to Unix domain sockets */
 intDebug;  /* debug flag */
 intForeground; /* run in foreground, instead of daemonizing */
 intStartup = 1;/* startup flag */
@@ -359,7 +359,12 @@ main(int argc, char *argv[])
int  ch, i;
int  lockpipe[2] = { -1, -1}, pair[2], nullfd, fd;
int  fd_ctlsock, fd_klog, fd_sendsys, fd_bind, fd_listen;
-   int  fd_unix[MAXUNIX];
+   int *fd_unix;
+
+   if ((path_unix = malloc(sizeof(*path_unix))) == NULL)
+   err(1, "malloc %s", _PATH_LOG);
+   path_unix[0] = _PATH_LOG;
+   nunix = 1;
 
while ((ch = getopt(argc, argv, "46a:C:c:dFf:hK:k:m:nP:p:S:s:T:U:uVZ"))
!= -1)
@@ -371,8 +376,9 @@ main(int argc, char *argv[])
Family = PF_INET6;
break;
case 'a':
-   if (nunix >= MAXUNIX)
-   errx(1, "out of descriptors: %s", optarg);
+   if ((path_unix = reallocarray(path_unix, nunix + 1,
+   sizeof(*path_unix))) == NULL)
+   err(1, "malloc %s", optarg);
path_unix[nunix++] = optarg;
break;
case 'C':   /* file containing CA certificates */
@@ -520,6 +526,8 @@ main(int argc, char *argv[])
die(0);
}
 
+   if ((fd_unix = reallocarray(NULL, nunix, sizeof(*fd_unix))) == NULL)
+   err(1, "malloc unix");
for (i = 0; i < nunix; i++) {
fd_unix[i] = unix_socket(path_unix[i], SOCK_DGRAM, 0666);
if (fd_unix[i] == -1) {
Index: usr.sbin/syslogd/syslogd.h
===
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.h,v
retrieving revision 1.25
diff -u -p -r1.25 syslogd.h
--- usr.sbin/syslogd/syslogd.h  16 Oct 2016 22:00:14 -  1.25
+++ usr.sbin/syslogd/syslogd.h  16 Oct 2016 22:44:41 -
@@ -42,9 +42,8 @@ void send_fd(int, int);
 int  receive_fd(int);
 
 /* The list of domain sockets */
-#define MAXUNIX21
 extern int nunix;
-extern char *path_unix[MAXUNIX];
+extern char **path_unix;
 extern char *path_ctlsock;
 
 #define MAXLINE8192/* maximum line length */



xenocara: make the ws driver reboust to EIO

2016-10-16 Thread Philip Guenther

On my Lenovo Yoga12, the touchscreen** will go into low power mode or some 
such if I leave the system alone to make dinner or something productive 
like that.  At that point the wsmouse device for it vanishes and the X 
server sees an infinite stream of EIO errors which it handlesby 
looping and filling /var/log/Xorg.0.log!  Thanks, but that's a horrible 
behavior and unproductive in every way.

So every time I upgrade my X sets I run out of space in /var, then 
remember to go back and rebuild the xf86-input-ws driver with the patch 
below and life is good.  Until I next upgrade.

Yes, the wsmouse or wscons, or maybe ums driver could handle this better, 
but can the ws X driver stop behaving badly on EIO?


Philip Guenther


** which I do periodically find useful, so suggestions to disable it will 
be ignored

Index: driver/xf86-input-ws/src/ws.c
===
RCS file: /cvs/xenocara/driver/xf86-input-ws/src/ws.c,v
retrieving revision 1.62
diff -u -p -r1.62 ws.c
--- driver/xf86-input-ws/src/ws.c   29 Aug 2015 08:48:29 -  1.62
+++ driver/xf86-input-ws/src/ws.c   16 Oct 2016 23:47:01 -
@@ -427,6 +427,11 @@ wsDeviceOn(DeviceIntPtr pWS)
}
}
}
+   if (priv->remove_timer == NULL) {
+   /* allocate here where it's safe */
+   priv->remove_timer = TimerSet(priv->remove_timer,
+ 0, 0, NULL, NULL);
+   }
xf86AddEnabledDevice(pInfo);
wsmbEmuOn(pInfo);
pWS->public.on = TRUE;
@@ -457,18 +462,41 @@ wsDeviceOff(DeviceIntPtr pWS)
pWS->public.on = FALSE;
 }
 
+/*
+ * Called as an OsTimer() callback to remove the device while xf86Wakeup()
+ * isn't playing with the list.
+ */
+static CARD32
+wsRemoveMouse(OsTimerPtr timer, CARD32 now, pointer arg)
+{
+   InputInfoPtr pInfo = (InputInfoPtr) arg;
+
+   xf86DisableDevice(pInfo->dev, TRUE);
+
+   return 0;   /* don't set to run again */
+}
+
 static size_t
 wsReadEvents(InputInfoPtr pInfo)
 {
WSDevicePtr priv = (WSDevicePtr)pInfo->private;
ssize_t len;
+   int err;
 
priv->events_count = priv->events_pos = 0;
len = read(pInfo->fd, priv->events, sizeof(priv->events));
if (len < 0) {
-   if (errno != EAGAIN)
+   err = errno;
+   if (err == EIO) {
+   xf86IDrvMsg(pInfo, X_ERROR,
+   "device no longer present - removing: %s\n",
+   strerror(err));
+   xf86RemoveEnabledDevice(pInfo);
+   priv->remove_timer = TimerSet(priv->remove_timer, 0, 1,
+ wsRemoveMouse, pInfo);
+   } else if (err != EAGAIN)
xf86IDrvMsg(pInfo, X_ERROR, "read error %s\n",
-   strerror(errno));
+   strerror(err));
} else if (len % sizeof(struct wscons_event)) {
xf86IDrvMsg(pInfo, X_ERROR,
"read error, invalid number of bytes\n");
@@ -680,6 +708,12 @@ wsOpen(InputInfoPtr pInfo)
 static void
 wsClose(InputInfoPtr pInfo)
 {
+   WSDevicePtr priv = (WSDevicePtr)pInfo->private;
+
+   if (priv->remove_timer != NULL) {
+   TimerFree(priv->remove_timer);
+   priv->remove_timer = NULL;
+   }
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
 }
Index: driver/xf86-input-ws/src/ws.h
===
RCS file: /cvs/xenocara/driver/xf86-input-ws/src/ws.h,v
retrieving revision 1.14
diff -u -p -r1.14 ws.h
--- driver/xf86-input-ws/src/ws.h   29 Aug 2015 08:48:29 -  1.14
+++ driver/xf86-input-ws/src/ws.h   16 Oct 2016 23:47:01 -
@@ -87,6 +87,8 @@ typedef struct WSDevice {
Time timeout;
} emulateWheel;
 
+   OsTimerPtr  remove_timer;   /* Callback for removal on EIO */
+
struct wscons_event events[NWSEVENTS];
size_t  events_count;
size_t  events_pos;



vmd: simplify fatal/fatalx errno handling

2016-10-16 Thread Philip Guenther

Instead of using errno as a hidden argument to vfatal(), make it an 
_actual_ argument named 'code', ala the errc/warnc family, and rename it 
to vfatalc() to match the pattern set.

ok?

Philip Guenther

Index: log.c
===
RCS file: /data/src/openbsd/src/usr.sbin/vmd/log.c,v
retrieving revision 1.3
diff -u -p -r1.3 log.c
--- log.c   12 Oct 2016 11:47:34 -  1.3
+++ log.c   16 Oct 2016 21:17:40 -
@@ -165,11 +165,10 @@ log_debug(const char *emsg, ...)
 }
 
 static void
-vfatal(const char *emsg, va_list ap)
+vfatalc(int code, const char *emsg, va_list ap)
 {
static char s[BUFSIZ];
const char  *sep;
-   int  saved_errno = errno;
 
if (emsg != NULL) {
(void)vsnprintf(s, sizeof(s), emsg, ap);
@@ -178,9 +177,9 @@ vfatal(const char *emsg, va_list ap)
s[0] = '\0';
sep = "";
}
-   if (saved_errno)
+   if (code)
logit(LOG_CRIT, "%s: %s%s%s",
-   log_procname, s, sep, strerror(saved_errno));
+   log_procname, s, sep, strerror(code));
else
logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
 }
@@ -191,7 +190,7 @@ fatal(const char *emsg, ...)
va_list ap;
 
va_start(ap, emsg);
-   vfatal(emsg, ap);
+   vfatalc(errno, emsg, ap);
va_end(ap);
exit(1);
 }
@@ -201,9 +200,8 @@ fatalx(const char *emsg, ...)
 {
va_list ap;
 
-   errno = 0;
va_start(ap, emsg);
-   vfatal(emsg, ap);
+   vfatalc(0, emsg, ap);
va_end(ap);
exit(1);
 }



CVS: cvs.openbsd.org: src (fwd)

2016-10-16 Thread Philip Guenther

Should have sent this to tech...

-- Forwarded message --
Date: Sun, 16 Oct 2016 13:57:42 -0700
From: Philip Guenther 
To: Jeremie Courreges-Anglas 
Cc: source-chan...@openbsd.org
Subject: Re: CVS: cvs.openbsd.org: src

On Sun, 16 Oct 2016, Jeremie Courreges-Anglas wrote:
> CVSROOT:  /cvs
> Module name:  src
> Changes by:   j...@cvs.openbsd.org2016/10/16 04:40:59
> 
> Modified files:
>   usr.sbin/rpc.bootparamd: bootparamd.c 
> 
> Log message:
> Rename local variable 'err' to 'error', to avoid -Wshadow conflicts with err.h

Ick.  I know that newer gcc's have stopped generating shadow warning for 
local variables vs global functions, such as this case.  Making these 
changes just because we have an old gcc is kinda annoying.

So let's fix that and make our gcc a bit more like new ones.  Written 
without peeking at the new ones and tested against the .c file at bottom 
to verify that it doesn't fail or crash on some weird combo of shadowing.

oks?

Philip Guenther


Index: c-decl.c
===
RCS file: /data/src/openbsd/src/gnu/gcc/gcc/c-decl.c,v
retrieving revision 1.4
diff -u -p -r1.4 c-decl.c
--- c-decl.c10 Sep 2015 10:56:35 -  1.4
+++ c-decl.c16 Oct 2016 20:50:10 -
@@ -1946,8 +1946,19 @@ warn_if_shadowing (tree new_decl)
  warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
   new_decl);
else if (DECL_FILE_SCOPE_P (old_decl))
- warning (OPT_Wshadow, "declaration of %q+D shadows a global "
-  "declaration", new_decl);
+ {
+   /* Don't warn about shadowing a global function unless the local
+  variable is a pointer to a function */
+   if (TREE_CODE (old_decl) == FUNCTION_DECL
+   && TREE_CODE (new_decl) != FUNCTION_DECL
+   && (TREE_CODE (new_decl) != VAR_DECL
+   || !POINTER_TYPE_P (TREE_TYPE (new_decl))
+   || TREE_CODE (TREE_TYPE (TREE_TYPE (new_decl)))
+  != FUNCTION_TYPE))
+ break;
+   warning (OPT_Wshadow, "declaration of %q+D shadows a global "
+"declaration", new_decl);
+ }
else if (TREE_CODE (old_decl) == FUNCTION_DECL
 && DECL_BUILT_IN (old_decl))
  {



-- test-shadow.c ---
int var;
void func(void);
void (*func_ptr)(void) = func;
enum { ENUM };
typedef int type;

void
test_var(int var_arg, void (*func_ptr_arg)(void))
{
int var;
int func;
int func_ptr;
int ENUM;
int type;
{
int var_arg;
int func_ptr_arg;
}
}

void
test_func_ptr(int var_arg, void (*func_ptr_arg)(void))
{
void (*var)(void);
void (*func)(void);
void (*func_ptr)(void);
void (*ENUM)(void);
void (*type)(void);
{
void (*var_arg)(void);
void (*func_ptr_arg)(void);
}
}

void
test_int_ptr(int var_arg, void (*func_ptr_arg)(void))
{
int *var;
int *func;
int *func_ptr;
int *ENUM;
int *type;
{
int *var_arg;
int *func_ptr_arg;
}
}

void
test_enum(int var_arg, void (*func_ptr_arg)(void))
{
enum { var };
enum { func };
enum { func_ptr };
enum { ENUM };
enum { type };
{
enum { var_arg };
enum { func_ptr_arg };
}
}

void
test_type(int var_arg, void (*func_ptr_arg)(void))
{
typedef int var;
typedef int func;
typedef int func_ptr;
typedef int ENUM;
typedef int type;
{
typedef int var_arg;
typedef int func_ptr_arg;
}
}

void
test_var_param(
int var,
int func,
int func_ptr,
int ENUM,
int type)
{
}

void
test_func_ptr_param(
void (*var)(void),
void (*func)(void),
void (*func_ptr)(void),
void (*ENUM)(void),
void (*type)(void))
{
}



make ssh -qO {stop,exit} less noisy

2016-10-16 Thread Tim Kuijsten

Hi tech@

This makes the tear down of ssh control masters better play with shell 
scripts that are run by cron. If nothing unexpected happens and in quiet 
mode, then don't echo back the command that is being requested.


-Tim


Index: mux.c
===
RCS file: /cvs/src/usr.bin/ssh/mux.c,v
retrieving revision 1.62
diff -u -p -r1.62 mux.c
--- mux.c   30 Sep 2016 09:19:13 -  1.62
+++ mux.c   16 Oct 2016 18:18:53 -
@@ -2212,7 +2212,8 @@ muxclient(const char *path)
exit(0);
case SSHMUX_COMMAND_TERMINATE:
mux_client_request_terminate(sock);
-   fprintf(stderr, "Exit request sent.\r\n");
+   if (options.log_level != SYSLOG_LEVEL_QUIET)
+   fprintf(stderr, "Exit request sent.\r\n");
exit(0);
case SSHMUX_COMMAND_FORWARD:
if (mux_client_forwards(sock, 0) != 0)
@@ -2230,7 +2231,8 @@ muxclient(const char *path)
exit(0);
case SSHMUX_COMMAND_STOP:
mux_client_request_stop_listening(sock);
-   fprintf(stderr, "Stop listening request sent.\r\n");
+   if (options.log_level != SYSLOG_LEVEL_QUIET)
+   fprintf(stderr, "Stop listening request sent.\r\n");
exit(0);
case SSHMUX_COMMAND_CANCEL_FWD:
if (mux_client_forwards(sock, 1) != 0)



vm.conf(5) manual tweak for switches

2016-10-16 Thread Edd Barrett
Hi,

In vm.conf(5):

---8<---
Virtual switches can be configured at any point in the configuration
file; they allow switchd to add network interfaces of VMs to the
underlying switch interfaces automatically.
--->8---

This confused me, since i've been using virtual switches without
switchd. I have a suspicion that "switchd" was supposed to be "vmd" in
that sentence (?).

The following diff attempts to fix this, and tweaks the surrounding text
a bit too.

Comments, OK?


Index: vm.conf.5
===
RCS file: /home/edd/cvsync/src/usr.sbin/vmd/vm.conf.5,v
retrieving revision 1.8
diff -u -p -r1.8 vm.conf.5
--- vm.conf.5   15 Oct 2016 14:02:11 -  1.8
+++ vm.conf.5   16 Oct 2016 18:14:00 -
@@ -165,23 +165,36 @@ is greater than the number of
 statements, additional default interfaces will be added.
 .El
 .Sh SWITCH CONFIGURATION
-Virtual switches can be configured at any point in the configuration file;
-they allow
-.Nm switchd
-to add network interfaces of VMs to the underlying switch interfaces
-automatically.
-It is possible to pre-configure switch interfaces using
+A virtual switch allows VMs to communicate with other network interfaces on the
+host system via either
+.Xr bridge 4
+or
+.Xr switch 4 .
+The network interface for each virtual switch defined in
+.Nm
+is automatically created by
+.Xr vmd 8 ,
+but it is also possible to pre-configure switch interfaces using
 .Xr hostname.if 5
 or
-.Xr ifconfig 8 ,
-see the sections
+.Xr ifconfig 8
+(see the
 .Sx BRIDGE
-or
+and
 .Sx SWITCH
-in
+sections in
 .Xr ifconfig 8
-accordingly.
+accordingly).
+When a VM is started, virtual network interfaces which are assigned to a
+virtual switch have their
+.Xr tap 4
+interface automatically added into the corresponding
+.Xr bridge 4
+or
+.Xr switch 4
+interface underlying the virtual switch.
 .Pp
+Virtual switches can be configured at any point in the configuration file.
 Each
 .Ic switch
 section starts with a declaration of the virtual switch:

-- 
Best Regards
Edd Barrett

http://www.theunixzoo.co.uk



Re: usb disk dirty after every reboot

2016-10-16 Thread Theo de Raadt
> * When sync(2)  is called. Though I am not 100% sure if we really want to do a
> cache flush for every sync. Thoughts?

We definately cannot it there.  The system call is asyncronous, and
makes no gaurantees.  Furthermore note that any user can call sync(2),
so unknown performance would come under control of any user.

Note the manual page declares this is a BUG, but it actually isn't.
This was by design.  Yes, it is a pretty old unix wart

BUGS
 sync() may return before the buffers are completely flushed.



Re: diff3: use boolean OR, not bitwise

2016-10-16 Thread Otto Moerbeek
On Sun, Oct 16, 2016 at 08:44:34AM -0600, Todd C. Miller wrote:

> On Sun, 16 Oct 2016 16:06:39 +0200, Otto Moerbeek wrote:
> 
> > On Sun, Oct 16, 2016 at 07:07:36AM -0600, Todd C. Miller wrote:
> > 
> > > It is effectively the same in this case but using the boolean OR
> > > is less surprising and may quiet over zealous compilers.
> > 
> > Are you sure? For example, in cvs/diff3.c
> > t2 is not always initialized with this change.
> 
> Good catch, that makes a difference.  I'm no longer sure changing
> this is worth it as the alternative is also ugly.

indeed, but I have a slight preference for your code,

-Otto

> 
>  - todd
> 
> Index: usr.bin/cvs/diff3.c
> ===
> RCS file: /cvs/src/usr.bin/cvs/diff3.c,v
> retrieving revision 1.61
> diff -u -p -u -r1.61 diff3.c
> --- usr.bin/cvs/diff3.c   16 Oct 2016 13:03:40 -  1.61
> +++ usr.bin/cvs/diff3.c   16 Oct 2016 14:43:49 -
> @@ -543,7 +543,12 @@ merge(size_t m1, size_t m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + for (;;) {
> + t1 = (d1 < d13 + m1);
> + t2 = (d2 < d23 + m2);
> + if (!t1 && !t2)
> + break;
> +
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from, d1->old.to,
> Index: usr.bin/diff3/diff3prog.c
> ===
> RCS file: /cvs/src/usr.bin/diff3/diff3prog.c,v
> retrieving revision 1.18
> diff -u -p -u -r1.18 diff3prog.c
> --- usr.bin/diff3/diff3prog.c 16 Oct 2016 13:03:40 -  1.18
> +++ usr.bin/diff3/diff3prog.c 16 Oct 2016 14:43:49 -
> @@ -301,7 +301,12 @@ merge(int m1, int m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + for (;;) {
> + t1 = (d1 < d13 + m1);
> + t2 = (d2 < d23 + m2);
> + if (!t1 && !t2)
> + break;
> +
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from,d1->old.to,
> Index: usr.bin/rcs/diff3.c
> ===
> RCS file: /cvs/src/usr.bin/rcs/diff3.c,v
> retrieving revision 1.39
> diff -u -p -u -r1.39 diff3.c
> --- usr.bin/rcs/diff3.c   26 Aug 2016 09:02:54 -  1.39
> +++ usr.bin/rcs/diff3.c   16 Oct 2016 14:43:49 -
> @@ -636,7 +636,12 @@ merge(size_t m1, size_t m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + for (;;) {
> + t1 = (d1 < d13 + m1);
> + t2 = (d2 < d23 + m2);
> + if (!t1 && !t2)
> + break;
> +
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from, d1->old.to,



Re: config(8): use {err,warn}{,x} instead of fprintf(stderr, ...)

2016-10-16 Thread Todd C. Miller
On Sun, 16 Oct 2016 11:47:32 +0200, Theo Buehler wrote:

> Many files already include  and there's a mix of hand-rolled
> warning messages and there's incorrect usage warn("config: ..."). This
> is a first sweep at unifying them.
> 
> In mkheaders.c, there is an err() function, rename it to emitwarn()
> since there are emit* functions already and it is non-fatal.

Some minor issues noted inline but otherwise OK.

 - todd

> Index: main.c
> ===
> RCS file: /cvs/src/usr.sbin/config/main.c,v
> retrieving revision 1.55
> diff -u -p -r1.55 main.c
> --- main.c16 Oct 2016 09:36:46 -  1.55
> +++ main.c16 Oct 2016 09:42:01 -
> @@ -75,7 +75,7 @@ static void optiondelta(void);
>  
>  int  verbose;
>  
> -void
> +__dead void
>  usage(void)
>  {
>   extern char *__progname;
> @@ -169,19 +169,15 @@ main(int argc, char *argv[])
>  
>   if (eflag) {
>  #ifdef MAKE_BOOTSTRAP
> - fprintf(stderr, "config: UKC not available in this binary\n");
> - exit(1);
> + errx(1, "UKC not available in this binary");
>  #else
>   return (ukc(argv[0], outfile, uflag, fflag));
>  #endif
>   }
>  
>   conffile = (argc == 1) ? argv[0] : "CONFIG";
> - if (firstfile(conffile)) {
> - (void)fprintf(stderr, "config: cannot read %s: %s\n",
> - conffile, strerror(errno));
> - exit(2);
> - }
> + if (firstfile(conffile))
> + err(2, "cannot read %s\n", conffile);

You don't want that trailing newline.

>  
>   /*
>* Init variables.
> @@ -246,8 +242,7 @@ main(int argc, char *argv[])
>   defmaxusers);
>   maxusers = defmaxusers;
>   } else {
> - (void)fprintf(stderr,
> - "config: need \"maxusers\" line\n");
> + warnx("need \"maxusers\" line");
>   errors++;
>   }
>   }
> @@ -269,7 +264,7 @@ main(int argc, char *argv[])
>   mkioconf())
>   stop();
>   optiondelta();
> - exit(0);
> + return (0);
>  }
>  
>  static int
> @@ -278,11 +273,11 @@ mksymlink(const char *value, const char 
>   int ret = 0;
>  
>   if (remove(path) && errno != ENOENT) {
> - warn("config: remove(%s)", path);
> + warn("remove(%s)", path);
>   ret = 1;
>   }
>   if (symlink(value, path)) {
> - warn("config: symlink(%s -> %s)", path, value);
> + warn("symlink(%s -> %s)", path, value);
>   ret = 1;
>   }
>   return (ret);
> @@ -624,8 +619,7 @@ badstar(void)
>   continue;
>   foundstar:
>   if (ht_lookup(needcnttab, d->d_name)) {
> - (void)fprintf(stderr,
> - "config: %s's cannot be *'d until its driver is fixed\n",
> + warnx("%s's cannot be *'d until its driver is fixed",
>   d->d_name);
>   errs++;
>   continue;
> @@ -662,26 +656,14 @@ setupdirs(void)
>   builddir = defbuilddir;
>  
>   if (stat(builddir, &st) != 0) {
> - if (mkdir(builddir, 0777)) {
> - (void)fprintf(stderr, "config: cannot create %s: %s\n",
> - builddir, strerror(errno));
> - exit(2);
> - }
> - } else if (!S_ISDIR(st.st_mode)) {
> - (void)fprintf(stderr, "config: %s is not a directory\n",
> - builddir);
> - exit(2);
> - }
> - if (chdir(builddir) != 0) {
> - (void)fprintf(stderr, "config: cannot change to %s\n",
> - builddir);
> - exit(2);
> - }
> - if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
> - (void)fprintf(stderr, "config: %s is not a directory\n",
> - srcdir);
> - exit(2);
> - }
> + if (mkdir(builddir, 0777))
> + err(2, "cannot create %s:\n", builddir);

Another trailing newline, also the ':' be removed.

> + } else if (!S_ISDIR(st.st_mode))
> + errx(2, "%s is not a directory\n", builddir);

Another trailing newline.  You might also consider:

errc(2, ENOTDIR, "%s", builddir);

> + if (chdir(builddir) != 0)
> + errx(2, "cannot change to %s\n", builddir);

Another trailing newline.

> + if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode))
> + errx(2, "%s is not a directory\n", srcdir);

Another trailing newline, maybe use errc().

>  
>   if (bflag) {
>   if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
> @@ -693,14 +675,10 @@ setupdirs(void)
>   goto reconfig;
>  
>   fp = fopen("Makefile", "w");
> - if (!fp) {
> - (void)fprintf(stderr, "config: cannot create Makefile\n");
> -

add c11 static_assert definition to assert.h

2016-10-16 Thread Jonathan Gray
I encountered static_assert when trying to build an optional part of a
newer version of Mesa with clang.

Definition taken from FreeBSD.

Index: sys/sys/cdefs.h
===
RCS file: /cvs/src/sys/sys/cdefs.h,v
retrieving revision 1.39
diff -u -p -r1.39 cdefs.h
--- sys/sys/cdefs.h 18 Apr 2014 11:51:17 -  1.39
+++ sys/sys/cdefs.h 16 Oct 2016 14:39:41 -
@@ -386,6 +386,15 @@
 #endif
 
 /*
+ * __STDC_VERSION__ overrides any of the other macros since
+ * they are non-exclusive.
+ */
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112
+# undef __ISO_C_VISIBLE
+# define __ISO_C_VISIBLE   2011
+#endif
+
+/*
  * Finally deal with BSD-specific interfaces that are not covered
  * by any standards.  We expose these when none of the POSIX or XPG
  * macros is defined or if the user explicitly asks for them.
Index: include/assert.h
===
RCS file: /cvs/src/include/assert.h,v
retrieving revision 1.13
diff -u -p -r1.13 assert.h
--- include/assert.h26 Jun 2011 21:11:41 -  1.13
+++ include/assert.h16 Oct 2016 14:05:24 -
@@ -61,6 +61,20 @@
 
 #ifndef _ASSERT_H_
 #define _ASSERT_H_
+
+/*
+ * Static assertions.  In principle we could define static_assert for
+ * C++ older than C++11, but this breaks if _Static_assert is
+ * implemented as a macro.
+ *
+ * C++ template parameters may contain commas, even if not enclosed in
+ * parentheses, causing the _Static_assert macro to be invoked with more
+ * than two parameters.
+ */
+#if __ISO_C_VISIBLE >= 2011 && !defined(__cplusplus)
+#definestatic_assert   _Static_assert
+#endif
+
 __BEGIN_DECLS
 __dead void __assert(const char *, int, const char *);
 __dead void __assert2(const char *, int, const char *, const char *);



Re: FAQ entry for vmm

2016-10-16 Thread Josh Grosse
On Sat, Oct 15, 2016 at 05:11:49PM +0100, Edd Barrett wrote:

> * attaching to a console by name would be nice touch.

This works. For example, I can use:

# vmctl cons i386



Re: diff3: use boolean OR, not bitwise

2016-10-16 Thread Todd C. Miller
On Sun, 16 Oct 2016 16:06:39 +0200, Otto Moerbeek wrote:

> On Sun, Oct 16, 2016 at 07:07:36AM -0600, Todd C. Miller wrote:
> 
> > It is effectively the same in this case but using the boolean OR
> > is less surprising and may quiet over zealous compilers.
> 
> Are you sure? For example, in cvs/diff3.c
> t2 is not always initialized with this change.

Good catch, that makes a difference.  I'm no longer sure changing
this is worth it as the alternative is also ugly.

 - todd

Index: usr.bin/cvs/diff3.c
===
RCS file: /cvs/src/usr.bin/cvs/diff3.c,v
retrieving revision 1.61
diff -u -p -u -r1.61 diff3.c
--- usr.bin/cvs/diff3.c 16 Oct 2016 13:03:40 -  1.61
+++ usr.bin/cvs/diff3.c 16 Oct 2016 14:43:49 -
@@ -543,7 +543,12 @@ merge(size_t m1, size_t m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   for (;;) {
+   t1 = (d1 < d13 + m1);
+   t2 = (d2 < d23 + m2);
+   if (!t1 && !t2)
+   break;
+
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from, d1->old.to,
Index: usr.bin/diff3/diff3prog.c
===
RCS file: /cvs/src/usr.bin/diff3/diff3prog.c,v
retrieving revision 1.18
diff -u -p -u -r1.18 diff3prog.c
--- usr.bin/diff3/diff3prog.c   16 Oct 2016 13:03:40 -  1.18
+++ usr.bin/diff3/diff3prog.c   16 Oct 2016 14:43:49 -
@@ -301,7 +301,12 @@ merge(int m1, int m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   for (;;) {
+   t1 = (d1 < d13 + m1);
+   t2 = (d2 < d23 + m2);
+   if (!t1 && !t2)
+   break;
+
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from,d1->old.to,
Index: usr.bin/rcs/diff3.c
===
RCS file: /cvs/src/usr.bin/rcs/diff3.c,v
retrieving revision 1.39
diff -u -p -u -r1.39 diff3.c
--- usr.bin/rcs/diff3.c 26 Aug 2016 09:02:54 -  1.39
+++ usr.bin/rcs/diff3.c 16 Oct 2016 14:43:49 -
@@ -636,7 +636,12 @@ merge(size_t m1, size_t m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   for (;;) {
+   t1 = (d1 < d13 + m1);
+   t2 = (d2 < d23 + m2);
+   if (!t1 && !t2)
+   break;
+
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from, d1->old.to,



Re: diff3: use boolean OR, not bitwise

2016-10-16 Thread Otto Moerbeek
On Sun, Oct 16, 2016 at 07:07:36AM -0600, Todd C. Miller wrote:

> It is effectively the same in this case but using the boolean OR
> is less surprising and may quiet over zealous compilers.

Are you sure? For example, in cvs/diff3.c
t2 is not always initialized with this change.

-Otto

> 
> OK?
> 
>  - todd
> 
> Index: cvs/diff3.c
> ===
> RCS file: /cvs/src/usr.bin/cvs/diff3.c,v
> retrieving revision 1.61
> diff -u -p -u -r1.61 diff3.c
> --- cvs/diff3.c   16 Oct 2016 13:03:40 -  1.61
> +++ cvs/diff3.c   16 Oct 2016 13:05:33 -
> @@ -543,7 +543,7 @@ merge(size_t m1, size_t m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from, d1->old.to,
> Index: diff3/diff3prog.c
> ===
> RCS file: /cvs/src/usr.bin/diff3/diff3prog.c,v
> retrieving revision 1.18
> diff -u -p -u -r1.18 diff3prog.c
> --- diff3/diff3prog.c 16 Oct 2016 13:03:40 -  1.18
> +++ diff3/diff3prog.c 16 Oct 2016 13:05:33 -
> @@ -301,7 +301,7 @@ merge(int m1, int m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from,d1->old.to,
> Index: rcs/diff3.c
> ===
> RCS file: /cvs/src/usr.bin/rcs/diff3.c,v
> retrieving revision 1.39
> diff -u -p -u -r1.39 diff3.c
> --- rcs/diff3.c   26 Aug 2016 09:02:54 -  1.39
> +++ rcs/diff3.c   16 Oct 2016 13:05:33 -
> @@ -636,7 +636,7 @@ merge(size_t m1, size_t m2)
>   d1 = d13;
>   d2 = d23;
>   j = 0;
> - while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
> + while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
>   if (debug) {
>   printf("%d,%d=%d,%d %d,%d=%d,%d\n",
>   d1->old.from, d1->old.to,



Re: config(8): use {err,warn}{,x} instead of fprintf(stderr, ...)

2016-10-16 Thread Martin Natano
On Sun, Oct 16, 2016 at 01:35:34PM +0200, Theo Buehler wrote:
> On Sun, Oct 16, 2016 at 11:47:32AM +0200, Theo Buehler wrote:
> > Many files already include  and there's a mix of hand-rolled
> > warning messages and there's incorrect usage warn("config: ..."). This
> > is a first sweep at unifying them.
> > 
> > In mkheaders.c, there is an err() function, rename it to emitwarn()
> > since there are emit* functions already and it is non-fatal.
> 
> Here's a slightly improved version: I failed to remove a few newlines in
> warning/error strings in main.c.

Looks good to me, but see some nits below.


> 
> Index: main.c
> ===
> RCS file: /cvs/src/usr.sbin/config/main.c,v
> retrieving revision 1.55
> diff -u -p -r1.55 main.c
> --- main.c16 Oct 2016 09:36:46 -  1.55
> +++ main.c16 Oct 2016 11:25:20 -
> @@ -75,7 +75,7 @@ static void optiondelta(void);
>  
>  int  verbose;
>  
> -void
> +__dead void
>  usage(void)

The usage declaration in ukc.c needs updating too.

__dead void usage(void);


>  {
>   extern char *__progname;
> @@ -662,26 +656,14 @@ setupdirs(void)
>   builddir = defbuilddir;
>  
>   if (stat(builddir, &st) != 0) {
> - if (mkdir(builddir, 0777)) {
> - (void)fprintf(stderr, "config: cannot create %s: %s\n",
> - builddir, strerror(errno));
> - exit(2);
> - }
> - } else if (!S_ISDIR(st.st_mode)) {
> - (void)fprintf(stderr, "config: %s is not a directory\n",
> - builddir);
> - exit(2);
> - }
> - if (chdir(builddir) != 0) {
> - (void)fprintf(stderr, "config: cannot change to %s\n",
> - builddir);
> - exit(2);
> - }
> - if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
> - (void)fprintf(stderr, "config: %s is not a directory\n",
> - srcdir);
> - exit(2);
> - }
> + if (mkdir(builddir, 0777))
> + err(2, "cannot create %s:", builddir);

No ":" at the end of format. This will be added by err() automatically.


> + } else if (!S_ISDIR(st.st_mode))
> + errx(2, "%s is not a directory", builddir);
> + if (chdir(builddir) != 0)
> + errx(2, "cannot change to %s", builddir);
> + if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode))
> + errx(2, "%s is not a directory", srcdir);
>  
>   if (bflag) {
>   if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
> Index: mkheaders.c
> ===
> RCS file: /cvs/src/usr.sbin/config/mkheaders.c,v
> retrieving revision 1.21
> diff -u -p -r1.21 mkheaders.c
> --- mkheaders.c   16 Jan 2015 06:40:16 -  1.21
> +++ mkheaders.c   16 Oct 2016 11:25:20 -
> @@ -102,22 +103,21 @@ emitcnt(struct nvlist *head)
>   nv = nv->nv_next;
>   }
>   if (ferror(fp))
> - return (err("read", fname, fp));
> + return (emitwarn("read", fname, fp));
>   (void)fclose(fp);
>   if (nv == NULL)
>   return (0);
>  writeit:
>   if ((fp = fopen(fname, "w")) == NULL) {
> - (void)fprintf(stderr, "config: cannot write %s: %s\n",
> - fname, strerror(errno));
> + warn("cannot write %s", fname);
>   return (1);

return(emitwarn("writ", fname, NULL)); ?


>   }
>   for (nv = head; nv != NULL; nv = nv->nv_next)
>   if (fprintf(fp, "#define\t%s\t%d\n",
>   cntname(nv->nv_name), nv->nv_int) < 0)
> - return (err("writ", fname, fp));
> + return (emitwarn("writ", fname, fp));
>   if (fclose(fp))
> - return (err("writ", fname, NULL));
> + return (emitwarn("writ", fname, NULL));
>   return (0);
>  }
>  
> @@ -175,23 +175,21 @@ writeit:
>* They're different, or the file doesn't exist.
>*/
>   if ((fp = fopen(fname, "w")) == NULL) {
> - (void)fprintf(stderr, "config: cannot write %s: %s\n",
> - fname, strerror(errno));
> + warn("cannot write %s", fname);
>   return (1);

return(emitwarn("writ", fname, NULL)); ?


>   }
>   if (fprintf(fp, "%s", new_contents) < 0)
> - return (err("writ", fname, fp));
> + return (emitwarn("writ", fname, fp));
>   if (fclose(fp))
> - return (err("writ", fname, fp));
> + return (emitwarn("writ", fname, fp));
>   return (0);
>  }
>  
>  static int
> -err(const char *what, char *fname, FILE *fp)
> +emitwarn(const char *what, char *fname, FILE *fp)
>  {
>  
> - (void)fprintf(stderr, "config: error %sing %s: %s\n",
> - what, fname, strerror(errno));
> + warn("error %sing %s", what, fname);
>   if (fp)
>   

diff3: use boolean OR, not bitwise

2016-10-16 Thread Todd C. Miller
It is effectively the same in this case but using the boolean OR
is less surprising and may quiet over zealous compilers.

OK?

 - todd

Index: cvs/diff3.c
===
RCS file: /cvs/src/usr.bin/cvs/diff3.c,v
retrieving revision 1.61
diff -u -p -u -r1.61 diff3.c
--- cvs/diff3.c 16 Oct 2016 13:03:40 -  1.61
+++ cvs/diff3.c 16 Oct 2016 13:05:33 -
@@ -543,7 +543,7 @@ merge(size_t m1, size_t m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from, d1->old.to,
Index: diff3/diff3prog.c
===
RCS file: /cvs/src/usr.bin/diff3/diff3prog.c,v
retrieving revision 1.18
diff -u -p -u -r1.18 diff3prog.c
--- diff3/diff3prog.c   16 Oct 2016 13:03:40 -  1.18
+++ diff3/diff3prog.c   16 Oct 2016 13:05:33 -
@@ -301,7 +301,7 @@ merge(int m1, int m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from,d1->old.to,
Index: rcs/diff3.c
===
RCS file: /cvs/src/usr.bin/rcs/diff3.c,v
retrieving revision 1.39
diff -u -p -u -r1.39 diff3.c
--- rcs/diff3.c 26 Aug 2016 09:02:54 -  1.39
+++ rcs/diff3.c 16 Oct 2016 13:05:33 -
@@ -636,7 +636,7 @@ merge(size_t m1, size_t m2)
d1 = d13;
d2 = d23;
j = 0;
-   while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
+   while ((t1 = (d1 < d13 + m1)) || (t2 = (d2 < d23 + m2))) {
if (debug) {
printf("%d,%d=%d,%d %d,%d=%d,%d\n",
d1->old.from, d1->old.to,



Re: /usr/src beforeinstall: make prereq as BUILDUSER

2016-10-16 Thread Theo Buehler
New version that cleans up all of /usr/obj on amd64, i386 and macppc
(including the machine@ symlinks). I verified that it survives
make build and make release on these three architectures.

The previous diff had a bug that made make 'includes error' out on archs
different from amd64 and i386 because I overlooked that mm_malloc.h in
gnu/usr.bin/cc/include/Makefile is only compiled there.

This patch adds a prereq target to bsd.subdir.mk, which is a no-op.
Since I add ./sys/arch/${MACHINE}/stand to the PRDIRS, this ensures that
the .BEGIN targets responsible for these symlinks fire at the prereq
stage, so the machine symlinks end up owned by BUILDUSER.

To test this, it's safer to install the makefiles first:

# cd /usr/src/share/mk && make install
# cd /usr/src && make obj && make includes

and then

$ find /usr/obj -user root -ls

should produce no output (or maybe /usr/obj).

Index: Makefile
===
RCS file: /var/cvs/src/Makefile,v
retrieving revision 1.129
diff -u -p -U4 -r1.129 Makefile
--- Makefile6 Oct 2016 18:56:17 -   1.129
+++ Makefile13 Oct 2016 17:31:04 -
@@ -78,10 +78,10 @@ build:
echo $@ must be called by root >&2; \
false; \
fi
cd ${.CURDIR}/share/mk && exec ${MAKE} install
-   exec ${MAKE} includes
exec ${MAKE} cleandir
+   exec ${MAKE} includes
cd ${.CURDIR}/lib && \
su ${BUILDUSER} -c '${MAKE} depend && exec ${MAKE}' && \
NOMAN=1 exec ${MAKE} install
cd ${.CURDIR}/gnu/lib && \
Index: include/Makefile
===
RCS file: /var/cvs/src/include/Makefile,v
retrieving revision 1.213
diff -u -p -U4 -r1.213 Makefile
--- include/Makefile23 Sep 2016 09:18:05 -  1.213
+++ include/Makefile15 Oct 2016 21:43:45 -
@@ -28,16 +28,16 @@ DIRS=   arpa protocols rpc rpcsvc
 LDIRS= crypto ddb dev isofs miscfs msdosfs net netinet netinet6 \
netmpls net80211 nfs ntfs scsi sys ufs uvm
 
 # Places that need a prerequisite target met before includes
-PRDIRS=
+PRDIRS=../lib/libcrypto ../lib/librpcsvc ../sys/arch/${MACHINE}/stand
 
 # Directories with an includes target
-RDIRS= ../lib/libcrypto ../lib/libcurses ../lib/libedit \
+RDIRS= ../lib/libcurses ../lib/libedit \
../lib/libevent ../lib/libexpat ../lib/libform \
../lib/libfuse ../lib/libkeynote ../lib/libmenu \
../lib/libossaudio ../lib/libpanel ../lib/libpcap \
-   ../lib/libradius ../lib/librpcsvc ../lib/librthread \
+   ../lib/libradius ../lib/librthread \
../lib/libskey ../lib/libssl  ../lib/libtls \
../lib/libusbhid ../lib/libutil ../lib/libz \
../usr.bin/lex ../gnu/lib/libreadline \
../sys/arch/${MACHINE}
@@ -46,10 +46,9 @@ RDIRS=   ../lib/libcrypto ../lib/libcurses
 RDIRS+= ../gnu/usr.bin/gcc ../gnu/lib/libobjc
 PRDIRS+= ../gnu/lib/libstdc++
 .else
 RDIRS+= ../gnu/usr.bin/cc/libobjc
-PRDIRS+= ../gnu/lib/libstdc++-v3
-RDIRS+= ../gnu/usr.bin/cc/include
+PRDIRS+= ../gnu/lib/libstdc++-v3 ../gnu/usr.bin/cc/include
 .endif
 
 # prereq implies includes
 RDIRS += ${PRDIRS}
Index: lib/libcrypto/Makefile
===
RCS file: /var/cvs/src/lib/libcrypto/Makefile,v
retrieving revision 1.7
diff -u -p -U4 -r1.7 Makefile
--- lib/libcrypto/Makefile  23 Sep 2016 21:49:30 -  1.7
+++ lib/libcrypto/Makefile  15 Oct 2016 14:19:17 -
@@ -372,9 +372,11 @@ HDRS=\
 HDRS_GEN=\
${.CURDIR}/arch/${MACHINE_CPU}/opensslconf.h \
${.OBJDIR}/obj_mac.h
 
-includes: obj_mac.h
+prereq: obj_mac.h
+
+includes: prereq
@test -d ${DESTDIR}/usr/include/openssl || \
mkdir ${DESTDIR}/usr/include/openssl
@for i in $(HDRS); do \
j="cmp -s $$i ${DESTDIR}/usr/include/openssl/`basename $$i` || \
Index: lib/librpcsvc/Makefile
===
RCS file: /var/cvs/src/lib/librpcsvc/Makefile,v
retrieving revision 1.10
diff -u -p -U4 -r1.10 Makefile
--- lib/librpcsvc/Makefile  2 Aug 2012 13:38:39 -   1.10
+++ lib/librpcsvc/Makefile  15 Oct 2016 14:24:39 -
@@ -12,8 +12,9 @@ LIB=  rpcsvc
 NOMAN=
 
 all:   ${HDRS}
 beforedepend:  ${HDRS}
+prereq:${HDRS}
 
 includes:  ${HDRS}
${INSTALL} -d -o ${BINOWN} -g ${BINGRP} -m 755 \
${DESTDIR}/usr/include/rpcsvc
Index: share/mk/bsd.subdir.mk
===
RCS file: /var/cvs/src/share/mk/bsd.subdir.mk,v
retrieving revision 1.22
diff -u -p -U4 -r1.22 bsd.subdir.mk
--- share/mk/bsd.subdir.mk  8 Oct 2016 09:43:46 -   1.22
+++ share/mk/bsd.subdir.mk  16 Oct 2016 00:25:59 -
@@ -75,9 +75,9 @@ afterinstall: realinstall
 realinstall: beforeinstall _SUBDIRUSE
 .endif
 
 
-.for t in all cleandir includes depend

Re: config(8): use {err,warn}{,x} instead of fprintf(stderr, ...)

2016-10-16 Thread Theo Buehler
On Sun, Oct 16, 2016 at 11:47:32AM +0200, Theo Buehler wrote:
> Many files already include  and there's a mix of hand-rolled
> warning messages and there's incorrect usage warn("config: ..."). This
> is a first sweep at unifying them.
> 
> In mkheaders.c, there is an err() function, rename it to emitwarn()
> since there are emit* functions already and it is non-fatal.

Here's a slightly improved version: I failed to remove a few newlines in
warning/error strings in main.c.

Index: main.c
===
RCS file: /cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.55
diff -u -p -r1.55 main.c
--- main.c  16 Oct 2016 09:36:46 -  1.55
+++ main.c  16 Oct 2016 11:25:20 -
@@ -75,7 +75,7 @@ static void optiondelta(void);
 
 intverbose;
 
-void
+__dead void
 usage(void)
 {
extern char *__progname;
@@ -169,19 +169,15 @@ main(int argc, char *argv[])
 
if (eflag) {
 #ifdef MAKE_BOOTSTRAP
-   fprintf(stderr, "config: UKC not available in this binary\n");
-   exit(1);
+   errx(1, "UKC not available in this binary");
 #else
return (ukc(argv[0], outfile, uflag, fflag));
 #endif
}
 
conffile = (argc == 1) ? argv[0] : "CONFIG";
-   if (firstfile(conffile)) {
-   (void)fprintf(stderr, "config: cannot read %s: %s\n",
-   conffile, strerror(errno));
-   exit(2);
-   }
+   if (firstfile(conffile))
+   err(2, "cannot read %s", conffile);
 
/*
 * Init variables.
@@ -246,8 +242,7 @@ main(int argc, char *argv[])
defmaxusers);
maxusers = defmaxusers;
} else {
-   (void)fprintf(stderr,
-   "config: need \"maxusers\" line\n");
+   warnx("need \"maxusers\" line");
errors++;
}
}
@@ -269,7 +264,7 @@ main(int argc, char *argv[])
mkioconf())
stop();
optiondelta();
-   exit(0);
+   return (0);
 }
 
 static int
@@ -278,11 +273,11 @@ mksymlink(const char *value, const char 
int ret = 0;
 
if (remove(path) && errno != ENOENT) {
-   warn("config: remove(%s)", path);
+   warn("remove(%s)", path);
ret = 1;
}
if (symlink(value, path)) {
-   warn("config: symlink(%s -> %s)", path, value);
+   warn("symlink(%s -> %s)", path, value);
ret = 1;
}
return (ret);
@@ -624,8 +619,7 @@ badstar(void)
continue;
foundstar:
if (ht_lookup(needcnttab, d->d_name)) {
-   (void)fprintf(stderr,
-   "config: %s's cannot be *'d until its driver is fixed\n",
+   warnx("%s's cannot be *'d until its driver is fixed",
d->d_name);
errs++;
continue;
@@ -662,26 +656,14 @@ setupdirs(void)
builddir = defbuilddir;
 
if (stat(builddir, &st) != 0) {
-   if (mkdir(builddir, 0777)) {
-   (void)fprintf(stderr, "config: cannot create %s: %s\n",
-   builddir, strerror(errno));
-   exit(2);
-   }
-   } else if (!S_ISDIR(st.st_mode)) {
-   (void)fprintf(stderr, "config: %s is not a directory\n",
-   builddir);
-   exit(2);
-   }
-   if (chdir(builddir) != 0) {
-   (void)fprintf(stderr, "config: cannot change to %s\n",
-   builddir);
-   exit(2);
-   }
-   if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
-   (void)fprintf(stderr, "config: %s is not a directory\n",
-   srcdir);
-   exit(2);
-   }
+   if (mkdir(builddir, 0777))
+   err(2, "cannot create %s:", builddir);
+   } else if (!S_ISDIR(st.st_mode))
+   errx(2, "%s is not a directory", builddir);
+   if (chdir(builddir) != 0)
+   errx(2, "cannot change to %s", builddir);
+   if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode))
+   errx(2, "%s is not a directory", srcdir);
 
if (bflag) {
if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
@@ -693,14 +675,10 @@ setupdirs(void)
goto reconfig;
 
fp = fopen("Makefile", "w");
-   if (!fp) {
-   (void)fprintf(stderr, "config: cannot create Makefile\n");
-   exit(2);
-   }
-   if (fprintf(fp, ".include \"../Makefile.inc\"\n") < 0) {
-   (void)fprintf(stderr, "config: cannot create Makefile\n");
-   exit(2);
-   }
+   if (!fp)
+   errx(2, "cannot create Makefile");
+   if

malloc canaries for > page sized objects

2016-10-16 Thread Otto Moerbeek
Hi,

this diff is somewhat big since I decided to rewrite wrterror() to be
able to get better error messages.

Please review and test this with malloc option C (and other flags). An
example run:

a.out(85360) in free(): chunk canary corrupted 0x13c68f696000 0x18a92@0x18a88

This means I overwrote a byte at offset 0x18a92 in a chunk of size
0x18a88 that is located at 0x13c68f696000.

Only max 32 bytes after the requested size are filled with 0xdb and
checked on free. 

-Otto

Index: malloc.c
===
RCS file: /cvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.202
diff -u -p -r1.202 malloc.c
--- malloc.c15 Oct 2016 18:24:40 -  1.202
+++ malloc.c16 Oct 2016 11:07:45 -
@@ -31,6 +31,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -199,6 +200,8 @@ static union {
 char   *malloc_options;/* compile-time options */
 
 static u_char getrbyte(struct dir_info *d);
+static __dead void wrterror(struct dir_info *d, char *msg, ...)
+__attribute__((__format__ (printf, 2, 3)));
 
 #ifdef MALLOC_STATS
 void malloc_dump(int, struct dir_info *);
@@ -261,40 +264,26 @@ struct dir_info *getpool(void) 
 }
 
 static __dead void
-wrterror(struct dir_info *d, char *msg, void *p)
+wrterror(struct dir_info *d, char *msg, ...)
 {
-   char*q = " error: ";
-   struct ioveciov[7];
-   charpidbuf[20];
-   charbuf[20];
-   int saved_errno = errno, i;
-
-   iov[0].iov_base = __progname;
-   iov[0].iov_len = strlen(__progname);
-   iov[1].iov_base = pidbuf;
-   snprintf(pidbuf, sizeof(pidbuf), "(%d) in ", getpid());
-   iov[1].iov_len = strlen(pidbuf);
-   if (d != NULL) {
-   iov[2].iov_base = d->func;
-   iov[2].iov_len = strlen(d->func);
-   } else {
-   iov[2].iov_base = "unknown";
-   iov[2].iov_len = 7;
-   }
-   iov[3].iov_base = q;
-   iov[3].iov_len = strlen(q);
-   iov[4].iov_base = msg;
-   iov[4].iov_len = strlen(msg);
-   iov[5].iov_base = buf;
-   if (p == NULL)
-   iov[5].iov_len = 0;
-   else {
-   snprintf(buf, sizeof(buf), " %010p", p);
-   iov[5].iov_len = strlen(buf);
-   }
-   iov[6].iov_base = "\n";
-   iov[6].iov_len = 1;
-   writev(STDERR_FILENO, iov, 7);
+   struct ioveciov[3];
+   charpidbuf[80];
+   charbuf[80];
+   int saved_errno = errno;
+   va_list ap;
+
+   iov[0].iov_base = pidbuf;
+   snprintf(pidbuf, sizeof(pidbuf), "%s(%d) in %s(): ", __progname,
+   getpid(), d->func ? d->func : "unknown");
+   iov[0].iov_len = strlen(pidbuf);
+   iov[1].iov_base = buf;
+   va_start(ap, msg);
+   vsnprintf(buf, sizeof(buf), msg, ap);
+   va_end(ap);
+   iov[1].iov_len = strlen(buf);
+   iov[2].iov_base = "\n";
+   iov[2].iov_len = 1;
+   writev(STDERR_FILENO, iov, 3);
 
 #ifdef MALLOC_STATS
if (mopts.malloc_stats)
@@ -342,12 +331,12 @@ unmap(struct dir_info *d, void *p, size_
u_int i, offset;
 
if (sz != PAGEROUND(sz))
-   wrterror(d, "munmap round", NULL);
+   wrterror(d, "munmap round");
 
if (psz > mopts.malloc_cache) {
i = munmap(p, sz);
if (i)
-   wrterror(d, "munmap", p);
+   wrterror(d, "munmap %p", p);
STATS_SUB(d->malloc_used, sz);
return;
}
@@ -361,7 +350,7 @@ unmap(struct dir_info *d, void *p, size_
if (r->p != NULL) {
rsz = r->size << MALLOC_PAGESHIFT;
if (munmap(r->p, rsz))
-   wrterror(d, "munmap", r->p);
+   wrterror(d, "munmap %p", r->p);
r->p = NULL;
if (tounmap > r->size)
tounmap -= r->size;
@@ -373,7 +362,7 @@ unmap(struct dir_info *d, void *p, size_
}
}
if (tounmap > 0)
-   wrterror(d, "malloc cache underflow", NULL);
+   wrterror(d, "malloc cache underflow");
for (i = 0; i < mopts.malloc_cache; i++) {
r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)];
if (r->p == NULL) {
@@ -393,9 +382,9 @@ unmap(struct dir_info *d, void *p, size_
}
}
if (i == mopts.malloc_cache)
-   wrterror(d, "malloc free slot lost", NULL);
+   wrterror(d, "malloc free slot lost");
if (d->free_regions_size > mopts.malloc_cache)
-   wrterror(d, "malloc cache overflow", NULL);
+   wrterror(d, "malloc cache overflow");
 }
 
 static void
@@ -410,7 +399,7 @@ zapcacheregion(struct dir_info *d

Re: config(8): use {err,warn}{,x} instead of fprintf(stderr, ...)

2016-10-16 Thread Jeremie Courreges-Anglas
Theo Buehler  writes:

> Many files already include  and there's a mix of hand-rolled
> warning messages and there's incorrect usage warn("config: ..."). This
> is a first sweep at unifying them.
>
> In mkheaders.c, there is an err() function, rename it to emitwarn()
> since there are emit* functions already and it is non-fatal.

ok

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



make obj in config

2016-10-16 Thread Martin Natano
We don't need to re-run 'make obj' when obj exists. Ok?

natano


Index: main.c
===
RCS file: /cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.55
diff -u -p -r1.55 main.c
--- main.c  16 Oct 2016 09:36:46 -  1.55
+++ main.c  16 Oct 2016 09:46:55 -
@@ -703,9 +703,9 @@ setupdirs(void)
}
fclose(fp);
 
-reconfig:
if (system("make obj") != 0)
exit(2);
+reconfig:
if (system("make config") != 0)
exit(2);
exit(0);



config(8): use {err,warn}{,x} instead of fprintf(stderr, ...)

2016-10-16 Thread Theo Buehler
Many files already include  and there's a mix of hand-rolled
warning messages and there's incorrect usage warn("config: ..."). This
is a first sweep at unifying them.

In mkheaders.c, there is an err() function, rename it to emitwarn()
since there are emit* functions already and it is non-fatal.

Index: main.c
===
RCS file: /cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.55
diff -u -p -r1.55 main.c
--- main.c  16 Oct 2016 09:36:46 -  1.55
+++ main.c  16 Oct 2016 09:42:01 -
@@ -75,7 +75,7 @@ static void optiondelta(void);
 
 intverbose;
 
-void
+__dead void
 usage(void)
 {
extern char *__progname;
@@ -169,19 +169,15 @@ main(int argc, char *argv[])
 
if (eflag) {
 #ifdef MAKE_BOOTSTRAP
-   fprintf(stderr, "config: UKC not available in this binary\n");
-   exit(1);
+   errx(1, "UKC not available in this binary");
 #else
return (ukc(argv[0], outfile, uflag, fflag));
 #endif
}
 
conffile = (argc == 1) ? argv[0] : "CONFIG";
-   if (firstfile(conffile)) {
-   (void)fprintf(stderr, "config: cannot read %s: %s\n",
-   conffile, strerror(errno));
-   exit(2);
-   }
+   if (firstfile(conffile))
+   err(2, "cannot read %s\n", conffile);
 
/*
 * Init variables.
@@ -246,8 +242,7 @@ main(int argc, char *argv[])
defmaxusers);
maxusers = defmaxusers;
} else {
-   (void)fprintf(stderr,
-   "config: need \"maxusers\" line\n");
+   warnx("need \"maxusers\" line");
errors++;
}
}
@@ -269,7 +264,7 @@ main(int argc, char *argv[])
mkioconf())
stop();
optiondelta();
-   exit(0);
+   return (0);
 }
 
 static int
@@ -278,11 +273,11 @@ mksymlink(const char *value, const char 
int ret = 0;
 
if (remove(path) && errno != ENOENT) {
-   warn("config: remove(%s)", path);
+   warn("remove(%s)", path);
ret = 1;
}
if (symlink(value, path)) {
-   warn("config: symlink(%s -> %s)", path, value);
+   warn("symlink(%s -> %s)", path, value);
ret = 1;
}
return (ret);
@@ -624,8 +619,7 @@ badstar(void)
continue;
foundstar:
if (ht_lookup(needcnttab, d->d_name)) {
-   (void)fprintf(stderr,
-   "config: %s's cannot be *'d until its driver is fixed\n",
+   warnx("%s's cannot be *'d until its driver is fixed",
d->d_name);
errs++;
continue;
@@ -662,26 +656,14 @@ setupdirs(void)
builddir = defbuilddir;
 
if (stat(builddir, &st) != 0) {
-   if (mkdir(builddir, 0777)) {
-   (void)fprintf(stderr, "config: cannot create %s: %s\n",
-   builddir, strerror(errno));
-   exit(2);
-   }
-   } else if (!S_ISDIR(st.st_mode)) {
-   (void)fprintf(stderr, "config: %s is not a directory\n",
-   builddir);
-   exit(2);
-   }
-   if (chdir(builddir) != 0) {
-   (void)fprintf(stderr, "config: cannot change to %s\n",
-   builddir);
-   exit(2);
-   }
-   if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
-   (void)fprintf(stderr, "config: %s is not a directory\n",
-   srcdir);
-   exit(2);
-   }
+   if (mkdir(builddir, 0777))
+   err(2, "cannot create %s:\n", builddir);
+   } else if (!S_ISDIR(st.st_mode))
+   errx(2, "%s is not a directory\n", builddir);
+   if (chdir(builddir) != 0)
+   errx(2, "cannot change to %s\n", builddir);
+   if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode))
+   errx(2, "%s is not a directory\n", srcdir);
 
if (bflag) {
if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
@@ -693,14 +675,10 @@ setupdirs(void)
goto reconfig;
 
fp = fopen("Makefile", "w");
-   if (!fp) {
-   (void)fprintf(stderr, "config: cannot create Makefile\n");
-   exit(2);
-   }
-   if (fprintf(fp, ".include \"../Makefile.inc\"\n") < 0) {
-   (void)fprintf(stderr, "config: cannot create Makefile\n");
-   exit(2);
-   }
+   if (!fp)
+   errx(2, "cannot create Makefile");
+   if (fprintf(fp, ".include \"../Makefile.inc\"\n") < 0)
+   errx(2, "cannot create Makefile");
fclose(fp);
 
 reconfig:
Index: mkheaders.c
===

remove config -g option

2016-10-16 Thread Martin Natano
The -g option is obsolete (and undocumented) at least since the initial
import in '95. Your finger memory should be updated by now.

Ok to remove?

natano


Index: main.c
===
RCS file: /cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.53
diff -u -p -r1.53 main.c
--- main.c  16 Oct 2016 08:47:17 -  1.53
+++ main.c  16 Oct 2016 09:11:02 -
@@ -109,7 +109,7 @@ main(int argc, char *argv[])
err(1, "pledge");
 
pflag = eflag = uflag = fflag = 0;
-   while ((ch = getopt(argc, argv, "egpfb:s:o:u")) != -1) {
+   while ((ch = getopt(argc, argv, "epfb:s:o:u")) != -1) {
switch (ch) {
 
case 'o':
@@ -127,18 +127,6 @@ main(int argc, char *argv[])
if (!isatty(STDIN_FILENO))
verbose = 1;
break;
-
-   case 'g':
-   /*
-* In addition to DEBUG, you probably wanted to
-* set "options KGDB" and maybe others.  We could
-* do that for you, but you really should just
-* put them in the config file.
-*/
-   (void)fputs(
-   "-g is obsolete (use makeoptions DEBUG=\"-g\")\n",
-   stderr);
-   usage();
 
case 'p':
/*



config(8): printf attributes for custom error functions

2016-10-16 Thread Theo Buehler
Add format attributes for the printf-like custom error functions in
util.c.

ok?

Index: config.h
===
RCS file: /var/cvs/src/usr.sbin/config/config.h,v
retrieving revision 1.28
diff -u -p -r1.28 config.h
--- config.h16 Jan 2015 06:40:16 -  1.28
+++ config.h15 Oct 2016 10:54:37 -
@@ -368,9 +368,12 @@ void   *emalloc(size_t);
 void   *ereallocarray(void *, size_t, size_t);
 void   *ecalloc(size_t, size_t);
 char   *sourcepath(const char *);
-void   error(const char *, ...);   /* immediate errs */
-void   xerror(const char *, int, const char *, ...);   /* delayed errs */
-__dead void panic(const char *, ...);
+void   error(const char *, ...)/* immediate errs */
+   __attribute__((__format__ (printf, 1, 2)));
+void   xerror(const char *, int, const char *, ...)/* delayed errs */
+   __attribute__((__format__ (printf, 3, 4)));
+__dead void panic(const char *, ...)
+   __attribute__((__format__ (printf, 1, 2)));
 struct nvlist *newnv(const char *, const char *, void *, int, struct nvlist *);
 void   nvfree(struct nvlist *);
 void   nvfreel(struct nvlist *);
Index: util.c
===
RCS file: /var/cvs/src/usr.sbin/config/util.c,v
retrieving revision 1.17
diff -u -p -r1.17 util.c
--- util.c  7 Sep 2016 18:36:52 -   1.17
+++ util.c  15 Oct 2016 11:11:35 -
@@ -52,7 +52,8 @@
 
 #include "config.h"
 
-static void vxerror(const char *, int, const char *, va_list);
+static void vxerror(const char *, int, const char *, va_list)
+   __attribute__((__format__ (printf, 3, 0)));
 
 /*
  * Malloc, with abort on error.



config(8) getopt cleanup

2016-10-16 Thread Martin Natano
The declarations for optarg and optind are pulled in from . No
need to declare them again. Ok?

natano


Index: main.c
===
RCS file: /cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.53
diff -u -p -r1.53 main.c
--- main.c  16 Oct 2016 08:47:17 -  1.53
+++ main.c  16 Oct 2016 08:58:07 -
@@ -58,9 +58,6 @@
 intfirstfile(const char *);
 intyyparse(void);
 
-extern char *optarg;
-extern int optind;
-
 static struct hashtab *mkopttab;
 static struct nvlist **nextopt;
 static struct nvlist **nextdefopt;



ffs/msdosfs: Flush cache when updating mount to R/O

2016-10-16 Thread Stefan Fritsch
On Sun, 16 Oct 2016, Stefan Fritsch wrote:
> > * When a R/W mount is updated to R/O. I will send patches for this in a 
> > separate mail.

Part 2: Use it


msdosfs & ffs: flush cache if updating mount to r/o

Other filesystems can be changed later.

ok?

diff --git sys/msdosfs/msdosfs_vfsops.c sys/msdosfs/msdosfs_vfsops.c
index e13b0b1..ae1a6d9 100644
--- sys/msdosfs/msdosfs_vfsops.c
+++ sys/msdosfs/msdosfs_vfsops.c
@@ -64,6 +64,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -128,8 +129,13 @@ msdosfs_mount(struct mount *mp, const char *path, void 
*data,
if (mp->mnt_flag & MNT_FORCE)
flags |= FORCECLOSE;
error = vflush(mp, NULLVP, flags);
-   if (!error)
+   if (!error) {
+   int force = 0;
+
pmp->pm_flags |= MSDOSFSMNT_RONLY;
+   /* may be not supported, ignore error */
+   VOP_IOCTL(pmp->pm_devvp, DIOCCACHESYNC, &force, 
 FWRITE, FSCRED, p);
+   }
}
if (!error && (mp->mnt_flag & MNT_RELOAD))
/* not yet implemented */
diff --git sys/ufs/ffs/ffs_vfsops.c sys/ufs/ffs/ffs_vfsops.c
index 99eaf52..abe5756 100644
--- sys/ufs/ffs/ffs_vfsops.c
+++ sys/ufs/ffs/ffs_vfsops.c
@@ -456,6 +456,16 @@ success:
fs->fs_flags &= ~FS_DOSOFTDEP;
}
ffs_sbupdate(ump, MNT_WAIT);
+   if (ronly) {
+   int force = 0;
+
+   /*
+* Updating mount to readonly. Try a cache flush.
+* Ignore error because the ioctl may not be supported.
+*/
+   VOP_IOCTL(ump->um_devvp, DIOCCACHESYNC, &force,
+   FWRITE, FSCRED, p);
+   }
}
return (0);
 



Add ioctl for disk cache flush

2016-10-16 Thread Stefan Fritsch
On Sun, 16 Oct 2016, Stefan Fritsch wrote:
> * When a R/W mount is updated to R/O. I will send patches for this in a 
> separate mail.

Part 1: Add an ioctl:


add DIOCCACHESYNC

Add an ioctl to tell storage devices to flush their internal caches.
Ported from netbsd by pedro@

OK?

diff --git sys/dev/ata/wd.c sys/dev/ata/wd.c
index 5e2461f..280ec56 100644
--- sys/dev/ata/wd.c
+++ sys/dev/ata/wd.c
@@ -26,7 +26,7 @@
  */
 
 /*-
- * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -138,7 +138,7 @@ void  wdstart(void *);
 void  __wdstart(struct wd_softc*, struct buf *);
 void  wdrestart(void *);
 int   wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
-void  wd_flushcache(struct wd_softc *, int);
+int   wd_flushcache(struct wd_softc *, int);
 void  wd_standby(struct wd_softc *, int);
 
 /* XXX: these should go elsewhere */
@@ -848,6 +848,10 @@ wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, 
struct proc *p)
}
 #endif
 
+   /* XXX pedro: should set AT_WAIT according to force flag */
+   case DIOCCACHESYNC:
+   return wd_flushcache(wd, AT_WAIT);
+
default:
error = wdc_ioctl(wd->drvp, xfer, addr, flag, p);
goto exit;
@@ -1067,13 +1071,13 @@ wd_get_params(struct wd_softc *wd, u_int8_t flags, 
struct ataparams *params)
}
 }
 
-void
+int
 wd_flushcache(struct wd_softc *wd, int flags)
 {
struct wdc_command wdc_c;
 
if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
-   return;
+   return EIO;
bzero(&wdc_c, sizeof(struct wdc_command));
wdc_c.r_command = (wd->sc_flags & WDF_LBA48 ? WDCC_FLUSHCACHE_EXT :
WDCC_FLUSHCACHE);
@@ -1088,20 +1092,18 @@ wd_flushcache(struct wd_softc *wd, int flags)
if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
printf("%s: flush cache command didn't complete\n",
wd->sc_dev.dv_xname);
+   return EIO;
}
-   if (wdc_c.flags & AT_TIMEOU) {
-   printf("%s: flush cache command timeout\n",
-   wd->sc_dev.dv_xname);
+   if (wdc_c.flags & AT_ERROR) {
+   if (wdc_c.r_error == WDCE_ABRT) /* command not supported */
+   return ENODEV;
}
-   if (wdc_c.flags & AT_DF) {
-   printf("%s: flush cache command: drive fault\n",
+   if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
+   printf("%s: flush cache command timeout\n",
wd->sc_dev.dv_xname);
+   return EIO;
}
-   /*
-* Ignore error register, it shouldn't report anything else
-* than COMMAND ABORTED, which means the device doesn't support
-* flush cache
-*/
+   return 0;
 }
 
 void
diff --git sys/scsi/sd.c sys/scsi/sd.c
index 30fb36b..8fe7375 100644
--- sys/scsi/sd.c
+++ sys/scsi/sd.c
@@ -2,7 +2,7 @@
 /* $NetBSD: sd.c,v 1.111 1997/04/02 02:29:41 mycroft Exp $ */
 
 /*-
- * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -96,7 +96,7 @@ int   sd_vpd_block_limits(struct sd_softc *, int);
 intsd_vpd_thin(struct sd_softc *, int);
 intsd_thin_params(struct sd_softc *, int);
 intsd_get_parms(struct sd_softc *, struct disk_parms *, int);
-void   sd_flush(struct sd_softc *, int);
+intsd_flush(struct sd_softc *, int);
 
 void   viscpy(u_char *, u_char *, int);
 
@@ -999,6 +999,15 @@ sdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, 
struct proc *p)
error = sd_ioctl_cache(sc, cmd, (struct dk_cache *)addr);
goto exit;
 
+   case DIOCCACHESYNC:
+   if (!ISSET(flag, FWRITE)) {
+   error = EBADF;
+   goto exit;
+   }
+   if ((sc->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)
+   error = sd_flush(sc, 0);
+   return (error);
+
default:
if (part != RAW_PART) {
error = ENOTTY;
@@ -1876,19 +1885,20 @@ die:
return (SDGP_RESULT_OFFLINE);
 }
 
-void
+int
 sd_flush(struct sd_softc *sc, int flags)
 {
struct scsi_link *link;
struct scsi_xfer *xs;
struct scsi_synchronize_cache *cmd;
+   int error;
 
if (sc->flags & SDF_DYING)
-   return;
+   return (ENXIO);
link = sc->sc_link;
 
if (link->quirks & SDEV_NOSYNCCACHE)
-   return;
+   return (0);
 
/*
 * Issue a SYNCHRONIZE CACHE. Address 0, length 0 means "all remaining
@@ -1899,7 +1909,7 @@ sd_flush(struct sd_softc *

Re: usb disk dirty after every reboot

2016-10-16 Thread Stefan Fritsch
[moving to tech@]

On Tuesday, 20 September 2016 08:03:32 CEST Stefan Fritsch wrote:
> On Tue, 20 Sep 2016, Darren Tucker wrote:
> > On Tue, Sep 20, 2016 at 1:43 AM, Jan Stary  wrote:
> > > This is current/i386 on an ALIX.1E (demsg below).
> > > I have an USB disk connected for /backup.
> > > 
> > > Upon every reboot, the filesystem on that disk is dirty:
> > > WARNING: R/W mount of /backup denied.  Filesystem is not clean - run
> > > fsck
> > 
> > I saw something similar on an APU where the root disk was on
> > (USB-attached) sdcard:
> > http://marc.info/?l=openbsd-misc&m=144237305322074&w=2
> > 
> > It seems to be a race.  There used to be a 4sec pause in the kernel
> > that was removed:
> > 
> > """
> > Remove 4 second delay on reboot/shutdown that was added 8 years
> > ago to "workaround MP timeout/splhigh/scsi race at reboot time".
> > """
> 
> I think before we re-add some arbitrary delays, we should check if we are
> actually sending an explicit cache flush command to the disk controllers.
> I have some code somewhere that does this for umount and mount -ur. I will
> look for it and send it to tech@, but probably not today.

I found a few cases, where we should send a cache flush but don't. 
Unfortunately, none of these cases explain the problem seen by Jan and Darren.

The cases I have found are:

* When a R/W mount is updated to R/O. I will send patches for this in a 
separate mail.

* When a R/W mount is unmounted but there is still another partition from the 
same disk mounted.

* When sync(2)  is called. Though I am not 100% sure if we really want to do a 
cache flush for every sync. Thoughts?

For the usb disk issue, some more debugging is necessary.

Cheers,
Stefan