BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / amd / amd / amd.c
index d2ce4b0..f8faa4d 100644 (file)
@@ -1,6 +1,4 @@
 /*
 /*
- * $Id: amd.c,v 5.2 90/06/23 22:19:18 jsp Rel $
- *
  * Copyright (c) 1989 Jan-Simon Pendry
  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  * Copyright (c) 1989 The Regents of the University of California.
  * Copyright (c) 1989 Jan-Simon Pendry
  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  * Copyright (c) 1989 The Regents of the University of California.
@@ -9,21 +7,38 @@
  * This code is derived from software contributed to Berkeley by
  * Jan-Simon Pendry at Imperial College, London.
  *
  * This code is derived from software contributed to Berkeley by
  * Jan-Simon Pendry at Imperial College, London.
  *
- * Redistribution and use in source and binary forms are permitted provided
- * that: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * Neither the name of the University 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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 University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
+ *
+ *     @(#)amd.c       5.3 (Berkeley) 5/12/91
+ *
+ * $Id: amd.c,v 5.2.1.5 91/05/07 22:17:44 jsp Alpha $
  *
  *
- *     @(#)amd.c       5.1 (Berkeley) 6/29/90
  */
 
 /*
  */
 
 /*
@@ -32,7 +47,6 @@
 
 #include "am.h"
 #include <sys/signal.h>
 
 #include "am.h"
 #include <sys/signal.h>
-#include <netdb.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <setjmp.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <setjmp.h>
 char pid_fsname[16 + MAXHOSTNAMELEN];  /* "kiska.southseas.nz:(pid%d)" */
 char *progname;                                /* "amd" */
 #ifdef HAS_HOST
 char pid_fsname[16 + MAXHOSTNAMELEN];  /* "kiska.southseas.nz:(pid%d)" */
 char *progname;                                /* "amd" */
 #ifdef HAS_HOST
+#ifdef HOST_EXEC
 char *host_helper;
 char *host_helper;
+#endif /* HOST_EXEC */
 #endif /* HAS_HOST */
 char *auto_dir = "/a";
 char *hostdomain = "unknown.domain";
 #endif /* HAS_HOST */
 char *auto_dir = "/a";
 char *hostdomain = "unknown.domain";
-char hostname[MAXHOSTNAMELEN];         /* Hostname */
+char hostname[MAXHOSTNAMELEN] = "localhost"; /* Hostname */
 char hostd[2*MAXHOSTNAMELEN];          /* Host+domain */
 char *op_sys = OS_REP;                 /* Name of current op_sys */
 char *arch = ARCH_REP;                 /* Name of current architecture */
 char *endian = ARCH_ENDIAN;            /* Big or Little endian */
 char hostd[2*MAXHOSTNAMELEN];          /* Host+domain */
 char *op_sys = OS_REP;                 /* Name of current op_sys */
 char *arch = ARCH_REP;                 /* Name of current architecture */
 char *endian = ARCH_ENDIAN;            /* Big or Little endian */
+char *wire;
 int foreground = 1;                    /* This is the top-level server */
 int mypid;                             /* Current process id */
 int immediate_abort;                   /* Should close-down unmounts be retried */
 struct in_addr myipaddr;               /* (An) IP address of this host */
 int foreground = 1;                    /* This is the top-level server */
 int mypid;                             /* Current process id */
 int immediate_abort;                   /* Should close-down unmounts be retried */
 struct in_addr myipaddr;               /* (An) IP address of this host */
-serv_state amd_state = Start;
+serv_state amd_state;
 struct amd_stats amd_stats;            /* Server statistics */
 time_t do_mapc_reload = 0;             /* mapc_reload() call required? */
 jmp_buf select_intr;
 struct amd_stats amd_stats;            /* Server statistics */
 time_t do_mapc_reload = 0;             /* mapc_reload() call required? */
 jmp_buf select_intr;
