diff options
author | comex | 2015-03-01 19:16:24 -0500 |
---|---|---|
committer | comex | 2015-03-01 19:16:24 -0500 |
commit | 8baebb7886a93e71863ac94a24e10477a42c127e (patch) | |
tree | 087e17ae72960dbbc74a369cc7727f881b7dc9c9 | |
parent | Let's get hacky! (diff) | |
download | substitute-8baebb7886a93e71863ac94a24e10477a42c127e.tar.gz |
minor changes
-rw-r--r-- | darwin-bootstrap/bundle-loader.c | 38 | ||||
-rw-r--r-- | darwin-bootstrap/ib-log.h | 19 | ||||
-rw-r--r-- | darwin-bootstrap/substituted-messages.h | 6 | ||||
-rw-r--r-- | darwin-bootstrap/substituted.c | 2 | ||||
-rwxr-xr-x | script/gen-deb.sh | 2 |
5 files changed, 52 insertions, 15 deletions
diff --git a/darwin-bootstrap/bundle-loader.c b/darwin-bootstrap/bundle-loader.c index 6de63d9..74913b7 100644 --- a/darwin-bootstrap/bundle-loader.c +++ b/darwin-bootstrap/bundle-loader.c @@ -1,3 +1,6 @@ +#define IB_LOG_NAME "bundle-loader" +#define IB_LOG_TO_SYSLOG +#include "ib-log.h" #include "darwin/mach-decls.h" #include "substituted-messages.h" #include <dlfcn.h> @@ -23,7 +26,7 @@ static bool pop(const void **buf, const void *end, op = *buf; *buf += sizeof(*op); *opc = op->opc; - if ((size_t) (end - *buf) < op->namelen + 1 || + if ((size_t) (end - *buf) <= op->namelen || ((const char *) buf)[op->namelen] != '\0') return false; *name = *buf; @@ -83,27 +86,38 @@ static bool objc_has_class(const char *name) { } static void use_dylib(const char *name) { - syslog(LOG_ERR, "loading dylib %s", name); + ib_log("loading dylib %s", name); // .. } static void load_bundle_list(const void *buf, size_t size) { + if (IB_VERBOSE) { + ib_log("load_bundle_list: %p,%zu", buf, size); + ib_log_hex(buf, size); + } int opc; const char *name; const void *end = buf + size; while (buf != end) { if (!pop(&buf, end, &opc, &name)) goto invalid; + bool pass; switch (opc) { case SUBSTITUTED_TEST_BUNDLE: - if (cf_has_bundle(name)) + pass = cf_has_bundle(name); + if (IB_VERBOSE) + ib_log("cf_has_bundle('%s'): %d", name, pass); + if (pass) goto pass_type; /* fail, so... */ if (peek(buf, end) != SUBSTITUTED_TEST_BUNDLE) goto fail; break; case SUBSTITUTED_TEST_CLASS: - if (objc_has_class(name)) + pass = objc_has_class(name); + if (IB_VERBOSE) + ib_log("objc_has_class('%s'): %d", name, pass); + if (pass) goto pass_type; if (peek(buf, end) != SUBSTITUTED_TEST_CLASS) goto fail; @@ -126,7 +140,7 @@ static void load_bundle_list(const void *buf, size_t size) { } return; invalid: - syslog(LOG_ERR, "invalid bundle list data"); + ib_log("invalid bundle list data"); } static kern_return_t substituted_hello(mach_port_t service, int proto_version, @@ -149,9 +163,12 @@ static kern_return_t substituted_hello(mach_port_t service, int proto_version, buf.hdr.msgh_id = SUBSTITUTED_MSG_HELLO; buf.u.req.proto_version = proto_version; strlcpy(buf.u.req.argv0, argv0, sizeof(buf.u.req.argv0)); - buf.hdr.msgh_size = sizeof(buf.hdr) + - sizeof(buf.u.req) + - strlen(buf.u.req.argv0); + size_t size = sizeof(buf.hdr) + + offsetof(struct substituted_msg_body_hello, argv0) + + strlen(buf.u.req.argv0); + size_t round = round_msg(size); + memset((char *) &buf + size, 0, round - size); + buf.hdr.msgh_size = round; kern_return_t kr = mach_msg(&buf.hdr, MACH_RCV_MSG | MACH_SEND_MSG, buf.hdr.msgh_size, sizeof(buf), reply_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); @@ -171,6 +188,7 @@ static kern_return_t substituted_hello(mach_port_t service, int proto_version, } if (buf.u.resp.error) { + ib_log("substituted_hello returned error %x", buf.u.resp.error); kr = KERN_FAILURE; goto out; } @@ -191,7 +209,7 @@ static void init() { kern_return_t kr = bootstrap_look_up(bootstrap_port, "com.ex.substituted", &service); if (kr) { - syslog(LOG_ERR, "bootstrap_look_up com.ex.substituted: %x", kr); + ib_log("bootstrap_look_up com.ex.substituted: %x", kr); return; } const char *argv0 = (*_NSGetArgv())[0]; @@ -200,7 +218,7 @@ static void init() { kr = substituted_hello(service, SUBSTITUTED_PROTO_VERSION, argv0, &bundle_list, &bundle_list_size); if (kr) { - syslog(LOG_ERR, "substituted_hello: %x", kr); + ib_log("substituted_hello: %x", kr); return; } load_bundle_list(bundle_list, bundle_list_size); diff --git a/darwin-bootstrap/ib-log.h b/darwin-bootstrap/ib-log.h index fee70b5..c80ae63 100644 --- a/darwin-bootstrap/ib-log.h +++ b/darwin-bootstrap/ib-log.h @@ -1,8 +1,13 @@ #pragma once #include <dispatch/dispatch.h> #include <stdio.h> +#include <stdlib.h> #include <unistd.h> +#ifdef IB_LOG_TO_SYSLOG +#include <syslog.h> +#define ib_log(fmt, args...) syslog(LOG_ERR, IB_LOG_NAME ": " fmt, ##args) +#else static FILE *logfp; static void open_logfp_if_necessary() { /* syslog() doesn't seem to work from launchd... */ @@ -23,5 +28,17 @@ static void open_logfp_if_necessary() { fprintf(logfp, fmt "\n", ##args); \ fflush(logfp); \ } while(0) +#endif -#define IB_VERBOSE 0 +static inline void ib_log_hex(const void *buf, size_t size) { + const uint8_t *up = buf; + char *hex = malloc(2 * size + 1), *p = hex; + for (size_t i = 0; i < size; i++) { + sprintf(p, "%02x", up[i]); + p += 2; + } + ib_log("%s", hex); + free(hex); +} + +#define IB_VERBOSE 1 diff --git a/darwin-bootstrap/substituted-messages.h b/darwin-bootstrap/substituted-messages.h index 62b92b3..04a3378 100644 --- a/darwin-bootstrap/substituted-messages.h +++ b/darwin-bootstrap/substituted-messages.h @@ -22,7 +22,9 @@ struct substituted_msg_body_hello_resp { int error; }; -/* bundle_list: a bunch of substituted_bundle_list_ops */ +/* bundle_list: a bunch of substituted_bundle_list_ops + * this is pretty silly because even low-level bootstrap_lookup uses xpc now - + * so could have just used xpc structures - but this is more fun */ enum substituted_bundle_list_opc { SUBSTITUTED_TEST_BUNDLE, @@ -34,4 +36,4 @@ struct substituted_bundle_list_op { uint16_t namelen; uint8_t opc; /* char name[namelen + 1]; */ -}; +} __attribute__((packed)); diff --git a/darwin-bootstrap/substituted.c b/darwin-bootstrap/substituted.c index 7eade8b..6b1693e 100644 --- a/darwin-bootstrap/substituted.c +++ b/darwin-bootstrap/substituted.c @@ -31,7 +31,7 @@ struct msgbuf { static void handle_hello(struct msgbuf *buf, pid_t pid) { if (buf->hdr.msgh_size < offsetof(struct msgbuf, u.hello.argv0)) { - ib_log("mesage too short"); + ib_log("message too short"); return; } ((char *) buf)[buf->hdr.msgh_size] = '\0'; /* overwrite trailer or whatever */ diff --git a/script/gen-deb.sh b/script/gen-deb.sh index c474e63..cd3298e 100755 --- a/script/gen-deb.sh +++ b/script/gen-deb.sh @@ -22,7 +22,7 @@ cp -a DEBIAN $debroot/ sed "s#{VERSION}#$version#g" DEBIAN/control > $debroot/DEBIAN/control #... add bootstrap stuff # yay, old forks and deprecated compression -dpkg-deb -Zlzma -b $debroot out/com.ex.substitute-$version.deb +fakeroot dpkg-deb -Zlzma -b $debroot out/com.ex.substitute-$version.deb |