Esempio n. 1
0
int
fixmount_check_mount(char *host, struct in_addr hostaddr, char *path)
{
  FILE *mtab;
  mntent_t *ment;
  int found = 0;

  /* scan mtab for path */
  if (!(mtab = setmntent(_PATH_MTAB, "r"))) {
    perror(_PATH_MTAB);
    exit(1);
  }

  /*
   * setmntent() doesn't do locking in read-only mode. Too bad -- it seems to
   * rely on mount() and friends to do atomic updates by renaming the file.
   * Well, our patched amd rewrites mtab in place to avoid NFS lossage, so
   * better do the locking ourselves.
   */
#ifdef HAVE_FLOCK
  if (flock(fileno(mtab), LOCK_SH) < 0) {
#else /* not HAVE_FLOCK */
  if (lockf(fileno(mtab), F_LOCK, 0) < 0) {
#endif /* not HAVE_FLOCK */
    perror(_PATH_MTAB);
    exit(1);
  }

  while (!found && (ment = getmntent(mtab))) {
    char *colon;

    if ((colon = strchr(ment->mnt_fsname, ':'))) {
      *colon = '\0';
      if ((STREQ(colon + 1, path) ||
	   STREQ(ment->mnt_dir, path)) &&
	  is_same_host(ment->mnt_fsname, host, hostaddr))
	  found = 1;
    }
  }

  (void) endmntent(mtab);

  if (!found) {
    char *swap;

    /* swap files never show up in mtab, only root fs */
    if ((swap = strstr(path, "swap"))) {
      strncpy(swap, "root", 4);	/* this should NOT use xstrlcpy  */
      found = fixmount_check_mount(host, hostaddr, path);
      strncpy(swap, "swap", 4);	/* this should NOT use xstrlcpy  */
    }
  }
  return found;
}
Esempio n. 2
0
int
fixmount_check_mount(char *host, struct in_addr hostaddr, char *path)
{
  FILE *mtab;
  struct mnttab ment;
  int err = 0;
  int found = 0;

  /* scan mtab for path */
  if (!(mtab = fopen(MNTTAB, "r"))) {
    perror(MNTTAB);
    exit(1);
  }

  while (!found && (err = getmntent(mtab, &ment)) == 0) {
    char *colon;

    if ((colon = strchr(ment.mnt_fsname, ':'))) {
      *colon = '\0';
      if ((STREQ(colon + 1, path) ||
	   STREQ(ment.mnt_dir, path)) &&
	  is_same_host(ment.mnt_fsname, host, hostaddr))
	  found = 1;
    }
  }

  if (err > 0) {
    fprintf(stderr, "getmntent: %s: %s\n", MNTTAB,
	    err == MNT_TOOLONG ? "entry exceeds MNT_LINE_MAX" :
	    err == MNT_TOOMANY ? "too many fields in line" :
	    err == MNT_TOOFEW ? "too few fields in line" :
	    "unknown error code");
    exit(1);
  }
  (void) fclose(mtab);

  /* XXX: Is this still valid in SunOS 5.x ? */
  if (!found) {
    char *swap;

    /* swap files never show up in mtab, only root fs */
    if ((swap = strstr(path, "swap"))) {
      strncpy(swap, "root", 4);	/* this should NOT use xstrlcpy  */
      found = fixmount_check_mount(host, hostaddr, path);
      strncpy(swap, "swap", 4);	/* this should NOT use xstrlcpy  */
    }
  }
  return found;
}