2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
27 * 16 January 2003 (debo)
31 #include <mach/mach_types.h>
33 #include <kern/ipc_kobject.h>
34 #include <kern/host_notify.h>
36 #include <kern/queue.h>
38 #include "mach/host_notify_reply.h"
40 static zone_t host_notify_zone
;
41 decl_mutex_data(static,host_notify_lock
)
43 static queue_head_t host_notify_queue
[HOST_NOTIFY_TYPE_MAX
+1];
45 static mach_msg_id_t host_notify_replyid
[HOST_NOTIFY_TYPE_MAX
+1] =
46 { HOST_CALENDAR_CHANGED_REPLYID
};
48 struct host_notify_entry
{
49 queue_chain_t entries
;
53 typedef struct host_notify_entry
*host_notify_t
;
56 host_notify_init(void)
60 for (i
= 0; i
<= HOST_NOTIFY_TYPE_MAX
; i
++)
61 queue_init(&host_notify_queue
[i
]);
63 mutex_init(&host_notify_lock
, ETAP_MISC_EVENT
);
65 i
= sizeof (struct host_notify_entry
);
67 zinit(i
, (4096 * i
), (16 * i
), "host_notify");
71 host_request_notification(
73 host_flavor_t notify_type
,
78 if (host
== HOST_NULL
)
79 return (KERN_INVALID_ARGUMENT
);
82 return (KERN_INVALID_CAPABILITY
);
84 if (notify_type
> HOST_NOTIFY_TYPE_MAX
|| notify_type
< 0)
85 return (KERN_INVALID_ARGUMENT
);
87 entry
= (host_notify_t
)zalloc(host_notify_zone
);
89 return (KERN_RESOURCE_SHORTAGE
);
91 mutex_lock(&host_notify_lock
);
94 if (!ip_active(port
) || ip_kotype(port
) != IKOT_NONE
) {
97 mutex_unlock(&host_notify_lock
);
98 zfree(host_notify_zone
, (vm_offset_t
)entry
);
100 return (KERN_FAILURE
);
104 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)entry
, IKOT_HOST_NOTIFY
);
107 enqueue_tail(&host_notify_queue
[notify_type
], (queue_entry_t
)entry
);
108 mutex_unlock(&host_notify_lock
);
110 return (KERN_SUCCESS
);
114 host_notify_port_destroy(
119 mutex_lock(&host_notify_lock
);
122 if (ip_kotype(port
) == IKOT_HOST_NOTIFY
) {
123 entry
= (host_notify_t
)port
->ip_kobject
;
124 assert(entry
!= NULL
);
125 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
128 assert(entry
->port
== port
);
129 remqueue(NULL
, (queue_entry_t
)entry
);
130 mutex_unlock(&host_notify_lock
);
131 zfree(host_notify_zone
, (vm_offset_t
)entry
);
133 ipc_port_release_sonce(port
);
138 mutex_unlock(&host_notify_lock
);
143 host_flavor_t notify_type
,
144 mach_msg_header_t
*msg
,
145 mach_msg_size_t msg_size
)
147 queue_t notify_queue
= &host_notify_queue
[notify_type
];
149 mutex_lock(&host_notify_lock
);
151 if (!queue_empty(notify_queue
)) {
152 queue_head_t send_queue
;
155 send_queue
= *notify_queue
;
156 queue_init(notify_queue
);
158 send_queue
.next
->prev
= &send_queue
;
159 send_queue
.prev
->next
= &send_queue
;
161 msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
162 msg
->msgh_local_port
= MACH_PORT_NULL
;
163 msg
->msgh_id
= host_notify_replyid
[notify_type
];
164 msg
->msgh_reserved
= 0;
166 while ((entry
= (host_notify_t
)dequeue(&send_queue
)) != NULL
) {
170 assert(port
!= IP_NULL
);
173 assert(ip_kotype(port
) == IKOT_HOST_NOTIFY
);
174 assert(port
->ip_kobject
== (ipc_kobject_t
)entry
);
175 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
178 mutex_unlock(&host_notify_lock
);
179 zfree(host_notify_zone
, (vm_offset_t
)entry
);
181 msg
->msgh_remote_port
= port
;
183 (void) mach_msg_send_from_kernel(msg
, msg_size
);
185 mutex_lock(&host_notify_lock
);
189 mutex_unlock(&host_notify_lock
);
193 host_notify_calendar_change(void)
195 __Request__host_calendar_changed_t msg
;
197 host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE
, &msg
.Head
, sizeof (msg
));