Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/pkg_install/lib Add a new utility function which us...



details:   https://anonhg.NetBSD.org/src/rev/1291ddb7cde3
branches:  trunk
changeset: 550903:1291ddb7cde3
user:      tron <tron%NetBSD.org@localhost>
date:      Sun Aug 24 21:10:47 2003 +0000

description:
Add a new utility function which use vfork(2) and exec(2) directly instead
of system(3) to run an external program.

diffstat:

 usr.sbin/pkg_install/lib/Makefile |    4 +-
 usr.sbin/pkg_install/lib/fexec.c  |  118 ++++++++++++++++++++++++++++++++++++++
 usr.sbin/pkg_install/lib/lib.h    |    3 +-
 3 files changed, 122 insertions(+), 3 deletions(-)

diffs (157 lines):

diff -r ac668a25335e -r 1291ddb7cde3 usr.sbin/pkg_install/lib/Makefile
--- a/usr.sbin/pkg_install/lib/Makefile Sun Aug 24 21:04:53 2003 +0000
+++ b/usr.sbin/pkg_install/lib/Makefile Sun Aug 24 21:10:47 2003 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.19 2002/09/18 13:31:55 lukem Exp $
+# $NetBSD: Makefile,v 1.20 2003/08/24 21:10:47 tron Exp $
 # Original from FreeBSD, no rcs id.
 
 NOLINT=                # defined
@@ -8,7 +8,7 @@
 
 LIB+=          install
 SRCS+=         exec.c file.c ftpio.c global.c lpkg.c pen.c pkgdb.c \
-               plist.c str.c version.c path.c
+               plist.c str.c version.c path.c fexec.c
 
 # only needed during build - prevent installation of library
 libinstall::
diff -r ac668a25335e -r 1291ddb7cde3 usr.sbin/pkg_install/lib/fexec.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/pkg_install/lib/fexec.c  Sun Aug 24 21:10:47 2003 +0000
@@ -0,0 +1,118 @@
+/*-
+ * Copyright (c) 2003 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Matthias Scheler.
+ *
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+
+#include <sys/cdefs.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "lib.h"
+
+#ifndef lint
+__RCSID("$NetBSD: fexec.c,v 1.1 2003/08/24 21:10:47 tron Exp $");
+#endif
+
+int
+fexec(const char *arg, ...)
+{
+       va_list         ap;
+       int             max, argc, status;
+       const char      **argv;
+       pid_t           child;
+
+       max = 4;
+       argv = malloc(max * sizeof(const char *));
+       if (argv == NULL) {
+               warnx("fexec can't alloc arg space");
+               return -1;
+       }
+       argv[0] = arg;
+       argc = 1;
+
+       va_start(ap, arg);
+       do {
+               if (argc == max) {
+                       const char **ptr;
+
+                       max <<= 1;
+                       ptr = realloc(argv, max * sizeof(const char *));
+                       if (ptr == NULL) {
+                               warnx("fexec can't alloc arg space");
+                               free(argv);
+                               va_end(ap);
+                               return -1;
+                       }
+                       argv = ptr;
+               }
+               arg = va_arg(ap, const char *);
+               argv[argc++] = arg;
+       } while (arg != NULL);
+       va_end(ap);
+
+#ifdef FEXEC_DEBUG
+       (void) printf("fexec(\"%s\"", argv[0]);
+       argc = 1;
+       while (argv[argc] != NULL)
+               (void) printf(", \"%s\"", argv[argc++]);
+       (void) printf(")\n");
+#endif
+
+       child = vfork();
+       if (child == 0) {
+               (void) execvp(argv[0], (char ** const)argv);
+               _exit(127);
+               /* NOTREACHED */
+       }
+
+       free(argv);
+       if (child < 0)
+               return -1;
+
+       while (waitpid(child, &status, 0) < 0) {
+               if (errno != EINTR)
+                       return -1;
+       }
+
+       if (!WIFEXITED(status))
+               return -1;
+
+       return WEXITSTATUS(status);
+}
diff -r ac668a25335e -r 1291ddb7cde3 usr.sbin/pkg_install/lib/lib.h
--- a/usr.sbin/pkg_install/lib/lib.h    Sun Aug 24 21:04:53 2003 +0000
+++ b/usr.sbin/pkg_install/lib/lib.h    Sun Aug 24 21:10:47 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lib.h,v 1.52 2003/04/22 01:17:05 hubertf Exp $ */
+/* $NetBSD: lib.h,v 1.53 2003/08/24 21:10:47 tron Exp $ */
 
 /* from FreeBSD Id: lib.h,v 1.25 1997/10/08 07:48:03 charnier Exp */
 
@@ -214,6 +214,7 @@
 void    save_dirs(char **c, char **p);
 void    restore_dirs(char *c, char *p);
 void    show_version(void);
+int    fexec(const char *, ...);
 
 /* String */
 char   *get_dash_string(char **);



Home | Main Index | Thread Index | Old Index