From 7b83448cf5019bae59e6d62d2360f05a3445255a Mon Sep 17 00:00:00 2001 From: jojoliang Date: Wed, 26 Mar 2025 16:39:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E6=97=B6?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1pid=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/curl.cpp | 33 +++++++++++++++++++++++++++++++- src/curl.h | 6 +++++- src/s3fs.cpp | 53 ++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/curl.cpp b/src/curl.cpp index d20c4fc..ae20128 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -311,6 +311,7 @@ int S3fsCurl::max_parallel_cnt = 10; // default off_t S3fsCurl::multipart_size = MULTIPART_SIZE; // default bool S3fsCurl::is_sigv4 = true; // default string S3fsCurl::skUserAgent = "tencentyun-cosfs-v5-" + string(VERSION); +bool S3fsCurl::is_client_info_in_delete = false; // default //------------------------------------------------------------------- // Class methods for S3fsCurl @@ -2135,7 +2136,7 @@ bool S3fsCurl::GetUploadId(string& upload_id) return result; } -int S3fsCurl::DeleteRequest(const char* tpath) +int S3fsCurl::DeleteRequest(const char* tpath, int pid) { S3FS_PRN_INFO3("[tpath=%s]", SAFESTRPTR(tpath)); @@ -2163,6 +2164,13 @@ int S3fsCurl::DeleteRequest(const char* tpath) string Signature = CalcSignature("DELETE", "", "", date, resource, ""); requestHeaders = curl_slist_sort_insert(requestHeaders, "Authorization", Signature.c_str()); } + if(S3fsCurl::IsClientInfoInDelete()){ + ostringstream pidstr; + pidstr << pid; + string clientinfo = S3fsCurl::GetClientInfo(pidstr.str()); + requestHeaders = curl_slist_sort_insert(requestHeaders, "x-delete-client-pid", pidstr.str().c_str()); + requestHeaders = curl_slist_sort_insert(requestHeaders, "x-delete-client-cgroup", clientinfo.c_str()); + } curl_easy_setopt(hCurl, CURLOPT_URL, url.c_str()); curl_easy_setopt(hCurl, CURLOPT_CUSTOMREQUEST, "DELETE"); @@ -3384,6 +3392,29 @@ int S3fsCurl::MultipartRenameRequest(const char* from, const char* to, headers_t return 0; } +std::string S3fsCurl::GetClientInfo(std::string pid) +{ + if (pid == "-1") { + return "can not get pid"; + } + std::string path = "/proc/" + pid + "/cgroup"; + std::ifstream ifs(path.c_str()); + if (!ifs) { + S3FS_PRN_ERR("failed to open %s", path.c_str()); + return "failed to open " + path; + } + std::string line; + while (std::getline(ifs, line)) { + if (line.find("cpu,cpuacct") != std::string::npos) { + return line; + } + if (line.find("cpuacct,cpu") != std::string::npos) { + return line; + } + } + return "not found cpu,cpuacct"; +} + //------------------------------------------------------------------- // Class S3fsMultiCurl //------------------------------------------------------------------- diff --git a/src/curl.h b/src/curl.h index faa36ee..f350adf 100644 --- a/src/curl.h +++ b/src/curl.h @@ -238,6 +238,7 @@ class S3fsCurl static off_t multipart_size; static bool is_sigv4; static std::string skUserAgent; + static bool is_client_info_in_delete; // variables CURL* hCurl; @@ -370,6 +371,9 @@ class S3fsCurl static off_t GetMultipartSize(void) { return S3fsCurl::multipart_size; } static bool SetSignatureV4(bool isset) { bool bresult = S3fsCurl::is_sigv4; S3fsCurl::is_sigv4 = isset; return bresult; } static bool IsSignatureV4(void) { return S3fsCurl::is_sigv4; } + static void SetClientInfoInDelete(bool is_set) { S3fsCurl::is_client_info_in_delete = is_set; } + static bool IsClientInfoInDelete(void) { return S3fsCurl::is_client_info_in_delete; } + static std::string GetClientInfo(std::string pid); // methods bool CreateCurlHandle(bool force = false); @@ -378,7 +382,7 @@ class S3fsCurl bool AddSseRequestHead(sse_type_t ssetype, std::string& ssevalue, bool is_only_c, bool is_copy); bool GetResponseCode(long& responseCode); int RequestPerform(void); - int DeleteRequest(const char* tpath); + int DeleteRequest(const char* tpath, int pid); bool PreHeadRequest(const char* tpath, const char* bpath = NULL, const char* savedpath = NULL, int ssekey_pos = -1); bool PreHeadRequest(std::string& tpath, std::string& bpath, std::string& savedpath, int ssekey_pos = -1) { return PreHeadRequest(tpath.c_str(), bpath.c_str(), savedpath.c_str(), ssekey_pos); diff --git a/src/s3fs.cpp b/src/s3fs.cpp index d92cc83..0206ffb 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -1025,8 +1025,10 @@ static int s3fs_unlink(const char* path) S3FS_PRN_INFO("[path=%s]", path); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1034,7 +1036,7 @@ static int s3fs_unlink(const char* path) return result; } S3fsCurl s3fscurl; - result = s3fscurl.DeleteRequest(path); + result = s3fscurl.DeleteRequest(path, pid); FdManager::DeleteCacheFile(path); StatCache::getStatCacheData()->DelStat(path); S3FS_MALLOCTRIM(0); @@ -1065,8 +1067,10 @@ static int s3fs_rmdir(const char* path) S3FS_PRN_INFO("[path=%s]", path); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1084,7 +1088,7 @@ static int s3fs_rmdir(const char* path) strpath += "/"; } S3fsCurl s3fscurl; - result = s3fscurl.DeleteRequest(strpath.c_str()); + result = s3fscurl.DeleteRequest(strpath.c_str(), pid); s3fscurl.DestroyCurlHandle(); StatCache::getStatCacheData()->DelStat(strpath.c_str()); @@ -1099,7 +1103,7 @@ static int s3fs_rmdir(const char* path) if(0 == get_object_attribute(strpath.c_str(), &stbuf, NULL, false)){ if(S_ISDIR(stbuf.st_mode)){ // Found "dir" object. - result = s3fscurl.DeleteRequest(strpath.c_str()); + result = s3fscurl.DeleteRequest(strpath.c_str(), pid); s3fscurl.DestroyCurlHandle(); StatCache::getStatCacheData()->DelStat(strpath.c_str()); } @@ -1111,7 +1115,7 @@ static int s3fs_rmdir(const char* path) // This processing is necessary for other OSS clients compatibility. if(is_special_name_folder_object(strpath.c_str())){ strpath += "_$folder$"; - result = s3fscurl.DeleteRequest(strpath.c_str()); + result = s3fscurl.DeleteRequest(strpath.c_str(), pid); } S3FS_MALLOCTRIM(0); @@ -1566,8 +1570,10 @@ static int s3fs_chmod(const char* path, mode_t mode) S3FS_PRN_INFO("[path=%s][mode=%04o]", path, mode); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1600,7 +1606,7 @@ static int s3fs_chmod(const char* path, mode_t mode) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -1665,8 +1671,10 @@ static int s3fs_chmod_nocopy(const char* path, mode_t mode) S3FS_PRN_INFO1("[path=%s][mode=%04o]", path, mode); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1700,7 +1708,7 @@ static int s3fs_chmod_nocopy(const char* path, mode_t mode) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -1750,8 +1758,10 @@ static int s3fs_chown(const char* path, uid_t uid, gid_t gid) S3FS_PRN_INFO("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1799,7 +1809,7 @@ static int s3fs_chown(const char* path, uid_t uid, gid_t gid) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -1864,8 +1874,10 @@ static int s3fs_chown_nocopy(const char* path, uid_t uid, gid_t gid) int nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO1("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1908,7 +1920,7 @@ static int s3fs_chown_nocopy(const char* path, uid_t uid, gid_t gid) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -1958,8 +1970,10 @@ static int s3fs_utimens(const char* path, const struct timespec ts[2]) int nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO("[path=%s][mtime=%jd]", path, (intmax_t)(ts[1].tv_sec)); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -1994,7 +2008,7 @@ static int s3fs_utimens(const char* path, const struct timespec ts[2]) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -2056,8 +2070,10 @@ static int s3fs_utimens_nocopy(const char* path, const struct timespec ts[2]) int nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO1("[path=%s][mtime=%s]", path, str(ts[1].tv_sec).c_str()); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -2093,7 +2109,7 @@ static int s3fs_utimens_nocopy(const char* path, const struct timespec ts[2]) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -3223,6 +3239,13 @@ static int s3fs_setxattr(const char* path, const char* name, const char* value, } #endif + int pid = -1; + struct fuse_context* pcxt; + if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; + S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); + } + int result; string strpath; string newpath; @@ -3265,7 +3288,7 @@ static int s3fs_setxattr(const char* path, const char* name, const char* value, // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ return result; } } @@ -3497,8 +3520,10 @@ static int s3fs_listxattr(const char* path, char* list, size_t size) static int s3fs_removexattr(const char* path, const char* name) { S3FS_PRN_INFO("[path=%s][name=%s]", path, name); + int pid = -1; struct fuse_context* pcxt; if(NULL != (pcxt = fuse_get_context())){ + pid = pcxt->pid; S3FS_PRN_INFO("%s, uid=[%d], gid=[%d], pid=[%d]", __FUNCTION__, pcxt->uid, pcxt->gid, pcxt->pid); } @@ -3575,7 +3600,7 @@ static int s3fs_removexattr(const char* path, const char* name) // At first, remove directory old object if(IS_RMTYPEDIR(nDirType)){ S3fsCurl s3fscurl; - if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str()))){ + if(0 != (result = s3fscurl.DeleteRequest(strpath.c_str(), pid))){ free_xattrs(xattrs); return result; } @@ -4633,6 +4658,10 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar FdManager::SetCacheDir(strchr(arg, '=') + sizeof(char)); return 0; } + if(0 == STR2NCMP(arg, "enable_clientinfo")){ + S3fsCurl::SetClientInfoInDelete(true); + return 0; + } if(0 == strcmp(arg, "del_cache")){ is_remove_cache = true; return 0;