Getting hostname from jail_getid(3) or ezjail(5)

Julian Fagir gnrp at gnrp.in-berlin.de
Tue Feb 8 17:08:30 UTC 2011


Hi,

I wanted to write a tool starting a shell in a jail. I know, jexec
accomplishes this too, but I want to compile the name of the jail into the
program, no commandline-parameters (more about this topic will be posted on
this list soon).
Thus, I used libjail, more specific jail_getid(3) to get the id of the jail
and start a shell in it.
The name of the jail is 'jail2', it was created by ezjail(5), and the
important lines in /usr/local/etc/ezjail/jail2 are:
  export jail_jail2_hostname="zweihorn2"
  export jail_jail2_ip="IPADDRESS"
  export jail_jail22_rootdir="/usr/jails/jail2"

Anyway, when starting the jail, `jls` bravely shows as the hostname of the
jail 'jail2'.

But when I compile this program (with `cc -ljail jexec_sh.c -o jexec_sh`)

  #include <sys/types.h>
  #include <sys/param.h>
  #include <sys/jail.h>

  #include <string.h>
  #include <unistd.h>
  #include <stdio.h>
  #include <jail.h>

  #define JAIL_NAME "jail2"

  int
  main(int argc, char* argv[]) {
          char *jname;
          int jid;

          jid = jail_getid(JAIL_NAME);
          printf("Jail 2 is running: %d\n", jid);
          jname = jail_getname(jid);
          printf("Jailname: '%s' to jid %d\n", jname, jid);
          return(0);
  }

jail_getid returns -1; meaning the jailname does not exist. When setting
JAIL_NAME to "1" (or "2", etc., according to the jid, if I restarted it), the
jid is correctly returned:
  zweihorn1# /home/julian/jexec_sh
  Jail 1 is running: 1
  Jailname: '1' to jid 1

Am i missing something? jail_getid/jail_getname should return exactly the
output I expected?! ezjail must be setting the name right as jls returns the
correct name. jls does nearly the same, though requesting directly the params
without libjail, but libjail does that internally, too.


Regards, Julian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-jail/attachments/20110208/9e08ebd7/signature.pgp


More information about the freebsd-jail mailing list