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>
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_all(si_mod_t
*si
)
527 if (si
== NULL
) return NULL
;
528 if (si
->vtable
->sim_user_all
== NULL
) return NULL
;
529 return si
->vtable
->sim_user_all(si
);
533 si_group_byname(si_mod_t
*si
, const char *name
)
535 if (si
== NULL
) return NULL
;
536 if (si
->vtable
->sim_group_byname
== NULL
) return NULL
;
537 return si
->vtable
->sim_group_byname(si
, name
);
541 si_group_bygid(si_mod_t
*si
, gid_t gid
)
543 if (si
== NULL
) return NULL
;
544 if (si
->vtable
->sim_group_bygid
== NULL
) return NULL
;
545 return si
->vtable
->sim_group_bygid(si
, gid
);
549 si_group_all(si_mod_t
*si
)
551 if (si
== NULL
) return NULL
;
552 if (si
->vtable
->sim_group_all
== NULL
) return NULL
;
553 return si
->vtable
->sim_group_all(si
);
557 si_grouplist(si_mod_t
*si
, const char *name
)
559 if (si
== NULL
) return NULL
;
560 if (si
->vtable
->sim_grouplist
== NULL
) return NULL
;
561 return si
->vtable
->sim_grouplist(si
, name
);
565 si_netgroup_byname(struct si_mod_s
*si
, const char *name
)
567 if (si
== NULL
) return NULL
;
568 if (si
->vtable
->sim_netgroup_byname
== NULL
) return NULL
;
569 return si
->vtable
->sim_netgroup_byname(si
, name
);
573 si_in_netgroup(struct si_mod_s
*si
, const char *name
, const char *host
, const char *user
, const char *domain
)
575 if (si
== NULL
) return 0;
576 if (si
->vtable
->sim_in_netgroup
== NULL
) return 0;
577 return si
->vtable
->sim_in_netgroup(si
, name
, host
, user
, domain
);
581 si_alias_byname(si_mod_t
*si
, const char *name
)
583 if (si
== NULL
) return NULL
;
584 if (si
->vtable
->sim_alias_byname
== NULL
) return NULL
;
585 return si
->vtable
->sim_alias_byname(si
, name
);
589 si_alias_all(si_mod_t
*si
)
591 if (si
== NULL
) return NULL
;
592 if (si
->vtable
->sim_alias_all
== NULL
) return NULL
;
593 return si
->vtable
->sim_alias_all(si
);
597 si_host_byname(si_mod_t
*si
, const char *name
, int af
, const char *interface
, uint32_t *err
)
599 if (si
== NULL
) return NULL
;
600 if (si
->vtable
->sim_host_byname
== NULL
) return NULL
;
601 return si
->vtable
->sim_host_byname(si
, name
, af
, interface
, err
);
605 si_host_byaddr(si_mod_t
*si
, const void *addr
, int af
, const char *interface
, uint32_t *err
)
607 if (si
== NULL
) return NULL
;
608 if (si
->vtable
->sim_host_byaddr
== NULL
) return NULL
;
609 return si
->vtable
->sim_host_byaddr(si
, addr
, af
, interface
, err
);
613 si_host_all(si_mod_t
*si
)
615 if (si
== NULL
) return NULL
;
616 if (si
->vtable
->sim_host_all
== NULL
) return NULL
;
617 return si
->vtable
->sim_host_all(si
);
621 si_mac_byname(struct si_mod_s
*si
, const char *name
)
623 if (si
== NULL
) return NULL
;
624 if (si
->vtable
->sim_mac_byname
== NULL
) return NULL
;
625 return si
->vtable
->sim_mac_byname(si
, name
);
629 si_mac_bymac(struct si_mod_s
*si
, const char *mac
)
631 if (si
== NULL
) return NULL
;
632 if (si
->vtable
->sim_mac_bymac
== NULL
) return NULL
;
633 return si
->vtable
->sim_mac_bymac(si
, mac
);
637 si_mac_all(si_mod_t
*si
)
639 if (si
== NULL
) return NULL
;
640 if (si
->vtable
->sim_mac_all
== NULL
) return NULL
;
641 return si
->vtable
->sim_mac_all(si
);
645 si_network_byname(si_mod_t
*si
, const char *name
)
647 if (si
== NULL
) return NULL
;
648 if (si
->vtable
->sim_network_byname
== NULL
) return NULL
;
649 return si
->vtable
->sim_network_byname(si
, name
);
653 si_network_byaddr(si_mod_t
*si
, uint32_t addr
)
655 if (si
== NULL
) return NULL
;
656 if (si
->vtable
->sim_network_byaddr
== NULL
) return NULL
;
657 return si
->vtable
->sim_network_byaddr(si
, addr
);
661 si_network_all(si_mod_t
*si
)
663 if (si
== NULL
) return NULL
;
664 if (si
->vtable
->sim_network_all
== NULL
) return NULL
;
665 return si
->vtable
->sim_network_all(si
);
669 si_service_byname(si_mod_t
*si
, const char *name
, const char *proto
)
671 if (si
== NULL
) return NULL
;
672 if (si
->vtable
->sim_service_byname
== NULL
) return NULL
;
673 return si
->vtable
->sim_service_byname(si
, name
, proto
);
677 si_service_byport(si_mod_t
*si
, int port
, const char *proto
)
679 if (si
== NULL
) return NULL
;
680 if (si
->vtable
->sim_service_byport
== NULL
) return NULL
;
681 return si
->vtable
->sim_service_byport(si
, port
, proto
);
685 si_service_all(si_mod_t
*si
)
687 if (si
== NULL
) return NULL
;
688 if (si
->vtable
->sim_service_all
== NULL
) return NULL
;
689 return si
->vtable
->sim_service_all(si
);
693 si_protocol_byname(si_mod_t
*si
, const char *name
)
695 if (si
== NULL
) return NULL
;
696 if (si
->vtable
->sim_protocol_byname
== NULL
) return NULL
;
697 return si
->vtable
->sim_protocol_byname(si
, name
);
701 si_protocol_bynumber(si_mod_t
*si
, uint32_t number
)
703 if (si
== NULL
) return NULL
;
704 if (si
->vtable
->sim_protocol_bynumber
== NULL
) return NULL
;
705 return si
->vtable
->sim_protocol_bynumber(si
, number
);
709 si_protocol_all(si_mod_t
*si
)
711 if (si
== NULL
) return NULL
;
712 if (si
->vtable
->sim_protocol_all
== NULL
) return NULL
;
713 return si
->vtable
->sim_protocol_all(si
);
717 si_rpc_byname(si_mod_t
*si
, const char *name
)
719 if (si
== NULL
) return NULL
;
720 if (si
->vtable
->sim_rpc_byname
== NULL
) return NULL
;
721 return si
->vtable
->sim_rpc_byname(si
, name
);
725 si_rpc_bynumber(si_mod_t
*si
, int number
)
727 if (si
== NULL
) return NULL
;
728 if (si
->vtable
->sim_rpc_bynumber
== NULL
) return NULL
;
729 return si
->vtable
->sim_rpc_bynumber(si
, number
);
733 si_rpc_all(si_mod_t
*si
)
735 if (si
== NULL
) return NULL
;
736 if (si
->vtable
->sim_rpc_all
== NULL
) return NULL
;
737 return si
->vtable
->sim_rpc_all(si
);
741 si_fs_byspec(si_mod_t
*si
, const char *spec
)
743 if (si
== NULL
) return NULL
;
744 if (si
->vtable
->sim_fs_byspec
== NULL
) return NULL
;
745 return si
->vtable
->sim_fs_byspec(si
, spec
);
749 si_fs_byfile(si_mod_t
*si
, const char *file
)
751 if (si
== NULL
) return NULL
;
752 if (si
->vtable
->sim_fs_byfile
== NULL
) return NULL
;
753 return si
->vtable
->sim_fs_byfile(si
, file
);
757 si_fs_all(si_mod_t
*si
)
759 if (si
== NULL
) return NULL
;
760 if (si
->vtable
->sim_fs_all
== NULL
) return NULL
;
761 return si
->vtable
->sim_fs_all(si
);
765 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
)
767 if (si
== NULL
) return NULL
;
771 case SI_CALL_USER_BYNAME
: return si_user_byname(si
, str1
);
772 case SI_CALL_USER_BYUID
: return si_user_byuid(si
, (uid_t
)num1
);
773 case SI_CALL_GROUP_BYNAME
: return si_group_byname(si
, str1
);
774 case SI_CALL_GROUP_BYGID
: return si_group_bygid(si
, (gid_t
)num1
);
775 case SI_CALL_GROUPLIST
: return si_grouplist(si
, str1
);
776 case SI_CALL_ALIAS_BYNAME
: return si_alias_byname(si
, str1
);
777 case SI_CALL_HOST_BYNAME
: return si_host_byname(si
, str1
, num1
, str3
, err
);
778 case SI_CALL_HOST_BYADDR
: return si_host_byaddr(si
, (void *)str1
, num1
, str3
, err
);
779 case SI_CALL_NETWORK_BYNAME
: return si_network_byname(si
, str1
);
780 case SI_CALL_NETWORK_BYADDR
: return si_network_byaddr(si
, num1
);
781 case SI_CALL_SERVICE_BYNAME
: return si_service_byname(si
, str1
, str2
);
782 case SI_CALL_SERVICE_BYPORT
: return si_service_byport(si
, num1
, str2
);
783 case SI_CALL_PROTOCOL_BYNAME
: return si_protocol_byname(si
, str1
);
784 case SI_CALL_PROTOCOL_BYNUMBER
: return si_protocol_bynumber(si
, num1
);
785 case SI_CALL_RPC_BYNAME
: return si_network_byname(si
, str1
);
786 case SI_CALL_RPC_BYNUMBER
: return si_rpc_bynumber(si
, num1
);
787 case SI_CALL_FS_BYSPEC
: return si_fs_byspec(si
, str1
);
788 case SI_CALL_FS_BYFILE
: return si_fs_byfile(si
, str1
);
789 case SI_CALL_NAMEINFO
: return si_nameinfo(si
, (const struct sockaddr
*)str1
, num1
, str3
, err
);
790 case SI_CALL_IPNODE_BYNAME
: return si_ipnode_byname(si
, (const char *)str1
, num1
, num2
, str3
, err
);
791 case SI_CALL_MAC_BYNAME
: return si_mac_byname(si
, (const char *)str1
);
792 case SI_CALL_MAC_BYMAC
: return si_mac_bymac(si
, (const char *)str1
);
794 /* Support for DNS async calls */
795 case SI_CALL_DNS_QUERY
:
796 case SI_CALL_DNS_SEARCH
:
798 if (si
->vtable
->sim_item_call
== NULL
) return NULL
;
799 return si
->vtable
->sim_item_call(si
, call
, str1
, str2
, str3
, num1
, num2
, err
);
802 default: return NULL
;
809 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
)
811 if (si
== NULL
) return NULL
;
815 case SI_CALL_USER_ALL
: return si_user_all(si
);
816 case SI_CALL_GROUP_ALL
: return si_group_all(si
);
817 case SI_CALL_ALIAS_ALL
: return si_alias_all(si
);
818 case SI_CALL_HOST_ALL
: return si_host_all(si
);
819 case SI_CALL_NETWORK_ALL
: return si_network_all(si
);
820 case SI_CALL_SERVICE_ALL
: return si_service_all(si
);
821 case SI_CALL_PROTOCOL_ALL
: return si_protocol_all(si
);
822 case SI_CALL_RPC_ALL
: return si_rpc_all(si
);
823 case SI_CALL_FS_ALL
: return si_fs_all(si
);
824 case SI_CALL_MAC_ALL
: return si_mac_all(si
);
825 case SI_CALL_ADDRINFO
: return si_addrinfo(si
, str1
, str2
, num1
, num2
, num3
, num4
, str3
, err
);
826 default: return NULL
;
833 si_async_worklist_add_unit(si_async_workunit_t
*r
)
835 pthread_mutex_lock(&module_mutex
);
836 r
->next
= si_async_worklist
;
837 si_async_worklist
= r
;
838 pthread_mutex_unlock(&module_mutex
);
842 si_async_worklist_remove_unit(si_async_workunit_t
*r
)
844 si_async_workunit_t
*x
;
846 pthread_mutex_lock(&module_mutex
);
847 if (si_async_worklist
== r
)
849 si_async_worklist
= r
->next
;
853 for (x
= si_async_worklist
; (x
!= NULL
) && (x
->next
!= r
); x
= x
->next
) {;}
854 if (x
!= NULL
) x
->next
= r
->next
;
856 pthread_mutex_unlock(&module_mutex
);
859 static si_async_workunit_t
*
860 si_async_worklist_find_unit(mach_port_t p
)
862 si_async_workunit_t
*r
;
864 pthread_mutex_lock(&module_mutex
);
865 for (r
= si_async_worklist
; (r
!= NULL
) && (r
->port
!= p
); r
= r
->next
) {;}
866 pthread_mutex_unlock(&module_mutex
);
871 static si_async_workunit_t
*
872 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
)
874 si_async_workunit_t
*r
;
875 kern_return_t status
;
876 mach_port_t reply
, send
;
877 mach_msg_type_name_t type
;
884 if (si_call_str1_is_buffer(call
))
888 s1
= calloc(1, num3
);
889 if (s1
== NULL
) return NULL
;
890 memcpy(s1
, str1
, num3
);
893 else if (str1
!= NULL
)
896 if (s1
== NULL
) return NULL
;
904 if (s1
!= NULL
) free(s1
);
914 if (s1
!= NULL
) free(s1
);
915 if (s2
!= NULL
) free(s2
);
920 r
= (si_async_workunit_t
*)calloc(1, sizeof(si_async_workunit_t
));
923 if (s1
!= NULL
) free(s1
);
924 if (s2
!= NULL
) free(s2
);
925 if (s3
!= NULL
) free(s3
);
929 reply
= MACH_PORT_NULL
;
930 send
= MACH_PORT_NULL
;
933 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &reply
);
934 if (status
== KERN_SUCCESS
) status
= mach_port_extract_right(mach_task_self(), reply
, MACH_MSG_TYPE_MAKE_SEND_ONCE
, &send
, &type
);
935 if (status
!= KERN_SUCCESS
)
937 if (reply
!= MACH_PORT_NULL
) mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
938 if (s1
!= NULL
) free(s1
);
939 if (s2
!= NULL
) free(s2
);
940 if (s3
!= NULL
) free(s3
);
957 if (si_call_returns_list(call
)) r
->flags
|= WORKUNIT_RETURNS_LIST
;
959 r
->callback
= callback
;
960 r
->context
= context
;
964 si_async_worklist_add_unit(r
);
970 si_async_workunit_release(si_async_workunit_t
*r
)
972 if (r
== NULL
) return;
974 if (OSAtomicDecrement32Barrier(&(r
->refcount
)) != 0) return;
977 fprintf(stderr
, "** %s freeing worklist item %p\n", __func__
, r
);
980 si_async_worklist_remove_unit(r
);
982 if (r
->resitem
!= NULL
) si_item_release(r
->resitem
);
983 if (r
->reslist
!= NULL
) si_list_release(r
->reslist
);
985 if (r
->str1
!= NULL
) free(r
->str1
);
986 if (r
->str2
!= NULL
) free(r
->str2
);
987 if (r
->str3
!= NULL
) free(r
->str3
);
993 si_async_launchpad(si_async_workunit_t
*r
)
995 kern_return_t status
;
996 mach_msg_empty_send_t msg
;
999 fprintf(stderr
, "** %s starting worklist item %p\n", __func__
, r
);
1002 if (r
->flags
& WORKUNIT_CANCELLED
)
1004 si_async_workunit_release(r
);
1006 fprintf(stderr
, "** %s worklist item %p was cancelled early\n", __func__
, r
);
1011 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
));
1012 else r
->resitem
= si_item_call(r
->si
, r
->call
, r
->str1
, r
->str2
, r
->str3
, r
->num1
, r
->num2
, &(r
->err
));
1015 * Test and set the cancelled flag.
1016 * If it was set, then this work item was cancelled.
1017 * Otherwise, setting it here prevents si_async_cancel from cancelling:
1018 * too late to cancel now!
1020 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1022 si_async_workunit_release(r
);
1024 fprintf(stderr
, "** %s worklist item %p was cancelled in flight\n", __func__
, r
);
1029 else fprintf(stderr
, "** %s worklist item %p flags are now 0x%08x\n", __func__
, r
, r
->flags
);
1032 memset(&msg
, 0, sizeof(mach_msg_empty_send_t
));
1034 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, MACH_MSGH_BITS_ZERO
);
1035 msg
.header
.msgh_remote_port
= r
->send
;
1036 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
1037 msg
.header
.msgh_size
= sizeof(mach_msg_empty_send_t
);
1038 msg
.header
.msgh_id
= r
->call
;
1040 status
= mach_msg(&(msg
.header
), MACH_SEND_MSG
, msg
.header
.msgh_size
, 0, MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1041 if (status
!= MACH_MSG_SUCCESS
)
1043 /* receiver failed - clean up to avoid a port leak */
1044 mach_msg_destroy(&(msg
.header
));
1046 fprintf(stderr
, "** %s mach message send failed for worklist item %p\n", __func__
, r
);
1050 si_async_workunit_release(r
);
1053 * The client is now responsible for calling si_async_handle_reply,
1054 * which will invoke the client's callback and then release the workunit.
1058 fprintf(stderr
, "** %s completed async worklist item %p\n", __func__
, r
);
1063 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
)
1065 si_async_workunit_t
*req
;
1067 if (si
== NULL
) return MACH_PORT_NULL
;
1068 if (callback
== NULL
) return MACH_PORT_NULL
;
1070 /* if module does async on it's own, hand off the call */
1071 if (si
->vtable
->sim_async_call
!= NULL
)
1073 return si
->vtable
->sim_async_call(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1076 req
= si_async_workunit_create(si
, call
, str1
, str2
, str3
, num1
, num2
, num3
, num4
, callback
, context
);
1077 if (req
== NULL
) return MACH_PORT_NULL
;
1079 /* queue the work on the global low-priority dispatch queue */
1081 fprintf(stderr
, "** %s dispatching worklist item %p\n", __func__
, req
);
1083 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, DISPATCH_QUEUE_OVERCOMMIT
), ^{ si_async_launchpad(req
); });
1089 si_async_cancel(mach_port_t p
)
1091 si_async_workunit_t
*r
;
1093 r
= si_async_worklist_find_unit(p
);
1097 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1103 * Test and set the WORKUNIT_CANCELLED flag.
1104 * If it was already set, this work item has been executed - too late to cancel.
1106 if (OSAtomicTestAndSetBarrier(WORKUNIT_CANCELLED_BIT_ADDRESS
, &(r
->flags
)) == 1)
1108 /* already executed */
1110 fprintf(stderr
, "** %s worklist item %p has executed\n", __func__
, r
);
1116 fprintf(stderr
, "** %s calling worklist item %p callback SI_STATUS_CALL_CANCELLED\n", __func__
, r
);
1119 if (r
->callback
!= NULL
)
1121 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1122 else ((item_async_callback
)(r
->callback
))(NULL
, SI_STATUS_CALL_CANCELLED
, r
->context
);
1125 si_async_workunit_release(r
);
1127 mach_port_mod_refs(mach_task_self(), p
, MACH_PORT_RIGHT_RECEIVE
, -1);
1131 si_async_handle_reply(mach_msg_header_t
*msg
)
1133 si_async_workunit_t
*r
;
1134 mach_port_t reply
= msg
->msgh_local_port
;
1136 r
= si_async_worklist_find_unit(reply
);
1140 fprintf(stderr
, "** %s can't find worklist item\n", __func__
);
1146 fprintf(stderr
, "** %s worklist item %p flags are 0x%08x\n", __func__
, r
, r
->flags
);
1148 if ((r
->flags
& WORKUNIT_CANCELLED
) == 0)
1151 fprintf(stderr
, "** %s workunit thread is still active\n", __func__
);
1156 if (r
->callback
!= NULL
)
1158 if (r
->flags
& WORKUNIT_RETURNS_LIST
) ((list_async_callback
)(r
->callback
))(r
->reslist
, r
->err
, r
->context
);
1159 else ((item_async_callback
)(r
->callback
))(r
->resitem
, r
->err
, r
->context
);
1167 fprintf(stderr
, "** %s workunit has no callback\n", __func__
);
1171 si_async_workunit_release(r
);
1173 mach_port_mod_refs(mach_task_self(), reply
, MACH_PORT_RIGHT_RECEIVE
, -1);
1177 si_standardize_mac_address(const char *addr
)
1181 struct ether_addr
*ether
;
1184 if (addr
== NULL
) return NULL
;
1186 /* ether_aton isn't thread-safe */
1187 pthread_mutex_lock(&module_mutex
);
1189 ether
= ether_aton(addr
);
1192 pthread_mutex_unlock(&module_mutex
);
1196 for (i
= 0; i
< 6; i
++)
1198 if (ether
->ether_addr_octet
[i
] <= 15)
1200 sprintf(e
[i
], "0%x", ether
->ether_addr_octet
[i
]);
1204 sprintf(e
[i
], "%x", ether
->ether_addr_octet
[i
]);
1208 pthread_mutex_unlock(&module_mutex
);
1211 asprintf(&out
, "%s:%s:%s:%s:%s:%s", e
[0], e
[1], e
[2], e
[3], e
[4], e
[5]);