Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XrdTpc] Added CLOEXEC flag for curl file descriptors #1653

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/XrdTpc/XrdTpcTPC.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ void CurlDeleter::operator()(CURL *curl)
if (curl) curl_easy_cleanup(curl);
}

/**
* The callback that will be called by libcurl when the socket has been created
* https://curl.se/libcurl/c/CURLOPT_SOCKOPTFUNCTION.html
*/
int TPCHandler::sockopt_setcloexec_callback(void *clientp, curl_socket_t curlfd, curlsocktype purpose) {
int oldFlags = fcntl(curlfd,F_GETFD,0);
if(oldFlags < 0) {
return CURL_SOCKOPT_ERROR;
}
oldFlags |= FD_CLOEXEC;
if(!fcntl(curlfd,F_SETFD,oldFlags)) {
return CURL_SOCKOPT_OK;
}
return CURL_SOCKOPT_ERROR;
}

// We need to utilize the full URL (including the query string), not just the
// resource name. The query portion is hidden in the `xrd-http-query` header;
Expand Down Expand Up @@ -641,7 +656,7 @@ int TPCHandler::ProcessPushReq(const std::string & resource, XrdHttpExtReq &req)
return req.SendSimpleResp(rec.status, NULL, NULL, msg, 0);
}
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_setcloexec_callback);
auto query_header = req.headers.find("xrd-http-fullresource");
std::string redirect_resource = req.resource;
if (query_header != req.headers.end()) {
Expand Down Expand Up @@ -714,6 +729,7 @@ int TPCHandler::ProcessPullReq(const std::string &resource, XrdHttpExtReq &req)
return req.SendSimpleResp(rec.status, NULL, NULL, msg, 0);
}
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl,CURLOPT_SOCKOPTFUNCTION,sockopt_setcloexec_callback);
std::unique_ptr<XrdSfsFile> fh(m_sfs->newFile(name, m_monid++));
if (!fh.get()) {
char msg[] = "Failed to initialize internal transfer file handle";
Expand Down
4 changes: 4 additions & 0 deletions src/XrdTpc/XrdTpcTPC.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "XrdTls/XrdTlsTempCA.hh"

#include <curl/curl.h>

class XrdOucErrInfo;
class XrdOucStream;
class XrdSfsFile;
Expand Down Expand Up @@ -46,6 +48,8 @@ public:

private:

static int sockopt_setcloexec_callback(void * clientp, curl_socket_t curlfd, curlsocktype purpose);

struct TPCLogRecord {

TPCLogRecord() : status( -1 ),
Expand Down