bootstrap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #ifndef __BOOTSTRAP_H__
  2. #define __BOOTSTRAP_H__
  3. /*
  4. * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
  5. *
  6. * @APPLE_APACHE_LICENSE_HEADER_START@
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * @APPLE_APACHE_LICENSE_HEADER_END@
  21. */
  22. /*
  23. * bootstrap -- fundamental service initiator and port server
  24. * Mike DeMoney, NeXT, Inc.
  25. * Copyright, 1990. All rights reserved.
  26. */
  27. /*
  28. * Interface: Bootstrap server
  29. *
  30. * The bootstrap server is the first user-mode task initiated by the Mach
  31. * kernel at system boot time. The bootstrap server provides two services,
  32. * it initiates other system tasks, and manages a table of name-port bindings
  33. * for fundamental system services (e.g. lookupd, Window Manager, etc...).
  34. *
  35. * Name-port bindings can be established with the bootstrap server by either
  36. * of two mechanisms:
  37. *
  38. * 1. The binding can be indicated, in advance of the service that backs it
  39. * being available, via a "service create" request. In this case, bootstrap
  40. * will immediately create a port and bind the indicated name with that port.
  41. * At a later time, a service may "checkin" for the name-port
  42. * binding and will be returned receive rights for the bound port. Lookup's
  43. * on bindings created by this mechanism will return send rights to the port,
  44. * even if no service has "checked-in". In this case, requests sent to the
  45. * bound port will be queued until a server has checked-in and can satisfy the
  46. * request.
  47. *
  48. * 2. Bindings can be established dynamically via a "register" request. In
  49. * this case, the register request provides bootstrap with a name and send
  50. * rights for a port. Bootstrap will provide send rights for the bound port
  51. * to any requestor via the lookup request.
  52. *
  53. * Bootstrap provides its service port to descendant tasks via the Mach
  54. * "bootstrap" special task port. All direct descendants of bootstrap receive
  55. * a "privileged" bootstrap service port. System services that initiate
  56. * untrusted tasks should replace the Mach bootstrap task special port with
  57. * a subset bootstrap port to prevent them from infecting the namespace.
  58. *
  59. * The bootstrap server creates a "backup" port for each service that it
  60. * creates. This is used to detect when a checked out service is no longer
  61. * being served. The bootstrap server regains all rights to the port and
  62. * it is marked available for check-out again. This allows crashed servers to
  63. * resume service to previous clients. Lookup's on this named port will
  64. * continue to be serviced by bootstrap while holding receive rights for the
  65. * bound port. A client may detect that the service is inactive via the
  66. * bootstrap status request. If an inactive service re-registers rather
  67. * than "checking-in" the original bound port is destroyed.
  68. *
  69. * The status of a named service may be obtained via the "status" request.
  70. * A service is "active" if a name-port binding exists and receive rights
  71. * to the bound port are held by a task other than bootstrap.
  72. *
  73. * The bootstrap server may also (re)start server processes associated with
  74. * with a set of services. The definition of the server process is done
  75. * through the "create server" request. The server will be launched in the
  76. * same bootstrap context in which it was registered.
  77. */
  78. #include <AvailabilityMacros.h>
  79. #include <mach/std_types.h>
  80. #include <mach/message.h>
  81. #include <sys/types.h>
  82. #include <sys/cdefs.h>
  83. #include <stdbool.h>
  84. __BEGIN_DECLS
  85. #pragma GCC visibility push(default)
  86. #define BOOTSTRAP_MAX_NAME_LEN 128
  87. #define BOOTSTRAP_MAX_CMD_LEN 512
  88. typedef char name_t[BOOTSTRAP_MAX_NAME_LEN];
  89. typedef char cmd_t[BOOTSTRAP_MAX_CMD_LEN];
  90. typedef name_t *name_array_t;
  91. typedef int bootstrap_status_t;
  92. typedef bootstrap_status_t *bootstrap_status_array_t;
  93. typedef unsigned int bootstrap_property_t;
  94. typedef bootstrap_property_t * bootstrap_property_array_t;
  95. typedef boolean_t *bool_array_t;
  96. #define BOOTSTRAP_MAX_LOOKUP_COUNT 20
  97. #define BOOTSTRAP_SUCCESS 0
  98. #define BOOTSTRAP_NOT_PRIVILEGED 1100
  99. #define BOOTSTRAP_NAME_IN_USE 1101
  100. #define BOOTSTRAP_UNKNOWN_SERVICE 1102
  101. #define BOOTSTRAP_SERVICE_ACTIVE 1103
  102. #define BOOTSTRAP_BAD_COUNT 1104
  103. #define BOOTSTRAP_NO_MEMORY 1105
  104. #define BOOTSTRAP_NO_CHILDREN 1106
  105. #define BOOTSTRAP_STATUS_INACTIVE 0
  106. #define BOOTSTRAP_STATUS_ACTIVE 1
  107. #define BOOTSTRAP_STATUS_ON_DEMAND 2
  108. /*
  109. * After main() starts, it is safe to assume that this variable is always set.
  110. */
  111. extern mach_port_t bootstrap_port;
  112. /*
  113. * bootstrap_create_server()
  114. *
  115. * Declares a server that mach_init will re-spawn within the specified
  116. * bootstrap context. The server is considered already "active"
  117. * (i.e. will not be re-spawned) until the returned server_port is
  118. * deallocated.
  119. *
  120. * In the meantime, services can be declared against the server,
  121. * by using the server_port as the privileged bootstrap target of
  122. * subsequent bootstrap_create_service() calls.
  123. *
  124. * When mach_init re-spawns the server, its task bootstrap port
  125. * is set to the privileged sever_port. Through this special
  126. * bootstrap port, it can access all of parent bootstrap's context
  127. * (and all services are created in the parent's namespace). But
  128. * all additional service declarations (and declaration removals)
  129. * will be associated with this particular server.
  130. *
  131. * Only a holder of the server_port privilege bootstrap port can
  132. * check in or register over those services.
  133. *
  134. * When all services associated with a server are deleted, and the server
  135. * exits, it will automatically be deleted itself.
  136. *
  137. * If the server is declared "on_demand," then a non-running server
  138. * will be re-launched on first use of one of the service ports
  139. * registered against it. Otherwise, it will be re-launched
  140. * immediately upon exiting (whether any client is actively using
  141. * any of the service ports or not).
  142. *
  143. * Errors: Returns appropriate kernel errors on rpc failure.
  144. * Returns BOOTSTRAP_NOT_PRIVILEGED, bootstrap or uid invalid.
  145. */
  146. kern_return_t bootstrap_create_server(
  147. mach_port_t bp,
  148. cmd_t server_cmd,
  149. uid_t server_uid,
  150. boolean_t on_demand,
  151. mach_port_t *server_port);
  152. /*
  153. * bootstrap_subset()
  154. *
  155. * Returns a new port to use as a bootstrap port. This port behaves
  156. * exactly like the previous bootstrap_port, except that ports dynamically
  157. * registered via bootstrap_register() are available only to users of this
  158. * specific subset_port. Lookups on the subset_port will return ports
  159. * registered with this port specifically, and ports registered with
  160. * ancestors of this subset_port. Duplications of services already
  161. * registered with an ancestor port may be registered with the subset port
  162. * are allowed. Services already advertised may then be effectively removed
  163. * by registering PORT_NULL for the service.
  164. * When it is detected that the requestor_port is destroyed the subset
  165. * port and all services advertized by it are destroyed as well.
  166. *
  167. * Errors: Returns appropriate kernel errors on rpc failure.
  168. */
  169. kern_return_t bootstrap_subset(
  170. mach_port_t bp,
  171. mach_port_t requestor_port,
  172. mach_port_t *subset_port);
  173. /*
  174. * bootstrap_unprivileged()
  175. *
  176. * Given a bootstrap port, return its unprivileged equivalent. If
  177. * the port is already unprivileged, another reference to the same
  178. * port is returned.
  179. *
  180. * This is most often used by servers, which are launched with their
  181. * bootstrap port set to the privileged port for the server, to get
  182. * an unprivileged version of the same port for use by its unprivileged
  183. * children (or any offspring that it does not want to count as part
  184. * of the "server" for mach_init registration and re-launch purposes).
  185. *
  186. * Native launchd jobs are always started with an unprivileged port.
  187. */
  188. kern_return_t bootstrap_unprivileged(
  189. mach_port_t bp,
  190. mach_port_t *unpriv_port)
  191. AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_5;
  192. /*
  193. * bootstrap_parent()
  194. *
  195. * Given a bootstrap subset port, return the parent bootstrap port.
  196. * If the specified bootstrap port is already the root subset,
  197. * the same port will be returned. Much like "." and ".." are the same
  198. * in the file system name space for the root directory ("/").
  199. *
  200. * Errors:
  201. * Returns BOOTSTRAP_NOT_PRIVILEGED if the caller is not running
  202. * with an effective user id of root (as determined by the security
  203. * token in the message trailer).
  204. */
  205. kern_return_t bootstrap_parent(
  206. mach_port_t bp,
  207. mach_port_t *parent_port);
  208. /*
  209. * bootstrap_register()
  210. *
  211. * Registers a send right for service_port with the service identified by
  212. * service_name. Attempts to register a service where an active binding
  213. * already exists are rejected.
  214. *
  215. * If the service was previously declared with bootstrap_create_service(),
  216. * but is not currently active, this call can be used to undeclare the
  217. * service. The bootstrap port used must have sufficient privilege to
  218. * do so. (Registering MACH_PORT_NULL is especially useful for shutting
  219. * down declared services).
  220. *
  221. * This API is deprecated. Old scenarios and recommendations:
  222. *
  223. * 1) Code that used to call bootstrap_check_in() and then bootstrap_register()
  224. * can now always call bootstrap_check_in().
  225. *
  226. * 2) If the code was registering a well known name, please switch to launchd.
  227. *
  228. * 3) If the code was registering a dynamically generated string and passing
  229. * the string to other applications, please rewrite the code to send a Mach
  230. * send-right directly.
  231. *
  232. * 4) If the launchd job maintained an optional Mach service, please reserve
  233. * the name with launchd and control the presense of the service through
  234. * ownership of the Mach receive right like so.
  235. *
  236. * <key>MachServices</key>
  237. * <dict>
  238. * <key>com.apple.windowserver</key>
  239. * <true/>
  240. * <key>com.apple.windowserver.active</key>
  241. * <dict>
  242. * <key>HideUntilCheckIn</key>
  243. * <true/>
  244. * </dict>
  245. * </dict>
  246. *
  247. *
  248. * Errors: Returns appropriate kernel errors on rpc failure.
  249. * Returns BOOTSTRAP_NOT_PRIVILEGED, if request directed to
  250. * bootstrap port without privilege.
  251. * Returns BOOTSTRAP_NAME_IN_USE, if service has already been
  252. * register or checked-in.
  253. */
  254. AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_5
  255. kern_return_t
  256. bootstrap_register(mach_port_t bp, name_t service_name, mach_port_t sp);
  257. /*
  258. * bootstrap_create_service()
  259. *
  260. * Creates a service named "service_name" and returns a send right to that
  261. * port in "service_port." The port may later be checked in as if this
  262. * port were configured in the bootstrap configuration file.
  263. *
  264. * This API is deprecated. Please call bootstrap_check_in() instead.
  265. *
  266. * Errors: Returns appropriate kernel errors on rpc failure.
  267. * Returns BOOTSTRAP_SERVICE_ACTIVE, if service already exists.
  268. */
  269. #ifdef AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_6
  270. AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_6
  271. #endif
  272. kern_return_t
  273. bootstrap_create_service(mach_port_t bp, name_t service_name, mach_port_t *sp);
  274. /*
  275. * bootstrap_check_in()
  276. *
  277. * Returns the receive right for the service named by service_name. The
  278. * service must have been declared in the launchd.plist(5) file associated
  279. * with the job. Attempts to check_in a service which is already active
  280. * are not allowed.
  281. *
  282. * If the service was declared as being associated with a server, the
  283. * check_in must come from the server's privileged port (server_port).
  284. *
  285. * Errors: Returns appropriate kernel errors on rpc failure.
  286. * Returns BOOTSTRAP_UNKNOWN_SERVICE, if service does not exist.
  287. * Returns BOOTSTRAP_NOT_PRIVILEGED, if request directed to
  288. * bootstrap port without privilege.
  289. * Returns BOOTSTRAP_SERVICE_ACTIVE, if service has already been
  290. * registered or checked-in.
  291. */
  292. kern_return_t bootstrap_check_in(
  293. mach_port_t bp,
  294. const name_t service_name,
  295. mach_port_t *sp);
  296. /*
  297. * bootstrap_look_up()
  298. *
  299. * Returns a send right for the service port declared/registered under the
  300. * name service_name. The service is not guaranteed to be active. Use the
  301. * bootstrap_status call to determine the status of the service.
  302. *
  303. * Errors: Returns appropriate kernel errors on rpc failure.
  304. * Returns BOOTSTRAP_UNKNOWN_SERVICE, if service does not exist.
  305. */
  306. kern_return_t bootstrap_look_up(
  307. mach_port_t bp,
  308. const name_t service_name,
  309. mach_port_t *sp);
  310. /*
  311. * bootstrap_status()
  312. *
  313. * In practice, this call was used to preflight whether the following two
  314. * APIs would succeed.
  315. *
  316. * bootstrap_look_up()
  317. * bootstrap_check_in()
  318. *
  319. * Please don't bother. Just call the above two APIs directly and check
  320. * for failure.
  321. */
  322. kern_return_t bootstrap_status(
  323. mach_port_t bp,
  324. name_t service_name,
  325. bootstrap_status_t *service_active)
  326. AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_5;
  327. /* bootstrap_strerror()
  328. *
  329. * Translate a return value from the bootstrap_*() APIs to a string.
  330. */
  331. const char *bootstrap_strerror(kern_return_t r) __attribute__((__nothrow__, __pure__, __warn_unused_result__));
  332. #pragma GCC visibility pop
  333. __END_DECLS
  334. #endif /* __BOOTSTRAP_H__ */