Source-Changes-HG archive

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

[src/trunk]: src/sbin/mount_qemufwcfg cleanup, knf, remove debugging printf, ...



details:   https://anonhg.NetBSD.org/src/rev/f90ab816312c
branches:  trunk
changeset: 828062:f90ab816312c
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Nov 26 03:06:24 2017 +0000

description:
cleanup, knf, remove debugging printf, homebrew alloc macros, types.

diffstat:

 sbin/mount_qemufwcfg/Makefile  |   9 ++-
 sbin/mount_qemufwcfg/defs.h    |  88 ------------------------------------------
 sbin/mount_qemufwcfg/fwcfg.c   |  46 ++++++++++++---------
 sbin/mount_qemufwcfg/virtdir.c |  78 ++++++++++++++++---------------------
 sbin/mount_qemufwcfg/virtdir.h |  10 +--
 5 files changed, 70 insertions(+), 161 deletions(-)

diffs (truncated from 476 to 300 lines):

diff -r 62d065d8913d -r f90ab816312c sbin/mount_qemufwcfg/Makefile
--- a/sbin/mount_qemufwcfg/Makefile     Sat Nov 25 23:29:43 2017 +0000
+++ b/sbin/mount_qemufwcfg/Makefile     Sun Nov 26 03:06:24 2017 +0000
@@ -1,11 +1,12 @@
-# $NetBSD: Makefile,v 1.1 2017/11/25 23:23:39 jmcneill Exp $
+# $NetBSD: Makefile,v 1.2 2017/11/26 03:06:24 christos Exp $
+
+WARNS= 6
 
 PROG=  mount_qemufwcfg
 SRCS=  fwcfg.c virtdir.c
-DPADD+=        ${LIBREFUSE}
-LDADD= -lrefuse
+DPADD+=        ${LIBREFUSE} ${LIBUTIL}
+LDADD= -lrefuse -lutil
 NOMAN= # defined
-WARNS= 3
 
 CPPFLAGS+=     -D_KERNTYPES
 
diff -r 62d065d8913d -r f90ab816312c sbin/mount_qemufwcfg/defs.h
--- a/sbin/mount_qemufwcfg/defs.h       Sat Nov 25 23:29:43 2017 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-/* $NetBSD: defs.h,v 1.1 2017/11/25 23:23:39 jmcneill Exp $ */
-
-/*
- * Copyright (c) 1999-2005 Alistair Crooks.  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. The name of the author may not be used to endorse or promote
- *    products derived from this software without specific prior written
- *    permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- */
-#ifndef DEFS_H_
-#define DEFS_H_
-
-#include <sys/types.h>
-#include <sys/param.h>
-
-#ifdef HAVE_STDINT_H
-#include <stdint.h>
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define NEWARRAY(type,ptr,size,where,action) do {                      \
-       if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \
-               (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \
-                       where, (unsigned long)(size * sizeof(type)));   \
-               action;                                                 \
-       }                                                               \
-} while( /* CONSTCOND */ 0)
-
-#define RENEW(type,ptr,size,where,action) do {                         \
-       type *_newptr;                                                  \
-       if ((_newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \
-               (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \
-                       where, (unsigned long)(size * sizeof(type)));   \
-               action;                                                 \
-       } else {                                                        \
-               ptr = _newptr;                                          \
-       }                                                               \
-} while( /* CONSTCOND */ 0)
-
-#define NEW(type, ptr, where, action)  NEWARRAY(type, ptr, 1, where, action)
-
-#define FREE(ptr)      (void) free(ptr)
-
-#define ALLOC(type, v, size, c, init, incr, where, action) do {                \
-       uint32_t        _newsize = size;                                \
-       if (size == 0) {                                                \
-               _newsize = init;                                        \
-               NEWARRAY(type, v, _newsize, where ": new", action);     \
-       } else if (c == size) {                                         \
-               _newsize = size + incr;                                 \
-               RENEW(type, v, _newsize, where ": renew", action);      \
-       }                                                               \
-       size = _newsize;                                                \
-} while( /* CONSTCOND */ 0)
-
-/*             (void) memset(&v[size], 0x0, sizeof(type) * incr);      \*/
-
-#define DEFINE_ARRAY(name, type)                                       \
-typedef struct name {                                                  \
-       uint32_t        c;                                              \
-       uint32_t        size;                                           \
-       type           *v;                                              \
-} name
-
-#endif /* !DEFS_H_ */
diff -r 62d065d8913d -r f90ab816312c sbin/mount_qemufwcfg/fwcfg.c
--- a/sbin/mount_qemufwcfg/fwcfg.c      Sat Nov 25 23:29:43 2017 +0000
+++ b/sbin/mount_qemufwcfg/fwcfg.c      Sun Nov 26 03:06:24 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fwcfg.c,v 1.1 2017/11/25 23:23:39 jmcneill Exp $ */
+/* $NetBSD: fwcfg.c,v 1.2 2017/11/26 03:06:24 christos Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: fwcfg.c,v 1.1 2017/11/25 23:23:39 jmcneill Exp $");
+__RCSID("$NetBSD: fwcfg.c,v 1.2 2017/11/26 03:06:24 christos Exp $");
 
 #include <sys/ioctl.h>
 
@@ -93,7 +93,7 @@
        switch (ep->type) {
        case 'f':
                memcpy(st, &fwcfg_virtdir.file, sizeof(*st));
-               st->st_size = ep->tgtlen;
+               st->st_size = (off_t)ep->tgtlen;
                st->st_mode = S_IFREG | fwcfg_file_mask;
                break;
        case 'd':
@@ -101,7 +101,7 @@
                st->st_mode = S_IFDIR | fwcfg_dir_mask;
                break;
        }
-       st->st_ino = virtdir_offset(&fwcfg_virtdir, ep) + 10;
+       st->st_ino = (ino_t)virtdir_offset(&fwcfg_virtdir, ep) + 10;
 
        return 0;
 }
@@ -152,15 +152,15 @@
 
        /* Seek to correct offset */
        while (offset > 0) {
-               const int len = MIN(sizeof(tmp), (size_t)offset);
+               const size_t len = MIN(sizeof(tmp), (size_t)offset);
                read_data(tmp, len);
-               offset -= len;
+               offset -= (off_t)len;
        }
 
        /* Read the data */
        read_data(buf, size);
 
