From: Junghyun Yeon Date: Tue, 15 Mar 2016 08:49:49 +0000 (+0900) Subject: change functions for thread-safe X-Git-Tag: accepted/tizen/common/20160321.145911^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F62298%2F4;p=platform%2Fcore%2Fappfw%2Fpkgmgr-info.git change functions for thread-safe Change-Id: I2c88ceaa0d80bcbfd0f7fb867f0516dc00f31e25 Signed-off-by: Junghyun Yeon --- diff --git a/parser/pkgmgr_parser.c b/parser/pkgmgr_parser.c index d4cf708..dd1788a 100644 --- a/parser/pkgmgr_parser.c +++ b/parser/pkgmgr_parser.c @@ -1486,16 +1486,17 @@ static void __ps_process_tag(manifest_x * mfx, char *const tagv[]) char delims[] = "="; char *ret_result = NULL; char *tag = NULL; + char *ptr = NULL; if (tagv == NULL) return; for (tag = strdup(tagv[0]); tag != NULL; ) { - ret_result = strtok(tag, delims); + ret_result = strtok_r(tag, delims, &ptr); /*check tag : preload */ if (strcmp(ret_result, "preload") == 0) { - ret_result = strtok(NULL, delims); + ret_result = strtok_r(NULL, delims, &ptr); if (strcmp(ret_result, "true") == 0) { free((void *)mfx->preload); mfx->preload = strdup("true"); @@ -1505,7 +1506,7 @@ static void __ps_process_tag(manifest_x * mfx, char *const tagv[]) } /*check tag : removable*/ } else if (strcmp(ret_result, "removable") == 0) { - ret_result = strtok(NULL, delims); + ret_result = strtok_r(NULL, delims, &ptr); if (strcmp(ret_result, "true") == 0){ free((void *)mfx->removable); mfx->removable = strdup("true"); diff --git a/parser/pkgmgr_parser_db.c b/parser/pkgmgr_parser_db.c index 1759e8d..91142ae 100644 --- a/parser/pkgmgr_parser_db.c +++ b/parser/pkgmgr_parser_db.c @@ -1711,6 +1711,7 @@ static int __insert_application_metadata_splashscreen_info(manifest_x *mfx) int ret; char query[MAX_QUERY_LEN]; char *token; + char *tmpptr = NULL; const char *operation; const char *portraitimg; const char *landscapeimg; @@ -1743,7 +1744,7 @@ static int __insert_application_metadata_splashscreen_info(manifest_x *mfx) portraitimg = NULL; landscapeimg = NULL; indicatordisplay = "true"; /* default */ - token = strtok(md->value, "|"); + token = strtok_r(md->value, "|", &tmpptr); while (token != NULL) { if (strcasestr(token, "portrait-effectimage=")) { portraitimg = index(token, '='); @@ -1765,7 +1766,7 @@ static int __insert_application_metadata_splashscreen_info(manifest_x *mfx) indicatordisplay = "true"; } - token = strtok(NULL, "|"); + token = strtok_r(NULL, "|", &tmpptr); } if (portraitimg) { @@ -2379,10 +2380,11 @@ API int pkgmgr_parser_initialize_db(uid_t uid) static int __parserdb_change_perm(const char *db_file, uid_t uid) { char buf[BUFSIZE]; + char pwuid_buf[1024] = {0, }; char journal_file[BUFSIZE]; char *files[3]; int ret, i; - struct passwd *userinfo = NULL; + struct passwd userinfo, *result = NULL; files[0] = (char *)db_file; files[1] = journal_file; files[2] = NULL; @@ -2396,20 +2398,20 @@ static int __parserdb_change_perm(const char *db_file, uid_t uid) snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); if (uid == OWNER_ROOT) uid = GLOBAL_USER; - userinfo = getpwuid(uid); - if (!userinfo) { + ret = getpwuid_r(uid, &userinfo, &pwuid_buf, sizeof(pwuid_buf), &result); + if (ret != 0 || result == NULL) { _LOGE("FAIL: user %d doesn't exist", uid); return -1; } snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); for (i = 0; files[i]; i++) { - ret = chown(files[i], uid, userinfo->pw_gid); + ret = chown(files[i], uid, userinfo.pw_gid); if (ret == -1) { if (strerror_r(errno, buf, sizeof(buf))) strncpy(buf, "", BUFSIZE - 1); _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid, - userinfo->pw_gid, buf); + userinfo.pw_gid, buf); return -1; } diff --git a/src/pkgmgrinfo_db.c b/src/pkgmgrinfo_db.c index 805d2ed..26d0d5a 100644 --- a/src/pkgmgrinfo_db.c +++ b/src/pkgmgrinfo_db.c @@ -85,6 +85,7 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) int ret; char *fullpath; char *subpath; + char buf[1024]; fullpath = strdup(dir); if (fullpath == NULL) @@ -111,7 +112,7 @@ static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid) ret = chown(dir, uid, gid); if (ret == -1) _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid, - gid, strerror(errno)); + gid, strerror_r(errno, buf, sizeof(buf))); } free(fullpath); diff --git a/src/pkgmgrinfo_pkginfo.c b/src/pkgmgrinfo_pkginfo.c index 12642e8..e49ee0d 100644 --- a/src/pkgmgrinfo_pkginfo.c +++ b/src/pkgmgrinfo_pkginfo.c @@ -141,50 +141,52 @@ long long _pkgmgr_calculate_dir_size(char *dirname) int q = 0; /*quotient*/ int r = 0; /*remainder*/ DIR *dp = NULL; - struct dirent *ep = NULL; + struct dirent ep, *result; struct stat fileinfo; char abs_filename[FILENAME_MAX] = { 0, }; retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL"); dp = opendir(dirname); - if (dp != NULL) { - while ((ep = readdir(dp)) != NULL) { - if (!strcmp(ep->d_name, ".") || - !strcmp(ep->d_name, "..")) { + if (dp == NULL) { + _LOGE("Couldn't open the directory\n"); + return -1; + } + + for (ret = readdir_r(dp, &ep, &result); + ret == 0 && result != NULL; + ret = readdir_r(dp, &ep, &result)) { + if (!strcmp(ep.d_name, ".") || + !strcmp(ep.d_name, "..")) { + continue; + } + snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, + ep.d_name); + if (lstat(abs_filename, &fileinfo) < 0) + perror(abs_filename); + else { + if (S_ISDIR(fileinfo.st_mode)) { + total += fileinfo.st_size; + if (strcmp(ep.d_name, ".") + && strcmp(ep.d_name, "..")) { + ret = _pkgmgr_calculate_dir_size + (abs_filename); + total = total + ret; + } + } else if (S_ISLNK(fileinfo.st_mode)) { continue; + } else { + /*It is a file. Calculate the actual + size occupied (in terms of 4096 blocks)*/ + q = (fileinfo.st_size / BLOCK_SIZE); + r = (fileinfo.st_size % BLOCK_SIZE); + if (r) { + q = q + 1; } - snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, - ep->d_name); - if (lstat(abs_filename, &fileinfo) < 0) - perror(abs_filename); - else { - if (S_ISDIR(fileinfo.st_mode)) { - total += fileinfo.st_size; - if (strcmp(ep->d_name, ".") - && strcmp(ep->d_name, "..")) { - ret = _pkgmgr_calculate_dir_size - (abs_filename); - total = total + ret; - } - } else if (S_ISLNK(fileinfo.st_mode)) { - continue; - } else { - /*It is a file. Calculate the actual - size occupied (in terms of 4096 blocks)*/ - q = (fileinfo.st_size / BLOCK_SIZE); - r = (fileinfo.st_size % BLOCK_SIZE); - if (r) { - q = q + 1; - } - total += q * BLOCK_SIZE; - } + total += q * BLOCK_SIZE; } } - (void)closedir(dp); - } else { - _LOGE("Couldn't open the directory\n"); - return -1; } + (void)closedir(dp); return total; }