1/*
2cc tools/iosetarg.c -o /tmp/iosetarg -framework IOKit -framework CoreFoundation -g -Wall
3*/
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <assert.h>
8#include <IOKit/IOKitLib.h>
9#include <mach/mach_error.h>
10
11#ifndef kIODebugArgumentsKey
12#define kIODebugArgumentsKey "IODebugArguments"
13#endif
14
15int main(int argc, char * argv[])
16{
17	kern_return_t          kr;
18    io_service_t           service;
19    CFStringRef            str;
20    CFMutableArrayRef	   array;
21    CFMutableDictionaryRef matching;
22    uint32_t	           idx;
23    uint64_t               id;
24
25	if (argc < 3) exit(1);
26
27    id = strtoll(argv[1], NULL, 0);
28
29    matching = id ? IORegistryEntryIDMatching(id) : IOServiceMatching(argv[1]);
30
31	array = CFArrayCreateMutable(kCFAllocatorDefault, argc - 2, &kCFTypeArrayCallBacks);
32
33	for (idx = 2; idx < argc; idx++)
34	{
35		str = CFStringCreateWithCString(kCFAllocatorDefault, argv[idx], CFStringGetSystemEncoding());
36		CFArrayAppendValue(array, str);
37	}
38
39    service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
40    assert(service);
41	kr = IORegistryEntrySetCFProperty(service, CFSTR(kIODebugArgumentsKey), array);
42
43	printf("result: 0x%x, %s\n", kr, mach_error_string(kr));
44
45	exit(0);
46}