Beispiel #1
0
int
bsexec(int argc, char *const argv[])
{
    kern_return_t result;
    mach_port_t bport;

    if (argc < 3) {
        fprintf(stderr, "usage: %s bsexec <PID> prog...\n", getprogname());
        return 1;
    }

    bport = str2bsport(argv[1]);

    result = task_set_bootstrap_port(mach_task_self(), bport);

    if (result != KERN_SUCCESS) {
        fprintf(stderr, "Couldn't switch to new bootstrap port: %s\n", mach_error_string(result));
        return 1;
    }

    setgid(getgid());
    setuid(getuid());

    if (fwexec((const char *const *)argv + 2, NULL) == -1) {
        fprintf(stderr, "%s bsexec failed: %s\n", getprogname(), strerror(errno));
        return 1;
    }

    return 0;
}
Beispiel #2
0
int 
main(int argc, char *argv[])
{
	struct kevent	kev;
	Action          anAction = kActionStart;
	int             ch, r, kq = kqueue();

	assert(kq  != -1);

	EV_SET(&kev, SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
	r = kevent(kq, &kev, 1, NULL, 0, NULL);
	assert(r != -1);
	signal(SIGTERM, dummy_sig);

	while ((ch = getopt(argc, argv, "gvxirdDqn?")) != -1) {
		switch (ch) {
		case 'v':
			gVerboseFlag = true;
			break;
		case 'x':
		case 'g':
		case 'r':
		case 'q':
			break;
		case 'd':
		case 'D':
			gDebugFlag = true;
			break;
		case 'n':
			gNoRunFlag = true;
			break;
		case '?':
		default:
			usage();
			break;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc > 2) {
		usage();
	}

	openlog(getprogname(), LOG_PID|LOG_CONS|(gDebugFlag ? LOG_PERROR : 0), LOG_DAEMON);
	if (gDebugFlag) {
		setlogmask(LOG_UPTO(LOG_DEBUG));
	} else if (gVerboseFlag) {
		setlogmask(LOG_UPTO(LOG_INFO));
	} else {
		setlogmask(LOG_UPTO(LOG_NOTICE));
	}

	if (!gNoRunFlag && (getuid() != 0)) {
		syslog(LOG_ERR, "must be root to run");
		exit(EXIT_FAILURE);
	}

	if (argc > 0) {
		if (strcmp(argv[0], "start") == 0) {
			anAction = kActionStart;
		} else if (strcmp(argv[0], "stop") == 0) {
			anAction = kActionStop;
		} else if (strcmp(argv[0], "restart") == 0) {
			anAction = kActionRestart;
		} else {
			usage();
		}
	}

	if (argc == 2) {
		exit(system_starter(anAction, argv[1]));
	}

	unlink(kFixerPath);

	mach_timespec_t w = { 600, 0 };
	kern_return_t kr;

	/*
	 * Too many old StartupItems had implicit dependancies on "Network" via
	 * other StartupItems that are now no-ops.
	 *
	 * SystemStarter is not on the critical path for boot up, so we'll
	 * stall here to deal with this legacy dependancy problem.
	 */

	if ((kr = IOKitWaitQuiet(kIOMasterPortDefault, &w)) != kIOReturnSuccess) {
		syslog(LOG_NOTICE, "IOKitWaitQuiet: %d\n", kr);
	}

	fwexec("/usr/sbin/ipconfig", "waitall", NULL);
	autodiskmount(); /* wait for Disk Arbitration to report idle */

	system_starter(kActionStart, NULL);

	if (StartupItemSecurityCheck("/etc/rc.local")) {
		fwexec(_PATH_BSHELL, "/etc/rc.local", NULL);
	}

	CFNotificationCenterPostNotificationWithOptions(
			CFNotificationCenterGetDistributedCenter(),
			CFSTR("com.apple.startupitems.completed"),
			NULL, NULL,
			kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);

	r = kevent(kq, NULL, 0, &kev, 1, NULL);
	assert(r != -1);
	assert(kev.filter == EVFILT_SIGNAL && kev.ident == SIGTERM);

	if (StartupItemSecurityCheck("/etc/rc.shutdown.local")) {
		fwexec(_PATH_BSHELL, "/etc/rc.shutdown.local", NULL);
	}

	system_starter(kActionStop, NULL);

	exit(EXIT_SUCCESS);
}