home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / wizards / 4592 < prev    next >
Encoding:
Text File  |  1992-11-10  |  1.9 KB  |  44 lines

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