home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!cf-cm!news
- From: spxtrb@thor.cf.ac.uk (Tim Barnett - programmer)
- Newsgroups: comp.unix.wizards
- Subject: Re: Detecting if running under chroot
- Message-ID: <21748.9211101815@thor.cf.ac.uk>
- Date: 10 Nov 92 18:15:28 GMT
- References: <1992Nov3.183208.20956@newsgate.sps.mot.com> <mark.721121538@coombs> <CONS.92Nov9100404@mercury.cern.ch>
- Sender: news@cm.cf.ac.uk (Network News System)
- Organization: University of Wales College at Cardiff
- Lines: 31
- X-Mailer: Cardiff Computing Maths PP Mail Open News Gateway
-
- In his article cons@mercury.cern.ch (Lionel Cons) writes:
- | I know a way to see if I run under chroot on my OS (HP-UX) and I think
- | it should work on many different platforms: the inode number of / is
- | always 2, so 'ls -id' will tell me...
-
- Close, but no cigar. 'ls -id /' giving inode of 2 just means that the
- root directory is the head of a filesystem - not necessarily the head
- of the 'true' root filesystem.
-
- What you need is a handle on the real root filesystem. Normally your
- controlling terminal was opened on the root filesystem, and stdin is
- normally a file descriptor pointing to it, so an fstat() of fileno(stdin)
- will reveal an st_dev that should be equal to that of your current /'s
- st_dev (if you are on the 'true' root filesystem).
- Now you can can check /'s st_ino to see if it is 2. Something like:
-
- void main(void) {
- struct stat tty, slash;
- if (-1 != fstat(fileno(stdin),&tty) && -1 != stat("/",&slash))
- if (tty.st_dev == slash.st_dev && slash.st_ino == 2)
- exit(0); /* Not chroot()ed */
- else exit(1); /* Am chroot()ed */
- else exit(127); /* Error */
- }
-
- Note that all bets are off if someone has redirected your stdin (you should
- probably at least check that tty.st_mode denotes a character device).
-
- I guess this is closer, but there must be a cleaner way.
- (Bet I slipped up somewhere - go on, flame me 8)
- Tim
-