kern/58497: sysctl knob to return current process' jid

Poul-Henning Kamp phk at
Fri Oct 24 14:00:41 PDT 2003

The following reply was made to PR kern/58497; it has been noted by GNATS.

From: "Poul-Henning Kamp" <phk at>
To: "C. Stephen Gunn" <csg at>
Cc: FreeBSD-gnats-submit at
Subject: Re: kern/58497: sysctl knob to return current process' jid 
Date: Fri, 24 Oct 2003 22:57:39 +0200

 >There is no easy way to tell if a process is operating in a Jail
 Yes, in fact there is:
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #include <sys/param.h>
 #include <sys/user.h>
  * Exit 0 = no
  * Exit 1 = maybe
  * Exit 2 = yes
 main(int argc, char **argv)
 	int mib[4];
 	int i, l;
 	struct kinfo_proc buf;
 	mib[0] = CTL_KERN;
 	mib[1] = KERN_PROC;
 	mib[2] = KERN_PROC_PID;
 	mib[3] = getpid();
 	l = sizeof buf;
 	i = sysctl(mib, 4, &buf, &l, NULL, 0);
 	if (i != 0 || l != sizeof buf)
 	if (buf.kp_proc.p_flag & P_JAILED)
 	exit (0);
 Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
 phk at FreeBSD.ORG         | TCP/IP since RFC 956
 FreeBSD committer       | BSD since 4.3-tahoe    
 Never attribute to malice what can adequately be explained by incompetence.

More information about the freebsd-bugs mailing list