Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > G

getmntent(3X)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

getmntent(), getmntent_r(), setmntent(), addmntent(), delmntent(), endmntent(), hasmntopt() — get file system descriptor file entry

SYNOPSIS

#include <mntent.h>

FILE *setmntent(const char *path, char *type);

struct mntent *getmntent(FILE *stream);

int getmntent_r( FILE *stream, struct mntent *result, char *buffer, int buflen);

int addmntent(FILE *stream, struct mntent *mnt);

int delmntent(FILE *stream, struct mntent *mnt);

char *hasmntopt(struct mntent *mnt, const char *opt);

int endmntent(FILE *stream);

DESCRIPTION

These routines replace the obsolete getfsent() routines (see getfsent(3X)) for accessing the file system description file /etc/fstab. They are also used to access the mounted file system description file /etc/mnttab.

setmntent()

Opens a file system description file and returns a file pointer which can then be used with getmntent(), addmntent(), delmntent(), or endmntent(). The type argument is the same as in fopen().

getmntent()

Reads the next line from stream and returns a pointer to an object with the following structure containing the broken-out fields of a line in the file-system description file, <mntent.h>. The fields have meanings described in fstab(4).

struct mntent { char *mnt_fsname; /* file system name */ char *mnt_dir; /* file system path prefix */ char *mnt_type; /* hfs, nfs, swap, or xx */ char *mnt_opts; /* ro, suid, etc. */ int mnt_freq; /* dump frequency, in days */ int mnt_passno; /* pass number on parallel fsck */ long mnt_time; /* When file system was mounted;*/ /* see mnttab(4). */ /* (0 for NFS) */ };

getmntent_r()

Uses three extra parameters to provide results equivalent to those produced by getmntent(). The extra parameters are:

1.

The address of a struct mntent where the result will be stored.

2.

A buffer to store character strings to which fields in the struct mntent will point.

3.

The length of the user-supplied buffer. A buffer length of 1025 is recommended.

addmntent()

Adds the mntent structure mnt to the end of the open file stream. Note that stream must be opened for writing. Upon return from the call to addmntent, the file position indicator for the stream will point to EOF.

delmntent()

Deletes all entries from the file stream opened with setmntent that match both mnt_fsname and mnt_dir in mntent structure mnt. If mnt_fsname is a null pointer, all entries that match mnt_dir will be deleted. If mnt_dir is a null pointer, all entries that match mnt_fsname will be deleted. It is an error if both mnt_fsname and mnt_dir are null pointers. Note that stream must be opened via setmntent for reading and writing (r+ or a+). Upon return from the call to delmntent, the file position indicator for the stream will point to EOF.

hasmntopt()

Scans the mnt_opts field of the mntent structure mnt for a substring that matches opt. It returns the address of the substring if a match is found. Otherwise, the return value takes one of the following default behaviors:

Option String

Corresponding Default Behavior

MNTOPT_RW

If the mnt_opts does not have the MNTOPT_RO option, MNTOPT_RW is returned. Otherwise, NULL is returned.

MNTOPT_SUID

If the mnt_opts does not have the MNTOPT_NOSUID option, MNTOPT_SUID is returned. Otherwise, NULL is returned.

MNTOPT_NOQUOTA

If the mnt_opts does not have the MNTOPT_USRQUOTA or MNTOPT_QUOTA option, MNTOPT_NOQUTOA is returned. Otherwise, NULL is returned.

MNTOPT_FG

If the mnt_opts does not have the MNTOPT_BG option and mnt_type is MNTTYPE_NFS, MNTOPT_FG is returned. Otherwise, NULL is returned.

MNTOPT_HARD

If the mnt_opts does not have the MNTOPT_SOFT option and mnt_type is MNTTYPE_NFS, MNTOPT_HARD is returned. Otherwise, NULL is returned.

MNTOPT_INTR

If the mnt_opts does not have the MNTOPT_NOINTR option and mnt_type is MNTTYPE_NFS, MNTOPT_INTR is returned. Otherwise, NULL is returned.

MNTOPT_DEVS

If the mnt_opts does not have the MNTOPT_NODEVS option and mnt_type is MNTTYPE_NFS, MNTOPT_DEVS is returned. Otherwise, NULL is returned.

If none of the default behaviors occur, NULL is returned.

Note: When the return value is the result of one of the default behaviors, it is a pseudo option string, and not a pointer in the mnt_ops field.

endmntent()

Closes the file.

The following definitions are provided in <mntent.h>:

#define MNT_CHECKLIST "/etc/fstab" #define MNT_MNTTAB "/etc/mnttab" #define MNTMAXSTR 128 /* Max size string in mntent */ #define MNTTYPE_HFS "hfs" /* HFS file system */ #define MNTTYPE_CDFS "cdfs" /* CD-ROM file system */ #define MNTTYPE_NFS "nfs" /* Network file system */ #define MNTTYPE_SWAP "swap" /* Swap device */ #define MNTTYPE_SWAPFS "swapfs" /* File system swap */ #define MNTTYPE_IGNORE "ignore" /* Ignore this entry */ #define MNTOPT_DEFAULTS "defaults" /* Use all default options */ #define MNTOPT_RO "ro" /* Read only */ #define MNTOPT_RW "rw" /* Read/write */ #define MNTOPT_SUID "suid" /* Set uid allowed */ #define MNTOPT_NOSUID "nosuid" /* No set uid allowed */ #define MNTOPT_QUOTA "quota" /* Enable user quotas */ #define MNTOPT_USRQUOTA "usrquota" /* Enable user quotas */ #define MNTOPT_GRPQUOTA "grpquota" /* Enable group quotas */ #define MNTOPT_NOQUOTA "noquota" /* Disable user quotas */ #define MNTOPT_DEV "dev" /* Device ID of the filesystem mounted */

