2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <net/ethernet.h>
27 #include <libkern/OSAtomic.h>
33 #include <mach/mach.h>
34 #include <dispatch/dispatch.h>
35 #include <dispatch/private.h>
36 #include "si_module.h"
38 #define PLUGIN_DIR_PATH "/usr/lib/info"
39 #define PLUGIN_BUNDLE_SUFFIX "so"
41 #define WORKUNIT_CANCELLED 0x00000001
42 #define WORKUNIT_RETURNS_LIST 0x00000002
45 #define WORKUNIT_CANCELLED_BIT_ADDRESS 31
47 #define WORKUNIT_CANCELLED_BIT_ADDRESS 7
50 typedef struct si_async_workunit_s
62 /* async support below */
71 struct si_async_workunit_s
*next
;
72 } si_async_workunit_t
;
74 static si_mod_t
**module_list
= NULL
;
75 static uint32_t module_count
= 0;
76 static pthread_mutex_t module_mutex
= PTHREAD_MUTEX_INITIALIZER
;
77 static si_async_workunit_t
*si_async_worklist
= NULL
;
79 si_mod_t
*si_module_static_search(void);
80 si_mod_t
*si_module_static_cache(void);
81 si_mod_t
*si_module_static_file(void);
82 #ifdef MUSER_AVAILABLE
83 si_mod_t
*si_module_static_muser(void);
86 si_mod_t
*si_module_static_ds(void);
88 si_mod_t
*si_module_static_mdns(void);
91 si_mod_dlsym(void *so
, const char *name
, const char *sym
)
96 if ((so
== NULL
) || (name
== NULL
) || (sym
== NULL
)) return NULL
;
99 asprintf(&str
, "%s_%s", name
, sym
);
100 if (str
== NULL
) return NULL
;
102 out
= dlsym(so
, str
);
108 si_module_with_path(const char *path
, const char *name
)
111 int (*si_sym_init
)(si_mod_t
*);
112 void (*si_sym_close
)(si_mod_t
*);
117 if ((path
== NULL
) || (name
== NULL
))
123 so
= dlopen(path
, RTLD_LOCAL
);
124 if (so
== NULL
) return NULL
;
126 si_sym_init
= si_mod_dlsym(so
, name
, "init");
127 if (si_sym_init
== NULL
)
130 errno
= ECONNREFUSED
;
134 si_sym_close
= si_mod_dlsym(so
, name
, "close");
135 if (si_sym_close
== NULL
)
138 errno
= ECONNREFUSED
;
142 out
= (si_mod_t
*)calloc(1, sizeof(si_mod_t
));
143 outname
= strdup(name
);
145 if ((out
== NULL
) || (outname
== NULL
))
159 status
= si_sym_init(out
);
165 errno
= ECONNREFUSED
;
173 si_module_with_name(const char *name
)
178 si_mod_t
*(*init
)(void);
182 { "search", si_module_static_search
, NULL
},
183 { "cache", si_module_static_cache
, NULL
},
184 { "file", si_module_static_file
, NULL
},
185 #ifdef MUSER_AVAILABLE
186 { "muser", si_module_static_muser
, NULL
},
189 { "ds", si_module_static_ds
, NULL
},
191 { "mdns", si_module_static_mdns
, NULL
},
199 * We don't need to worry about locking during initialization
200 * because all modules init routine returns static storage.
203 /* Find the module by name */
204 for (i
= 0; modules
[i
].name
!= NULL
; ++i
)
206 if (string_equal(name
, modules
[i
].name
))
208 si
= modules
[i
].module
;
211 si
= modules
[i
].init();
212 modules
[i
].module
= si
;
219 if (si
!= NULL
) return si
;
221 pthread_mutex_lock(&module_mutex
);
224 asprintf(&path
, "%s/%s.%s", PLUGIN_DIR_PATH
, name
, PLUGIN_BUNDLE_SUFFIX
);
229 pthread_mutex_unlock(&module_mutex
);
233 si
= si_module_with_path(path
, name
);
238 pthread_mutex_unlock(&module_mutex
);
242 /* add out to module_list */
243 module_list
= (si_mod_t
**)reallocf(module_list
, (module_count
+ 1) * sizeof(si_mod_t
*));
244 if (module_list
== NULL
)
246 pthread_mutex_unlock(&module_mutex
);
250 module_list
[module_count
] = si
;
253 pthread_mutex_unlock(&module_mutex
);
259 si_module_retain(si_mod_t
*si
)
261 if (si
== NULL
) return NULL
;
262 if (si
->flags
& SI_MOD_FLAG_STATIC
) return si
;
264 OSAtomicIncrement32Barrier(&si
->refcount
);
270 si_module_release(si_mod_t
*si
)
274 if (si
== NULL
) return;
275 if (si
->flags
& SI_MOD_FLAG_STATIC
) return;
277 i
= OSAtomicDecrement32Barrier(&si
->refcount
);
280 pthread_mutex_lock(&module_mutex
);
282 for (i
= 0; (i
< module_count
) && (module_list
[i
] != si
); i
++);
283 if (i
>= module_count
)
285 pthread_mutex_unlock(&module_mutex
);
289 if (module_count
== 1)
294 pthread_mutex_unlock(&module_mutex
);
298 for (i
++; i
< module_count
; i
++) module_list
[i
- 1] = module_list
[i
];
300 module_list
= (si_mod_t
**)reallocf(module_list
, module_count
* sizeof(si_mod_t
*));
301 if (module_list
== NULL
) module_count
= 0;
303 pthread_mutex_unlock(&module_mutex
);
305 if (si
->vtable
->sim_close
!= NULL
) si
->vtable
->sim_close(si
);
306 if (si
->bundle
!= NULL
) dlclose(si
->bundle
);
312 si_module_name(si_mod_t
*si
)
314 if (si
== NULL
) return NULL
;
316 return (const char *)si
->name
;
320 si_module_vers(si_mod_t
*si
)
322 if (si
== NULL
) return 0;
328 si_item_match(si_item_t
*item
, int cat
, const void *name
, uint32_t num
, int which
)
336 struct grouplist_s
*l
;
347 if (item
== NULL
) return 0;
348 if (which
== SEL_ALL
) return 1;
349 if ((which
== SEL_NAME
) && (name
== NULL
)) return 0;
351 ent
.x
= (char *)((uintptr_t)item
+ sizeof(si_item_t
));
357 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.u
->pw_name
))) return 1;
358 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.u
->pw_uid
)) return 1;
363 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.g
->gr_name
))) return 1;
364 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.g
->gr_gid
)) return 1;
367 case CATEGORY_GROUPLIST
:
369 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.l
->gl_user
))) return 1;
374 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.a
->alias_name
))) return 1;
377 case CATEGORY_HOST_IPV4
:
378 case CATEGORY_HOST_IPV6
:
380 /* N.B. address family is passed in num variable */
381 if (ent
.h
->h_addrtype
!= num
) return 0;
382 if (which
== SEL_NAME
)
384 if (string_equal(name
, ent
.h
->h_name
)) return 1;
385 if (ent
.h
->h_aliases
!= NULL
)
387 for (i
= 0; ent
.h
->h_aliases
[i
] != NULL
; i
++)
389 if (string_equal(name
, ent
.h
->h_aliases
[i
])) return 1;
393 else if (which
== SEL_NUMBER
)
395 if (memcmp(name
, ent
.h
->h_addr_list
[0], ent
.h
->h_length
) == 0) return 1;
399 case CATEGORY_NETWORK
:
401 if (which
== SEL_NAME
)
403 if (string_equal(name
, ent
.n
->n_name
)) return 1;
404 if (ent
.n
->n_aliases
!= NULL
)
406 for (i
= 0; ent
.n
->n_aliases
[i
] != NULL
; i
++)
408 if (string_equal(name
, ent
.n
->n_aliases
[i
])) return 1;
412 else if (which
== SEL_NUMBER
)
414 if (num
== ent
.n
->n_net
) return 1;
418 case CATEGORY_SERVICE
:
420 if (which
== SEL_NAME
)
422 /* N.B. for SEL_NAME, num is 0 (don't care), 1 (udp) or 2 (tcp) */
423 if ((num
== 1) && (string_not_equal("udp", ent
.s
->s_proto
))) return 0;
424 if ((num
== 2) && (string_not_equal("tcp", ent
.s
->s_proto
))) return 0;
426 if (string_equal(name
, ent
.s
->s_name
)) return 1;
427 if (ent
.s
->s_aliases
!= NULL
)
429 for (i
= 0; ent
.s
->s_aliases
[i
] != NULL
; i
++)
431 if (string_equal(name
, ent
.s
->s_aliases
[i
])) return 1;
435 else if (which
== SEL_NUMBER
)
437 /* N.B. for SEL_NUMBER, protocol is sent in name variable */
438 if ((name
!= NULL
) && (string_not_equal(name
, ent
.s
->s_proto
))) return 0;
439 if (num
== ent
.s
->s_port
) return 1;
443 case CATEGORY_PROTOCOL
:
445 if (which
== SEL_NAME
)
447 if (string_equal(name
, ent
.p
->p_name
)) return 1;
448 if (ent
.p
->p_aliases
!= NULL
)
450 for (i
= 0; ent
.p
->p_aliases
[i
] != NULL
; i
++)
452 if (string_equal(name
, ent
.p
->p_aliases
[i
])) return 1;
456 else if (which
== SEL_NUMBER
)
458 if (num
== ent
.p
->p_proto
) return 1;
464 if (which
== SEL_NAME
)
466 if (string_equal(name
, ent
.r
->r_name
)) return 1;
467 if (ent
.r
->r_aliases
!= NULL
)
469 for (i
= 0; ent
.r
->r_aliases
[i
] != NULL
; i
++)
471 if (string_equal(name
, ent
.r
->r_aliases
[i
])) return 1;
475 else if (which
== SEL_NUMBER
)
477 if (num
== ent
.r
->r_number
) return 1;
483 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.f
->fs_spec
))) return 1;
484 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.f
->fs_file
))) return 1;
489 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.m
->host
))) return 1;
490 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.m
->mac
))) return 1;
500 si_item_is_valid(si_item_t
*item
)
504 if (item
== NULL
) return 0;
508 if (si
== NULL
) return 0;
509 if (si
->vtable
->sim_is_valid
== NULL
) return 0;
511 return si
->vtable
->sim_is_valid(si
, item
);
515 si_user_byname(si_mod_t
*si
, const char *name
)
517 if (si
== NULL
) return NULL
;
518 if (si
->vtable
->sim_user_byname
== NULL
) return NULL
;
519 return si
->vtable
->sim_user_byname(si
, name
);
523 si_user_byuid(si_mod_t
*si
, uid_t uid
)
525 if (si
== NULL
) return NULL
;
526 if (si
->vtable
->sim_user_byuid
== NULL
) return NULL
;
527 return si
->vtable
->sim_user_byuid(si
, uid
);
531 si_user_byuuid(si_mod_t
*si
, uuid_t uuid
)
533 if (si
== NULL
) return NULL
;
534 if (si
->vtable
->sim_user_byuuid
== NULL
) return NULL
;
535 return si
->vtable
->sim_user_byuuid(si
, uuid
);
539 si_user_all(si_mod_t
*si
)
541 if (si
== NULL
) return NULL
;
542 if (si
->vtable
->sim_user_all
== NULL
) return NULL
;
543 return si
->vtable
->sim_user_all(si
);
547 si_group_byname(si_mod_t
*si
, const char *name
)
549 if (si
== NULL
) return NULL
;
550 if (si
->vtable
->sim_group_byname
== NULL
) return NULL
;
551 return si
->vtable
->sim_group_byname(si
, name
);
555 si_group_bygid(si_mod_t
*si
, gid_t gid
)
557 if (si
== NULL
) return NULL
;
558 if (si
->vtable
->sim_group_bygid
== NULL
) return NULL
;
559 return si
->vtable
->sim_group_bygid(si
, gid
);
563 si_group_byuuid(si_mod_t
*si
, uuid_t uuid
)
565 if (si
== NULL
) return NULL
;
566 if (si
->vtable
->sim_group_byuuid
== NULL
) return NULL
;
567 return si
->vtable
->sim_group_byuuid(si
, uuid
);
571 si_group_all(si_mod_t
*si
)
573 if (si
== NULL
) return NULL
;
574 if (si
->vtable
->sim_group_all
== NULL
) return NULL
;
575 return si
->vtable
->sim_group_all(si
);
579 si_grouplist(si_mod_t
*si
, const char *name
, uint32_t count
)
581 if (si
== NULL
) return NULL
;
582 if (si
->vtable
->sim_grouplist
== NULL
) return NULL
;
583 return si
->vtable
->sim_grouplist(si
, name
, count
);
587 si_netgroup_byname(struct si_mod_s
*si
, const char *name
)
589 if (si
== NULL
) return NULL
;
590 if (si
->vtable
->sim_netgroup_byname
== NULL
) return NULL
;
591 return si
->vtable
->sim_netgroup_byname(si
, name
);
595 si_in_netgroup(struct si_mod_s
*si
, const char *name
, const char *host
, const char *user
, const char *domain
)
597 if (si
== NULL
) return 0;
598 if (si
->vtable
->sim_in_netgroup
== NULL
) return 0;
599 return si
->vtable
->sim_in_netgroup(si
, name
, host
, user
, domain
);
603 si_alias_byname(si_mod_t
*si
, const char *name
)
605 if (si
== NULL
) return NULL
;
606 if (si
->vtable
->sim_alias_byname
== NULL
) return NULL
;
607 return si
->vtable
->sim_alias_byname(si
, name
);
611 si_alias_all(si_mod_t
*si
)
613 if (si
== NULL
) return NULL
;
614 if (si
->vtable
->sim_alias_all
== NULL
) return NULL
;
615 return si
->vtable
->sim_alias_all(si
);
619 si_host_byname(si_mod_t
*si
, const char *name
, int af
, const char *interface
, uint32_t *err
)
621 if (si
== NULL
) return NULL
;
622 if (si
->vtable
->sim_host_byname
== NULL
) return NULL
;
623 return si
->vtable
->sim_host_byname(si
, name
, af
, interface
, err
);
627 si_host_byaddr(si_mod_t
*si
, const void *addr
, int af
, const char *interface
, uint32_t *err
)
629 if (si
== NULL
) return NULL
;
630 if (si
->vtable
->sim_host_byaddr
== NULL
) return NULL
;
631 return si
->vtable
->sim_host_byaddr(si
, addr
, af
, interface
, err
);
635 si_host_all(si_mod_t
*si
)
637 if (si
== NULL
) return NULL
;
638 if (si
->vtable
->sim_host_all
== NULL
) return NULL
;
639 return si
->vtable
->sim_host_all(si
);
643 si_mac_byname(struct si_mod_s
*si
, const char *name
)
645 if (si
== NULL
) return NULL
;
646 if (si
->vtable
->sim_mac_byname
== NULL
) return NULL
;
647 return si
->vtable
->sim_mac_byname(si
, name
);
651 si_mac_bymac(struct si_mod_s
*si
, const char *mac
)
653 if (si
== NULL
) return NULL
;
654 if (si
->vtable
->sim_mac_bymac
== NULL
) return NULL
;
655 return si
->vtable
->sim_mac_bymac(si
, mac
);
659 si_mac_all(si_mod_t
*si
)
661 if (si
== NULL
) return NULL
;
662 if (si
->vtable
->sim_mac_all
== NULL
) return NULL
;
663 return si
->vtable
->sim_mac_all(si
);
667 si_network_byname(si_mod_t
*si
, const char *name
)
669 if (si
== NULL
) return NULL
;
670 if (si
->vtable
->sim_network_byname
== NULL
) return NULL
;
671 return si
->vtable
->sim_network_byname(si
, name
);
675 si_network_byaddr(si_mod_t
*si
, uint32_t addr
)
677 if (si
== NULL
) return NULL
;
678 if (si
->vtable
->sim_network_byaddr
== NULL
) return NULL
;
679 return si
->vtable
->sim_network_byaddr(si
, addr
);
683 si_network_all(si_mod_t
*si
)
685 if (si
== NULL
) return NULL
;
686 if (si
->vtable
->sim_network_all
== NULL
) return NULL
;
687 return si
->vtable
->sim_network_all(si
);
691 si_service_byname(si_mod_t
*si
, const char *name
, const char *proto
)
693 if (si
== NULL
) return NULL
;
694 if (si
->vtable
->sim_service_byname
== NULL
) return NULL
;
695 return si
->vtable
->sim_service_byname(si
, name
, proto
);
699 si_service_byport(si_mod_t
*si
, int port
, const char *proto
)
701 if (si
== NULL
) return NULL
;
702 if (si
->vtable
->sim_service_byport
== NULL
) return NULL
;
703 return si
->vtable
->sim_service_byport(si
, port
, proto
);
707 si_service_all(si_mod_t
*si
)
709 if (si
== NULL
) return NULL
;
710 if (si
->vtable
->sim_service_all
== NULL
) return NULL
;
711 return si
->vtable
->sim_service_all(si
);
715 si_protocol_byname(si_mod_t
*si
, const char *name
)
717 if (si
== NULL
) return NULL
;
718 if (si
->vtable
->sim_protocol_byname
== NULL
) return NULL
;
719 return si
->vtable
->sim_protocol_byname(si
, name
);
723 si_protocol_bynumber(si_mod_t
*si
, uint32_t number
)
725 if (si
== NULL
) return NULL
;
726 if (si
->vtable
->sim_protocol_bynumber
== NULL
) return NULL
;
727 return si
->vtable
->sim_protocol_bynumber(si
, number
);
731 si_protocol_all(si_mod_t
*si
)
733 if (si
== NULL
) return NULL
;
734 if (si
->vtable
->sim_protocol_all
== NULL
) return NULL
;
735 return si
->vtable
->sim_protocol_all(si
);
739 si_rpc_byname(si_mod_t
*si
, const char *name
)
741 if (si
== NULL
) return NULL
;
742 if (si
->vtable
->sim_rpc_byname
== NULL
) return NULL
;
743 return si
->vtable
->sim_rpc_byname(si
, name
);
747 si_rpc_bynumber(si_mod_t
*si
, int number
)
749 if (si
== NULL
) return NULL
;
750 if (si
->vtable
->sim_rpc_bynumber
== NULL
) return NULL
;
751 return si
->vtable
->sim_rpc_bynumber(si
, number
);
755 si_rpc_all(si_mod_t
*si
)
757 if (si
== NULL
) return NULL
;
758 if (si
->vtable
->sim_rpc_all
== NULL
) return NULL
;
759 return si
->vtable
->sim_rpc_all(si
);
763 si_fs_byspec(si_mod_t
*si
, const char *spec
)
765 if (si
== NULL
) return NULL
;
766 if (si
->vtable
->sim_fs_byspec
== NULL
) return NULL
;
767 return si
->vtable
->sim_fs_byspec(si
, spec
);
771 si_fs_byfile(si_mod_t
*si
, const char *file
)
773 if (si
== NULL
) return NULL
;
774 if (si
->vtable
->sim_fs_byfile
== NULL
) return NULL
;
775 return si
->vtable
->sim_fs_byfile(si
, file
);
779 si_fs_all(si_mod_t
*si
)
781 if (si
== NULL
) return NULL
;
782 if (si
->vtable
->sim_fs_all
== NULL
) return NULL
;
783 return si
->vtable
->sim_fs_all(si
);
787 si_item_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, const char *str3
, uint32_t num1
, uint32_t num2
, uint32_t *err
)
789 if (si
== NULL
) return NULL
;
793 case SI_CALL_USER_BYNAME
: return si_user_byname(si
, str1
);
794 case SI_CALL_USER_BYUID
: return si_user_byuid(si
, (uid_t
)num1
);
795 case SI_CALL_GROUP_BYNAME
: return si_group_byname(si
, str1
);
796 case SI_CALL_GROUP_BYGID
: return si_group_bygid(si
, (gid_t
)num1
);
797 case SI_CALL_GROUPLIST
: return si_grouplist(si
, str1
, (int) num1
);
798 case SI_CALL_ALIAS_BYNAME
: return si_alias_byname(si
, str1
);
799 case SI_CALL_HOST_BYNAME
: return si_host_byname(si
, str1
, num1
, str3
, err
);
800 case SI_CALL_HOST_BYADDR
: return si_host_byaddr(si
, (void *)str1
, num1
, str3
, err
);
801 case SI_CALL_NETWORK_BYNAME
: return si_network_byname(si
, str1
);
802 case SI_CALL_NETWORK_BYADDR
: return si_network_byaddr(si
, num1
);
803 case SI_CALL_SERVICE_BYNAME
: return si_service_byname(si
, str1
, str2
);
804 case SI_CALL_SERVICE_BYPORT
: return si_service_byport(si
, num1
, str2
);
805 case SI_CALL_PROTOCOL_BYNAME
: return si_protocol_byname(si
, str1
);
806 case SI_CALL_PROTOCOL_BYNUMBER
: return si_protocol_bynumber(si
, num1
);
807 case SI_CALL_RPC_BYNAME
: return si_network_byname(si
, str1
);
808 case SI_CALL_RPC_BYNUMBER
: return si_rpc_bynumber(si
, num1
);
809 case SI_CALL_FS_BYSPEC
: return si_fs_byspec(si
, str1
);
810 case SI_CALL_FS_BYFILE
: return si_fs_byfile(si
, str1
);
811 case SI_CALL_NAMEINFO
: return si_nameinfo(si
, (const struct sockaddr
*)str1
, num1
, str3
, err
);
812 case SI_CALL_IPNODE_BYNAME
: return si_ipnode_byname(si
, (const char *)str1
, num1
, num2
, str3
, err
);
813 case SI_CALL_MAC_BYNAME
: return si_mac_byname(si
, (const char *)str1
);
814 case SI_CALL_MAC_BYMAC
: return si_mac_bymac(si
, (const char *)str1
);
816 /* Support for DNS async calls */
817 case SI_CALL_DNS_QUERY
:
818 case SI_CALL_DNS_SEARCH
:
820 if (si
->vtable
->sim_item_call
== NULL
) return NULL
;
821 return si
->vtable
->sim_item_call(si
, call
, str1
, str2
, str3
, num1
, num2
, err
);
824 default: return NULL
;
831 si_list_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, const char *str3
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, uint32_t *err
)
833 if (si
== NULL
) return NULL
;
837 case SI_CALL_USER_ALL
: return si_user_all(si
);
838 case SI_CALL_GROUP_ALL
: return si_group_all(si
);
839 case SI_CALL_ALIAS_ALL
: return si_alias_all(si
);
840 case SI_CALL_HOST_ALL
: return si_host_all(si
);
841 case SI_CALL_NETWORK_ALL
: return si_network_all(si
);
842 case SI_CALL_SERVICE_ALL
: return si_service_all(si
);
843 case SI_CALL_PROTOCOL_ALL
: return si_protocol_all(si
);
844 case SI_CALL_RPC_ALL
: return si_rpc_all(si
);
845 case SI_CALL_FS_ALL
: return si_fs_all(si
);
846 case SI_CALL_MAC_ALL
: return si_mac_all(si
);
847 case SI_CALL_ADDRINFO
: return si_addrinfo(si
, str1
, str2
, num1
, num2
, num3
, num4
, str3
, err
);
848 default: return NULL
;
855 si_async_worklist_add_unit(si_async_workunit_t
*r
)
857 pthread_mutex_lock(&module_mutex
);
858 r
->next
= si_async_worklist
;
859 si_async_worklist
= r
;
860 pthread_mutex_unlock(&module_mutex
);
864 si_async_worklist_remove_unit(si_async_workunit_t
*r
)
866 si_async_workunit_t
*x
;
868 pthread_mutex_lock(&module_mutex
);
869 if (si_async_worklist
== r
)
871 si_async_worklist
= r
->next
;
875 for (x
= si_async_worklist
; (x
!= NULL
) && (x
->next
!= r
); x
= x
->next
) {;}
876 if (x
!= NULL
) x
->next
= r
->next
;
878 pthread_mutex_unlock(&module_mutex
);
881 static si_async_workunit_t
*
882 si_async_worklist_find_unit(mach_port_t p
)
884 si_async_workunit_t
*r
;
886 pthread_mutex_lock(&module_mutex
);
887 for (r
= si_async_worklist
; (r
!= NULL
) && (r
->port
!= p
); r
= r
->next
) {;}
888 pthread_mutex_unlock(&module_mutex
);
893 static si_async_workunit_t
*
894 si_async_workunit_create(si_mod_t
*si
, int call
, const char *str1
, const char *str2
, const char *str3
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, void *callback
, void *context
)
896 si_async_workunit_t
*r
;
897 kern_return_t status
;
898 mach_port_t reply
, send
;
899 mach_msg_type_name_t type
;
906 if (si_call_str1_is_buffer(call
))
910 s1
= calloc(1, num3
);
911 if (s1
== NULL
) return NULL
;
912 memcpy(s1
, str1
, num3
);
915 else if (str1
!= NULL
)
918 if (s1
== NULL
) return NULL
;
926 if (s1
!= NULL
) free(s1
);
936 if (s1
!= NULL
) free(s1
);
937 if (s2
!= NULL
) free(s2
);
942 r
= (si_async_workunit_t
*)calloc(1, sizeof(si_async_workunit_t
));
945 if (s1
!= NULL
) free(s1
);
946 if (s2
!= NULL
) free(s2
);
947 if (s3
!= NULL
) free(s3
);
951 reply
= MACH_PORT_NULL
;
952 send
= MACH_PORT_NULL
;
955 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &reply
);
956 if (status
== KERN_SUCCESS
) status
= mach_port_extract_right(mach_task_self(), reply
, MACH_MSG_TYPE_MAKE_SEND_ONCE
, &send
, &type
);
957 if (status
!= KERN_SUCCESS
)
959 if (reply
!= MACH_PORT_NULL
) mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
960 if (s1
!= NULL
) free(s1
);
961 if (s2
!= NULL
) free(s2
);
962 if (s3
!= NULL
) free(s3
);
979 if (si_call_returns_list(call
)) r
->flags
|= WORKUNIT_RETURNS_LIST
;
981 r
->callback
= callback
;
982 r
->context
= context
;
986 si_async_worklist_add_unit(r
);
992 si_async_workunit_release(si_async_workunit_t
*r
)
994 if (r
== NULL
) return;
996 if (OSAtomicDecrement32Barrier(&(r
->refcount
)) != 0) return;
999 fprintf(stderr
, "** %s freeing worklist item %p\n", __func__
, r
);
1002 si_async_worklist_remove_unit(r
);
1004 if (r
->resitem
!= NULL
) si_item_release(r
->resitem
);
1005 if (r
->reslist
!= NULL
) si_list_release(r
->reslist
);
1007 if (r
->str1
!= NULL
) free(r
->str1
);
1008 if (r
->str2
!= NULL
) free(r
->str2
);
1009 if (r
->str3
!= NULL
) free(r
->str3
);
1011 /* release send-once right if it has not been used */
1012 if (r
->send
!= MACH_PORT_NULL
) mach_port_deallocate(mach_task_self(), r
->send
);
1014 /* release receive right */
1015 mach_port_mod_refs(mach_task_self(), r
->port
, MACH_PORT_RIGHT_RECEIVE
, -1);
1021 si_async_launchpad(si_async_workunit_t
*r
)
1023 kern_return_t status
;
1024 mach_msg_empty_send_t msg
;
1027 fprintf(stderr
, "** %s starting worklist item %p\n", __func__
, r
);
1030 if (r
->flags
& WORKUNIT_CANCELLED
)
1032 si_async_workunit_release(r
);
1034 fprintf(stderr
, "** %s worklist item %p was cancelled early\n", __func__
, r
);
1039 if (r
->flags
& WORKUNIT_RETURNS_LIST
) r
->reslist
= si_list_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->str3
, r
->num1
, r
->num2
, r
->num3
, r
->num4
, &(r
->err
));
1040 else r
->resitem
= si_item_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->str3
, r
->num1
, r
->num2
, &(r
->err
));
1043 * Test and set the cancelled flag.
1044 * If it was set, then this work item was cancelled.
1045 * Otherwise, setting it here prevents si_async_cancel from cancelling:
1046 * too late to cancel now!
1048 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1050 si_async_workunit_release(r
);
1052 fprintf(stderr
, "** %s worklist item %p was cancelled in flight\n", __func__
, r
);
1057 else fprintf(stderr
, "** %s worklist item %p flags are now 0x%08x\n", __func__
, r
, r
->flags
);
1060 memset(&msg
, 0, sizeof(mach_msg_empty_send_t
));
1062 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, MACH_MSGH_BITS_ZERO
);
1063 msg
.header
.msgh_remote_port
= r
->send
;
1064 r
->send
= MACH_PORT_NULL
;
1065 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
1066 msg
.header
.msgh_size
= sizeof(mach_msg_empty_send_t
);
1067 msg
.header
.msgh_id
= r
->call
;
1069 status
= mach_msg(&(msg
.header
), MACH_SEND_MSG
, msg
.header
.msgh_size
, 0, MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1070 if (status
!= MACH_MSG_SUCCESS
)
1072 /* receiver failed - clean up to avoid a port leak */
1073 mach_msg_destroy(&(msg
.header
));
1075 fprintf(stderr
, "** %s mach message send failed for worklist item %p\n", __func__
, r
);
1079 si_async_workunit_release(r
);
1082 * The client is now responsible for calling si_async_handle_reply,
1083 * which will invoke the client's callback and then release the workunit.
1087 fprintf(stderr
, "** %s completed async worklist item %p\n", __func__
, r
);
1092 si_async_call(struct si_mod_s
*si
, int call
, const char *str1
, const char *str2
, const char *str3
, uint32_t num1
, uint32_t num2
, uint32_t num3
, uint32_t num4
, void *callback
, void *context
)
1094 si_async_workunit_t
*req
;
1096 if (si
== NULL
) return MACH_PORT_NULL
;
1097 if (callback
== NULL
) return MACH_PORT_NULL
;
1099 /* if module does async on it's own, hand off the call */
1100 if (si
->vtable
->sim_async_call
!= NULL
)
1102 return si
->vtable
->sim_async_call(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1105 req
= si_async_workunit_create(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1106 if (req
== NULL
) return MACH_PORT_NULL
;
1108 /* queue the work on the global low-priority dispatch queue */
1110 fprintf(stderr
, "** %s dispatching worklist item %p\n", __func__
, req
);
1112 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, DISPATCH_QUEUE_OVERCOMMIT
), ^{ si_async_launchpad(req
); });
1118 si_async_cancel(mach_port_t p
)
1120 si_async_workunit_t
*r
;
1122 r
= si_async_worklist_find_unit(p
);
1126 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1132 * Test and set the WORKUNIT_CANCELLED flag.
1133 * If it was already set, this work item has been executed - too late to really cancel.
1135 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1137 /* already executed */
1139 fprintf(stderr
, "** %s worklist item %p has executed\n", __func__
, r
);
1144 fprintf(stderr
, "** %s calling worklist item %p callback SI_STATUS_CALL_CANCELLED\n", __func__
, r
);
1147 if (r
->callback
!= NULL
)
1149 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1150 else ((item_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1153 si_async_workunit_release(r
);
1157 si_async_handle_reply(mach_msg_header_t
*msg
)
1159 si_async_workunit_t
*r
;
1160 mach_port_t reply
= msg
->msgh_local_port
;
1162 r
= si_async_worklist_find_unit(reply
);
1166 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1172 fprintf(stderr
, "** %s worklist item %p flags are 0x%08x\n", __func__
, r
, r
->flags
);
1174 if ((r
->flags
& WORKUNIT_CANCELLED
) == 0)
1177 fprintf(stderr
, "** %s workunit thread is still active\n", __func__
);
1182 if (r
->callback
!= NULL
)
1184 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(r
->reslist
, r
->err
, r
->context
);
1185 else ((item_async_callback
)(r
->callback
))(r
->resitem
, r
->err
, r
->context
);
1193 fprintf(stderr
, "** %s workunit has no callback\n", __func__
);
1197 si_async_workunit_release(r
);
1201 si_standardize_mac_address(const char *addr
)
1205 struct ether_addr
*ether
;
1208 if (addr
== NULL
) return NULL
;
1210 /* ether_aton isn't thread-safe */
1211 pthread_mutex_lock(&module_mutex
);
1213 ether
= ether_aton(addr
);
1216 pthread_mutex_unlock(&module_mutex
);
1220 for (i
= 0; i
< 6; i
++)
1222 if (ether
->ether_addr_octet
[i
] <= 15)
1224 sprintf(e
[i
], "0%x", ether
->ether_addr_octet
[i
]);
1228 sprintf(e
[i
], "%x", ether
->ether_addr_octet
[i
]);
1232 pthread_mutex_unlock(&module_mutex
);
1235 asprintf(&out
, "%s:%s:%s:%s:%s:%s", e
[0], e
[1], e
[2], e
[3], e
[4], e
[5]);