HP-UX Reference (11i v3 07/02) - 3 Library Functions A-M (vol 6)
g
getmntent(3X) getmntent(3X)
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;
}
}
528 Hewlett-Packard Company − 4 − HP-UX 11i Version 3: February 2007