The following definition is provided for device swap in <mntent.h>:

#define MNTOPT_END "end" /* swap after end of file system, Series 300/400/700 only */

The following definitions are provided for file system swap in <mntent.h>:

#define MNTOPT_MIN "min" /* minimum file system swap */ #define MNTOPT_LIM "lim" /* maximum file system swap */ #define MNTOPT_RES "res" /* reserve space for file system */ #define MNTOPT_PRI "pri" /* file system swap priority */

NETWORKING FEATURES

NFS

The following definitions are provided in <mntent.h>:

#define MNTOPT_BG "bg" /* Retry mount in background */ #define MNTOPT_FG "fg" /* Retry mount in foreground */ #define MNTOPT_RETRY "retry" /* Number of retries allowed */ #define MNTOPT_RSIZE "rsize" /* Read buffer size in bytes */ #define MNTOPT_WSIZE "wsize" /* Write buffer size in bytes*/ #define MNTOPT_TIMEO "timeo" /* Timeout in 1/10 seconds */ #define MNTOPT_RETRANS "retrans" /* Number of retransmissions */ #define MNTOPT_PORT "port" /* Server's IP NFS port */ #define MNTOPT_SOFT "soft" /* Soft mount */ #define MNTOPT_HARD "hard" /* Hard mount */ #define MNTOPT_INTR "intr" /* Interruptible hard mounts */ #define MNTOPT_NOINTR "nointr" /* Uninterruptible hard mounts*/ #define MNTOPT_DEVS "devs" /* Device file access allowed */ #define MNTOPT_NODEVS "nodevs" /* No device file access allowed*/

RETURN VALUE

setmntent()

Returns a null pointer on error. setmntent() attempts to establish an exclusive write lock on the file it is opening; that is, when one of the following types is passed to setmntent() to open the file for write/update: "w", "a", "r+", "w+", or "a+". If setmntent() cannot get the lock, it returns a null pointer and sets errno to either EACCES or EAGAIN. See APPLICATION USAGE below for more information about setmntent().

getmntent()

Returns a null pointer on error or EOF. Otherwise, getmntent() returns a pointer to a mntent structure. Some of the fields comprising a mntent structure are optional in /etc/fstab and /etc/mnttab. In the supplied structure, such missing character pointer fields are set to NULL and missing integer fields are set to -1 for mnt_freq and mnt_passno. If the integer field for mnt_time is missing, it is set to 0.

getmntent_r()

Returns a -1 on error or EOF, or if the supplied buffer is of insufficient length. If the operation is successful, 0 is returned.

addmntent()

Returns 1 on error.

delmntent()

Returns -1 on error. Sets errno to EINVAL if stream or mnt are null pointers, or if both mnt_fsname and mnt_dir in mntent structure mnt are null pointers. Sets errno to EBADF if stream has been opened for read (r), append (a) or write (w). If the operation is successful, returns the number of entries deleted from the file. When no entries are matched, delmntent returns 0 and does not set errno.

endmntent()

Returns 1, and unlocks the file if it was locked by setmntent().

EXAMPLES

The following code deletes an entry:

struct mntent mnt_entry; FILE *fp; int retval = NOT_DELETED; mnt_entry.mnt_fsname = "/dev/vg00/lvol7"; mnt_entry.mnt_dir = "/disk7"; if ((fp = setmntent(MNT_MNTTAB, "r+")) != NULL) { if (delmntent(fp, &mnt_entry) > 0) retval = DELETED; (void)endmntent(fp); } return(retval);

APPLICATION USAGE

Data integrity is not guaranteed when reading the mntent data because setmntent() does not lock the file when opening it with read-only permission. To overcome this, as one approach, programs may need to loop until the last modification time and file size before and after reading the file are the same.

The following code achieves the data integrity.

struct stat statbuf; FILE *fp; time_t orig_mtime; off_t orig_size; int read_status = FALSE; int retry; for (retry = 0; retry < NO_OF_RETRIES; retry++){ /* * If file is empty, do not bother reading it. * Sleep and then retry the stat(). */ if (stat(MNT_MNTTAB, &statbuf) != 0){ return (STAT_FAILED); } if (statbuf.st_size == 0){ sleep(1); if (stat(MNT_MNTTAB, &statbuf) != 0){ return(STAT_FAILED); } else{ if (statbuf.st_size == 0){ continue; } } } if ((fp = setmntent(MNT_MNTTAB, "r")) == NULL){ return (SETMNTENT_FAILED); } /* * operation on MNT_MNTTAB goes here... */ orig_mtime = statbuf.st_mtime; orig_size = statbuf.st_size; if (stat(MNT_MNTTAB, &statbuf) != 0){ return (STAT_FAILED); } if ((statbuf.st_mtime == orig_mtime) && (statbuf.st_size == orig_size)){ read_status = TRUE; break; } }

Programs should expect that the file accessed by these APIs may be write locked by another process because setmntent() attempts to establish an exclusive write lock when opening it for write/update.

Use of a text editor to manipulate the file accessed by these APIs is not supported. setmntent() and endmntent() are safe for per process locking. setmntent(), getmntent(), addmntent(), delmntent(), hasmntent(), and endmntent() are not safe to be called by a child process after fork() but before exec().

AUTHOR

addmntent(), endmntent(), getmntent(), hasmntopt(), and setmntent() were developed by The University of California, Berkeley, Sun Microsystems, Inc., and HP. delmntent() was developed by HP.

FILES

/etc/fstab /etc/mnttab

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.