]> git.saurik.com Git - apple/configd.git/blobdiff - libSystemConfiguration/libSystemConfiguration_client.c
configd-1109.101.1.tar.gz
[apple/configd.git] / libSystemConfiguration / libSystemConfiguration_client.c
index 531a3fdc5903c36fd418e8b693f6e22908187d33..760c301045ffe74949b59fef017aceedb3a53e70 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, 2015 Apple Inc. All rights reserved.
+ * Copyright (c) 2012, 2013, 2015, 2016, 2018, 2019 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#include <Availability.h>
 #include <TargetConditionals.h>
-#include <asl.h>
 #include <dispatch/dispatch.h>
+#include <dispatch/private.h>
+#include <os/log.h>
 #include <vproc.h>
 #include <vproc_priv.h>
 #include <xpc/xpc.h>
 
 #include "libSystemConfiguration_client.h"
+#include "libSystemConfiguration_internal.h"
 
 
 #pragma mark -
 #pragma mark libSC fork handlers
 
 
-__attribute__((weak_import)) bool _dispatch_is_multithreaded(void);
-
-static boolean_t _has_forked = FALSE;
+static boolean_t _available    = TRUE;
 
 // These functions are registered with libSystem to
 // handle pthread_atfork callbacks.
@@ -58,9 +57,8 @@ _libSC_info_fork_parent()
 void
 _libSC_info_fork_child()
 {
-       if (_dispatch_is_multithreaded()) {
-               // if dispatch was active before fork
-               _has_forked = TRUE;
+       if (_dispatch_is_fork_of_multithreaded_parent()) {
+               _available = FALSE;
        }
 
        return;
@@ -77,11 +75,29 @@ log_xpc_object(const char *msg, xpc_object_t obj)
        char    *desc;
 
        desc = xpc_copy_description(obj);
-       asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s = %s", msg, desc);
+       os_log(OS_LOG_DEFAULT, "%s = %s", msg, desc);
        free(desc);
 }
 
 
+__private_extern__
+_Bool
+libSC_info_available()
+{
+       return _available;
+}
+
+
+static void
+libSC_info_client_dealloc(libSC_info_client_t *client)
+{
+       free(client->service_description);
+       free(client->service_name);
+       free(client);
+       return;
+}
+
+
 __private_extern__
 libSC_info_client_t *
 libSC_info_client_create(dispatch_queue_t      q,
@@ -90,13 +106,13 @@ libSC_info_client_create(dispatch_queue_t  q,
 {
        xpc_connection_t        c;
        libSC_info_client_t     *client;
-#if    !TARGET_IPHONE_SIMULATOR
+#if    !TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
        const uint64_t          flags   =       XPC_CONNECTION_MACH_SERVICE_PRIVILEGED;
-#else  // !TARGET_IPHONE_SIMULATOR
+#else  // !TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
        const uint64_t          flags   =       0;
-#endif // !TARGET_IPHONE_SIMULATOR
+#endif // !TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
 
-       if (_has_forked) {
+       if (!_available) {
                return NULL;
        }
 
@@ -112,34 +128,37 @@ libSC_info_client_create(dispatch_queue_t q,
 
                type = xpc_get_type(xobj);
                if (type == XPC_TYPE_DICTIONARY) {
-                       asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s: unexpected message", client->service_name);
+                       os_log(OS_LOG_DEFAULT, "%s: unexpected message", client->service_name);
                        log_xpc_object("  dict = ", xobj);
                } else if (type == XPC_TYPE_ERROR) {
                        if (xobj == XPC_ERROR_CONNECTION_INVALID) {
-                               asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s: server not available", client->service_name);
+                               os_log(OS_LOG_DEFAULT, "%s: server not available", client->service_name);
                                client->active = FALSE;
                        } else if (xobj == XPC_ERROR_CONNECTION_INTERRUPTED) {
-                               asl_log(NULL, NULL, ASL_LEVEL_DEBUG, "%s: server failed", client->service_name);
+                               os_log_debug(OS_LOG_DEFAULT, "%s: server failed", client->service_name);
                        } else {
                                const char      *desc;
 
                                desc = xpc_dictionary_get_string(xobj, XPC_ERROR_KEY_DESCRIPTION);
-                               asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
-                                       "%s: connection error: %d : %s",
-                                       client->service_name,
-                                       xpc_connection_get_pid(c),
-                                       desc);
+                               os_log_debug(OS_LOG_DEFAULT,
+                                            "%s: connection error: %d : %s",
+                                            client->service_name,
+                                            xpc_connection_get_pid(c),
+                                            desc);
                        }
                } else {
-                       asl_log(NULL, NULL, ASL_LEVEL_ERR,
-                               "%s: unknown event type : %p",
-                               client->service_name,
-                               type);
+                       os_log(OS_LOG_DEFAULT,
+                              "%s: unknown event type : %p",
+                              client->service_name,
+                              type);
                }
        });
 
        client->connection = c;
 
+       xpc_connection_set_context(c, client);
+       xpc_connection_set_finalizer_f(c, (xpc_finalizer_t)libSC_info_client_dealloc);
+
        xpc_connection_resume(c);
 
        return client;
@@ -151,9 +170,7 @@ void
 libSC_info_client_release(libSC_info_client_t *client)
 {
        xpc_release(client->connection);
-       free(client->service_description);
-       free(client->service_name);
-       free(client);
+       return;
 }
 
 
@@ -177,23 +194,23 @@ libSC_send_message_with_reply_sync(libSC_info_client_t    *client,
                        }
 
                        if ((type == XPC_TYPE_ERROR) && (reply == XPC_ERROR_CONNECTION_INTERRUPTED)) {
-                               asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
-                                       "%s server failure, retrying",
-                                       client->service_description);
+                               os_log_debug(OS_LOG_DEFAULT,
+                                            "%s server failure, retrying",
+                                            client->service_description);
                                // retry request
                                xpc_release(reply);
                                continue;
                        }
 
                        if ((type == XPC_TYPE_ERROR) && (reply == XPC_ERROR_CONNECTION_INVALID)) {
-                               asl_log(NULL, NULL, ASL_LEVEL_ERR,
-                                       "%s server not available",
-                                       client->service_description);
+                               os_log(OS_LOG_DEFAULT,
+                                      "%s server not available",
+                                      client->service_description);
                                client->active = FALSE;
                        } else {
-                               asl_log(NULL, NULL, ASL_LEVEL_ERR,
-                                       "%s xpc_connection_send_message_with_reply_sync() with unexpected reply",
-                                       client->service_description);
+                               os_log(OS_LOG_DEFAULT,
+                                      "%s xpc_connection_send_message_with_reply_sync() with unexpected reply",
+                                      client->service_description);
                                log_xpc_object("  reply", reply);
                        }