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