-       return size;
+       return (int)size;
 }
 
 static int
@@ -198,8 +198,6 @@
        st.st_gid = fwcfg_gid;
        virtdir_init(v, NULL, &st, &st, &st);
 
-printf("init with uid = %d, gid = %d\n", st.st_uid, st.st_gid);
-
        set_index(FW_CFG_FILE_DIR);
        read_data(&count, sizeof(count));
        for (n = 0; n < be32toh(count); n++) {
@@ -210,11 +208,20 @@
        }
 }
 
+static __dead void
+usage(void)
+{
+       fprintf(stderr, "Usage: %s [-F <path>] [-g <gid>] [-m <file-mode>] "
+           "[-M <dir-mode>] [-u <uid>] [<fuse-options>]", getprogname());
+       exit(EXIT_FAILURE);
+}
+
 int
 main(int argc, char *argv[])
 {
        const char *path = _PATH_FWCFG;
-       int ch, m;
+       int ch;
+       long m;
        char *ep;
 
        fwcfg_uid = geteuid();
@@ -226,23 +233,27 @@
                        path = optarg;
                        break;
                case 'g':
-                       fwcfg_gid = atoi(optarg);
+                       fwcfg_gid = (gid_t)atoi(optarg);
                        break;
                case 'm':
                        m = strtol(optarg, &ep, 8);
                        if (optarg == ep || *ep || m < 0)
-                               errx(1, "invalid file mode: %s", optarg);
-                       fwcfg_file_mask = m;
+                               errx(EXIT_FAILURE, "invalid file mode: %s",
+                                   optarg);
+                       fwcfg_file_mask = (mode_t)m;
                        break;
                case 'M':
                        m = strtol(optarg, &ep, 8);
                        if (optarg == ep || *ep || m < 0)
-                               errx(1, "invalid file mode: %s", optarg);
-                       fwcfg_dir_mask = m;
+                               errx(EXIT_FAILURE, "invalid directory mode: %s",
+                                   optarg);
+                       fwcfg_dir_mask = (mode_t)m;
                        break;
                case 'u':
-                       fwcfg_uid = atoi(optarg);
+                       fwcfg_uid = (uid_t)atoi(optarg);
                        break;
+               default:
+                       usage();
                }
        }
 
@@ -252,8 +263,5 @@
 
        build_tree(&fwcfg_virtdir);
 
-       for (int i = 0; i < argc; i++)
-               printf("argv[%d] = \"%s\"\n", i, argv[i]);
-
        return fuse_main(argc, argv, &fwcfg_ops, NULL);
 }
diff -r 62d065d8913d -r f90ab816312c sbin/mount_qemufwcfg/virtdir.c
--- a/sbin/mount_qemufwcfg/virtdir.c    Sat Nov 25 23:29:43 2017 +0000
+++ b/sbin/mount_qemufwcfg/virtdir.c    Sun Nov 26 03:06:24 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: virtdir.c,v 1.1 2017/11/25 23:23:39 jmcneill Exp $ */
+/* $NetBSD: virtdir.c,v 1.2 2017/11/26 03:06:24 christos Exp $ */
 
 /*
  * Copyright © 2007 Alistair Crooks.  All rights reserved.
@@ -29,40 +29,27 @@
  */
 
 #include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "virtdir.h"
-#include "defs.h"
 
  /* utility comparison routine for sorting and searching */
 static int
 compare(const void *vp1, const void *vp2)
 {
-       const virt_dirent_t     *tp1 = (const virt_dirent_t *) vp1;
-       const virt_dirent_t     *tp2 = (const virt_dirent_t *) vp2;
+       const virt_dirent_t *tp1 = (const virt_dirent_t *) vp1;
+       const virt_dirent_t *tp2 = (const virt_dirent_t *) vp2;
 
        return strcmp(tp1->name, tp2->name);
 }
 
-/* save `n' chars of `s' in allocated storage */
-static char *
-strnsave(const char *s, int n)
-{
-       char    *cp;
-
-       if (n < 0) {
-               n = strlen(s);
-       }
-       NEWARRAY(char, cp, n + 1, "strnsave", return NULL);
-       (void) memcpy(cp, s, n);
-       cp[n] = 0x0;
-       return cp;
-}
-
 /* ensure intermediate directories exist */
 static void
 mkdirs(virtdir_t *tp, const char *path, size_t size)
@@ -82,22 +69,24 @@
 }
 
 /* get rid of multiple slashes in input */



Home | Main Index | Thread Index | Old Index