File: mntinfo.h

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (163 lines) | stat: -rw-r--r-- 7,510 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*********************************************************
 * Copyright (C) 2006 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * mntinfo.h --
 *
 *    Macros to abstract differences between structures and functions used in
 *    accessing information about mounted file systems.
 *
 */


#ifndef __MNTINFO_H__
#define __MNTINFO_H__

#ifdef sun
# include <sys/mnttab.h>
# include <libgen.h>
# include <limits.h>
#elif defined(__linux__)
# include <mntent.h>
#elif defined(__FreeBSD__)
# include <sys/mount.h>
#endif
#include "posix.h"

/*
 *----------------------------------------------------------------------------
 *
 * DECLARE_MNTINFO, MNTINFO
 * OPEN_MNTFILE, GETNEXT_MNTINFO, CLOSE_MNTFILE,
 * MNTINFO_NAME, MNTINFO_FSTYPE, MNTINFO_MNTPT --
 *
 *    Cross-platform macros for accessing information about the mounted file
 *    systems.  This is necessary since the interfaces for getmntent(3) are
 *    slightly different on Linux and Solaris.
 *
 *    DECLARE_MNTINFO() is used to declare the variable used when invoking
 *    GETNEXT_MNTINFO().  MNTINFO is the type that can be used when passing
 *    between functions.
 *
 *    OPEN_MNTFILE() and CLOSE_MNTFILE() must be called before and after
 *    a series of GETNEXT_MNTINFO() calls, respectively.  GETNEXT_MNTINFO() is
 *    called successively to retrieve information about the next mounted file
 *    system.
 *
 *    MNTINFO_NAME, MNTINFO_FSTYPE, and MNTINFO_MNTPT retrieve the name, file
 *    system type, and mount point of the provided MNTINFO, respectively.
 *
 *    MNTFILE is a string with the name of the file containing mount
 *    information.
 *
 * Results:
 *    OPEN_MNTFILE:    MNTHANDLE on success, NULL on failure
 *    GETNEXT_MNTINFO: on success, TRUE and mnt is filled with file system's
 *                     information; FALSE when no mounts left or on failure
 *    CLOSE_MNTFILE:   TRUE on success, FALSE on failure
 *
 *    MNTINFO_NAME:    mount's name on success, NULL on failure
 *    MNTINFO_FSTYPE:  mount's file system type on success, NULL on failure
 *    MNTINFO_MNTPT:   mount's mount point on success, NULL on failure
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

#ifdef sun
# define MNTFILE                        MNTTAB
# define MNTHANDLE                      FILE *
# define MNTINFO                        struct mnttab
# define DECLARE_MNTINFO(name)          struct mnttab __ ## name; \
                                        struct mnttab *name = &__ ## name
# define OPEN_MNTFILE(mode)             Posix_Fopen(MNTFILE, mode)
# define GETNEXT_MNTINFO(fp, mnt)       (Posix_Getmntent(fp, mnt) == 0)
# define CLOSE_MNTFILE(fp)              (fclose(fp) == 0)
# define MNTINFO_NAME(mnt)              mnt->mnt_special
# define MNTINFO_FSTYPE(mnt)            mnt->mnt_fstype
# define MNTINFO_MNTPT(mnt)             mnt->mnt_mountp
# define MNTINFO_MNT_IS_RO(mnt)         (hasmntopt((mnt), "rw") == NULL)
#elif defined(__linux__)
# define MNTFILE                        MOUNTED
# define MNTHANDLE                      FILE *
# define MNTINFO                        struct mntent
# define DECLARE_MNTINFO(name)          struct mntent *name
# define OPEN_MNTFILE(mode)             Posix_Setmntent(MNTFILE, mode)
# define GETNEXT_MNTINFO(fp, mnt)       ((mnt = Posix_Getmntent(fp)) != NULL)
# define CLOSE_MNTFILE(fp)              (endmntent(fp) == 1)
# define MNTINFO_NAME(mnt)              mnt->mnt_fsname
# define MNTINFO_FSTYPE(mnt)            mnt->mnt_type
# define MNTINFO_MNTPT(mnt)             mnt->mnt_dir
# define MNTINFO_MNT_IS_RO(mnt)         (hasmntopt((mnt), "rw") == NULL)
#elif defined(__FreeBSD__) || defined(__APPLE__)
struct mntHandle {
   struct statfs *mountPoints;  // array of mountpoints per getmntinfo(3)
   int numMountPoints;          // number of elements in mntArray
   int mountIndex;              // current location within mountPoints array
};
# define MNTFILE                        _PATH_FSTAB
# define MNTHANDLE                      struct mntHandle *
# define MNTINFO                        struct statfs
# define DECLARE_MNTINFO(name)          struct statfs __ ## name; \
                                        struct statfs *name = &__ ## name

# define OPEN_MNTFILE(mode)                                             \
({                                                                      \
   MNTHANDLE mntHandle;                                                 \
   mntHandle = malloc(sizeof *mntHandle);                               \
   if (mntHandle != NULL) {                                             \
      mntHandle->numMountPoints = getmntinfo(&mntHandle->mountPoints,   \
                                             MNT_NOWAIT);               \
      mntHandle->mountIndex = 0;                                        \
   }                                                                    \
   mntHandle;                                                           \
})

# define GETNEXT_MNTINFO(mntHandle, mnt)                                \
({                                                                      \
   /* Avoid multiple evaluations/expansions. */                         \
   MNTHANDLE thisHandle = (mntHandle);                                  \
   MNTINFO *thisMnt = (mnt);                                            \
   Bool boolVal = FALSE;                                                \
   ASSERT(thisHandle);                                                  \
   if (thisHandle->mountIndex < thisHandle->numMountPoints) {           \
      memcpy(thisMnt,                                                   \
             &thisHandle->mountPoints[thisHandle->mountIndex],          \
             sizeof *thisMnt);                                          \
      ++thisHandle->mountIndex;                                         \
      boolVal = TRUE;                                                   \
   }                                                                    \
   boolVal;                                                             \
})

# define CLOSE_MNTFILE(mntHandle)                                       \
({                                                                      \
   free(mntHandle);                                                     \
   TRUE;                                                                \
})
# define MNTINFO_NAME(mnt)              mnt->f_mntfromname
# define MNTINFO_FSTYPE(mnt)            mnt->f_fstypename
# define MNTINFO_MNTPT(mnt)             mnt->f_mntonname
# define MNTINFO_MNT_IS_RO(mnt)         ((mnt)->f_flags & MNT_RDONLY)
#else
# error "Define mount information macros for your OS type"
#endif

#endif /* __MNTINFO_H__ */