home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / amd / fsinfo / wr_atab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-12  |  7.9 KB  |  285 lines

  1. /*
  2.  * Copyright (c) 1989 Jan-Simon Pendry
  3.  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Jan-Simon Pendry at Imperial College, London.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  *
  38.  *    @(#)wr_atab.c    5.3 (Berkeley) 5/12/91
  39.  *
  40.  * $Id: wr_atab.c,v 5.2.1.3 91/05/07 22:19:09 jsp Alpha $
  41.  *
  42.  */
  43.  
  44. #include "../fsinfo/fsinfo.h"
  45.  
  46. /*
  47.  * Write a sequence of automount mount map entries
  48.  */
  49. static int write_amount_info(af, ap, sk)
  50. FILE *af;
  51. automount *ap;
  52. int sk;
  53. {
  54.     int errors = 0;
  55.     if (ap->a_mount) {
  56.         /*
  57.          * A pseudo-directory.
  58.          * This can also be a top-level directory, in which
  59.          * case the type:=auto is not wanted...
  60.          *
  61.          * type:=auto;fs:=${map};pref:=whatever/
  62.          */
  63.         automount *ap2;
  64.         if (strlen(ap->a_name) > sk) {
  65.             fprintf(af, "%s type:=auto;fs:=${map};pref:=%s/\n",
  66.                 ap->a_name + sk, ap->a_name + sk);
  67.         }
  68.         ITER(ap2, automount, ap->a_mount)
  69.             errors += write_amount_info(af, ap2, sk);
  70.     } else if (ap->a_mounted) {
  71.         /*
  72.          * A mounted partition
  73.          * type:=link [ link entries ] type:=nfs [ nfs entries ]
  74.          */
  75.         dict_data *dd;
  76.         dict_ent *de = ap->a_mounted;
  77.         int done_type_link = 0;
  78.         char *key = ap->a_name + sk;
  79.  
  80.         /*
  81.          * Output the map key
  82.          */
  83.         fputs(key, af);
  84.  
  85.         /*
  86.          * First output any Link locations that would not
  87.          * otherwise be correctly mounted.  These refer
  88.          * to filesystem which are not mounted in the same
  89.          * place which the automounter would use.
  90.          */
  91.         ITER(dd, dict_data, &de->de_q) {
  92.             mount *mp = (mount *) dd->dd_data;
  93.             /*
  94.              * If the mount point and the exported volname are the
  95.              * same then this filesystem will be recognised by
  96.              * the restart code - so we don't need to put out a
  97.              * special rule for it.
  98.              */
  99.             if (mp->m_dk->d_host->h_lochost) {
  100.                 char amountpt[1024];
  101.                 compute_automount_point(amountpt, mp->m_dk->d_host, mp->m_exported->m_volname);
  102.                 if (strcmp(mp->m_dk->d_mountpt, amountpt) != 0) {
  103.                     /*
  104.                      * ap->a_volname is the name of the aliased volume
  105.                      * mp->m_name is the mount point of the filesystem
  106.                      * mp->m_volname is the volume name of the filesystems
  107.                      */
  108.  
  109.                     /*
  110.                      * Find length of key and volume names
  111.                      */
  112.                     int avlen = strlen(ap->a_volname);
  113.                     int mnlen = strlen(mp->m_volname);
  114.                     /*
  115.                      * Make sure a -type:=link is output once
  116.                      */
  117.                     if (!done_type_link) {
  118.                         done_type_link = 1;
  119.                         fputs(" -type:=link", af);
  120.                     }
  121.                     /*
  122.                      * Output a selector for the hostname,
  123.                      * the device from which to mount and
  124.                      * where to mount.  This will correspond
  125.                      * to the values output for the fstab.
  126.                      */
  127.                     if (mp->m_dk->d_host->h_lochost)
  128.                         fprintf(af, " host==%s", mp->m_dk->d_host->h_lochost);
  129.                     else
  130.                         fprintf(af, " hostd==%s", mp->m_dk->d_host->h_hostname);
  131.                     fprintf(af, ";fs:=%s", mp->m_name);
  132.                     /*
  133.                      * ... and a sublink if needed
  134.                      */
  135.                     if (mnlen < avlen) {
  136.                         char *sublink = ap->a_volname + mnlen + 1;
  137.                         fprintf(af, "/%s", sublink);
  138.                     }
  139.                     fputs(" ||", af);
  140.                 }
  141.             }
  142.         }
  143.  
  144.         /*
  145.          * Next do the NFS locations
  146.          */
  147.  
  148.         if (done_type_link)
  149.             fputs(" -", af);
  150.  
  151.         ITER(dd, dict_data, &de->de_q) {
  152.             mount *mp = (mount *) dd->dd_data;
  153.             int namelen = mp->m_name_len;
  154.             int exp_namelen = mp->m_exported->m_name_len;
  155.             int volnlen = strlen(ap->a_volname);
  156.             int mvolnlen = strlen(mp->m_volname);
  157.             fputc(' ', af);
  158. #ifdef notdef
  159.             fprintf(af, "\\\n /* avolname = %s, mname = %s,\n  * mvolname = %s, mexp_name = %s,\n  * mexp_volname = %s\n  */\\\n",
  160.                 ap->a_volname, mp->m_name, mp->m_volname, mp->m_exported->m_name, mp->m_exported->m_volname);
  161. #endif
  162.             /*
  163.              * Output any selectors
  164.              */
  165.             if (mp->m_sel)
  166.                 fprintf(af, "%s;", mp->m_sel);
  167.             /*
  168.              * Print host and volname of exported filesystem
  169.              */
  170.             fprintf(af, "rhost:=%s",
  171.                 mp->m_dk->d_host->h_lochost ?
  172.                 mp->m_dk->d_host->h_lochost :
  173.                 mp->m_dk->d_host->h_hostname);
  174.             fprintf(af, ";rfs:=%s", mp->m_exported->m_volname);
  175.             /*
  176.              * Now determine whether a sublink is required.
  177.              */
  178.             if (exp_namelen < namelen || mvolnlen < volnlen) {
  179.                 char sublink[1024];
  180.                 sublink[0] = '\0';
  181.                 if (exp_namelen < namelen) {
  182.                     strcat(sublink, mp->m_name + exp_namelen + 1);
  183.                     if (mvolnlen < volnlen)
  184.                         strcat(sublink, "/");
  185.                 }
  186.                 if (mvolnlen < volnlen)
  187.                     strcat(sublink, ap->a_volname + mvolnlen + 1);
  188.  
  189.                 fprintf(af, ";sublink:=%s", sublink);
  190.             }
  191.         }
  192.         fputc('\n', af);
  193.     } else if (ap->a_symlink) {
  194.         /*
  195.          * A specific link.
  196.          *
  197.          * type:=link;fs:=whatever
  198.          */
  199.         fprintf(af, "%s type:=link;fs:=%s\n", ap->a_name + sk, ap->a_symlink);
  200.     }
  201.     return errors;
  202. }
  203.  
  204. /*
  205.  * Write a single automount configuration file
  206.  */
  207. static int write_amount(q, def)
  208. qelem *q;
  209. char *def;
  210. {
  211.     automount *ap;
  212.     int errors = 0;
  213.     int direct = 0;
  214.  
  215.     /*
  216.      * Output all indirect maps
  217.      */
  218.     ITER(ap, automount, q) {
  219.         FILE *af;
  220.         char *p;
  221.         /*
  222.          * If there is no a_mount node then this is really
  223.          * a direct mount, so just keep a count and continue.
  224.          * Direct mounts are output into a special file during
  225.          * the second pass below.
  226.          */
  227.         if (!ap->a_mount) {
  228.             direct++;
  229.             continue;
  230.         }
  231.         p = strrchr(ap->a_name, '/');
  232.         if (!p) p = ap->a_name;
  233.         else p++;
  234.         af = pref_open(mount_pref, p, gen_hdr, ap->a_name);
  235.         if (af) {
  236.             show_new(ap->a_name);
  237.             fputs("/defaults ", af);
  238.             if (*def)
  239.                 fprintf(af, "%s;", def);
  240.             fputs("type:=nfs\n", af);
  241.             errors += write_amount_info(af, ap, strlen(ap->a_name) + 1);
  242.             errors += pref_close(af);
  243.         }
  244.     }
  245.  
  246.     /*
  247.      * Output any direct map entries which were found during the
  248.      * previous pass over the data.
  249.      */
  250.     if (direct) {
  251.         FILE *af = pref_open(mount_pref, "direct.map", info_hdr, "direct mount");
  252.         if (af) {
  253.             show_new("direct mounts");
  254.             fputs("/defaults ", af);
  255.             if (*def)
  256.                 fprintf(af, "%s;", def);
  257.             fputs("type:=nfs\n", af);
  258.             ITER(ap, automount, q)
  259.                 if (!ap->a_mount)
  260.                     errors += write_amount_info(af, ap, 1);
  261.             errors += pref_close(af);
  262.         }
  263.     }
  264.     
  265.     return errors;
  266. }
  267.  
  268. /*
  269.  * Write all the needed automount configuration files
  270.  */
  271. write_atab(q)
  272. qelem *q;
  273. {
  274.     int errors = 0;
  275.  
  276.     if (mount_pref) {
  277.         auto_tree *tp;
  278.         show_area_being_processed("write automount", "");
  279.         ITER(tp, auto_tree, q)
  280.             errors += write_amount(tp->t_mount, tp->t_defaults);
  281.     }
  282.  
  283.     return errors;
  284. }
  285.