Source-Changes-HG archive

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

[src/trunk]: src/sys/lib/libsa Introduce fdloadfile() to load an image from a...



details:   https://anonhg.NetBSD.org/src/rev/eeae3a581fa7
branches:  trunk
changeset: 543397:eeae3a581fa7
user:      pk <pk%NetBSD.org@localhost>
date:      Mon Feb 24 10:51:05 2003 +0000

description:
Introduce fdloadfile() to load an image from an open file descriptor;
implement loadfile() in terms of it.

This allows clients to open a file once and "load" it multiple times (e.g.
first with COUNT_KERNEL, then with LOAD_KERNEL) without the side-effects
of multiple open calls.

diffstat:

 sys/lib/libsa/loadfile.c |  47 +++++++++++++++++++++++++++++++++++------------
 sys/lib/libsa/loadfile.h |   3 ++-
 2 files changed, 37 insertions(+), 13 deletions(-)

diffs (100 lines):

diff -r 3d1f160feca3 -r eeae3a581fa7 sys/lib/libsa/loadfile.c
--- a/sys/lib/libsa/loadfile.c  Mon Feb 24 10:18:38 2003 +0000
+++ b/sys/lib/libsa/loadfile.c  Mon Feb 24 10:51:05 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: loadfile.c,v 1.21 2002/12/11 09:55:20 pk Exp $ */
+/* $NetBSD: loadfile.c,v 1.22 2003/02/24 10:51:05 pk Exp $ */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -104,6 +104,35 @@
        u_long *marks;
        int flags;
 {
+       int fd, error;
+
+       /* Open the file. */
+       if ((fd = open(fname, 0)) < 0) {
+               WARN(("open %s", fname ? fname : "<default>"));
+               return (-1);
+       }
+
+       /* Load it; save the value of errno across the close() call */
+       if ((error = fdloadfile(fd, marks, flags)) != 0) {
+               (void)close(fd);
+               errno = error;
+               return (-1);
+       }
+
+       return (fd);
+}
+
+/*
+ * Read in program from the given file descriptor.
+ * Return error code (0 on success).
+ * Fill in marks.
+ */
+int
+fdloadfile(fd, marks, flags)
+       int fd;
+       u_long *marks;
+       int flags;
+{
        union {
 #ifdef BOOT_ECOFF
                struct ecoff_exechdr coff;
@@ -119,15 +148,11 @@
 #endif
        } hdr;
        ssize_t nr;
-       int fd, rval;
-
-       /* Open the file. */
-       if ((fd = open(fname, 0)) < 0) {
-               WARN(("open %s", fname ? fname : "<default>"));
-               return -1;
-       }
+       int rval;
 
        /* Read the exec header. */
+       if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
+               goto err;
        if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
                WARN(("read header"));
                goto err;
@@ -162,16 +187,14 @@
        {
                rval = 1;
                errno = EFTYPE;
-               WARN(("%s", fname ? fname : "<default>"));
        }
 
        if (rval == 0) {
                if ((flags & LOAD_ALL) != 0)
                        PROGRESS(("=0x%lx\n",
                                  marks[MARK_END] - marks[MARK_START]));
-               return fd;
+               return (0);
        }
 err:
-       (void)close(fd);
-       return -1;
+       return (errno);
 }
diff -r 3d1f160feca3 -r eeae3a581fa7 sys/lib/libsa/loadfile.h
--- a/sys/lib/libsa/loadfile.h  Mon Feb 24 10:18:38 2003 +0000
+++ b/sys/lib/libsa/loadfile.h  Mon Feb 24 10:51:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: loadfile.h,v 1.3 2001/10/31 17:20:50 thorpej Exp $      */
+/*     $NetBSD: loadfile.h,v 1.4 2003/02/24 10:51:05 pk Exp $   */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -66,6 +66,7 @@
 #define COUNT_ALL      0x3f00
 
 int    loadfile(const char *, u_long *, int);
+int    fdloadfile(int fd, u_long *, int);
 
 #include "machine/loadfile_machdep.h"
 



Home | Main Index | Thread Index | Old Index