Commit 54f1bf5a authored by Mike Hibler's avatar Mike Hibler

replace injail perl script with C program

parent def13ddb
/*
* EMULAB-COPYRIGHT
* Copyright (c) 2000-2003 University of Utah and the Flux Group.
* All rights reserved.
*/
/*
* The C version of injail.pl
*
* A much smaller memory footprint.
*/
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
char *vnode;
char *prog;
int myuid;
void
handler(int signo)
{
pid_t killpid = myuid ? 0 : -1;
signal(SIGUSR1, SIG_IGN);
kill(killpid, SIGTERM);
sleep(1);
kill(killpid, SIGKILL);
exit(signo);
}
void
usage(void)
{
fprintf(stderr, "%s: [ -v vnodename ] cmd cmdargs ...\n", prog);
}
char *defargv[] = { "/bin/sh", "/etc/rc", 0 };
int
main(int argc, char **argv)
{
int ch;
pid_t child;
extern char **environ;
prog = argv[0];
myuid = getuid();
while ((ch = getopt(argc, argv, "v:")) != -1)
switch(ch) {
case 'v':
vnode = optarg;
break;
default:
usage();
}
argc -= optind;
argv += optind;
#ifdef __FreeBSD__
if (vnode)
setproctitle("%s", vnode);
#endif
signal(SIGTERM, SIG_IGN);
signal(SIGUSR1, handler);
child = fork();
if (child == 0) {
signal(SIGTERM, SIG_DFL);
if (argc == 0)
argv = defargv;
execve(argv[0], argv, environ);
fprintf(stderr, "exec of %s failed\n", argv[0]);
_exit(1);
}
#if 0
/* XXX don't think we want this? */
daemon(0, 0);
#endif
(void) waitpid(child, 0, 0);
/*
* If a command list was provided, we exit.
* Otherwise we wait forever.
*/
if (argc)
handler(0);
else {
sigset_t mask;
sigprocmask(0, 0, &mask);
while (1)
sigsuspend(&mask);
}
exit(0);
}
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;
#
# The point of this is to fire up the init code inside the jail,
# and then wait for a signal from outside the jail. When that happens
# kill off everything inside the jail and exit. So, like a mini version
# of /sbin/init, since killing the jail cleanly from outside the jail
# turns out to be rather difficult, and doing it from inside is very easy!
#
# Note though, when the machine is shutdown we can cleanly send this
# script a signal from mkjail. If reboot is used instead of shutdown,
# the system broadcasts SIGTERM, and this script will catch that and
# die. However, thats not good cause the caller (mkjail) wipes out the
# jail when this script exits of its own accord. So, ignore TERM, and wait
# for mkjail to send us a USR1, which means to exit.
#
my $DEFCONSIX = "/bin/sh /etc/rc";
#
# Catch TERM.
#
sub handler () {
$SIG{USR1} = 'IGNORE';
system("kill -TERM -1");
sleep(1);
system("kill -KILL -1");
exit(1);
}
$SIG{TERM} = 'IGNORE';
$SIG{USR1} = \&handler;
my $childpid = fork();
if (!$childpid) {
$SIG{TERM} = 'DEFAULT';
if (@ARGV) {
exec @ARGV;
}
else {
exec $DEFCONSIX;
}
die("*** $0:\n".
" exec failed: '@ARGV'\n");
}
#
# If a command list was provided, we wait for whatever it was to
# finish. Otherwise sleep forever.
#
if (@ARGV) {
waitpid($childpid, 0);
$SIG{TERM} = 'IGNORE';
system("kill -TERM -1");
sleep(1);
system("kill -KILL -1");
}
else {
#
# Otherwise, wait for the command to exit (prevent zombie), but
# then just wait forever. The only way to die is to be killed
# from outside the jail via the signal handler above. I suppose
# we could look at the exit status of the child ...
#
waitpid($childpid, 0);
while (1) {
system("/bin/sleep 10000");
}
}
exit(0);
......@@ -278,8 +278,8 @@ else {
$SIG{TERM} = 'DEFAULT';
$ENV{'TMCCVNODEID'} = $vnodeid;
my $cmd = "jail $jailoptions ".
"$JAILPATH/$vnodeid/root $jailhostname $IP /etc/jail/injail.pl";
my $cmd = "jail $jailoptions $JAILPATH/$vnodeid/root $jailhostname $IP ".
"/etc/jail/injail -v $vnodeid";
if ($interactive) {
$cmd .= " /bin/csh";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment