Debian Bug report logs - #627205
debian-utils: please add a way to detect chroots

version graph

Package: debianutils; Maintainer for debianutils is Clint Adams <clint@debian.org>; Source for debianutils is src:debianutils.

Reported by: Aurelien Jarno <aurel32@debian.org>

Date: Wed, 18 May 2011 17:36:02 UTC

Severity: wishlist

Tags: patch

Found in version debianutils/3.4.3

Fixed in version debianutils/4

Done: Clint Adams <clint@debian.org>

Bug is archived. No further changes may be made.

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Clint Adams <clint@debian.org>:
Bug#627205; Package debianutils. (Wed, 18 May 2011 17:36:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Aurelien Jarno <aurel32@debian.org>:
New Bug report received and forwarded. Copy sent to Clint Adams <clint@debian.org>. (Wed, 18 May 2011 17:36:05 GMT) Full text and rfc822 format available.

Message #5 received at submit@bugs.debian.org (full text, mbox):

From: Aurelien Jarno <aurel32@debian.org>
To: Debian Bug Tracking System <submit@bugs.debian.org>
Subject: debian-utils: please add a way to detect chroots
Date: Wed, 18 May 2011 19:32:05 +0200
Package: debianutils
Version: 3.4.3
Severity: wishlist
Tags: patch

A few maintainer scripts try to detect the presence of a chroot using
slightly different manners, some of them being buggy depending on the
operating system. I think it's time to have a binary in debianutils
which can be used by the maintainer scripts. This way it will be easy
to fix or improve the detection method without having to change all 
the scripts.

The patch below adds a "ischroot" binary detecting a chroot. I am quite
open for using another name, or for fixes (especially in the comments 
and manpage as I am not a native speaker). I have tested it on 
GNU/Linux, GNU/kFreeBSD and GNU/Hurd.


diff -Nru a/debian/rules b/debian/rules
--- a/debian/rules	
+++ b/debian/rules	
@@ -64,7 +64,8 @@
 ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
 	strip --remove-section=.comment --remove-section=.note \
 		debian/tmp/bin/run-parts \
-		debian/tmp/bin/tempfile
+		debian/tmp/bin/tempfile \
+		debian/tmp/usr/bin/ischroot
 endif
 
 ifneq ($(DEB_HOST_ARCH_OS),hurd)
@@ -91,7 +92,7 @@
 
 	cd debian/tmp && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
 
-	dpkg-shlibdeps debian/tmp/bin/run-parts debian/tmp/bin/tempfile
+	dpkg-shlibdeps run-parts tempfile ischroot
 	dpkg-gencontrol -isp
 	dpkg --build debian/tmp ..
 
