xref: /freebsd/usr.sbin/jexec/jexec.c (revision ebf5d9bc2cad900f20b385a671e82ecaad28b3c7)
1ebf5d9bcSMike Barcroft /*-
2ebf5d9bcSMike Barcroft  * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3ebf5d9bcSMike Barcroft  * All rights reserved.
4ebf5d9bcSMike Barcroft  *
5ebf5d9bcSMike Barcroft  * Redistribution and use in source and binary forms, with or without
6ebf5d9bcSMike Barcroft  * modification, are permitted provided that the following conditions
7ebf5d9bcSMike Barcroft  * are met:
8ebf5d9bcSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
9ebf5d9bcSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
10ebf5d9bcSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
11ebf5d9bcSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
12ebf5d9bcSMike Barcroft  *    documentation and/or other materials provided with the distribution.
13ebf5d9bcSMike Barcroft  *
14ebf5d9bcSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ebf5d9bcSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ebf5d9bcSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ebf5d9bcSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ebf5d9bcSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ebf5d9bcSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ebf5d9bcSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ebf5d9bcSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ebf5d9bcSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ebf5d9bcSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ebf5d9bcSMike Barcroft  * SUCH DAMAGE.
25ebf5d9bcSMike Barcroft  *
26ebf5d9bcSMike Barcroft  * $FreeBSD$
27ebf5d9bcSMike Barcroft  */
28ebf5d9bcSMike Barcroft 
29ebf5d9bcSMike Barcroft #include <sys/param.h>
30ebf5d9bcSMike Barcroft #include <sys/jail.h>
31ebf5d9bcSMike Barcroft 
32ebf5d9bcSMike Barcroft #include <err.h>
33ebf5d9bcSMike Barcroft #include <stdio.h>
34ebf5d9bcSMike Barcroft #include <stdlib.h>
35ebf5d9bcSMike Barcroft #include <unistd.h>
36ebf5d9bcSMike Barcroft 
37ebf5d9bcSMike Barcroft static void	usage(void);
38ebf5d9bcSMike Barcroft 
39ebf5d9bcSMike Barcroft int
40ebf5d9bcSMike Barcroft main(int argc, char *argv[])
41ebf5d9bcSMike Barcroft {
42ebf5d9bcSMike Barcroft 	int jid;
43ebf5d9bcSMike Barcroft 
44ebf5d9bcSMike Barcroft 	if (argc < 3)
45ebf5d9bcSMike Barcroft 		usage();
46ebf5d9bcSMike Barcroft 	jid = (int)strtol(argv[1], NULL, 10);
47ebf5d9bcSMike Barcroft 	if (jail_attach(jid) == -1)
48ebf5d9bcSMike Barcroft 		err(1, "jail_attach(): %d", jid);
49ebf5d9bcSMike Barcroft 	if (chdir("/") == -1)
50ebf5d9bcSMike Barcroft 		err(1, "chdir(): /");
51ebf5d9bcSMike Barcroft 	if (execv(argv[2], argv + 2) == -1)
52ebf5d9bcSMike Barcroft 		err(1, "execv(): %s", argv[2]);
53ebf5d9bcSMike Barcroft 	exit(0);
54ebf5d9bcSMike Barcroft }
55ebf5d9bcSMike Barcroft 
56ebf5d9bcSMike Barcroft static void
57ebf5d9bcSMike Barcroft usage(void)
58ebf5d9bcSMike Barcroft {
59ebf5d9bcSMike Barcroft 
60ebf5d9bcSMike Barcroft 	fprintf(stderr, "usage: jexec jid command [...]\n");
61ebf5d9bcSMike Barcroft 	exit(1);
62ebf5d9bcSMike Barcroft }
63