1/*	$NetBSD: mtab_aix.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_aix.c
39 *
40 */
41
42/*
43 * AIX systems don't write their mount tables on a file.  Instead, they
44 * use a (better) system where the kernel keeps this state, and you access
45 * the mount tables via a known interface.
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52#include <amu.h>
53
54/*
55 * These were missing external definitions from old AIX's headers.  They
56 * appear to be available in <sys/vmount.h> on AIX 5.3, and possibly
57 * earlier. Hence I commented this out.
58 */
59#ifndef HAVE_EXTERN_MNTCTL
60extern int mntctl(int cmd, int size, voidp buf);
61#endif /* not HAVE_EXTERN_MNTCTL */
62
63
64static mntent_t *
65mnt_dup(struct vmount *mp)
66{
67  mntent_t *new_mp = ALLOC(mntent_t);
68  char *ty;
69  char *fsname = xstrdup(vmt2dataptr(mp, VMT_OBJECT));
70
71  new_mp->mnt_dir = strdup(vmt2dataptr(mp, VMT_STUB));
72  new_mp->mnt_opts = strdup(vmt2dataptr(mp, VMT_ARGS));
73
74  switch (mp->vmt_gfstype) {
75
76  case MOUNT_TYPE_UFS:
77    ty = MNTTAB_TYPE_UFS;
78    new_mp->mnt_fsname = xstrdup(fsname);
79    break;
80
81  case MOUNT_TYPE_NFS:
82    ty = MNTTAB_TYPE_NFS;
83    new_mp->mnt_fsname = str3cat((char *) NULL,
84				 vmt2dataptr(mp, VMT_HOSTNAME), ":",
85				 fsname);
86    break;
87
88#ifdef HAVE_FS_NFS3
89  case MOUNT_TYPE_NFS3:
90    ty = MNTTAB_TYPE_NFS3;
91    new_mp->mnt_fsname = str3cat((char *) NULL,
92				 vmt2dataptr(mp, VMT_HOSTNAME), ":",
93				 fsname);
94    break;
95#endif /* HAVE_FS_NFS3 */
96
97  default:
98    ty = "unknown";
99    new_mp->mnt_fsname = xstrdup(fsname);
100    break;
101
102  }
103
104  new_mp->mnt_type = xstrdup(ty);
105  /* store the VFS ID for uvmount() */
106  new_mp->mnt_passno = mp->vmt_vfsnumber;
107  new_mp->mnt_freq = 0;
108
109  XFREE(fsname);
110
111  return new_mp;
112}
113
114
115/*
116 * Read a mount table into memory
117 */
118mntlist *
119read_mtab(char *fs, const char *mnttabname)
120{
121  mntlist **mpp, *mhp;
122  int i;
123  char *mntinfo = NULL, *cp;
124  struct vmount *vp;
125  int ret;
126  int maxtry = 10;		/* maximum number of times to try mntctl */
127
128  /*
129   * Figure out size of mount table and allocate space for a copy.  Then get
130   * mount table for real.  We repeat this loop at most 10 times to minimze
131   * the chance of a race condition (something gets un/mounted in between
132   * calls to mntctl()
133   */
134  i = sizeof(int);
135  do {
136    if (mntinfo)
137      XFREE(mntinfo);
138    mntinfo = xmalloc(i);
139    ret = mntctl(MCTL_QUERY, i, mntinfo);
140    if (ret == 0)
141      i = *(int*) mntinfo;
142    if (--maxtry <= 0) {
143      plog(XLOG_ERROR, "mntctl: could not get a stable result");
144      ret = -1;
145      errno = EINVAL;
146      break;
147    }
148  } while (ret == 0);
149  if (ret < 0) {
150    plog(XLOG_ERROR, "mntctl: %m");
151    goto out;
152  }
153
154  mpp = &mhp;
155  for (i = 0, cp = mntinfo; i < ret; i++, cp += vp->vmt_length) {
156    vp = (struct vmount *) cp;
157
158    /*
159     * Allocate a new slot
160     */
161    *mpp = ALLOC(struct mntlist);
162
163    /*
164     * Copy the data returned by mntctl
165     */
166    (*mpp)->mnt = mnt_dup(vp);
167
168    /*
169     * Move to next pointer
170     */
171    mpp = &(*mpp)->mnext;
172  }
173
174  *mpp = NULL;
175
176out:
177  if (mntinfo)
178    XFREE(mntinfo);
179  return mhp;
180}
181