[comp.sys.sun] determining the real directory when using automounts

mjk@trout.rice.edu (Mark J. Kilgard) (02/09/89)

The automount feature of Sun OS 4.O is an incrediably nice feature but it
has the annoying side effect of blurring what your current working
directory is.  Example:

uncle-bens:/marsh/mjk#1 -> pwd
/tmp_mnt/auto274a00144/mjk

It kind of makes it hard to know where you are.

The following short program will return the "real" path name when you use
automounts:

/*
 *  realpwd - returns the "real" name of a directory even when
 *    obscurred by automounting
 *
 *  by Mark Kilgard
 *   1/29/88
 */

#include <stdio.h>
#include <mntent.h>
#include <sys/param.h>
#include <string.h>

#define TMP_MNT_PREFIX "/tmp_mnt/"
#define MTAB "/etc/mtab"

char* real_getwd()
{
   static char cwd[MAXPATHLEN];
   char mnt_prefix[MAXPATHLEN];
   char mnt_suffix[MAXPATHLEN];
   int i;
   int j;
   char* ptr;
   FILE* mtab;
   struct mntent* mount;

   getwd(cwd);
   if(!strncmp(cwd,TMP_MNT_PREFIX,sizeof(TMP_MNT_PREFIX)-1)) {
      mtab=setmntent(MTAB,"r");
      if(mtab==NULL) {
         return(cwd);
      }
      mnt_prefix[0]='/';
      i=1;
      while(cwd[i]!='/') {
         mnt_prefix[i]=cwd[i];
         i++;
      }
      mnt_prefix[i]='/';
      i++;
      while(cwd[i]!='/' && cwd[i]!='\0') {
         mnt_prefix[i]=cwd[i];
         i++;
      }
      mnt_prefix[i]='\0';
      j=0;
      while(cwd[i]!='\0') {
         mnt_suffix[j]=cwd[i];
         i++;
         j++;
      }
      mnt_suffix[j]='\0';

      while((mount=getmntent(mtab))!=NULL) {
         if(!strcmp(mount->mnt_dir,mnt_prefix)) {
            ptr=strrchr(mount->mnt_fsname,':');
            if(ptr==NULL) {
               return(cwd);
            }
            ptr++;
            strcpy(mnt_prefix,ptr);
            strcat(mnt_prefix,mnt_suffix);
            return(mnt_prefix);
         }
      }
      return(cwd);
   }
   return(cwd);
}

main()
{
   printf("%s\n",real_getwd());
}

Extracting the routine may be useful.  Realize the routine stores the
result in a static area for each call.  Essentially the routine does a
lookup in /etc/mtab.

- Mark Kilgard <mjk@rice.edu>

ekrell@ulysses.att.com (02/15/89)

In ksh, pwd is a builtin which returns the "logical" name of the current
working directory, so if you "cd /foo/bar", pwd always returns "/foo/bar",
even if you're in a different place as a consequence of following symbolic
links or other such wierdness.

Eduardo Krell                   AT&T Bell Laboratories, Murray Hill, NJ

UUCP: {att,decvax,ucbvax}!ulysses!ekrell  Internet: ekrell@ulysses.att.com

lmb@vicom.com (Larry Blair) (02/23/89)

ekrell@ulysses.att.com writes:
>In ksh, pwd is a builtin which returns the "logical" name of the current
>working directory, so if you "cd /foo/bar", pwd always returns "/foo/bar",
>even if you're in a different place as a consequence of following symbolic
>links or other such wierdness.

when I used csh, I used to have a "cwd", which just did a "echo $cwd".
That way I could get both the logical and physical current directory.
When I changed to ksh, I wanted to retain this functionality, so I put the
following in the .kshrc:

alias cwd='print - ~+'
unalias pwd

Now "pwd" and "cwd" work like they did for csh.
-- 
Larry Blair   ames!vsi1!lmb   lmb@vicom.com

brent%terra@sun.com (Brent Callaghan) (02/28/89)

