4 #include <dispatch/dispatch.h>
6 #include <mach/mach_port.h>
8 #include <mach-o/dyld.h>
14 launch_data_t msg
, config
, dict
, array
, val
;
16 config
= launch_data_alloc(LAUNCH_DATA_DICTIONARY
);
18 val
= launch_data_new_string("com.apple.test.mach-server");
19 launch_data_dict_insert(config
, val
, LAUNCH_JOBKEY_LABEL
);
21 val
= launch_data_new_bool(1);
22 dict
= launch_data_alloc(LAUNCH_DATA_DICTIONARY
);
23 launch_data_dict_insert(dict
, val
, "com.apple.test.mach-server");
24 launch_data_dict_insert(config
, dict
, LAUNCH_JOBKEY_MACHSERVICES
);
27 uint32_t size
= sizeof(path
);
28 _NSGetExecutablePath(path
, &size
);
30 array
= launch_data_alloc(LAUNCH_DATA_ARRAY
);
31 val
= launch_data_new_string(path
);
32 launch_data_array_set_index(array
, val
, 0);
33 val
= launch_data_new_string("-launchd");
34 launch_data_array_set_index(array
, val
, 1);
35 launch_data_dict_insert(config
, array
, LAUNCH_JOBKEY_PROGRAMARGUMENTS
);
38 msg
= launch_data_alloc(LAUNCH_DATA_DICTIONARY
);
39 launch_data_dict_insert(msg
, config
, LAUNCH_KEY_SUBMITJOB
);
40 fprintf(stderr
, "loading launchd job com.apple.test.mach-server\n");
41 msg
= launch_msg(msg
);
42 if (msg
&& launch_data_get_type(config
) == LAUNCH_DATA_ERRNO
) {
43 fprintf(stderr
, "launch load failed: %s\n", strerror(launch_data_get_errno(msg
)));
45 fprintf(stderr
, "successful\n");
52 launch_data_t config
= NULL
, checkin
= NULL
;
53 checkin
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
54 config
= launch_msg(checkin
);
55 if (!config
|| launch_data_get_type(config
) == LAUNCH_DATA_ERRNO
)
56 return MACH_PORT_NULL
;
59 svc
= launch_data_dict_lookup(config
, LAUNCH_JOBKEY_MACHSERVICES
);
60 if (!svc
) return MACH_PORT_NULL
;
62 svc
= launch_data_dict_lookup(svc
, "com.apple.test.mach-server");
63 if (!svc
) return MACH_PORT_NULL
;
65 mach_port_t mp
= launch_data_get_machport(svc
);
70 #include <CoreFoundation/CoreFoundation.h>
72 void my_cf_callback(CFMachPortRef mp
, void *msg
, CFIndex size
, void *info
)
74 char *payload
= (char *)((uintptr_t)msg
+ sizeof(mach_msg_header_t
));
75 asl_log(NULL
, NULL
, ASL_LEVEL_NOTICE
, "message received: %s %s", payload
, (char *)info
);
78 DISPATCH_CFMACHPORT_CALLBACK_DECL(my_mig_callback
, my_cf_callback
);
81 mach_msg_header_t header
;
83 mach_msg_security_trailer_t trailer
;
87 client(int argc
, char* argv
[])
93 char *str
= (argc
> 1) ? argv
[1] : "<unspecified>";
95 kr
= bootstrap_look_up(bootstrap_port
, "com.apple.test.mach-server",
97 printf("lookup %s\n", !kr
? "successful" : mach_error_string(kr
));
99 strlcpy(msg
.payload
, str
, sizeof(msg
.payload
));
100 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,MACH_MSG_TYPE_MAKE_SEND_ONCE
);
101 msg
.header
.msgh_size
= round_msg(sizeof(msg
.header
) + strlen(msg
.payload
) + 1);
102 msg
.header
.msgh_remote_port
= mp
;
103 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
104 msg
.header
.msgh_id
= 0x12345678;
105 kr
= mach_msg(&msg
.header
, MACH_SEND_MSG
, msg
.header
.msgh_size
, 0, MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
106 printf("client send %s\n", !kr
? "successful" : mach_error_string(kr
));
109 int main(int argc
, char* argv
[])
111 if (argc
> 1 && strcmp(argv
[1], "-launchd") == 0) {
112 mach_port_t mp
= checkin();
113 printf("checkin %s\n", mp
? "successful" : "failed");
116 kr
= mach_port_set_context(mach_task_self(), mp
, (mach_vm_address_t
)"phear");
118 dispatch_source_t mig
= dispatch_source_mig_create(mp
, 0 /* max msg size */,
119 NULL
, dispatch_get_main_queue(), my_mig_callback
);
122 } else if (argc
> 1) {