StartupItemContext.c [plain text]
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <servers/bootstrap.h>
int main(int argc, char *argv[])
{
mach_port_t root_bootstrap_port;
kern_return_t ret;
#if 0
task_t init_task;
int err;
#endif
if (argc < 2) {
fprintf(stderr, "usage: %s executable [args...]\n", argv[0]);
exit(1);
}
if (geteuid() != 0) {
fprintf(stderr, "%s: permission denied: must be run as root\n", argv[0]);
return(EPERM);
exit(1);
}
#if 0
ret = task_for_pid((mach_task_self)(), 1, &init_task);
if (ret != KERN_SUCCESS) {
fprintf(stderr, "%s: task_for_pid() failed: permission denied\n",
argv[0]);
exit(1);
}
ret = task_get_bootstrap_port(init_task, &root_bootstrap_port);
if (ret != KERN_SUCCESS) {
fprintf(stderr, "%s: task_get_bootstrap_port() failed: %s\n",
argv[0], mach_error_string(ret));
exit(2);
}
#else
{
mach_port_t cur_bootstrap;
root_bootstrap_port = bootstrap_port;
do {
cur_bootstrap = root_bootstrap_port;
ret = bootstrap_parent(cur_bootstrap, &root_bootstrap_port);
if (ret == BOOTSTRAP_NOT_PRIVILEGED) {
fprintf(stderr, "%s: must be run as root\n", argv[0]);
exit(1);
}
} while (root_bootstrap_port != cur_bootstrap);
}
#endif
ret = task_set_bootstrap_port(mach_task_self(), root_bootstrap_port);
if (ret != KERN_SUCCESS) {
fprintf(stderr, "%s: task_set_bootstrap_port() failed: %s\n",
argv[0], mach_error_string(ret));
exit(3);
}
execv(argv[1], &argv[1]);
fprintf(stderr, "%s: exec failed: %s(%d)\n", argv[0], strerror(errno), errno);
return(4);
}