@@ -70,7 +87,7 @@ int sig;
 {
 #ifdef SYS5_SIGNALS
        signal(sig, sigterm);
 {
 #ifdef SYS5_SIGNALS
        signal(sig, sigterm);
-#endif * SYS5_SIGNALS */
+#endif /* SYS5_SIGNALS */
 
        switch (sig) {
        case SIGINT:
 
        switch (sig) {
        case SIGINT:
@@ -93,12 +110,13 @@ int sig;
  * Hook for cache reload.
  * When a SIGHUP arrives it schedules a call to mapc_reload
  */
  * Hook for cache reload.
  * When a SIGHUP arrives it schedules a call to mapc_reload
  */
+/*ARGSUSED*/
 static void sighup(sig)
 int sig;
 {
 #ifdef SYS5_SIGNALS
        signal(sig, sighup);
 static void sighup(sig)
 int sig;
 {
 #ifdef SYS5_SIGNALS
        signal(sig, sighup);
-#endif /* SUS5_SIGNALS */
+#endif /* SYS5_SIGNALS */
 
 #ifdef DEBUG
        if (sig != SIGHUP)
 
 #ifdef DEBUG
        if (sig != SIGHUP)
@@ -111,6 +129,7 @@ int sig;
                do_mapc_reload = 0;
 }
 
                do_mapc_reload = 0;
 }
 
+/*ARGSUSED*/
 static void parent_exit(sig)
 int sig;
 {
 static void parent_exit(sig)
 int sig;
 {
@@ -119,7 +138,10 @@ int sig;
 
 static int daemon_mode(P_void)
 {
 
 static int daemon_mode(P_void)
 {
-       int bgpid = background();
+       int bgpid;
+
+       signal(SIGQUIT, parent_exit);
+       bgpid = background();
 
        if (bgpid != 0) {
                if (print_pid) {
 
        if (bgpid != 0) {
                if (print_pid) {
@@ -130,23 +152,27 @@ static int daemon_mode(P_void)
                 * Now wait for the automount points to
                 * complete.
                 */
                 * Now wait for the automount points to
                 * complete.
                 */
-               signal(SIGQUIT, parent_exit);
                for (;;)
                        pause();
        }
 
                for (;;)
                        pause();
        }
 
+       signal(SIGQUIT, SIG_DFL);
+
        /*
         * Pretend we are in the foreground again
         */
        foreground = 1;
        /*
         * Pretend we are in the foreground again
         */
        foreground = 1;
+
 #ifdef TIOCNOTTY
        {
                int t = open("/dev/tty", O_RDWR);
                if (t < 0) {
                        if (errno != ENXIO)     /* not an error if already no controlling tty */
                                plog(XLOG_WARNING, "Could not open controlling tty: %m");
 #ifdef TIOCNOTTY
        {
                int t = open("/dev/tty", O_RDWR);
                if (t < 0) {
                        if (errno != ENXIO)     /* not an error if already no controlling tty */
                                plog(XLOG_WARNING, "Could not open controlling tty: %m");
-               } else if (ioctl(t, TIOCNOTTY, 0) < 0) {
-                       plog(XLOG_WARNING, "Could not disassociate tty (TIOCNOTTY): %m");
+               } else {
+                       if (ioctl(t, TIOCNOTTY, 0) < 0 && errno != ENOTTY)
+                               plog(XLOG_WARNING, "Could not disassociate tty (TIOCNOTTY): %m");
+                       (void) close(t);
                }
        }
 #else
                }
        }
 #else
@@ -160,7 +186,6 @@ main(argc, argv)
 int argc;
 char *argv[];
 {
 int argc;
 char *argv[];
 {
-       struct hostent *hp, *gethostbyname();
        char *domdot;
        int ppid = 0;
        int error;
        char *domdot;
        int ppid = 0;
        int error;
@@ -176,6 +201,27 @@ char *argv[];
         */
        amd_state = Start;
 
         */
        amd_state = Start;
 
+       /*
+        * Determine program name
+        */
+       if (argv[0]) {
+               progname = strrchr(argv[0], '/');
+               if (progname && progname[1])
+                       progname++;
+               else
+                       progname = argv[0];
+       }
+
+       if (!progname)
+               progname = "amd";
+
+       /*
+        * Initialise process id.  This is kept
+        * cached since it is used for generating
+        * and using file handles.
+        */
+       mypid = getpid();
+
        /*
         * Get local machine name
         */
        /*
         * Get local machine name
         */
@@ -226,31 +272,17 @@ char *argv[];
         */
        (void) signal(SIGCHLD, sigchld);
 
         */
        (void) signal(SIGCHLD, sigchld);
 
-       /*
-        * Initialise process id.  This is kept
-        * cached since it is used for generating
-        * and using file handles.
-        */
-       mypid = getpid();
-
-#ifdef notdef
-/*
- * XXX - Doing this plugs most of a memory leak in
- * gethostbyname on SunOS 4.  I see no good reason
- * why the host database needs to grab 1.5K of
- * private data space...  However, for the moment,
- * I will take its word that it is a _good thing_
- * (jsp)
- */
-       (void) sethostent(0);
-#endif /* notdef */
-
        /*
         * Fix-up any umask problems.  Most systems default
         * to 002 which is not too convenient for our purposes
         */
        orig_umask = umask(0);
 
        /*
         * Fix-up any umask problems.  Most systems default
         * to 002 which is not too convenient for our purposes
         */
        orig_umask = umask(0);
 
+       /*
+        * Figure out primary network name
+        */
+       wire = getwire();
+
        /*
         * Determine command-line arguments
         */
        /*
         * Determine command-line arguments
         */
@@ -258,18 +290,12 @@ char *argv[];
 
        /*
         * Get our own IP address so that we
 
        /*
         * Get our own IP address so that we
-        * can mount the automounter.  There
-        * is probably a better way of doing
-        * this, but messing about with SIOCGIFCONF
-        * seems to be heading towards the non-portable
-        * arena.
+        * can mount the automounter.
         */
         */
-       hp = gethostbyname(hostname);
-       if (!hp || hp->h_addrtype != AF_INET) {
-               plog(XLOG_FATAL, "Can't determine IP address of this host (%s)", hostname);
-               going_down(1);
+       { struct sockaddr_in sin;
+         get_myaddress(&sin);
+         myipaddr.s_addr = sin.sin_addr.s_addr;
        }
        }
-       myipaddr = *(struct in_addr *) hp->h_addr;
 
        /*
         * Now check we are root.
 
        /*
         * Now check we are root.