hostip.c: Move macOS-specific calls into global init call

https://github.com/curl/curl/pull/7121 introduced a macOS system call
to `SCDynamicStoreCopyProxies`, which is invoked every time an IP
address needs to be resolved.

However, this system call is not thread-safe, and macOS will kill the
process if the system call is run first in a fork. To make it possible
for the parent process to call this once and prevent the crash, only
invoke this system call in the global initialization routine.

In addition, this change is beneficial because it:

1. Avoids extra macOS system calls for every IP lookup.
2. Consolidates macOS-specific initialization in a separate file.

Fixes #11252
Closes #11254
This commit is contained in:
Stan Hu 2023-06-05 10:05:48 -07:00 committed by Daniel Stenberg
parent 22c92a6d51
commit c7308592fb
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 108 additions and 21 deletions

View File

@ -180,6 +180,7 @@ LIB_CFILES = \
krb5.c \
ldap.c \
llist.c \
macos.c \
md4.c \
md5.c \
memdebug.c \
@ -316,6 +317,7 @@ LIB_HFILES = \
inet_ntop.h \
inet_pton.h \
llist.h \
macos.h \
memdebug.h \
mime.h \
mqtt.h \

View File

@ -63,6 +63,7 @@
#include "slist.h"
#include "mime.h"
#include "amigaos.h"
#include "macos.h"
#include "warnless.h"
#include "sigpipe.h"
#include "vssh/ssh.h"
@ -181,6 +182,11 @@ static CURLcode global_init(long flags, bool memoryfuncs)
}
#endif
if(Curl_macos_init()) {
DEBUGF(fprintf(stderr, "Error: Curl_macos_init failed\n"));
goto fail;
}
if(Curl_resolver_global_init()) {
DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
goto fail;

View File

@ -67,10 +67,6 @@
#include "curl_memory.h"
#include "memdebug.h"
#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#endif
#if defined(CURLRES_SYNCH) && \
defined(HAVE_ALARM) && \
defined(SIGALRM) && \
@ -743,23 +739,6 @@ enum resolve_t Curl_resolv(struct Curl_easy *data,
return CURLRESOLV_ERROR;
}
#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
{
/*
* The automagic conversion from IPv4 literals to IPv6 literals only
* works if the SCDynamicStoreCopyProxies system function gets called
* first. As Curl currently doesn't support system-wide HTTP proxies, we
* therefore don't use any value this function might return.
*
* This function is only available on a macOS and is not needed for
* IPv4-only builds, hence the conditions above.
*/
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
if(dict)
CFRelease(dict);
}
#endif
#ifndef USE_RESOLVE_ON_IPS
/* First check if this is an IPv4 address string */
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)

62
lib/macos.c Normal file
View File

@ -0,0 +1,62 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#if defined(__APPLE__)
#if defined(TARGET_OS_OSX) && TARGET_OS_OSX
#include <curl/curl.h>
#include "macos.h"
#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#endif
CURLcode Curl_macos_init(void)
{
#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
{
/*
* The automagic conversion from IPv4 literals to IPv6 literals only
* works if the SCDynamicStoreCopyProxies system function gets called
* first. As Curl currently doesn't support system-wide HTTP proxies, we
* therefore don't use any value this function might return.
*
* This function is only available on a macOS and is not needed for
* IPv4-only builds, hence the conditions above.
*/
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
if(dict)
CFRelease(dict);
}
#endif
return CURLE_OK;
}
#endif /* TARGET_OS_OSX */
#endif /* __APPLE__ */

38
lib/macos.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef HEADER_CURL_MACOS_H
#define HEADER_CURL_MACOS_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
CURLcode Curl_macos_init(void);
#else
#define Curl_macos_init() CURLE_OK
#endif
#endif /* HEADER_CURL_MACOS_H */