In article <8902090049.AA21344@hector.homer.nj.att.com>, ekrell@ulysses.att.com writes:
> In ksh, pwd is a builtin which returns the "logical" name of the current
> working directory, so if you "cd /foo/bar", pwd always returns "/foo/bar",
> even if you're in a different place as a consequence of following symbolic
> links or other such wierdness.

Just to follow up on Eduardo's comment: I automount my home directory.
I'm a ksh user so I see what I expect to see when I do a pwd in my home
directory "/home/sparky/brent".  The C shell also maintains $PWD.  You can
get the same effect as the ksh builtin just by setting up pwd as an alias
to echo $PWD.  Just make sure that the csh hardpaths option isn't set (I
don't think it is by default).

The mount point names are a little more attractive if you use the 4.0.1
automounter.  The "/tmp_mnt/autoa12345" mount points have been replaced by
a scheme that gives you the same path except for a prepended "/tmp_mnt"
e.g. a /bin/pwd of my home directory now gives
"/tmp_mnt/home/sparky/brent".

Made in New Zealand -->  Brent Callaghan  @ Sun Microsystems
			 uucp: sun!bcallaghan
			 phone: (415) 336 1051

stanonik@nprdc.navy.mil (Ron Stanonik) (04/05/89)

One quirk of automount using subdirectories seems to be that the /tmp_mnt
mount point includes the full path, not just the parent.  For example, we
automount /usr/local using this map

bin     alpha:/usr/local:$ARCH/bin
lib     alpha:/usr/local:$ARCH/lib
*       alpha:/usr/local:&

After referencing /usr/local/bin we find (using df) that alpha:/usr/local
is mounted on /tmp_mnt/usr/local/bin, not on /tmp_mnt/usr/local.
automount gets the symlinks correct, eg

beta# ls -l /usr/local/bin
lrwxrwxrwx  1 root    /usr/local/bin -> /tmp_mnt/usr/local/bin/sun4/bin

but it would be nice (from an admin/systemhack view) if the
mount point was /tmp_mnt/usr/local.

Maybe I'm doing something wrong?

Thanks,

Ron Stanonik
stanonik@nprdc.navy.mil

[[ The next message is a response to this one.  --wnl ]]

brent@sun.com (Brent Callaghan) (04/05/89)

The symbolic links for the 4.0.1 automounter were made more attractive by
attempting to recreate the original path, with the exception of "/tmp_mnt"
stuck on the front.  The 4.0 version used to invent disgusting pathnames
via tmpnam(3).

The symbolic links look pretty good for most mounts - except if you use
the subdirectory feature in a map (i.e. a second colon).  This feature
allows the automounter to do the mount at a higher level and thus allow
several map entries to share a common mount.  One side effect of this
shared mount point is that it's now impossible to build a completely
satisfying symbolic link.  The 4.0.1 automounter fudges a path by
incorporating the key used in the first reference that caused the mount.
This is best illustrated with an example:

An automounter map to mount user's home directories.  The map is looked up
by loginname - not servername.

	brent      sparky:/home/sparky:brent
	derek      sparky:/home/sparky:derek
	cindy      sparky:/home/sparky:cindy
	fred       gurgle:/home/gurgle:fred

I mount this map on /home thus:

	automount /home /etc/auto.home

Then I do the following:

	$ ls -l /home/brent
	lrwxrwxrwx  /home/brent -> /tmp_mnt/home/brent/brent
	$ ls -l /home/derek
	lrwxrwxrwx  /home/derek -> /tmp_mnt/home/brent/derek
	$ ls -l /home/cindy
	lrwxrwxrwx  /home/cindy -> /tmp_mnt/home/brent/cindy
	$ ls -l /home/fred
	lrwxrwxrwx  /home/fred -> /tmp_mnt/home/fred/fred

The symbolic links are all pointing to the right places, it's just that
the names look a little weird.  If you use this scheme for mounting your
home directory you can "fix up" the name by having the shell keep track
for you.  Both the C shell an Korn shell keep the original path in $PWD.
You can set up an alias so a "pwd" will echo $PWD.  If you still don't
like the idea of weird symbolic links, then just replace second colon in
the map entry with a slash and put up with the overhead of extra mounting.
The pathname will still be longer than you expect, but not quite so weird
and unpredictable.

	Brent