xref: /NextBSD/lib/libnotify/notify_internal.h (revision 33da5adc555b3bc29986eeadca03829e4ad06b1e)
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 
30 struct 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 };
75 typedef 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")))
85 void _notify_init_globals(void * /* notify_globals_t */ globals);
86 
87 __attribute__((visibility("hidden")))
88 notify_globals_t _notify_globals_impl(void);
89 
90 uint32_t notify_set_owner(const char *name, uint32_t uid, uint32_t gid);
91 uint32_t notify_get_owner(const char *name, uint32_t *uid, uint32_t *gid);
92 uint32_t notify_set_access(const char *name, uint32_t access);
93 uint32_t notify_get_access(const char *name, uint32_t *access);
94 uint32_t notify_release_name(const char *name);
95 uint32_t notify_register_plain(const char *name, int *out_token);
96 uint32_t notify_peek(int token, uint32_t *val);
97 int *notify_check_addr(int token);
98 uint32_t notify_monitor_file(int token, char *path, int flags);
99 uint32_t notify_get_event(int token, int *ev, char *buf, int *len);
100 void _notify_lib_cancel_port(notify_state_t *ns, mach_port_t port);
101 uint32_t _notify_lib_check(notify_state_t *ns, pid_t pid, int token, int *check);
102 int *_notify_lib_check_addr(notify_state_t *ns, pid_t pid, int token);
103 uint32_t _notify_lib_peek(notify_state_t *ns, pid_t pid, int token, int *val);
104 void _notify_fork_child(void);
105 
106 __attribute__((__pure__))
107 static inline notify_globals_t
_notify_globals(void)108 _notify_globals(void) {
109 #if _NOTIFY_HAS_ALLOC_ONCE
110 	return (notify_globals_t)os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_NOTIFY,
111 		sizeof(struct notify_globals_s), &_notify_init_globals);
112 #else
113 	return _notify_globals_impl();
114 #endif
115 }
116