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);
83 si_mod_t
*si_module_static_ds(void);
85 si_mod_t
*si_module_static_mdns(void);
88 si_mod_dlsym(void *so
, const char *name
, const char *sym
)
93 if ((so
== NULL
) || (name
== NULL
) || (sym
== NULL
)) return NULL
;
96 asprintf(&str
, "%s_%s", name
, sym
);
97 if (str
== NULL
) return NULL
;
105 si_module_with_path(const char *path
, const char *name
)
108 int (*si_sym_init
)(si_mod_t
*);
109 void (*si_sym_close
)(si_mod_t
*);
114 if ((path
== NULL
) || (name
== NULL
))
120 so
= dlopen(path
, RTLD_LOCAL
);
121 if (so
== NULL
) return NULL
;
123 si_sym_init
= si_mod_dlsym(so
, name
, "init");
124 if (si_sym_init
== NULL
)
127 errno
= ECONNREFUSED
;
131 si_sym_close
= si_mod_dlsym(so
, name
, "close");
132 if (si_sym_close
== NULL
)
135 errno
= ECONNREFUSED
;
139 out
= (si_mod_t
*)calloc(1, sizeof(si_mod_t
));
140 outname
= strdup(name
);
142 if ((out
== NULL
) || (outname
== NULL
))
156 status
= si_sym_init(out
);
162 errno
= ECONNREFUSED
;
170 si_module_with_name(const char *name
)
175 si_mod_t
*(*init
)(void);
179 { "search", si_module_static_search
, NULL
},
180 { "cache", si_module_static_cache
, NULL
},
181 { "file", si_module_static_file
, NULL
},
183 { "ds", si_module_static_ds
, NULL
},
185 { "mdns", si_module_static_mdns
, NULL
},
193 * We don't need to worry about locking during initialization
194 * because all modules init routine returns static storage.
197 /* Find the module by name */
198 for (i
= 0; modules
[i
].name
!= NULL
; ++i
)
200 if (string_equal(name
, modules
[i
].name
))
202 si
= modules
[i
].module;
205 si
= modules
[i
].init();
206 modules
[i
].module = si
;
213 if (si
!= NULL
) return si
;
215 pthread_mutex_lock(&module_mutex
);
218 asprintf(&path
, "%s/%s.%s", PLUGIN_DIR_PATH
, name
, PLUGIN_BUNDLE_SUFFIX
);
223 pthread_mutex_unlock(&module_mutex
);
227 si
= si_module_with_path(path
, name
);
232 pthread_mutex_unlock(&module_mutex
);
236 /* add out to module_list */
237 module_list
= (si_mod_t
**)reallocf(module_list
, (module_count
+ 1) * sizeof(si_mod_t
*));
238 if (module_list
== NULL
)
240 pthread_mutex_unlock(&module_mutex
);
244 module_list
[module_count
] = si
;
247 pthread_mutex_unlock(&module_mutex
);
253 si_module_retain(si_mod_t
*si
)
255 if (si
== NULL
) return NULL
;
256 if (si
->flags
& SI_MOD_FLAG_STATIC
) return si
;
258 OSAtomicIncrement32Barrier(&si
->refcount
);
264 si_module_release(si_mod_t
*si
)
268 if (si
== NULL
) return;
269 if (si
->flags
& SI_MOD_FLAG_STATIC
) return;
271 i
= OSAtomicDecrement32Barrier(&si
->refcount
);
274 pthread_mutex_lock(&module_mutex
);
276 for (i
= 0; (i
< module_count
) && (module_list
[i
] != si
); i
++);
277 if (i
>= module_count
)
279 pthread_mutex_unlock(&module_mutex
);
283 if (module_count
== 1)
288 pthread_mutex_unlock(&module_mutex
);
292 for (i
++; i
< module_count
; i
++) module_list
[i
- 1] = module_list
[i
];
294 module_list
= (si_mod_t
**)reallocf(module_list
, module_count
* sizeof(si_mod_t
*));
295 if (module_list
== NULL
) module_count
= 0;
297 pthread_mutex_unlock(&module_mutex
);
299 if (si
->vtable
->sim_close
!= NULL
) si
->vtable
->sim_close(si
);
300 if (si
->bundle
!= NULL
) dlclose(si
->bundle
);
306 si_module_name(si_mod_t
*si
)
308 if (si
== NULL
) return NULL
;
310 return (const char *)si
->name
;
314 si_module_vers(si_mod_t
*si
)
316 if (si
== NULL
) return 0;
322 si_item_match(si_item_t
*item
, int cat
, const void *name
, uint32_t num
, int which
)
330 struct grouplist_s
*l
;
341 if (item
== NULL
) return 0;
342 if (which
== SEL_ALL
) return 1;
343 if ((which
== SEL_NAME
) && (name
== NULL
)) return 0;
345 ent
.x
= (char *)((uintptr_t)item
+ sizeof(si_item_t
));
351 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.u
->pw_name
))) return 1;
352 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.u
->pw_uid
)) return 1;
357 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.g
->gr_name
))) return 1;
358 else if ((which
== SEL_NUMBER
) && (num
== (uint32_t)ent
.g
->gr_gid
)) return 1;
361 case CATEGORY_GROUPLIST
:
363 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.l
->gl_user
))) return 1;
368 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.a
->alias_name
))) return 1;
371 case CATEGORY_HOST_IPV4
:
372 case CATEGORY_HOST_IPV6
:
374 /* N.B. address family is passed in num variable */
375 if (ent
.h
->h_addrtype
!= num
) return 0;
376 if (which
== SEL_NAME
)
378 if (string_equal(name
, ent
.h
->h_name
)) return 1;
379 if (ent
.h
->h_aliases
!= NULL
)
381 for (i
= 0; ent
.h
->h_aliases
[i
] != NULL
; i
++)
383 if (string_equal(name
, ent
.h
->h_aliases
[i
])) return 1;
387 else if (which
== SEL_NUMBER
)
389 if (memcmp(name
, ent
.h
->h_addr_list
[0], ent
.h
->h_length
) == 0) return 1;
393 case CATEGORY_NETWORK
:
395 if (which
== SEL_NAME
)
397 if (string_equal(name
, ent
.n
->n_name
)) return 1;
398 if (ent
.n
->n_aliases
!= NULL
)
400 for (i
= 0; ent
.n
->n_aliases
[i
] != NULL
; i
++)
402 if (string_equal(name
, ent
.n
->n_aliases
[i
])) return 1;
406 else if (which
== SEL_NUMBER
)
408 if (num
== ent
.n
->n_net
) return 1;
412 case CATEGORY_SERVICE
:
414 if (which
== SEL_NAME
)
416 /* N.B. for SEL_NAME, num is 0 (don't care), 1 (udp) or 2 (tcp) */
417 if ((num
== 1) && (string_not_equal("udp", ent
.s
->s_proto
))) return 0;
418 if ((num
== 2) && (string_not_equal("tcp", ent
.s
->s_proto
))) return 0;
420 if (string_equal(name
, ent
.s
->s_name
)) return 1;
421 if (ent
.s
->s_aliases
!= NULL
)
423 for (i
= 0; ent
.s
->s_aliases
[i
] != NULL
; i
++)
425 if (string_equal(name
, ent
.s
->s_aliases
[i
])) return 1;
429 else if (which
== SEL_NUMBER
)
431 /* N.B. for SEL_NUMBER, protocol is sent in name variable */
432 if ((name
!= NULL
) && (string_not_equal(name
, ent
.s
->s_proto
))) return 0;
433 if (num
== ent
.s
->s_port
) return 1;
437 case CATEGORY_PROTOCOL
:
439 if (which
== SEL_NAME
)
441 if (string_equal(name
, ent
.p
->p_name
)) return 1;
442 if (ent
.p
->p_aliases
!= NULL
)
444 for (i
= 0; ent
.p
->p_aliases
[i
] != NULL
; i
++)
446 if (string_equal(name
, ent
.p
->p_aliases
[i
])) return 1;
450 else if (which
== SEL_NUMBER
)
452 if (num
== ent
.p
->p_proto
) return 1;
458 if (which
== SEL_NAME
)
460 if (string_equal(name
, ent
.r
->r_name
)) return 1;
461 if (ent
.r
->r_aliases
!= NULL
)
463 for (i
= 0; ent
.r
->r_aliases
[i
] != NULL
; i
++)
465 if (string_equal(name
, ent
.r
->r_aliases
[i
])) return 1;
469 else if (which
== SEL_NUMBER
)
471 if (num
== ent
.r
->r_number
) return 1;
477 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.f
->fs_spec
))) return 1;
478 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.f
->fs_file
))) return 1;
483 if ((which
== SEL_NAME
) && (string_equal(name
, ent
.m
->host
))) return 1;
484 if ((which
== SEL_NUMBER
) && (string_equal(name
, ent
.m
->mac
))) return 1;
494 si_item_is_valid(si_item_t
*item
)
498 if (item
== NULL
) return 0;
502 if (si
== NULL
) return 0;
503 if (si
->vtable
->sim_is_valid
== NULL
) return 0;
505 return si
->vtable
->sim_is_valid(si
, item
);
509 si_user_byname(si_mod_t
*si
, const char *name
)
511 if (si
== NULL
) return NULL
;
512 if (si
->vtable
->sim_user_byname
== NULL
) return NULL
;
513 return si
->vtable
->sim_user_byname(si
, name
);
517 si_user_byuid(si_mod_t
*si
, uid_t uid
)
519 if (si
== NULL
) return NULL
;
520 if (si
->vtable
->sim_user_byuid
== NULL
) return NULL
;
521 return si
->vtable
->sim_user_byuid(si
, uid
);
525 si_user_byuuid(si_mod_t
*si
, uuid_t uuid
)
527 if (si
== NULL
) return NULL
;
528 if (si
->vtable
->sim_user_byuuid
== NULL
) return NULL
;
529 return si
->vtable
->sim_user_byuuid(si
, uuid
);
533 si_user_all(si_mod_t
*si
)
535 if (si
== NULL
) return NULL
;
536 if (si
->vtable
->sim_user_all
== NULL
) return NULL
;
537 return si
->vtable
->sim_user_all(si
);
541 si_group_byname(si_mod_t
*si
, const char *name
)
543 if (si
== NULL
) return NULL
;
544 if (si
->vtable
->sim_group_byname
== NULL
) return NULL
;
545 return si
->vtable
->sim_group_byname(si
, name
);
549 si_group_bygid(si_mod_t
*si
, gid_t gid
)
551 if (si
== NULL
) return NULL
;
552 if (si
->vtable
->sim_group_bygid
== NULL
) return NULL
;
553 return si
->vtable
->sim_group_bygid(si
, gid
);
557 si_group_byuuid(si_mod_t
*si
, uuid_t uuid
)
559 if (si
== NULL
) return NULL
;
560 if (si
->vtable
->sim_group_byuuid
== NULL
) return NULL
;
561 return si
->vtable
->sim_group_byuuid(si
, uuid
);
565 si_group_all(si_mod_t
*si
)
567 if (si
== NULL
) return NULL
;
568 if (si
->vtable
->sim_group_all
== NULL
) return NULL
;
569 return si
->vtable
->sim_group_all(si
);
573 si_grouplist(si_mod_t
*si
, const char *name
, uint32_t count
)
575 if (si
== NULL
) return NULL
;
576 if (si
->vtable
->sim_grouplist
== NULL
) return NULL
;
577 return si
->vtable
->sim_grouplist(si
, name
, count
);
581 si_netgroup_byname(struct si_mod_s
*si
, const char *name
)
583 if (si
== NULL
) return NULL
;
584 if (si
->vtable
->sim_netgroup_byname
== NULL
) return NULL
;
585 return si
->vtable
->sim_netgroup_byname(si
, name
);
589 si_in_netgroup(struct si_mod_s
*si
, const char *name
, const char *host
, const char *user
, const char *domain
)
591 if (si
== NULL
) return 0;
592 if (si
->vtable
->sim_in_netgroup
== NULL
) return 0;
593 return si
->vtable
->sim_in_netgroup(si
, name
, host
, user
, domain
);
597 si_alias_byname(si_mod_t
*si
, const char *name
)
599 if (si
== NULL
) return NULL
;
600 if (si
->vtable
->sim_alias_byname
== NULL
) return NULL
;
601 return si
->vtable
->sim_alias_byname(si
, name
);
605 si_alias_all(si_mod_t
*si
)
607 if (si
== NULL
) return NULL
;
608 if (si
->vtable
->sim_alias_all
== NULL
) return NULL
;
609 return si
->vtable
->sim_alias_all(si
);
613 si_host_byname(si_mod_t
*si
, const char *name
, int af
, const char *interface
, uint32_t *err
)
615 if (si
== NULL
) return NULL
;
616 if (si
->vtable
->sim_host_byname
== NULL
) return NULL
;
617 return si
->vtable
->sim_host_byname(si
, name
, af
, interface
, err
);
621 si_host_byaddr(si_mod_t
*si
, const void *addr
, int af
, const char *interface
, uint32_t *err
)
623 if (si
== NULL
) return NULL
;
624 if (si
->vtable
->sim_host_byaddr
== NULL
) return NULL
;
625 return si
->vtable
->sim_host_byaddr(si
, addr
, af
, interface
, err
);
629 si_host_all(si_mod_t
*si
)
631 if (si
== NULL
) return NULL
;
632 if (si
->vtable
->sim_host_all
== NULL
) return NULL
;
633 return si
->vtable
->sim_host_all(si
);
637 si_mac_byname(struct si_mod_s
*si
, const char *name
)
639 if (si
== NULL
) return NULL
;
640 if (si
->vtable
->sim_mac_byname
== NULL
) return NULL
;
641 return si
->vtable
->sim_mac_byname(si
, name
);
645 si_mac_bymac(struct si_mod_s
*si
, const char *mac
)
647 if (si
== NULL
) return NULL
;
648 if (si
->vtable
->sim_mac_bymac
== NULL
) return NULL
;
649 return si
->vtable
->sim_mac_bymac(si
, mac
);
653 si_mac_all(si_mod_t
*si
)
655 if (si
== NULL
) return NULL
;
656 if (si
->vtable
->sim_mac_all
== NULL
) return NULL
;
657 return si
->vtable
->sim_mac_all(si
);
661 si_network_byname(si_mod_t
*si
, const char *name
)
663 if (si
== NULL
) return NULL
;
664 if (si
->vtable
->sim_network_byname
== NULL
) return NULL
;
665 return si
->vtable
->sim_network_byname(si
, name
);
669 si_network_byaddr(si_mod_t
*si
, uint32_t addr
)
671 if (si
== NULL
) return NULL
;
672 if (si
->vtable
->sim_network_byaddr
== NULL
) return NULL
;
673 return si
->vtable
->sim_network_byaddr(si
, addr
);
677 si_network_all(si_mod_t
*si
)
679 if (si
== NULL
) return NULL
;
680 if (si
->vtable
->sim_network_all
== NULL
) return NULL
;
681 return si
->vtable
->sim_network_all(si
);
685 si_service_byname(si_mod_t
*si
, const char *name
, const char *proto
)
687 if (si
== NULL
) return NULL
;
688 if (si
->vtable
->sim_service_byname
== NULL
) return NULL
;
689 return si
->vtable
->sim_service_byname(si
, name
, proto
);
693 si_service_byport(si_mod_t
*si
, int port
, const char *proto
)
695 if (si
== NULL
) return NULL
;
696 if (si
->vtable
->sim_service_byport
== NULL
) return NULL
;
697 return si
->vtable
->sim_service_byport(si
, port
, proto
);
701 si_service_all(si_mod_t
*si
)
703 if (si
== NULL
) return NULL
;
704 if (si
->vtable
->sim_service_all
== NULL
) return NULL
;
705 return si
->vtable
->sim_service_all(si
);
709 si_protocol_byname(si_mod_t
*si
, const char *name
)
711 if (si
== NULL
) return NULL
;
712 if (si
->vtable
->sim_protocol_byname
== NULL
) return NULL
;
713 return si
->vtable
->sim_protocol_byname(si
, name
);
717 si_protocol_bynumber(si_mod_t
*si
, uint32_t number
)
719 if (si
== NULL
) return NULL
;
720 if (si
->vtable
->sim_protocol_bynumber
== NULL
) return NULL
;
721 return si
->vtable
->sim_protocol_bynumber(si
, number
);
725 si_protocol_all(si_mod_t
*si
)
727 if (si
== NULL
) return NULL
;
728 if (si
->vtable
->sim_protocol_all
== NULL
) return NULL
;
729 return si
->vtable
->sim_protocol_all(si
);
733 si_rpc_byname(si_mod_t
*si
, const char *name
)
735 if (si
== NULL
) return NULL
;
736 if (si
->vtable
->sim_rpc_byname
== NULL
) return NULL
;
737 return si
->vtable
->sim_rpc_byname(si
, name
);
741 si_rpc_bynumber(si_mod_t
*si
, int number
)
743 if (si
== NULL
) return NULL
;
744 if (si
->vtable
->sim_rpc_bynumber
== NULL
) return NULL
;
745 return si
->vtable
->sim_rpc_bynumber(si
, number
);
749 si_rpc_all(si_mod_t
*si
)
751 if (si
== NULL
) return NULL
;
752 if (si
->vtable
->sim_rpc_all
== NULL
) return NULL
;
753 return si
->vtable
->sim_rpc_all(si
);
757 si_fs_byspec(si_mod_t
*si
, const char *spec
)
759 if (si
== NULL
) return NULL
;
760 if (si
->vtable
->sim_fs_byspec
== NULL
) return NULL
;
761 return si
->vtable
->sim_fs_byspec(si
, spec
);
765 si_fs_byfile(si_mod_t
*si
, const char *file
)
767 if (si
== NULL
) return NULL
;
768 if (si
->vtable
->sim_fs_byfile
== NULL
) return NULL
;
769 return si
->vtable
->sim_fs_byfile(si
, file
);
773 si_fs_all(si_mod_t
*si
)
775 if (si
== NULL
) return NULL
;
776 if (si
->vtable
->sim_fs_all
== NULL
) return NULL
;
777 return si
->vtable
->sim_fs_all(si
);
781 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
)
783 if (si
== NULL
) return NULL
;
787 case SI_CALL_USER_BYNAME
: return si_user_byname(si
, str1
);
788 case SI_CALL_USER_BYUID
: return si_user_byuid(si
, (uid_t
)num1
);
789 case SI_CALL_GROUP_BYNAME
: return si_group_byname(si
, str1
);
790 case SI_CALL_GROUP_BYGID
: return si_group_bygid(si
, (gid_t
)num1
);
791 case SI_CALL_GROUPLIST
: return si_grouplist(si
, str1
, (int) num1
);
792 case SI_CALL_ALIAS_BYNAME
: return si_alias_byname(si
, str1
);
793 case SI_CALL_HOST_BYNAME
: return si_host_byname(si
, str1
, num1
, str3
, err
);
794 case SI_CALL_HOST_BYADDR
: return si_host_byaddr(si
, (void *)str1
, num1
, str3
, err
);
795 case SI_CALL_NETWORK_BYNAME
: return si_network_byname(si
, str1
);
796 case SI_CALL_NETWORK_BYADDR
: return si_network_byaddr(si
, num1
);
797 case SI_CALL_SERVICE_BYNAME
: return si_service_byname(si
, str1
, str2
);
798 case SI_CALL_SERVICE_BYPORT
: return si_service_byport(si
, num1
, str2
);
799 case SI_CALL_PROTOCOL_BYNAME
: return si_protocol_byname(si
, str1
);
800 case SI_CALL_PROTOCOL_BYNUMBER
: return si_protocol_bynumber(si
, num1
);
801 case SI_CALL_RPC_BYNAME
: return si_network_byname(si
, str1
);
802 case SI_CALL_RPC_BYNUMBER
: return si_rpc_bynumber(si
, num1
);
803 case SI_CALL_FS_BYSPEC
: return si_fs_byspec(si
, str1
);
804 case SI_CALL_FS_BYFILE
: return si_fs_byfile(si
, str1
);
805 case SI_CALL_NAMEINFO
: return si_nameinfo(si
, (const struct sockaddr
*)str1
, num1
, str3
, err
);
806 case SI_CALL_IPNODE_BYNAME
: return si_ipnode_byname(si
, (const char *)str1
, num1
, num2
, str3
, err
);
807 case SI_CALL_MAC_BYNAME
: return si_mac_byname(si
, (const char *)str1
);
808 case SI_CALL_MAC_BYMAC
: return si_mac_bymac(si
, (const char *)str1
);
810 /* Support for DNS async calls */
811 case SI_CALL_DNS_QUERY
:
812 case SI_CALL_DNS_SEARCH
:
814 if (si
->vtable
->sim_item_call
== NULL
) return NULL
;
815 return si
->vtable
->sim_item_call(si
, call
, str1
, str2
, str3
, num1
, num2
, err
);
818 default: return NULL
;
825 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
)
827 if (si
== NULL
) return NULL
;
831 case SI_CALL_USER_ALL
: return si_user_all(si
);
832 case SI_CALL_GROUP_ALL
: return si_group_all(si
);
833 case SI_CALL_ALIAS_ALL
: return si_alias_all(si
);
834 case SI_CALL_HOST_ALL
: return si_host_all(si
);
835 case SI_CALL_NETWORK_ALL
: return si_network_all(si
);
836 case SI_CALL_SERVICE_ALL
: return si_service_all(si
);
837 case SI_CALL_PROTOCOL_ALL
: return si_protocol_all(si
);
838 case SI_CALL_RPC_ALL
: return si_rpc_all(si
);
839 case SI_CALL_FS_ALL
: return si_fs_all(si
);
840 case SI_CALL_MAC_ALL
: return si_mac_all(si
);
841 case SI_CALL_ADDRINFO
: return si_addrinfo(si
, str1
, str2
, num1
, num2
, num3
, num4
, str3
, err
);
842 default: return NULL
;
849 si_async_worklist_add_unit(si_async_workunit_t
*r
)
851 pthread_mutex_lock(&module_mutex
);
852 r
->next
= si_async_worklist
;
853 si_async_worklist
= r
;
854 pthread_mutex_unlock(&module_mutex
);
858 si_async_worklist_remove_unit(si_async_workunit_t
*r
)
860 si_async_workunit_t
*x
;
862 pthread_mutex_lock(&module_mutex
);
863 if (si_async_worklist
== r
)
865 si_async_worklist
= r
->next
;
869 for (x
= si_async_worklist
; (x
!= NULL
) && (x
->next
!= r
); x
= x
->next
) {;}
870 if (x
!= NULL
) x
->next
= r
->next
;
872 pthread_mutex_unlock(&module_mutex
);
875 static si_async_workunit_t
*
876 si_async_worklist_find_unit(mach_port_t p
)
878 si_async_workunit_t
*r
;
880 pthread_mutex_lock(&module_mutex
);
881 for (r
= si_async_worklist
; (r
!= NULL
) && (r
->port
!= p
); r
= r
->next
) {;}
882 pthread_mutex_unlock(&module_mutex
);
887 static si_async_workunit_t
*
888 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
)
890 si_async_workunit_t
*r
;
891 kern_return_t status
;
892 mach_port_t reply
, send
;
893 mach_msg_type_name_t type
;
900 if (si_call_str1_is_buffer(call
))
904 s1
= calloc(1, num3
);
905 if (s1
== NULL
) return NULL
;
906 memcpy(s1
, str1
, num3
);
909 else if (str1
!= NULL
)
912 if (s1
== NULL
) return NULL
;
920 if (s1
!= NULL
) free(s1
);
930 if (s1
!= NULL
) free(s1
);
931 if (s2
!= NULL
) free(s2
);
936 r
= (si_async_workunit_t
*)calloc(1, sizeof(si_async_workunit_t
));
939 if (s1
!= NULL
) free(s1
);
940 if (s2
!= NULL
) free(s2
);
941 if (s3
!= NULL
) free(s3
);
945 reply
= MACH_PORT_NULL
;
946 send
= MACH_PORT_NULL
;
949 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &reply
);
950 if (status
== KERN_SUCCESS
) status
= mach_port_extract_right(mach_task_self(), reply
, MACH_MSG_TYPE_MAKE_SEND_ONCE
, &send
, &type
);
951 if (status
!= KERN_SUCCESS
)
953 if (reply
!= MACH_PORT_NULL
) mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
954 if (s1
!= NULL
) free(s1
);
955 if (s2
!= NULL
) free(s2
);
956 if (s3
!= NULL
) free(s3
);
973 if (si_call_returns_list(call
)) r
->flags
|= WORKUNIT_RETURNS_LIST
;
975 r
->callback
= callback
;
976 r
->context
= context
;
980 si_async_worklist_add_unit(r
);
986 si_async_workunit_release(si_async_workunit_t
*r
)
988 if (r
== NULL
) return;
990 if (OSAtomicDecrement32Barrier(&(r
->refcount
)) != 0) return;
993 fprintf(stderr
, "** %s freeing worklist item %p\n", __func__
, r
);
996 si_async_worklist_remove_unit(r
);
998 if (r
->resitem
!= NULL
) si_item_release(r
->resitem
);
999 if (r
->reslist
!= NULL
) si_list_release(r
->reslist
);
1001 if (r
->str1
!= NULL
) free(r
->str1
);
1002 if (r
->str2
!= NULL
) free(r
->str2
);
1003 if (r
->str3
!= NULL
) free(r
->str3
);
1005 /* release send-once right if it has not been used */
1006 if (r
->send
!= MACH_PORT_NULL
) mach_port_deallocate(mach_task_self(), r
->send
);
1008 /* release receive right */
1009 mach_port_mod_refs(mach_task_self(), r
->port
, MACH_PORT_RIGHT_RECEIVE
, -1);
1015 si_async_launchpad(si_async_workunit_t
*r
)
1017 kern_return_t status
;
1018 mach_msg_empty_send_t msg
;
1021 fprintf(stderr
, "** %s starting worklist item %p\n", __func__
, r
);
1024 if (r
->flags
& WORKUNIT_CANCELLED
)
1026 si_async_workunit_release(r
);
1028 fprintf(stderr
, "** %s worklist item %p was cancelled early\n", __func__
, r
);
1033 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
));
1034 else r
->resitem
= si_item_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->str3
, r
->num1
, r
->num2
, &(r
->err
));
1037 * Test and set the cancelled flag.
1038 * If it was set, then this work item was cancelled.
1039 * Otherwise, setting it here prevents si_async_cancel from cancelling:
1040 * too late to cancel now!
1042 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1044 si_async_workunit_release(r
);
1046 fprintf(stderr
, "** %s worklist item %p was cancelled in flight\n", __func__
, r
);
1051 else fprintf(stderr
, "** %s worklist item %p flags are now 0x%08x\n", __func__
, r
, r
->flags
);
1054 memset(&msg
, 0, sizeof(mach_msg_empty_send_t
));
1056 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, MACH_MSGH_BITS_ZERO
);
1057 msg
.header
.msgh_remote_port
= r
->send
;
1058 r
->send
= MACH_PORT_NULL
;
1059 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
1060 msg
.header
.msgh_size
= sizeof(mach_msg_empty_send_t
);
1061 msg
.header
.msgh_id
= r
->call
;
1063 status
= mach_msg(&(msg
.header
), MACH_SEND_MSG
, msg
.header
.msgh_size
, 0, MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1064 if (status
!= MACH_MSG_SUCCESS
)
1066 /* receiver failed - clean up to avoid a port leak */
1067 mach_msg_destroy(&(msg
.header
));
1069 fprintf(stderr
, "** %s mach message send failed for worklist item %p\n", __func__
, r
);
1073 si_async_workunit_release(r
);
1076 * The client is now responsible for calling si_async_handle_reply,
1077 * which will invoke the client's callback and then release the workunit.
1081 fprintf(stderr
, "** %s completed async worklist item %p\n", __func__
, r
);
1086 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
)
1088 si_async_workunit_t
*req
;
1090 if (si
== NULL
) return MACH_PORT_NULL
;
1091 if (callback
== NULL
) return MACH_PORT_NULL
;
1093 /* if module does async on it's own, hand off the call */
1094 if (si
->vtable
->sim_async_call
!= NULL
)
1096 return si
->vtable
->sim_async_call(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1099 req
= si_async_workunit_create(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1100 if (req
== NULL
) return MACH_PORT_NULL
;
1102 /* queue the work on the global low-priority dispatch queue */
1104 fprintf(stderr
, "** %s dispatching worklist item %p\n", __func__
, req
);
1106 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, DISPATCH_QUEUE_OVERCOMMIT
), ^{ si_async_launchpad(req
); });
1112 si_async_cancel(mach_port_t p
)
1114 si_async_workunit_t
*r
;
1116 r
= si_async_worklist_find_unit(p
);
1120 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1126 * Test and set the WORKUNIT_CANCELLED flag.
1127 * If it was already set, this work item has been executed - too late to really cancel.
1129 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1131 /* already executed */
1133 fprintf(stderr
, "** %s worklist item %p has executed\n", __func__
, r
);
1138 fprintf(stderr
, "** %s calling worklist item %p callback SI_STATUS_CALL_CANCELLED\n", __func__
, r
);
1141 if (r
->callback
!= NULL
)
1143 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1144 else ((item_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1147 si_async_workunit_release(r
);
1151 si_async_handle_reply(mach_msg_header_t
*msg
)
1153 si_async_workunit_t
*r
;
1154 mach_port_t reply
= msg
->msgh_local_port
;
1156 r
= si_async_worklist_find_unit(reply
);
1160 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1166 fprintf(stderr
, "** %s worklist item %p flags are 0x%08x\n", __func__
, r
, r
->flags
);
1168 if ((r
->flags
& WORKUNIT_CANCELLED
) == 0)
1171 fprintf(stderr
, "** %s workunit thread is still active\n", __func__
);
1176 if (r
->callback
!= NULL
)
1178 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(r
->reslist
, r
->err
, r
->context
);
1179 else ((item_async_callback
)(r
->callback
))(r
->resitem
, r
->err
, r
->context
);
1187 fprintf(stderr
, "** %s workunit has no callback\n", __func__
);
1191 si_async_workunit_release(r
);
1195 si_standardize_mac_address(const char *addr
)
1199 struct ether_addr
*ether
;
1202 if (addr
== NULL
) return NULL
;
1204 /* ether_aton isn't thread-safe */
1205 pthread_mutex_lock(&module_mutex
);
1207 ether
= ether_aton(addr
);
1210 pthread_mutex_unlock(&module_mutex
);
1214 for (i
= 0; i
< 6; i
++)
1216 if (ether
->ether_addr_octet
[i
] <= 15)
1218 sprintf(e
[i
], "0%x", ether
->ether_addr_octet
[i
]);
1222 sprintf(e
[i
], "%x", ether
->ether_addr_octet
[i
]);
1226 pthread_mutex_unlock(&module_mutex
);
1229 asprintf(&out
, "%s:%s:%s:%s:%s:%s", e
[0], e
[1], e
[2], e
[3], e
[4], e
[5]);