mirror of
https://github.com/clearlinux/rkt.git
synced 2026-11-26 06:01:12 +00:00
Merge pull request #300 from vcaputo/diagexec_post_make
exec diagnostic wrapper for stage1
This commit is contained in:
@@ -83,7 +83,8 @@ func LoadContainer(root string) (*Container, error) {
|
||||
func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error {
|
||||
name := am.Name.String()
|
||||
app := am.App
|
||||
execStart := strings.Join(app.Exec, " ")
|
||||
execWrap := []string{"/diagexec", rktpath.RelAppRootfsPath(id)}
|
||||
execStart := strings.Join(append(execWrap, app.Exec...), " ")
|
||||
opts := []*unit.UnitOption{
|
||||
&unit.UnitOption{"Unit", "Description", name},
|
||||
&unit.UnitOption{"Unit", "DefaultDependencies", "false"},
|
||||
@@ -91,7 +92,6 @@ func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error
|
||||
&unit.UnitOption{"Unit", "OnFailure", "reaper.service"},
|
||||
&unit.UnitOption{"Unit", "Wants", "exit-watcher.service"},
|
||||
&unit.UnitOption{"Service", "Restart", "no"},
|
||||
&unit.UnitOption{"Service", "RootDirectory", rktpath.RelAppRootfsPath(id)},
|
||||
&unit.UnitOption{"Service", "ExecStart", execStart},
|
||||
&unit.UnitOption{"Service", "User", app.User},
|
||||
&unit.UnitOption{"Service", "Group", app.Group},
|
||||
@@ -107,7 +107,7 @@ func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error
|
||||
default:
|
||||
return fmt.Errorf("unrecognized eventHandler: %v", eh.Name)
|
||||
}
|
||||
exec := strings.Join(eh.Exec, " ")
|
||||
exec := strings.Join(append(execWrap, eh.Exec...), " ")
|
||||
opts = append(opts, &unit.UnitOption{"Service", typ, exec})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
_SUBDIRS=usr shim
|
||||
_SUBDIRS=usr shim diagexec
|
||||
SUBDIRS=$(_SUBDIRS) aggregate
|
||||
export CFLAGS=-Wall -Os
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ install -m 0644 units/reaper.service "$RD/usr/lib/systemd/system"
|
||||
install -m 0644 units/sockets.target "$RD/usr/lib/systemd/system"
|
||||
install -m 0755 scripts/reaper.sh "$RD"
|
||||
install -m 0755 ../shim/shim.so "$RD"
|
||||
install -m 0755 ../diagexec/diagexec "$RD"
|
||||
ln -s shim.so "$RD/fakesdboot.so"
|
||||
|
||||
install -d "$RD/etc"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
BIN=diagexec
|
||||
SRC=diagexec.c
|
||||
|
||||
$(BIN): $(SRC) elf.h Makefile
|
||||
$(CC) $(CFLAGS) -o $@ $(SRC) -static -s
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(BIN)
|
||||
|
||||
test:
|
||||
echo TODO
|
||||
@@ -0,0 +1,141 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "elf.h"
|
||||
|
||||
static int exit_err;
|
||||
#define exit_if(_cond, _fmt, _args...) \
|
||||
exit_err++; \
|
||||
if(_cond) { \
|
||||
fprintf(stderr, "Error: " _fmt "\n", ##_args); \
|
||||
exit(exit_err); \
|
||||
}
|
||||
#define pexit_if(_cond, _fmt, _args...) \
|
||||
exit_if(_cond, _fmt ": %s", ##_args, strerror(errno))
|
||||
|
||||
#define MAX_DIAG_DEPTH 10
|
||||
#define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
|
||||
|
||||
static void diag(const char *exe)
|
||||
{
|
||||
static const uint8_t elf[] = {0x7f, 'E', 'L', 'F'};
|
||||
static const uint8_t shebang[] = {'#','!'};
|
||||
static int diag_depth;
|
||||
int fd;
|
||||
struct stat st;
|
||||
const uint8_t *mm;
|
||||
const char *itrp = NULL;
|
||||
|
||||
pexit_if((fd = open(exe, O_RDONLY)) == -1,
|
||||
"Unable to open \"%s\"", exe);
|
||||
pexit_if(fstat(fd, &st) == -1,
|
||||
"Cannot stat \"%s\"", exe);
|
||||
exit_if(!S_ISREG(st.st_mode), "\"%s\" is not a regular file", exe);
|
||||
exit_if(!((S_IXUSR|S_IXGRP|S_IXOTH) & st.st_mode),
|
||||
"\"%s\" is not executable", exe)
|
||||
pexit_if(!(mm = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)),
|
||||
"Mmap of \"%s\" failed", exe);
|
||||
|
||||
if(st.st_size >= sizeof(shebang) &&
|
||||
!memcmp(mm, shebang, sizeof(shebang))) {
|
||||
const uint8_t *nl;
|
||||
int maxlen = MIN(PATH_MAX, st.st_size - sizeof(shebang));
|
||||
/* TODO(vc): EOF-terminated shebang lines are technically possible */
|
||||
exit_if(!(nl = memchr(&mm[sizeof(shebang)], '\n', maxlen)),
|
||||
"Shebang line too long");
|
||||
pexit_if(!(itrp = strndup((char *)&mm[sizeof(shebang)], (nl - mm) - 2)),
|
||||
"Failed to dup interpreter path");
|
||||
} else if(st.st_size >= sizeof(elf) &&
|
||||
!memcmp(mm, elf, sizeof(elf))) {
|
||||
uint64_t (*lget)(const uint8_t *) = NULL;
|
||||
uint32_t (*iget)(const uint8_t *) = NULL;
|
||||
uint16_t (*sget)(const uint8_t *) = NULL;
|
||||
const void *phoff = NULL, *phesz = NULL, *phecnt = NULL;
|
||||
const uint8_t *ph = NULL;
|
||||
int i, phreloff, phrelsz;
|
||||
|
||||
exit_if(mm[ELF_VERSION] != 1,
|
||||
"Unsupported ELF version: %hhx", mm[ELF_VERSION]);
|
||||
|
||||
/* determine which accessors to use and where */
|
||||
if(mm[ELF_BITS] == ELF_BITS_32) {
|
||||
if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) {
|
||||
lget = le32_lget;
|
||||
sget = le_sget;
|
||||
iget = le_iget;
|
||||
} else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) {
|
||||
lget = be32_lget;
|
||||
sget = be_sget;
|
||||
iget = be_iget;
|
||||
}
|
||||
phoff = &mm[ELF32_PHT_OFF];
|
||||
phesz = &mm[ELF32_PHTE_SIZE];
|
||||
phecnt = &mm[ELF32_PHTE_CNT];
|
||||
phreloff = ELF32_PHE_OFF;
|
||||
phrelsz = ELF32_PHE_SIZE;
|
||||
} else if(mm[ELF_BITS] == ELF_BITS_64) {
|
||||
if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) {
|
||||
lget = le64_lget;
|
||||
sget = le_sget;
|
||||
iget = le_iget;
|
||||
} else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) {
|
||||
lget = be64_lget;
|
||||
sget = be_sget;
|
||||
iget = be_iget;
|
||||
}
|
||||
phoff = &mm[ELF64_PHT_OFF];
|
||||
phesz = &mm[ELF64_PHTE_SIZE];
|
||||
phecnt = &mm[ELF64_PHTE_CNT];
|
||||
phreloff = ELF64_PHE_OFF;
|
||||
phrelsz = ELF64_PHE_SIZE;
|
||||
}
|
||||
|
||||
exit_if(!lget, "Unsupported ELF format");
|
||||
|
||||
if(!phoff) /* program header may be absent, don't make it an error */
|
||||
return;
|
||||
|
||||
/* TODO(vc): sanity checks on values before using them */
|
||||
for(ph = &mm[lget(phoff)], i = 0; i < sget(phecnt); i++, ph += sget(phesz)) {
|
||||
if(iget(ph) == ELF_PT_INTERP) {
|
||||
itrp = strndup((char *)&mm[lget(&ph[phreloff])], lget(&ph[phrelsz]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
exit_if(1, "Unsupported file type");
|
||||
}
|
||||
|
||||
exit_if(!itrp, "Unable to determine interpreter for \"%s\"", exe);
|
||||
exit_if(*itrp != '/', "Path must be absolute: \"%s\"", itrp);
|
||||
exit_if(++diag_depth > MAX_DIAG_DEPTH,
|
||||
"Excessive interpreter recursion, giving up");
|
||||
diag(itrp);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const char *root, *exe;
|
||||
exit_if(argc < 3,
|
||||
"Usage: %s /path/to/root /to/exec [args ...]", argv[0]);
|
||||
root = argv[1];
|
||||
exe = argv[2];
|
||||
pexit_if(chroot(root) == -1, "Chroot failed");
|
||||
pexit_if(chdir("/") == -1, "Chdir failed");
|
||||
pexit_if(execv(exe, &argv[2]) == -1 &&
|
||||
errno != ENOENT && errno != EACCES,
|
||||
"Exec of \"%s\" failed", exe);
|
||||
diag(exe);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/* just enough ELF support for finding the interpreter in the program header
|
||||
* table, this should theoretically work as-is on both big-endian and
|
||||
* little-endian
|
||||
*/
|
||||
|
||||
/* values of interest */
|
||||
#define ELF_BITS_32 0x1
|
||||
#define ELF_BITS_64 0x2
|
||||
#define ELF_ENDIAN_LITL 0x1
|
||||
#define ELF_ENDIAN_BIG 0x2
|
||||
#define ELF_PT_INTERP 0x3
|
||||
|
||||
/* offsets of interest */
|
||||
#define ELF_BITS 0x4
|
||||
#define ELF_ENDIAN 0x5
|
||||
#define ELF_VERSION 0x6
|
||||
#define ELF32_PHT_OFF 0x1c
|
||||
#define ELF32_PHTE_SIZE 0x2a
|
||||
#define ELF32_PHTE_CNT 0x2c
|
||||
#define ELF32_PHE_OFF 0x4
|
||||
#define ELF32_PHE_SIZE 0x10
|
||||
#define ELF64_PHT_OFF 0x20
|
||||
#define ELF64_PHTE_SIZE 0x36
|
||||
#define ELF64_PHTE_CNT 0x38
|
||||
#define ELF64_PHE_OFF 0x8
|
||||
#define ELF64_PHE_SIZE 0x20
|
||||
|
||||
/* multibyte value accessors, choose which based on ELF_BITS and ELF_ENDIAN */
|
||||
|
||||
#define SHIFT(_val, _bytes) ((unsigned long long)(_val) << ((_bytes) * 8))
|
||||
static uint64_t le32_lget(const uint8_t *addr)
|
||||
{
|
||||
uint64_t val = 0;
|
||||
val += SHIFT(addr[3], 3);
|
||||
val += SHIFT(addr[2], 2);
|
||||
val += SHIFT(addr[1], 1);
|
||||
val += SHIFT(addr[0], 0);
|
||||
return val;
|
||||
}
|
||||
static uint64_t be32_lget(const uint8_t *addr)
|
||||
{
|
||||
uint64_t val = 0;
|
||||
val += SHIFT(addr[0], 3);
|
||||
val += SHIFT(addr[1], 2);
|
||||
val += SHIFT(addr[2], 1);
|
||||
val += SHIFT(addr[3], 0);
|
||||
return val;
|
||||
}
|
||||
|
||||
static uint64_t le64_lget(const uint8_t *addr)
|
||||
{
|
||||
uint64_t val = 0;
|
||||
val += SHIFT(addr[7], 7);
|
||||
val += SHIFT(addr[6], 6);
|
||||
val += SHIFT(addr[5], 5);
|
||||
val += SHIFT(addr[4], 4);
|
||||
val += SHIFT(addr[3], 3);
|
||||
val += SHIFT(addr[2], 2);
|
||||
val += SHIFT(addr[1], 1);
|
||||
val += SHIFT(addr[0], 0);
|
||||
return val;
|
||||
}
|
||||
static uint64_t be64_lget(const uint8_t *addr)
|
||||
{
|
||||
uint64_t val = 0;
|
||||
val += SHIFT(addr[0], 7);
|
||||
val += SHIFT(addr[1], 6);
|
||||
val += SHIFT(addr[2], 5);
|
||||
val += SHIFT(addr[3], 4);
|
||||
val += SHIFT(addr[4], 3);
|
||||
val += SHIFT(addr[5], 2);
|
||||
val += SHIFT(addr[6], 1);
|
||||
val += SHIFT(addr[7], 0);
|
||||
return val;
|
||||
}
|
||||
static uint32_t le_iget(const uint8_t *addr)
|
||||
{
|
||||
return (uint32_t)le32_lget(addr);
|
||||
}
|
||||
static uint32_t be_iget(const uint8_t *addr)
|
||||
{
|
||||
return (uint32_t)be32_lget(addr);
|
||||
}
|
||||
static uint16_t le_sget(const uint8_t *addr)
|
||||
{
|
||||
uint16_t val = 0;
|
||||
val += SHIFT(addr[1], 1);
|
||||
val += SHIFT(addr[0], 0);
|
||||
return val;
|
||||
}
|
||||
static uint16_t be_sget(const uint8_t *addr)
|
||||
{
|
||||
uint16_t val = 0;
|
||||
val += SHIFT(addr[0], 0);
|
||||
val += SHIFT(addr[1], 1);
|
||||
return val;
|
||||
}
|
||||
Reference in New Issue
Block a user