xref: /NextBSD/lib/libnotify/libnotify.h (revision 33da5adc555b3bc29986eeadca03829e4ad06b1e)
1 /*
2  * Copyright (c) 2003-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 #ifndef _LIBNOTIFY_H_
25 #define _LIBNOTIFY_H_
26 
27 #include <pthread.h>
28 #include <mach/mach.h>
29 #include <dispatch/dispatch.h>
30 #include "table.h"
31 
32 #include <TargetConditionals.h>
33 
34 #if TARGET_IPHONE_SIMULATOR
35 extern const char *_notify_shm_id();
36 #define SHM_ID _notify_shm_id()
37 #else
38 #ifdef __FreeBSD__
39 #define SHM_ID "/apple.shm.notification_center"
40 #else
41 #define SHM_ID "apple.shm.notification_center"
42 #endif
43 #endif
44 
45 #define NOTIFY_IPC_VERSION_NAME "com.apple.system.notify.ipc_version"
46 #define NOTIFY_IPC_VERSION_NAME_LEN 35
47 #define NOTIFY_SERVICE_NAME "com.apple.system.notification_center"
48 #define NOTIFY_SERVICE_NAME_LEN 36
49 
50 #define COMMON_PORT_KEY "com.apple.system.notify.common"
51 
52 /* Notification types */
53 #define NOTIFY_TYPE_NONE   0x00000000
54 #define NOTIFY_TYPE_MEMORY 0x00000001
55 #define NOTIFY_TYPE_PLAIN  0x00000002
56 #define NOTIFY_TYPE_PORT   0x00000004
57 #define NOTIFY_TYPE_FILE   0x00000008
58 #define NOTIFY_TYPE_SIGNAL 0x00000010
59 #define NOTIFY_TYPE_MASK   0x000000ff
60 #define NOTIFY_FLAG_SELF   0x80000000
61 #define NOTIFY_FLAG_REGEN  0x40000000
62 #define NOTIFY_FLAG_RELEASE_SEND 0x20000000
63 
64 /* Return values for notify_check() */
65 #define NOTIFY_CHECK_FALSE 0
66 #define NOTIFY_CHECK_TRUE 1
67 #define NOTIFY_CHECK_ERROR 2
68 
69 /* Access control */
70 #define NOTIFY_ACCESS_READ   1
71 #define NOTIFY_ACCESS_WRITE  2
72 
73 #define NOTIFY_ACCESS_OTHER_SHIFT 8
74 #define NOTIFY_ACCESS_GROUP_SHIFT 4
75 #define NOTIFY_ACCESS_USER_SHIFT  0
76 
77 #define NOTIFY_ACCESS_DEFAULT 0x00000333
78 #define NOTIFY_ACCESS_USER_RW 0x00000003
79 
80 /* Filesystem Services */
81 #define NOTIFY_SERVICE_FILE_STATUS_QUO 0x00
82 #define NOTIFY_SERVICE_FILE_ADD        0x01
83 #define NOTIFY_SERVICE_FILE_DELETE     0x02
84 #define NOTIFY_SERVICE_FILE_MODIFY     0x04
85 #define NOTIFY_SERVICE_FILE_ATTR       0x08
86 
87 #define NOTIFY_SERVICE_DIR_FILE_ADD    0x10
88 #define NOTIFY_SERVICE_DIR_FILE_DELETE 0x20
89 
90 #define NOTIFY_CLIENT_STATE_SUSPENDED 0x00000001
91 #define NOTIFY_CLIENT_STATE_PENDING   0x00000002
92 #define NOTIFY_CLIENT_STATE_TIMEOUT   0x00000004
93 
94 #define NOTIFY_PORT_PROC_TYPE_PORT			0x00000010
95 #define NOTIFY_PORT_PROC_TYPE_PROC			0x00000020
96 #define NOTIFY_PORT_PROC_TYPE_MASK			0x000000f0
97 #define NOTIFY_PORT_PROC_STATE_INVALID		0x00000001
98 #define NOTIFY_PORT_PROC_STATE_SUSPENDED	0x00000002
99 #define NOTIFY_PORT_PROC_STATE_MASK			0x0000000f
100 
101 /* notify state flags */
102 #define NOTIFY_STATE_USE_LOCKS 0x00000001
103 #define NOTIFY_STATE_ENABLE_RESEND 0x00000002
104 
105 #define NOTIFY_CLIENT_SELF 0
106 #define SIGNAL_NONE -1
107 #define FD_NONE -1
108 #define SLOT_NONE -1
109 
110 #define _notify_lib_port_new(A,B,C,D) _notify_lib_port_proc_new(A,B,0,C,D)
111 #define _notify_lib_proc_new(A,B,C,D) _notify_lib_port_proc_new(A,MACH_PORT_NULL,B,C,D)
112 #define _notify_lib_port_find(A,B) _notify_lib_port_proc_find(A,B,0)
113 #define _notify_lib_proc_find(A,B) _notify_lib_port_proc_find(A,MACH_PORT_NULL,B)
114 #define _notify_lib_port_release(A,B) _notify_lib_port_proc_release(A,B,0)
115 #define _notify_lib_proc_release(A,B) _notify_lib_port_proc_release(A,MACH_PORT_NULL,B)
116 
117 typedef struct
118 {
119 	char *name;
120 	uint64_t name_id;
121 	uint32_t uid;
122 	uint32_t gid;
123 	uint32_t access;
124 	uint32_t slot;
125 	uint32_t refcount;
126 	uint32_t val;
127 	uint64_t state;
128 	uint64_t state_time;
129 	void *private;
130 	list_t *subscriptions;
131 } name_info_t;
132 
133 typedef struct
134 {
135 	uint64_t client_id;
136 	uint32_t state;
137 	name_info_t *name_info;
138 	uint32_t suspend_count;
139 	uint32_t notify_type;
140 	uint32_t lastval;
141 	mach_port_t port;
142 	int fd;
143 	uint32_t send_val;
144 	pid_t pid;
145 	uint32_t sig;
146 	void *private;
147 } client_t;
148 
149 typedef struct
150 {
151 	uint32_t refcount;
152 	uint32_t flags;
153 	dispatch_source_t src;
154 } portproc_data_t;
155 
156 typedef struct
157 {
158 	uint32_t flags;
159 	table_t *name_table;
160 	table_t *name_id_table;
161 	table_t *client_table;
162 	table_t *port_table;
163 	table_t *proc_table;
164 	name_info_t **controlled_name;
165 	uint32_t controlled_name_count;
166 	pthread_mutex_t *lock;
167 	int sock;
168 	uint32_t stat_name_alloc;
169 	uint32_t stat_name_free;
170 	uint32_t stat_client_alloc;
171 	uint32_t stat_client_free;
172 	uint32_t stat_portproc_alloc;
173 	uint32_t stat_portproc_free;
174 } notify_state_t;
175 
176 notify_state_t *_notify_lib_notify_state_new(uint32_t flags, uint32_t table_size);
177 void _notify_lib_notify_state_free(notify_state_t *ns);
178 
179 uint32_t _notify_lib_post(notify_state_t *ns, const char *name, uint32_t uid, uint32_t gid);
180 uint32_t _notify_lib_post_nid(notify_state_t *ns, uint64_t nid, uid_t uid, gid_t gid);
181 uint32_t _notify_lib_post_client(notify_state_t *ns, client_t *c);
182 
183 uint32_t _notify_lib_check(notify_state_t *ns, pid_t pid, int token, int *check);
184 uint32_t _notify_lib_get_state(notify_state_t *ns, uint64_t nid, uint64_t *state, uint32_t uid, uint32_t gid);
185 uint32_t _notify_lib_set_state(notify_state_t *ns, uint64_t nid, uint64_t state, uint32_t uid, uint32_t gid);
186 
187 uint32_t _notify_lib_register_plain(notify_state_t *ns, const char *name, pid_t pid, int token, uint32_t slot, uint32_t uid, uint32_t gid, uint64_t *out_nid);
188 uint32_t _notify_lib_register_signal(notify_state_t *ns, const char *name, pid_t pid, int token, uint32_t sig, uint32_t uid, uint32_t gid, uint64_t *out_nid);
189 uint32_t _notify_lib_register_mach_port(notify_state_t *ns, const char *name, pid_t pid, int token, mach_port_t port, uint32_t uid, uint32_t gid, uint64_t *out_nid);
190 uint32_t _notify_lib_register_file_descriptor(notify_state_t *ns, const char *name, pid_t pid, int token, int fd, uint32_t uid, uint32_t gid, uint64_t *out_nid);
191 
192 uint32_t _notify_lib_get_owner(notify_state_t *ns, const char *name, uint32_t *uid, uint32_t *gid);
193 uint32_t _notify_lib_get_access(notify_state_t *ns, const char *name, uint32_t *access);
194 
195 uint32_t _notify_lib_set_owner(notify_state_t *ns, const char *name, uint32_t uid, uint32_t gid);
196 uint32_t _notify_lib_set_access(notify_state_t *ns, const char *name, uint32_t access);
197 
198 uint32_t _notify_lib_release_name(notify_state_t *ns, const char *name, uint32_t uid, uint32_t gid);
199 
200 void _notify_lib_cancel(notify_state_t *ns, pid_t pid, int token);
201 void _notify_lib_suspend(notify_state_t *ns, pid_t pid, int token);
202 uint32_t _notify_lib_resume(notify_state_t *ns, pid_t pid, int token);
203 
204 void _notify_lib_cancel_proc(notify_state_t *ns, pid_t pid);
205 void _notify_lib_suspend_proc(notify_state_t *ns, pid_t pid);
206 void _notify_lib_resume_proc(notify_state_t *ns, pid_t pid);
207 
208 void _notify_lib_suspend_port(notify_state_t *ns, mach_port_t port);
209 void _notify_lib_resume_port(notify_state_t *ns, mach_port_t port);
210 
211 uint32_t _notify_lib_check_controlled_access(notify_state_t *ns, char *name, uid_t uid, gid_t gid, int req);
212 
213 uint64_t make_client_id(pid_t pid, int token);
214 
215 uint32_t _notify_lib_port_proc_new(notify_state_t *ns, mach_port_t port, pid_t proc, uint32_t state, dispatch_source_t src);
216 portproc_data_t *_notify_lib_port_proc_find(notify_state_t *ns, mach_port_t port, pid_t proc);
217 void _notify_lib_port_proc_release(notify_state_t *ns, mach_port_t port, pid_t proc);
218 
219 
220 #endif /* _LIBNOTIFY_H_ */
221