1/*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <dispatch/dispatch.h>
25#include <mach/mach.h>
26#include <pthread.h>
27#include <stdint.h>
28#include <TargetConditionals.h>
29
30struct notify_globals_s {
31	// Global lock.
32	pthread_mutex_t notify_lock;
33
34	int32_t notify_ipc_version;
35	pid_t notify_server_pid;
36
37	uint32_t client_opts;
38	uint32_t saved_opts;
39
40	// Last allocated name id.
41	uint64_t name_id;
42
43	dispatch_once_t self_state_once;
44	notify_state_t *self_state;
45
46	dispatch_once_t notify_server_port_once;
47	mach_port_t notify_server_port;
48	mach_port_t saved_server_port;
49
50	mach_port_t notify_common_port;
51	int notify_common_token;
52	dispatch_source_t notify_dispatch_source;
53	dispatch_source_t server_proc_source;
54
55	dispatch_once_t token_table_once;
56	table_t *token_table;
57	table_t *token_name_table;
58	uint32_t token_id;
59
60	// File descriptor list.
61	uint32_t fd_count;
62	int *fd_clnt;
63	int *fd_srv;
64	int *fd_refcount;
65
66	// Mach port list.
67	uint32_t mp_count;
68	mach_port_t *mp_list;
69	int *mp_refcount;
70	int *mp_mine;
71
72	// Shared memory base address.
73	uint32_t *shm_base;
74};
75typedef struct notify_globals_s *notify_globals_t;
76
77#if __has_include(<os/alloc_once_private.h>)
78#include <os/alloc_once_private.h>
79#if defined(OS_ALLOC_ONCE_KEY_LIBSYSTEM_NOTIFY)
80#define _NOTIFY_HAS_ALLOC_ONCE 1
81#endif
82#endif
83
84__attribute__((visibility("hidden")))
85void _notify_init_globals(void * /* notify_globals_t */ globals);
86
87__attribute__((visibility("hidden")))
88notify_globals_t _notify_globals_impl(void);
89
90__attribute__((__pure__))
91static inline notify_globals_t
92_notify_globals(void) {
93#if _NOTIFY_HAS_ALLOC_ONCE
94	return (notify_globals_t)os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_NOTIFY,
95		sizeof(struct notify_globals_s), &_notify_init_globals);
96#else
97	return _notify_globals_impl();
98#endif
99}
100