diff -Nru a/ischroot.1 b/ischroot.1
--- a/ischroot.1	1970-01-01 01:00:00.000000000 +0100
+++ b/ischroot.1	
@@ -0,0 +1,37 @@
+.\" -*- nroff -*-
+.TH ISCHROOT 1 "17 May 2011" "Debian"
+.SH NAME
+ischroot \- detect if running in a chroot
+.SH SYNOPSIS
+.B ischroot
+[\-\-default\-false] [\-\-default\-true] [\-\-help] [\-\-version]
+.SH DESCRIPTION
+.PP
+.B ischroot
+detects if it is currently running in a chroot. The exit status is:
+.TP
+0
+if currently running in a chroot
+.TP
+1
+if currently not running in a chroot
+.TP
+2
+if the detection is not possible (On GNU/Linux in happens if the script
+is not run as root).
+.SH OPTIONS
+.TP
+.B "-f, --default-false "
+Exit with status 1 if the detection is not possible.
+.TP
+.B "-t, --default-true "
+Exit with status 0 if the detection is not possible.
+.TP
+.B "--help"
+Print a usage message on standard output and exit successfully.
+.TP
+.B "--version"
+Print version information on standard output and exit successfully.
+.SH BUGS
+On GNU/Linux, chroot detection is not possible as root. This works correctly
+on GNU/Hurd and GNU/kFreeBSD.
diff -Nru a/ischroot.c b/ischroot.c
--- a/ischroot.c	1970-01-01 01:00:00.000000000 +0100
+++ b/ischroot.c	
@@ -0,0 +1,196 @@
+/* ischroot: detect if running in a chroot
+ *
+ * Debian ischroot program
+ * Copyright (C) 2011 Aurelien Jarno <aurel32@debian.org>
+ *
+ * This is free software; see the GNU General Public License version 2
+ * or later for copying conditions.  There is NO warranty.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif /* HAVE_GETOPT_H */
+
+void version()
+{
+  fprintf(stderr, "Debian ischroot, version " PACKAGE_VERSION
+          "Copyright (C) 2011 Aurelien Jarno\n"
+          "This is free software; see the GNU General Public License version 2\n"
+          "or later for copying conditions.  There is NO warranty.\n");
+  exit(0);
+}
+
+void usage()
+{
+  fprintf(stderr, "Usage: ischroot [OPTION]\n"
+          "  -f, --default-false return false if detection fails\n"
+          "  -t, --default-true  return true if detection fails\n"
+          "  -V, --version       output version information and exit.\n"
+          "  -h, --help          display this help and exit.\n");
+  exit(0);
+}
+
+#if defined (__linux__)
+
+/* On Linux we can detect chroots by checking if the 
+ * devicenumber/inode pair of / are the same as that of 
+ * /sbin/init's. This may fail if not running as root or if
+ * /proc is not mounted, in which case 2 is returned.
+ */
+
+static int ischroot()
+{
+  struct stat st1, st2;
+
+  if (stat("/", &st1) || stat("/proc/1/root", &st2))
+    return 2;
+  else if ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino))
+    return 1;
+  else
+    return 0;
+}
+
+#elif defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
+
+#include <sys/sysctl.h>
+#include <sys/user.h>
+
+/* On FreeBSD we can detect chroot by looking for a specific
+ * file descriptor pointing to the location of the chroot. There
+ * is not need to be root, so it is unlikely to fail in normal 
+ * cases, but return 2 if a memory failure or the like happens. */
+
+static int ischroot()
+{
+  int mib[4];
+  size_t kf_len = 0;
+  char *kf_buf, *kf_bufp;
+
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC;
+  mib[2] = KERN_PROC_FILEDESC;
+  mib[3] = getpid ();
+
+  if (sysctl (mib, 4, NULL, &kf_len, NULL, 0) != 0)
+    return 2;
+
+  kf_buf = kf_bufp = malloc (kf_len);
+  if (kf_buf == NULL)
+    return 2;
+
+  if (sysctl (mib, 4, kf_buf, &kf_len, NULL, 0) != 0)
+  {
+    free(kf_buf);
+    return 2;
+  }
+
+  while (kf_bufp < kf_buf + kf_len)
+  {   
+    struct kinfo_file *kf = (struct kinfo_file *) (uintptr_t) kf_bufp;
+
+    if (kf->kf_fd == KF_FD_TYPE_JAIL)
+    {
+      free(kf_buf);
+      return 0;
+    }
+    kf_bufp += kf->kf_structsize;
+  }   
+
+  free(kf_buf);
+  return 1;
+}
+
+#elif defined (__GNU__)
+
+/* On Hurd we can detect chroot by looking at the device number
+ * containing /. The device number of the first mounted filesystem
+ * equals 3, and due to bug http://savannah.gnu.org/bugs/?23213
+ * chroots have to be created on a different filesystem. Return 2
+ * if it is not possible to probe this device. */
+
+static int ischroot()
+{
+  struct stat st;
+
+  if (stat("/", &st))
+    return 2;
+  else if (st.st_dev == 3)
+    return 1;
+  else
+    return 0;
+}
+
+#else
+
+static int ischroot()
+{
+  return 2;
+}
+
+#warning unknown system, chroot detection will always fail
+
+#endif
+
+/* Process options */
+int main(int argc, char *argv[])
+{
+  int default_false = 0;
+  int default_true = 0;
+  int exit_status;
+
+  for (;;) {
+    int c;
+    int option_index = 0;
+
+    static struct option long_options[] = {
+      {"default-false", 0, 0, 'f'},
+      {"default-true", 0, 0, 't'},
+      {"help", 0, 0, 'h'},
+      {"version", 0, 0, 'V'},
+      {0, 0, 0, 0}
+    };
+    c = getopt_long(argc, argv, "fthV", long_options, &option_index);
+    if (c == EOF)
+      break;
+    switch (c) {
+    case 'f':
+      default_false = 1;
+      break;
+    case 't':
+      default_true = 1;
+      break;
+    case 'h':
+      usage();
+      break;
+    case 'V':
+      version();
+      break;
+    default:
+      fprintf(stderr, "Try `ischroot --help' for more information.\n");
+      exit(1);
+    }
+  }
+
+  if (default_false && default_true) {
+    fprintf(stderr, "Can't default to both true and false!\n");
+    fprintf(stderr, "Try `ischroot --help' for more information.\n");
+    exit(1);
+  }
+
+  exit_status = ischroot();
+
+  if (exit_status == 2) {
+    if (default_true)
+      exit_status = 0;
+    if (default_false)
+      exit_status = 1;
+  }
+
+  return exit_status;
+}
diff -Nru a/Makefile.am b/Makefile.am
--- a/Makefile.am	
+++ b/Makefile.am	
@@ -2,9 +2,10 @@
 
 SUBDIRS = po4a
 
