]> git.saurik.com Git - apple/system_cmds.git/blob - mach_init.tproj/testServer/boot_subset.c
system_cmds-175.tar.gz
[apple/system_cmds.git] / mach_init.tproj / testServer / boot_subset.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Bootstrap subset exercise.
26 *
27 * do {
28 * create a subset port with requestor = req_port;
29 * register "foo" on subset_port;
30 * deallocate req_port;
31 * } until error;
32 */
33
34 #import <sys/types.h>
35 #import <mach.h>
36 #import <servers/bootstrap.h>
37 #import <libc.h>
38 #import <mach_error.h>
39
40 void log_boot_servers(port_t boot_port);
41
42 int main(int argc, char **argv)
43 {
44 kern_return_t krtn;
45 port_t subset_port;
46 port_t requestor_port;
47 port_t foo_port;
48 int loop = 0;
49 int deallocate_subset = 0;
50
51 if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 'r') {
52 port_t newboot;
53
54 krtn = bootstrap_look_up(bootstrap_port, &argv[1][2], &newboot);
55 if (krtn) {
56 mach_error("bootstrap lookup", krtn);
57 exit(1);
58 }
59 bootstrap_port = newboot;
60 --argc; ++argv;
61 }
62 if(argc >= 2) {
63 if(argv[1][0] == '-' && argv[1][0] == 'd')
64 deallocate_subset = 1;
65 }
66
67 /*
68 * Allocate some resources.
69 */
70 krtn = port_allocate(task_self(), &foo_port);
71 if(krtn) {
72 mach_error("port_allocate", krtn);
73 exit(1);
74 }
75
76 do {
77 krtn = port_allocate(task_self(), &requestor_port);
78 if(krtn) {
79 mach_error("port_allocate", krtn);
80 exit(1);
81 }
82 krtn = bootstrap_subset(bootstrap_port,
83 requestor_port, /* requestor */
84 &subset_port);
85 if(krtn) {
86 mach_error("bootstrap_subset", krtn);
87 break;
88 }
89 printf("Loop %d, prior to bootstrap_register:\n", loop);
90 log_boot_servers(subset_port);
91
92 krtn = bootstrap_register(subset_port,
93 "foo",
94 foo_port);
95 if(krtn) {
96 mach_error("bootstrap_register (subset)", krtn);
97 exit(1);
98 }
99 printf("Loop %d, after bootstrap_register:\n", loop);
100 log_boot_servers(subset_port);
101
102 /*
103 * Delete requestor_port, subset should go away.
104 */
105 krtn = port_deallocate(task_self(), requestor_port);
106 if(krtn) {
107 mach_error("port_deallocate", krtn);
108 exit(1);
109 }
110
111 if(deallocate_subset) {
112 krtn = port_deallocate(task_self(), subset_port);
113 if(krtn) {
114 mach_error("port_deallocate(subset)", krtn);
115 exit(1);
116 }
117 }
118 loop++;
119 } while(krtn == KERN_SUCCESS);
120
121 printf("...done\n");
122 exit(0);
123 }
124
125 void log_boot_servers(port_t boot_port)
126 {
127 int i;
128 name_array_t service_names;
129 unsigned int service_cnt;
130 name_array_t server_names;
131 unsigned int server_cnt;
132 bool_array_t service_active;
133 unsigned int service_active_cnt;
134 kern_return_t krtn;
135
136 krtn = bootstrap_info(boot_port,
137 &service_names,
138 &service_cnt,
139 &server_names,
140 &server_cnt,
141 &service_active,
142 &service_active_cnt);
143 if (krtn != BOOTSTRAP_SUCCESS)
144 printf("ERROR: info failed: %d", krtn);
145 else {
146 printf("log_boot_server: service_cnt = %d\n", service_cnt);
147 for (i = 0; i < service_cnt; i++)
148 printf("Name: %-15s Server: %-15s "
149 "Active: %-4s",
150 service_names[i],
151 server_names[i][0] == '\0' ?
152 "Unknown" : server_names[i],
153 service_active[i] ? "Yes\n" : "No\n");
154 }
155 }
156
157