[comp.sys.hp] pwd

davidb@Pacer.UUCP (David Barts) (12/20/89)

How does /bin/pwd work?  There is no kernel call to find your current
directory in UNIX, so it has to do some backhanded peeking in memory
somewhere, probably in kernel memory.  This suspicion is reinforced by
/bin/pwd being setuid to root on my system (HP-UX, System V
compatible).

My guess is that the system uses one of each process's file
descriptors to refer to the opened current working directory file, and
pwd takes this descriptor and somehow comes up with the name of the
file associated with it.

This brings up the possibility of doing the following:
	saved_cwd_fd = dup(cwd_fd);
	dup2(mystery_fd, cwd_fd);
	s = getcwd();  /* s now points to name of mystery file */
	dup2(saved_cwd_fd, cwd_fd);
	close(saved_cwd_fd);

(At least on our system, getcwd is not a system call; it forks
/bin/pwd and reads the current directory from a pipe.)

If so, what is the value of cwd_fd for various UNIX systems,
especially HP-UX?  Or does the system just store the cwd as a string
somewhere in the process header?

Reply to me and I'll summarize.
-- 
David Barts			Pacer Corporation
davidb@pacer.uucp		...!fluke!pacer!davidb

ken@hpubrcf.HP.COM (Ken Green) (12/21/89)

> 
> How does /bin/pwd work?  There is no kernel call to find your current
> directory in UNIX, so it has to do some backhanded peeking in memory
> somewhere, probably in kernel memory.  This suspicion is reinforced by
> /bin/pwd being setuid to root on my system (HP-UX, System V
> compatible).

	If you look in the header file /usr/include/sys/user.h, which describes
	the contence of a processes u_area you'll find that it contains a
	pointer to the vnode for the current directory.

#Guess mode on

	This must point to a directory, you could then open .. the parent
	and find the directory name from the inode number, of course you'd
	need to keep going till you got to the processes root directory (
	this too is stored in the u_area ).

#Guess mode off

davidb@Pacer.UUCP (David Barts) (12/22/89)

As numerous responses have told me, the system does not maintain
an open file descriptor into the current working directory.  Rather,
pwd works its way back up the tree by finding the i number of .
and searching for the entry under .. whose i-number matches that
of dot.  Special trickery is needed to handle mount points.

/bin/pwd is setuid to root so it can handle unreadable directories
in the path.
-- 
David Barts			Pacer Corporation
davidb@pacer.uucp		...!fluke!pacer!davidb

rdg@hpfcmgw.HP.COM (Rob Gardner) (12/22/89)

> How does /bin/pwd work?  There is no kernel call to find your current
> directory in UNIX, so it has to do some backhanded peeking in memory
> somewhere, probably in kernel memory.  This suspicion is reinforced by
> /bin/pwd being setuid to root on my system (HP-UX, System V
> compatible).

That's not how it currently works. The actual method is pretty
disgusting, but here's a brief explanation of how it does it:
Open your parent directory (..) and read entries until you find
one whose inode matches the inode of your current directory (.).
Now you know the (base)name of your current directory. Now
you chdir(".."), and repeat to find out the basename of the
parent. You keep doing this until you get to /, and build
up the pathname as you go. This is, of course, a very simplistic
explanation, since there are cases that require special handling.

I think that there will be a kernel call in a "future" release.

 
    Rob Gardner                     hplabs!hpfcmr!rdg
    Hewlett Packard                 or rdg%hpfcmr@hplabs.hp.com
    Fort Collins, Colorado          303-229-2048
		80525-9599

		     "Ask me about Home Brewing"

andrew@hpqtdla.HP.COM (Andrew Mackenzie) (12/22/89)

In HP-UX there is a sub-routine libary call (HP-UX reference manual,
Volume 2, section 3) 'GETCWD', or get current working directory.

This will give you the full path name of the current working directory.

Hope this helps you out.

Andrew Mackenzie
andrew@hpsqf