-bin_PROGRAMS = run-parts tempfile
+bin_PROGRAMS = run-parts tempfile ischroot
 run_parts_SOURCES = run-parts.c
 tempfile_SOURCES = tempfile.c
+ischroot_SOURCES = ischroot.c
 
 bin_SCRIPTS = which savelog
 
@@ -12,4 +13,5 @@
 
 man_MANS = run-parts.8 \
 	   installkernel.8 savelog.8 \
-	   tempfile.1 which.1 add-shell.8 remove-shell.8
+	   tempfile.1 which.1 add-shell.8 \
+	   remove-shell.8 ischroot.1
diff -Nru a/po4a/po4a.conf b/po4a/po4a.conf
--- a/po4a/po4a.conf	
+++ b/po4a/po4a.conf	
@@ -11,6 +11,10 @@
 	   		      sl:sl/installkernel.8 add_sl:sl/translator_slovene.add \
 	   		      de:de/installkernel.8 add_de:de/translator_german.add \
 	   		      es:es/installkernel.8
+[type:man] ../ischroot.1 fr:fr/ischroot.1 add_fr:fr/ischroot.1.fr.add \
+	   	      sl:sl/ischroot.1 add_sl:sl/translator_slovene.add \
+	   	      de:de/ischroot.1 add_de:de/translator_german.add \
+	   	      es:es/ischroot.1
 # There is no mdoc module in po4a. This one is still translated manually
 [type:man] ../remove-shell.8 fr:fr/remove-shell.8 add_fr:fr/translator_french.add \
 	   		     sl:sl/remove-shell.8 add_sl:sl/translator_slovene.add \


-- System Information:
Debian Release: wheezy/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.38-2-amd64 (SMP w/4 CPU cores)
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages debianutils depends on:
ii  libc6                         2.13-2     Embedded GNU C Library: Shared lib
ii  sensible-utils                0.0.6      Utilities for sensible alternative

debianutils recommends no packages.

debianutils suggests no packages.

-- no debconf information




Reply sent to Clint Adams <clint@debian.org>:
You have taken responsibility. (Wed, 18 May 2011 19:33:08 GMT) Full text and rfc822 format available.

Notification sent to Aurelien Jarno <aurel32@debian.org>:
Bug acknowledged by developer. (Wed, 18 May 2011 19:33:08 GMT) Full text and rfc822 format available.

Message #10 received at 627205-close@bugs.debian.org (full text, mbox):

From: Clint Adams <clint@debian.org>
To: 627205-close@bugs.debian.org
Subject: Bug#627205: fixed in debianutils 4
Date: Wed, 18 May 2011 19:32:09 +0000
Source: debianutils
Source-Version: 4

We believe that the bug you reported is fixed in the latest version of
debianutils, which is due to be installed in the Debian FTP archive:

debianutils_4.dsc
  to main/d/debianutils/debianutils_4.dsc
debianutils_4.tar.gz
  to main/d/debianutils/debianutils_4.tar.gz
debianutils_4_amd64.deb
  to main/d/debianutils/debianutils_4_amd64.deb



A summary of the changes between this version and the previous one is
attached.

