Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!usenet.ins.cwru.edu!cwjcc!ncoast!allbery From: allbery@NCoast.ORG (Brandon S. Allbery) Newsgroups: comp.unix.wizards Subject: Re: Got a process struct / need a tty. Message-ID: <1990Feb26.011723.16061@NCoast.ORG> Date: 26 Feb 90 01:17:23 GMT References: <598@uncw.UUCP> Reply-To: allbery@ncoast.ORG (Brandon S. Allbery) Followup-To: comp.unix.wizards Organization: North Coast Public Access UN*X, Cleveland, OH Lines: 41 As quoted from <598@uncw.UUCP> by english@uncw.UUCP (Warren R. English Jr.): +--------------- | How would one go about finding the control terminal | of a process? After reading all the process structures from /dev/kmem | into any array, I can't seem to figure out how to get the filename | of the controlling tty. It seemed to me that after you have | access to each and every process on the machine, it would be easy | to find the tty. So far, I have tried looking at the user structure and | the vnode structure that the process structure points to, but still, | no /dev/ttyxx can I find. The machine is a Sequent Balance Series, | running Dynix. +--------------- Unix kernels do not use pathnames; they use pairs. System calls like open(), creat(), chdir(), chroot(), etc. spend most of their time (modulo slow device I/O or block on no carrier, of course) decoding a pathname into the much faster format. In the case of controlling ttys, it goes on step farther: since a controlling tty is *always* a device, the is further decoded into a device number by looking in the *node (vnode, I assume, in your case) table. This produces a pair; the major device number specifies the driver, the minor number specifies the specific device handled by the driver. The only way to go from these back to pathnames is to stat() every entry in the /dev directory (convention) for character devices with the correct major and minor device numbers. The major/minor device numbers are stored in the user struct, in u.u_ttyd on USG Unix; I don't know if this was changed in BSD-based Unixes like Dynix. They are stored together; macros exist to extract them, but you want to compare both. The stat struct has the device major/minor numbers in the st_rdev element; it is only valid if, for a (struct stat statbuf) set by a successful call to stat(), (statbuf.st_mode & S_IFMT) is equal to S_IFCHR or S_IFBLK. (TTY devices are always S_IFCHR.) (The S_* macros are defined in on both USG and AT&T.) ++Brandon -- Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail) ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie) BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)