1 /*	$NetBSD: mtab_osf.c,v 1.1.1.3 2015/01/17 16:34:16 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2014 Erez Zadok
5  * Copyright (c) 1990 Jan-Simon Pendry
6  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7  * Copyright (c) 1990 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry at Imperial College, London.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *
38  * File: am-utils/conf/mtab/mtab_osf.c
39  *
40  */
41 
42 #ifdef HAVE_CONFIG_H
43 # include <config.h>
44 #endif /* HAVE_CONFIG_H */
45 #include <am_defs.h>
46 #include <amu.h>
47 
48 
49 static mntent_t *
mnt_dup(struct statfs * mp)50 mnt_dup(struct statfs *mp)
51 {
52   mntent_t *new_mp = ALLOC(mntent_t);
53   char *ty;
54   char *at;
55   char mntfrombuf[MNAMELEN];
56   char *mntfromptr;
57 
58   /*
59    * Under DEC OSF/1 T1.2-2 the f_mntfromname fields of
60    * the statfs structure can be in the format <fs>@<host>
61    * instead of <host>:<fs>.  Here we check for this and
62    * reformat it if necessary.
63    */
64   mntfromptr = mp->f_mntfromname;
65 
66   switch (mp->f_type) {
67   case MOUNT_TYPE_NFS:
68 #ifdef HAVE_FS_NFS3
69   case MOUNT_TYPE_NFS3:
70 #endif /* HAVE_FS_NFS3 */
71     at = strchr(mp->f_mntfromname, '@');
72     if (at != '\0') {
73       xstrlcpy(mntfrombuf, (at + 1), sizeof(mntfrombuf));
74       xstrlcat(mntfrombuf, ":", sizeof(mntfrombuf));
75       strncat(mntfrombuf, mp->f_mntfromname, (at - mp->f_mntfromname));
76       mntfromptr = mntfrombuf;
77     }
78   }
79   new_mp->mnt_fsname = xstrdup(mntfromptr);
80 
81   new_mp->mnt_dir = xstrdup(mp->f_mntonname);
82   switch (mp->f_type) {
83   case MOUNT_TYPE_UFS:
84     ty = MNTTAB_TYPE_UFS;
85     break;
86 #ifdef HAVE_FS_NFS3
87   case MOUNT_TYPE_NFS3:
88     ty = MNTTAB_TYPE_NFS3;
89     break;
90 #endif /* HAVE_FS_NFS3 */
91   case MOUNT_TYPE_NFS:
92     ty = MNTTAB_TYPE_NFS;
93     break;
94   case MOUNT_TYPE_MFS:
95     ty = MNTTAB_TYPE_MFS;
96     break;
97   default:
98     ty = "unknown";
99     break;
100   }
101 
102   new_mp->mnt_type = xstrdup(ty);
103   new_mp->mnt_opts = xstrdup("unset");
104   new_mp->mnt_freq = 0;
105   new_mp->mnt_passno = 0;
106 
107   return new_mp;
108 }
109 
110 
111 /*
112  * Read a mount table into memory
113  */
114 mntlist *
read_mtab(char * fs,const char * mnttabname)115 read_mtab(char *fs, const char *mnttabname)
116 {
117   mntlist **mpp, *mhp;
118   struct statfs *mntbufp, *mntp;
119 
120   int nloc = getmntinfo(&mntbufp, MNT_NOWAIT);
121 
122   if (nloc == 0) {
123     plog(XLOG_ERROR, "Can't read mount table");
124     return 0;
125   }
126   mpp = &mhp;
127   for (mntp = mntbufp; mntp < mntbufp + nloc; mntp++) {
128     /*
129      * Allocate a new slot
130      */
131     *mpp = ALLOC(struct mntlist);
132 
133     /*
134      * Copy the data returned by getmntent
135      */
136     (*mpp)->mnt = mnt_dup(mntp);
137 
138     /*
139      * Move to next pointer
140      */
141     mpp = &(*mpp)->mnext;
142   }
143 
144   /* terminate the linked list */
145   *mpp = NULL;
146 
147   return mhp;
148 }
149