2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
33 * 16 January 2003 (debo)
37 #include <mach/mach_types.h>
38 #include <mach/mach_host.h>
40 #include <kern/kern_types.h>
41 #include <kern/ipc_kobject.h>
42 #include <kern/host_notify.h>
44 #include <kern/queue.h>
46 #include "mach/host_notify_reply.h"
48 decl_lck_mtx_data(,host_notify_lock
)
50 lck_mtx_ext_t host_notify_lock_ext
;
51 lck_grp_t host_notify_lock_grp
;
52 lck_attr_t host_notify_lock_attr
;
53 static lck_grp_attr_t host_notify_lock_grp_attr
;
54 static zone_t host_notify_zone
;
56 static queue_head_t host_notify_queue
[HOST_NOTIFY_TYPE_MAX
+1];
58 static mach_msg_id_t host_notify_replyid
[HOST_NOTIFY_TYPE_MAX
+1] =
59 { HOST_CALENDAR_CHANGED_REPLYID
};
61 struct host_notify_entry
{
62 queue_chain_t entries
;
66 typedef struct host_notify_entry
*host_notify_t
;
69 host_notify_init(void)
73 for (i
= 0; i
<= HOST_NOTIFY_TYPE_MAX
; i
++)
74 queue_init(&host_notify_queue
[i
]);
76 lck_grp_attr_setdefault(&host_notify_lock_grp_attr
);
77 lck_grp_init(&host_notify_lock_grp
, "host_notify", &host_notify_lock_grp_attr
);
78 lck_attr_setdefault(&host_notify_lock_attr
);
80 lck_mtx_init_ext(&host_notify_lock
, &host_notify_lock_ext
, &host_notify_lock_grp
, &host_notify_lock_attr
);
82 i
= sizeof (struct host_notify_entry
);
84 zinit(i
, (4096 * i
), (16 * i
), "host_notify");
88 host_request_notification(
90 host_flavor_t notify_type
,
95 if (host
== HOST_NULL
)
96 return (KERN_INVALID_ARGUMENT
);
99 return (KERN_INVALID_CAPABILITY
);
101 if (notify_type
> HOST_NOTIFY_TYPE_MAX
|| notify_type
< 0)
102 return (KERN_INVALID_ARGUMENT
);
104 entry
= (host_notify_t
)zalloc(host_notify_zone
);
106 return (KERN_RESOURCE_SHORTAGE
);
108 lck_mtx_lock(&host_notify_lock
);
111 if (!ip_active(port
) || ip_kotype(port
) != IKOT_NONE
) {
114 lck_mtx_unlock(&host_notify_lock
);
115 zfree(host_notify_zone
, entry
);
117 return (KERN_FAILURE
);
121 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)entry
, IKOT_HOST_NOTIFY
);
124 enqueue_tail(&host_notify_queue
[notify_type
], (queue_entry_t
)entry
);
125 lck_mtx_unlock(&host_notify_lock
);
127 return (KERN_SUCCESS
);
131 host_notify_port_destroy(
136 lck_mtx_lock(&host_notify_lock
);
139 if (ip_kotype(port
) == IKOT_HOST_NOTIFY
) {
140 entry
= (host_notify_t
)port
->ip_kobject
;
141 assert(entry
!= NULL
);
142 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
145 assert(entry
->port
== port
);
146 remqueue(NULL
, (queue_entry_t
)entry
);
147 lck_mtx_unlock(&host_notify_lock
);
148 zfree(host_notify_zone
, entry
);
150 ipc_port_release_sonce(port
);
155 lck_mtx_unlock(&host_notify_lock
);
160 host_flavor_t notify_type
,
161 mach_msg_header_t
*msg
,
162 mach_msg_size_t msg_size
)
164 queue_t notify_queue
= &host_notify_queue
[notify_type
];
166 lck_mtx_lock(&host_notify_lock
);
168 if (!queue_empty(notify_queue
)) {
169 queue_head_t send_queue
;
172 send_queue
= *notify_queue
;
173 queue_init(notify_queue
);
175 send_queue
.next
->prev
= &send_queue
;
176 send_queue
.prev
->next
= &send_queue
;
178 msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
179 msg
->msgh_local_port
= MACH_PORT_NULL
;
180 msg
->msgh_id
= host_notify_replyid
[notify_type
];
181 msg
->msgh_reserved
= 0;
183 while ((entry
= (host_notify_t
)dequeue(&send_queue
)) != NULL
) {
187 assert(port
!= IP_NULL
);
190 assert(ip_kotype(port
) == IKOT_HOST_NOTIFY
);
191 assert(port
->ip_kobject
== (ipc_kobject_t
)entry
);
192 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
195 lck_mtx_unlock(&host_notify_lock
);
196 zfree(host_notify_zone
, entry
);
198 msg
->msgh_remote_port
= port
;
200 (void) mach_msg_send_from_kernel_proper(msg
, msg_size
);
202 lck_mtx_lock(&host_notify_lock
);
206 lck_mtx_unlock(&host_notify_lock
);
210 host_notify_calendar_change(void)
212 __Request__host_calendar_changed_t msg
;
214 host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE
, &msg
.Head
, sizeof (msg
));