Add error handling if setting the keyboard encoding fails. After
authorbluhm <bluhm@openbsd.org>
Thu, 5 May 2022 16:12:42 +0000 (16:12 +0000)
committerbluhm <bluhm@openbsd.org>
Thu, 5 May 2022 16:12:42 +0000 (16:12 +0000)
open of all /dev/wskbd* devices failed, report the error from the
first one.  Also wrap long lines.
OK mpi@

sbin/kbd/kbd_wscons.c

index 539d53c..c7437f6 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: kbd_wscons.c,v 1.35 2022/04/17 17:33:50 bluhm Exp $ */
+/*     $OpenBSD: kbd_wscons.c,v 1.36 2022/05/05 16:12:42 bluhm Exp $ */
 
 /*
  * Copyright (c) 2001 Mats O Jansson.  All rights reserved.
@@ -232,7 +232,7 @@ void
 kbd_set(char *name, int verbose)
 {
        char    buf[LINE_MAX], *c, *b, device[sizeof "/dev/wskbd00"];
-       int     map = 0, v, i, fd;
+       int     map = 0, v, i, fd, error = 0;
        struct nameint *n;
 
        c = name;
@@ -271,20 +271,30 @@ kbd_set(char *name, int verbose)
                fd = open(device, O_WRONLY);
                if (fd == -1)
                        fd = open(device, O_RDONLY);
-               if (fd >= 0) {
+               if (fd == -1) {
+                       /* remember the first error number */
+                       if (error == 0)
+                               error = errno;
+               } else {
+                       /* at least one success, do not print error */
+                       error = -1;
+
                        if (ioctl(fd, WSKBDIO_SETENCODING, &map) == -1) {
-                               if (errno == EINVAL) {
-                                       fprintf(stderr,
-                                           "%s: unsupported encoding %s on %s\n",
-                                           __progname, name, device);
-                               } else
-                                       err(1, "WSKBDIO_SETENCODING: %s", device);
-                               v--;
-                       }
-                       v++;
+                               if (errno != EINVAL)
+                                       err(1, "WSKBDIO_SETENCODING %s",
+                                           device);
+                               fprintf(stderr,
+                                   "%s: unsupported encoding %s on %s\n",
+                                   __progname, name, device);
+                       } else
+                               v++;
                        close(fd);
                }
        }
+       if (error > 0) {
+               errno = error;
+               err(1, "/dev/wskbd0");
+       }
 
        if (verbose && v > 0)
                fprintf(stderr, "kbd: keyboard mapping set to %s\n", name);