Thank you for reporting the bug, which will now be closed.  If you
have further comments please address them to 627205@bugs.debian.org,
and the maintainer will reopen the bug report if appropriate.

Debian distribution maintenance software
pp.
Clint Adams <clint@debian.org> (supplier of updated debianutils package)

(This message was generated automatically at their request; if you
believe that there is a problem with it please contact the archive
administrators by mailing ftpmaster@debian.org)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Format: 1.8
Date: Wed, 18 May 2011 15:04:15 -0400
Source: debianutils
Binary: debianutils
Architecture: source amd64
Version: 4
Distribution: unstable
Urgency: low
Maintainer: Clint Adams <clint@debian.org>
Changed-By: Clint Adams <clint@debian.org>
Description: 
 debianutils - Miscellaneous utilities specific to Debian
Closes: 627205
Changes: 
 debianutils (4) unstable; urgency=low
 .
   * ischroot utility by Aurelien Jarno to detect chrootedness.
     closes: #627205.
Checksums-Sha1: 
 fe833d68c4befe7437e0ecaf3630e0932ce8ebba 1505 debianutils_4.dsc
 43b62cd3cbc8bc1536b9501624f1ae0de8c92257 197812 debianutils_4.tar.gz
 c4c5e2f5e7f8569c1520100a92bdbd09d9d043e0 75408 debianutils_4_amd64.deb
Checksums-Sha256: 
 99b06c9c293d417edbca4b4694e2fd2f288fee4230d0113b0df8c8eda3ad6736 1505 debianutils_4.dsc
 ec0320a9173a249efe21ff9e85ffd264924606e7d0eaa36a37588d1a806effbc 197812 debianutils_4.tar.gz
 45607b4f11661d79f9d01064806ae4231ab648bbe05b3f6c984a632d5b2a242e 75408 debianutils_4_amd64.deb
Files: 
 2eed4c352325820a0957511e470f0346 1505 utils required debianutils_4.dsc
 c43e4d600eecdaaf06cdbee44e4cace2 197812 utils required debianutils_4.tar.gz
 e17495b2e9e3e991da5f21f1c1953161 75408 utils required debianutils_4_amd64.deb

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Debian!

iQIcBAEBCgAGBQJN1Bq+AAoJEFWSMx4ZnTioIXoP/3rgI1pAM8AIdMMsiyyXin7r
rGq/h2Y86z+Y9Ee/eUmD0fwaPDH57QhS9Q4zaBYpuWTuRPJClwQLb6fLx67k3Ks/
Wzoa3offn7tY5Hxm6onVIKxpoomgqng6fApMkuAUsyYJB3irWECKHd1IBOGWfmX0
qXlBf8sWHonrsbDNOcnd2I8Nxn2P+89YEaYYECNw3x+B23tXMmHPBHewrpNnRVwG
b0lu9N2JwpbKKB+NyP3meqY0RX8dvMN90729Y2ZilX75n+BYfYLtggr5eGquxTn6
ZnpB3xiu3YpveNl8to09oESFPge8nan1VJI/POZRd/06npOiik35R17j3tHQCbdy
NDQFGXd+Ai1shW9fbutkR8vYVjhgk0lnNuCXwwhv3NaKq/8m09TMZ5QgWnrhAu4Z
6yJNyY7eiGOJXl/0xgSiqRsMP2d8Y1fSm1I/jERQf3OgIXx+cHpErSEc9pjh5dyO
JAz/Oq48Rjd07le+JwDxasRxP5a5hL2E25LBuZobpJv/aei2T/jWi3hrOk6G5CP6
oGDhpLKBgomGPndpix3bsAnwD7eedVtflu5uMOZrB8TWKOMEd166ZXElONgC3i+T
StbQJI2FUd7ZqKyFhUBdyZZs2FKhs1EwGbLYvVqrG04TuQU/YtfLdda91fIl8UxZ
ZyHMzOLNPOaI+0FvhytR
=V3++
-----END PGP SIGNATURE-----





Bug archived. Request was from Debbugs Internal Request <owner@bugs.debian.org> to internal_control@bugs.debian.org. (Sun, 26 Jun 2011 07:32:17 GMT) Full text and rfc822 format available.

Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Thu Apr 17 01:08:50 2014; Machine Name: beach.debian.org

Debian Bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.