1*31bdb48aSchristos /*	$NetBSD: mtab_aix.c,v 1.1.1.3 2015/01/17 16:34:16 christos Exp $	*/
2a53f50b9Schristos 
3a53f50b9Schristos /*
4*31bdb48aSchristos  * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos  * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos  * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos  * All rights reserved.
9a53f50b9Schristos  *
10a53f50b9Schristos  * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos  * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos  *
13a53f50b9Schristos  * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos  * modification, are permitted provided that the following conditions
15a53f50b9Schristos  * are met:
16a53f50b9Schristos  * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos  * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos  *    documentation and/or other materials provided with the distribution.
21*31bdb48aSchristos  * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos  *    may be used to endorse or promote products derived from this software
23a53f50b9Schristos  *    without specific prior written permission.
24a53f50b9Schristos  *
25a53f50b9Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos  * SUCH DAMAGE.
36a53f50b9Schristos  *
37a53f50b9Schristos  *
38a53f50b9Schristos  * File: am-utils/conf/mtab/mtab_aix.c
39a53f50b9Schristos  *
40a53f50b9Schristos  */
41a53f50b9Schristos 
42a53f50b9Schristos /*
43a53f50b9Schristos  * AIX systems don't write their mount tables on a file.  Instead, they
44a53f50b9Schristos  * use a (better) system where the kernel keeps this state, and you access
45a53f50b9Schristos  * the mount tables via a known interface.
46a53f50b9Schristos  */
47a53f50b9Schristos 
48a53f50b9Schristos #ifdef HAVE_CONFIG_H
49a53f50b9Schristos # include <config.h>
50a53f50b9Schristos #endif /* HAVE_CONFIG_H */
51a53f50b9Schristos #include <am_defs.h>
52a53f50b9Schristos #include <amu.h>
53a53f50b9Schristos 
54a53f50b9Schristos /*
55a53f50b9Schristos  * These were missing external definitions from old AIX's headers.  They
56a53f50b9Schristos  * appear to be available in <sys/vmount.h> on AIX 5.3, and possibly
57a53f50b9Schristos  * earlier. Hence I commented this out.
58a53f50b9Schristos  */
59a53f50b9Schristos #ifndef HAVE_EXTERN_MNTCTL
60a53f50b9Schristos extern int mntctl(int cmd, int size, voidp buf);
61a53f50b9Schristos #endif /* not HAVE_EXTERN_MNTCTL */
62a53f50b9Schristos 
63a53f50b9Schristos 
64a53f50b9Schristos static mntent_t *
mnt_dup(struct vmount * mp)65a53f50b9Schristos mnt_dup(struct vmount *mp)
66a53f50b9Schristos {
67a53f50b9Schristos   mntent_t *new_mp = ALLOC(mntent_t);
68a53f50b9Schristos   char *ty;
69*31bdb48aSchristos   char *fsname = xstrdup(vmt2dataptr(mp, VMT_OBJECT));
70a53f50b9Schristos 
71a53f50b9Schristos   new_mp->mnt_dir = strdup(vmt2dataptr(mp, VMT_STUB));
72a53f50b9Schristos   new_mp->mnt_opts = strdup(vmt2dataptr(mp, VMT_ARGS));
73a53f50b9Schristos 
74a53f50b9Schristos   switch (mp->vmt_gfstype) {
75a53f50b9Schristos 
76a53f50b9Schristos   case MOUNT_TYPE_UFS:
77a53f50b9Schristos     ty = MNTTAB_TYPE_UFS;
78*31bdb48aSchristos     new_mp->mnt_fsname = xstrdup(fsname);
79a53f50b9Schristos     break;
80a53f50b9Schristos 
81a53f50b9Schristos   case MOUNT_TYPE_NFS:
82a53f50b9Schristos     ty = MNTTAB_TYPE_NFS;
83a53f50b9Schristos     new_mp->mnt_fsname = str3cat((char *) NULL,
84a53f50b9Schristos 				 vmt2dataptr(mp, VMT_HOSTNAME), ":",
85a53f50b9Schristos 				 fsname);
86a53f50b9Schristos     break;
87a53f50b9Schristos 
88a53f50b9Schristos #ifdef HAVE_FS_NFS3
89a53f50b9Schristos   case MOUNT_TYPE_NFS3:
90a53f50b9Schristos     ty = MNTTAB_TYPE_NFS3;
91a53f50b9Schristos     new_mp->mnt_fsname = str3cat((char *) NULL,
92a53f50b9Schristos 				 vmt2dataptr(mp, VMT_HOSTNAME), ":",
93a53f50b9Schristos 				 fsname);
94a53f50b9Schristos     break;
95a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
96a53f50b9Schristos 
97a53f50b9Schristos   default:
98a53f50b9Schristos     ty = "unknown";
99*31bdb48aSchristos     new_mp->mnt_fsname = xstrdup(fsname);
100a53f50b9Schristos     break;
101a53f50b9Schristos 
102a53f50b9Schristos   }
103a53f50b9Schristos 
104*31bdb48aSchristos   new_mp->mnt_type = xstrdup(ty);
105a53f50b9Schristos   /* store the VFS ID for uvmount() */
106a53f50b9Schristos   new_mp->mnt_passno = mp->vmt_vfsnumber;
107a53f50b9Schristos   new_mp->mnt_freq = 0;
108a53f50b9Schristos 
109a53f50b9Schristos   XFREE(fsname);
110a53f50b9Schristos 
111a53f50b9Schristos   return new_mp;
112a53f50b9Schristos }
113a53f50b9Schristos 
114a53f50b9Schristos 
115a53f50b9Schristos /*
116a53f50b9Schristos  * Read a mount table into memory
117a53f50b9Schristos  */
118a53f50b9Schristos mntlist *
read_mtab(char * fs,const char * mnttabname)119a53f50b9Schristos read_mtab(char *fs, const char *mnttabname)
120a53f50b9Schristos {
121a53f50b9Schristos   mntlist **mpp, *mhp;
122a53f50b9Schristos   int i;
123a53f50b9Schristos   char *mntinfo = NULL, *cp;
124a53f50b9Schristos   struct vmount *vp;
125a53f50b9Schristos   int ret;
126a53f50b9Schristos   int maxtry = 10;		/* maximum number of times to try mntctl */
127a53f50b9Schristos 
128a53f50b9Schristos   /*
129a53f50b9Schristos    * Figure out size of mount table and allocate space for a copy.  Then get
130a53f50b9Schristos    * mount table for real.  We repeat this loop at most 10 times to minimze
131a53f50b9Schristos    * the chance of a race condition (something gets un/mounted in between
132a53f50b9Schristos    * calls to mntctl()
133a53f50b9Schristos    */
134a53f50b9Schristos   i = sizeof(int);
135a53f50b9Schristos   do {
136a53f50b9Schristos     if (mntinfo)
137a53f50b9Schristos       XFREE(mntinfo);
138a53f50b9Schristos     mntinfo = xmalloc(i);
139a53f50b9Schristos     ret = mntctl(MCTL_QUERY, i, mntinfo);
140a53f50b9Schristos     if (ret == 0)
141a53f50b9Schristos       i = *(int*) mntinfo;
142a53f50b9Schristos     if (--maxtry <= 0) {
143a53f50b9Schristos       plog(XLOG_ERROR, "mntctl: could not get a stable result");
144a53f50b9Schristos       ret = -1;
145a53f50b9Schristos       errno = EINVAL;
146a53f50b9Schristos       break;
147a53f50b9Schristos     }
148a53f50b9Schristos   } while (ret == 0);
149a53f50b9Schristos   if (ret < 0) {
150a53f50b9Schristos     plog(XLOG_ERROR, "mntctl: %m");
151a53f50b9Schristos     goto out;
152a53f50b9Schristos   }
153a53f50b9Schristos 
154a53f50b9Schristos   mpp = &mhp;
155a53f50b9Schristos   for (i = 0, cp = mntinfo; i < ret; i++, cp += vp->vmt_length) {
156a53f50b9Schristos     vp = (struct vmount *) cp;
157a53f50b9Schristos 
158a53f50b9Schristos     /*
159a53f50b9Schristos      * Allocate a new slot
160a53f50b9Schristos      */
161a53f50b9Schristos     *mpp = ALLOC(struct mntlist);
162a53f50b9Schristos 
163a53f50b9Schristos     /*
164a53f50b9Schristos      * Copy the data returned by mntctl
165a53f50b9Schristos      */
166a53f50b9Schristos     (*mpp)->mnt = mnt_dup(vp);
167a53f50b9Schristos 
168a53f50b9Schristos     /*
169a53f50b9Schristos      * Move to next pointer
170a53f50b9Schristos      */
171a53f50b9Schristos     mpp = &(*mpp)->mnext;
172a53f50b9Schristos   }
173a53f50b9Schristos 
174a53f50b9Schristos   *mpp = NULL;
175a53f50b9Schristos 
176a53f50b9Schristos out:
177a53f50b9Schristos   if (mntinfo)
178a53f50b9Schristos     XFREE(mntinfo);
179a53f50b9Schristos   return mhp;
180a53f50b9Schristos }
181