xref: /NextBSD/usr.bin/mach-tests/set-bport/set-bport.c (revision 33da5adc555b3bc29986eeadca03829e4ad06b1e)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <mach/mach.h>
5 
6 mach_port_t bootstrap_port;
7 static void
error(int exitcode,int macherr,const char * funcname)8 error(int exitcode, int macherr, const char *funcname)
9 {
10 #if 0
11 	mach_error(funcname, macherr);
12 #endif
13 	printf("%s failed with %x/%d\n", funcname, macherr, macherr);
14 	exit(1);
15 }
16 
17 int
main(int argc,char ** argv)18 main(int argc, char **argv)
19 {
20 	int err;
21 	mach_port_t port, bport;
22 
23 	bport = -1;
24 
25 	err = mach_port_allocate(mach_task_self(),
26 							 MACH_PORT_RIGHT_RECEIVE, &port);
27 	if (err)
28 		error(1, err, "mach_port_allocate");
29 
30 	err = mach_port_insert_right (mach_task_self (),
31 				      port,
32 				      port,
33 				      MACH_MSG_TYPE_MAKE_SEND);
34 
35 	if (err)
36 		error(10, err, "mach_port_insert_right");
37 
38 	printf("setting bootstrap port to %d\n", port);
39 	sleep(1);
40 	err = task_set_bootstrap_port(mach_task_self(), port);
41 	if (err)
42 		error(1, err, "task_set_bootstrap_port");
43 
44 	err = task_get_bootstrap_port(mach_task_self(), &bport);
45 	if (err)
46 		error(1, err, "task_get_bootstrap_port");
47 	printf("bport=%d\n", bport);
48 
49 	return (0);
50 }
51