From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id C782B3857C50 for ; Thu, 31 Mar 2022 12:04:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C782B3857C50 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from tarox.wildebeest.org (83-87-18-245.cable.dynamic.v4.ziggo.nl [83.87.18.245]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 69E16304319C; Thu, 31 Mar 2022 14:04:17 +0200 (CEST) Received: by tarox.wildebeest.org (Postfix, from userid 1000) id 9A114413CD0E; Thu, 31 Mar 2022 14:04:15 +0200 (CEST) Message-ID: <1ea188affb14c7b55ec1d54fe95627d83e730bb4.camel@klomp.org> Subject: Using libcurl in another library, when/if to call curl_global_init? From: Mark Wielaard To: curl-library@lists.haxx.se Cc: elfutils-devel@sourceware.org Date: Thu, 31 Mar 2022 14:04:15 +0200 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Evolution 3.28.5 (3.28.5-10.el7) Mime-Version: 1.0 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Mar 2022 12:04:23 -0000 Hi, We we are using libcurl in our own library now to (optionally) fetch resources from http(s) locations. It is an implementation detail we use libcurl for this and don't expose any Curl handles outside our own library. It works great, thanks! But we are struggling a bit with how to safely/correctly initialize libcurl. Our own library doesn't need any global initialization, so users won't use any global init like function. We have therefore added a constructor function to the library that is called as soon as the library is loaded which simply calls curl_global_init(CURL_GLOBAL_DEFAULT); We added it in the constructor so that it is called as early as possible (hopefully) before the program created any threads because the curl_global_init documentation says to call it before any threads are created by the program. This seems to work, but it means that curl_global_init is always called even if our own library happens to not use libcurl to fetch any remote resources. And this is causing some problems for our users because (in FIPS mode) libcurl does significant initialization work (to check the ssl implementation?) The documentation seems to imply that you can also call curl_easy_init without calling curl_global_init first. And doing that seems to work fine. But our own testsuite doesn't contain many multi-threaded examples. Since the documentation does imply that doing without curl_global_init might not be thread-safe we wonder how other libraries that use libcurl are doing this. Or whether there is a thread-safe way to call curl_global_init at a later time (to get rid of the library constructor init function). Thanks, Mark