2 * Copyright (c) 2003-2004 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>
32 #include <mach/mach_host.h>
34 #include <kern/kern_types.h>
35 #include <kern/ipc_kobject.h>
36 #include <kern/host_notify.h>
38 #include <kern/queue.h>
40 #include "mach/host_notify_reply.h"
42 static zone_t host_notify_zone
;
43 decl_mutex_data(static,host_notify_lock
)
45 static queue_head_t host_notify_queue
[HOST_NOTIFY_TYPE_MAX
+1];
47 static mach_msg_id_t host_notify_replyid
[HOST_NOTIFY_TYPE_MAX
+1] =
48 { HOST_CALENDAR_CHANGED_REPLYID
};
50 struct host_notify_entry
{
51 queue_chain_t entries
;
55 typedef struct host_notify_entry
*host_notify_t
;
58 host_notify_init(void)
62 for (i
= 0; i
<= HOST_NOTIFY_TYPE_MAX
; i
++)
63 queue_init(&host_notify_queue
[i
]);
65 mutex_init(&host_notify_lock
, 0);
67 i
= sizeof (struct host_notify_entry
);
69 zinit(i
, (4096 * i
), (16 * i
), "host_notify");
73 host_request_notification(
75 host_flavor_t notify_type
,
80 if (host
== HOST_NULL
)
81 return (KERN_INVALID_ARGUMENT
);
84 return (KERN_INVALID_CAPABILITY
);
86 if (notify_type
> HOST_NOTIFY_TYPE_MAX
|| notify_type
< 0)
87 return (KERN_INVALID_ARGUMENT
);
89 entry
= (host_notify_t
)zalloc(host_notify_zone
);
91 return (KERN_RESOURCE_SHORTAGE
);
93 mutex_lock(&host_notify_lock
);
96 if (!ip_active(port
) || ip_kotype(port
) != IKOT_NONE
) {
99 mutex_unlock(&host_notify_lock
);
100 zfree(host_notify_zone
, entry
);
102 return (KERN_FAILURE
);
106 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)entry
, IKOT_HOST_NOTIFY
);
109 enqueue_tail(&host_notify_queue
[notify_type
], (queue_entry_t
)entry
);
110 mutex_unlock(&host_notify_lock
);
112 return (KERN_SUCCESS
);
116 host_notify_port_destroy(
121 mutex_lock(&host_notify_lock
);
124 if (ip_kotype(port
) == IKOT_HOST_NOTIFY
) {
125 entry
= (host_notify_t
)port
->ip_kobject
;
126 assert(entry
!= NULL
);
127 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
130 assert(entry
->port
== port
);
131 remqueue(NULL
, (queue_entry_t
)entry
);
132 mutex_unlock(&host_notify_lock
);
133 zfree(host_notify_zone
, entry
);
135 ipc_port_release_sonce(port
);
140 mutex_unlock(&host_notify_lock
);
145 host_flavor_t notify_type
,
146 mach_msg_header_t
*msg
,
147 mach_msg_size_t msg_size
)
149 queue_t notify_queue
= &host_notify_queue
[notify_type
];
151 mutex_lock(&host_notify_lock
);
153 if (!queue_empty(notify_queue
)) {
154 queue_head_t send_queue
;
157 send_queue
= *notify_queue
;
158 queue_init(notify_queue
);
160 send_queue
.next
->prev
= &send_queue
;
161 send_queue
.prev
->next
= &send_queue
;
163 msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
164 msg
->msgh_local_port
= MACH_PORT_NULL
;
165 msg
->msgh_id
= host_notify_replyid
[notify_type
];
166 msg
->msgh_reserved
= 0;
168 while ((entry
= (host_notify_t
)dequeue(&send_queue
)) != NULL
) {
172 assert(port
!= IP_NULL
);
175 assert(ip_kotype(port
) == IKOT_HOST_NOTIFY
);
176 assert(port
->ip_kobject
== (ipc_kobject_t
)entry
);
177 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
180 mutex_unlock(&host_notify_lock
);
181 zfree(host_notify_zone
, entry
);
183 msg
->msgh_remote_port
= port
;
185 (void) mach_msg_send_from_kernel(msg
, msg_size
);
187 mutex_lock(&host_notify_lock
);
191 mutex_unlock(&host_notify_lock
);
195 host_notify_calendar_change(void)
197 __Request__host_calendar_changed_t msg
;
199 host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE
, &msg
.Head
, sizeof (msg
));