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