Fix usage of strerror_r() 48/205548/1
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 7 May 2019 07:30:40 +0000 (16:30 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 7 May 2019 09:25:12 +0000 (18:25 +0900)
Use the GNU style. The GNU-specific strerror_r() returns a pointer to
a string containing the error message.
The buffer passed by is not used it doesn't need it.

Change-Id: Ifc61a09626602e41986b7945bb440b899d549726
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
plugin/app2sd/mount_helper/mount_helper.c
plugin/app2sd/server/app2sd_interface.c
plugin/app2sd/server/app2sd_internals.c
plugin/app2sd/server/app2sd_internals_registry.c
plugin/app2sd/server/app2sd_internals_utils.c

index 4b83525..b7e434c 100644 (file)
@@ -35,5 +35,8 @@ int main(int argc, char *argv[])
        filesystemtype = argv[3];
        mountflags = strtoul(argv[4], NULL, 0);
 
-       return mount(source, target, filesystemtype, mountflags, NULL);
+       if (mount(source, target, filesystemtype, mountflags, NULL))
+               return errno;
+
+       return 0;
 }
index 84696f4..f926c57 100644 (file)
@@ -21,6 +21,7 @@
  *
  */
 
+#define _GNU_SOURCE
 #include <pkgmgr-info.h>
 #include <package-manager.h>
 #include <aul.h>
@@ -948,6 +949,7 @@ int app2sd_enable_full_pkg(void)
        DIR *dir;
        struct dirent *entry;
        uid_t uid;
+       const char *err_str;
 
        /* check whether MMC is present or not */
        ret = _app2sd_check_mmc_status(&sdpath);
@@ -960,8 +962,8 @@ int app2sd_enable_full_pkg(void)
 
        dir = opendir(app2sd_path);
        if (!dir) {
-               strerror_r(errno, buf, sizeof(buf));
-               _E("failed to opendir (%s)", buf);
+               err_str = strerror_r(errno, buf, sizeof(buf));
+               _E("failed to opendir (%s)", err_str);
                return APP2EXT_ERROR_OPEN_DIR;
        }
 
@@ -1221,6 +1223,7 @@ int app2sd_migrate_legacy_all(void)
        struct dirent *entry;
        uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
        pkgmgr_client *pc;
+       const char *err_str;
 
        /* check whether MMC is present or not */
        ret = _app2sd_check_mmc_status(&sdpath);
@@ -1233,8 +1236,8 @@ int app2sd_migrate_legacy_all(void)
 
        dir = opendir(app2sd_path);
        if (!dir) {
-               strerror_r(errno, buf, sizeof(buf));
-               _E("failed to opendir (%s)", buf);
+               err_str = strerror_r(errno, buf, sizeof(buf));
+               _E("failed to opendir (%s)", err_str);
                return APP2EXT_ERROR_OPEN_DIR;
        }
 
index 81a2015..4e2fc37 100644 (file)
@@ -94,6 +94,7 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
        char *passwd;
        char dmcrypt_setup_cmd[BUF_SIZE];
        char err_buf[BUF_SIZE];
+       const char *err_str;
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -136,9 +137,9 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
        free(passwd);
        ret = system(dmcrypt_setup_cmd);
        if (ret) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                _E("Error setting up dmcrypt on app2sd file, error:%s, ret:%d",
-                               err_buf, ret);
+                               err_str, ret);
                return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
 
@@ -153,6 +154,7 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
        char dmcrypt_open_cmd[BUF_SIZE];
        char dev_name[BUF_SIZE];
        char buf[BUF_SIZE];
+       const char *err_str;
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -193,8 +195,8 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
 
        ret = system(dmcrypt_open_cmd);
        if (ret) {
-               strerror_r(errno, buf, sizeof(buf));
-               _E("error opening dmcrypt device, error: [%s]", buf);
+               err_str = strerror_r(errno, buf, sizeof(buf));
+               _E("error opening dmcrypt device, error: [%s]", err_str);
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
        }
 
@@ -214,6 +216,7 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
        char dev_node[BUF_SIZE];
        char dmcrypt_close_cmd[BUF_SIZE];
        char err_buf[BUF_SIZE];
+       const char *err_str;
        char *t_dev_node;
 
        if (pkgid == NULL) {
@@ -234,8 +237,8 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
                        "/sbin/cryptsetup -q luksClose %s", dev_node);
        ret = system(dmcrypt_close_cmd);
        if (ret) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
-               _E("error closing dmcrypt on app2sd file: %s", err_buf);
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+               _E("error closing dmcrypt on app2sd file: %s", err_str);
                return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
        }
 
@@ -345,6 +348,7 @@ int _app2sd_create_file_system(const char *device_path)
        int ret;
        FILE *fp;
        char err_buf[1024];
+       const char *err_str;
        const char *argv[] = { "/sbin/mkfs.ext4", device_path, NULL };
 
        if (device_path == NULL) {
@@ -355,7 +359,7 @@ int _app2sd_create_file_system(const char *device_path)
        /* Format the filesystem [create a filesystem]*/
        fp = fopen(device_path, "r+");
        if (fp == NULL) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                _E("unable to access (%s) error is (%d, %s)",
                                device_path, errno, err_buf);
                return APP2EXT_ERROR_ACCESS_FILE;
@@ -364,8 +368,8 @@ int _app2sd_create_file_system(const char *device_path)
 
        ret = _xsystem(argv);
        if (ret) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
-               _E("creating file system failed, error is (%s)", err_buf);
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+               _E("creating file system failed, error is (%s)", err_str);
                return APP2EXT_ERROR_CREATE_FS;
        }
        return ret;
@@ -431,6 +435,7 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        char app_mmc_path[FILENAME_MAX];
        char temp_path[FILENAME_MAX];
        char err_buf[1024];
+       const char *err_str;
        struct timespec time = {
                .tv_sec = 0,
                .tv_nsec = 1000 * 1000 * 200
@@ -469,10 +474,10 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                snprintf(mountflags_str, sizeof(mountflags_str), "%u",
                                MS_MGC_VAL | MS_RDONLY | MS_NOSUID);
                ret = _xsystem(argv_mount);
-               if (ret < 0) {
+               if (ret) {
                        _E("read only mount failed, errono is (%d), "
                                        "dev is (%s) path is (%s)",
-                                       errno, dev, app_mmc_path);
+                                       ret, dev, app_mmc_path);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
@@ -480,8 +485,8 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                snprintf(mountflags_str, sizeof(mountflags_str), "%u",
                                MS_MGC_VAL | MS_NOSUID);
                ret = _xsystem(argv_mount);
-               if (ret < 0) {
-                       _E("read write mount failed, errono is (%d)", errno);
+               if (ret) {
+                       _E("read write mount failed, errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
@@ -489,8 +494,8 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                snprintf(mountflags_str, sizeof(mountflags_str), "%u",
                                MS_MGC_VAL | MS_NOEXEC | MS_NOSUID);
                ret = _xsystem(argv_mount);
-               if (ret < 0) {
-                       _E("rwnx mount failed errono is (%d)", errno);
+               if (ret) {
+                       _E("rwnx mount failed errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
@@ -499,8 +504,8 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                                MS_MGC_VAL | MS_RDONLY | MS_REMOUNT |
                                MS_NOSUID);
                ret = _xsystem(argv_mount);
-               if (ret < 0) {
-                       _E("read remount failed errono is (%d)", errno);
+               if (ret) {
+                       _E("read remount failed errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
@@ -508,10 +513,10 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                snprintf(mountflags_str, sizeof(mountflags_str), "%u",
                                MS_MGC_VAL | MS_REMOUNT | MS_NOSUID);
                ret = _xsystem(argv_mount);
-               strerror_r(errno, err_buf, sizeof(err_buf));
-               if (ret < 0) {
-                       _E("read write remount failed errono(%d), errstr(%s)",
-                                       errno, err_buf);
+               err_str = strerror_r(ret, err_buf, sizeof(err_buf));
+               if (ret) {
+                       _E("read write remount failed errno(%d), errstr(%s)",
+                                       ret, err_str);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
@@ -546,14 +551,15 @@ int _app2sd_unmount_app_content(const char *application_path)
        int ret;
        char app_mmc_path[FILENAME_MAX];
        char err_buf[1024];
+       const char *err_str;
 
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                        application_path);
        ret = umount(app_mmc_path);
        if (ret < 0) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                _D("unable to umount the dir, ret(%d) error(%d, %s)",
-                               ret, errno, err_buf);
+                               ret, errno, err_str);
        }
 
        return ret;
@@ -563,12 +569,13 @@ static int _app2sd_move_to_archive(const char *src_path, const char *arch_path)
 {
        int ret;
        char err_buf[1024];
+       const char *err_str;
 
        ret = _app2sd_copy_dir(src_path, arch_path);
        if (ret && ret != APP2EXT_ERROR_ACCESS_FILE) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                _E("unable to copy from (%s) to (%s), err is (%s)",
-                               src_path, arch_path, err_buf);
+                               src_path, arch_path, err_str);
                return APP2EXT_ERROR_MOVE;
        }
 
@@ -600,6 +607,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        GList *list;
        app2ext_dir_details *dir_detail;
        char err_buf[1024];
+       const char *err_str;
 
        ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
                        loopback_device, sizeof(loopback_device));
@@ -737,10 +745,11 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                        if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                _E("unable to access (%s)", temp_dir_path);
                        } else {
-                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               err_str = strerror_r(errno, err_buf,
+                                               sizeof(err_buf));
                                _E("unable to copy from (%s) to (%s),"
                                                " error is (%s)", temp_dir_path,
-                                               app_mmc_path, err_buf);
+                                               app_mmc_path, err_str);
                        }
                        goto ERR;
                }
@@ -787,6 +796,7 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        int free_internal_mem;
        unsigned long long temp = 0;
        char err_buf[1024];
+       const char *err_str;
        int mount_type;
 
        ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
@@ -896,10 +906,11 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                        if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                _E("unable to access (%s)", temp_dir_path);
                        } else {
-                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               err_str = strerror_r(errno, err_buf,
+                                               sizeof(err_buf));
                                _E("unable to copy from (%s) to (%s),"
                                                "error is (%s)", temp_dir_path,
-                                               app_archive_path, err_buf);
+                                               app_archive_path, err_str);
                        }
                        goto ERR;
                }
@@ -927,10 +938,11 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                        if (ret == APP2EXT_ERROR_ACCESS_FILE) {
                                _E("unable to access (%s)", temp_dir_path);
                        } else {
-                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               err_str = strerror_r(errno, err_buf,
+                                               sizeof(err_buf));
                                _E("unable to copy from (%s) to (%s), "
                                                "error is (%s)", temp_dir_path,
-                                               application_path, err_buf);
+                                               application_path, err_str);
                        }
                        goto ERR;
                }
index 9cade23..bd286dc 100644 (file)
@@ -21,6 +21,7 @@
  *
  */
 
+#define _GNU_SOURCE
 #include <dirent.h>
 #include <time.h>
 #include <db-util.h>
@@ -58,6 +59,7 @@ static int _app2sd_db_change_perm(const char *db_file)
        files[0] = (char *)db_file;
        files[1] = journal_file;
        files[2] = NULL;
+       const char *err_str;
 
        if (db_file == NULL)
                return -1;
@@ -79,27 +81,24 @@ static int _app2sd_db_change_perm(const char *db_file)
        for (i = 0; files[i]; i++) {
                fd = open(files[i], O_RDONLY);
                if (fd == -1) {
-                       if (strerror_r(errno, buf, sizeof(buf)))
-                               strncpy(buf, "", BUFSIZE - 1);
-                       _E("failed to open %s : %s", files[i], buf);
+                       err_str = strerror_r(errno, buf, sizeof(buf));
+                       _E("failed to open %s : %s", files[i], err_str);
                        return -1;
                }
 
                ret = fchown(fd, uid, userinfo.pw_gid);
                if (ret == -1) {
-                       if (strerror_r(errno, buf, sizeof(buf)))
-                               strncpy(buf, "", BUFSIZE - 1);
+                       err_str = strerror_r(errno, buf, sizeof(buf));
                        _E("failed to fchown %s %d.%d : %s", files[i], uid,
-                                       userinfo.pw_gid, buf);
+                                       userinfo.pw_gid, err_str);
                        close(fd);
                        return -1;
                }
 
                ret = fchmod(fd, 0644);
                if (ret == -1) {
-                       if (strerror_r(errno, buf, sizeof(buf)))
-                               strncpy(buf, "", BUFSIZE - 1);
-                       _E("failed to fchmod %s : %s", files[i], buf);
+                       err_str = strerror_r(errno, buf, sizeof(buf));
+                       _E("failed to fchmod %s : %s", files[i], err_str);
                        close(fd);
                        return -1;
                }
index 055e7bd..678ace0 100644 (file)
@@ -21,6 +21,7 @@
  *
  */
 
+#define _GNU_SOURCE
 #include <dirent.h>
 #include <glib.h>
 #include <storage-internal.h>
@@ -42,6 +43,7 @@ int _xsystem(const char *argv[])
        int status;
        pid_t pid;
        char err_buf[1024];
+       const char *err_str;
 
        pid = fork();
        switch (pid) {
@@ -50,10 +52,10 @@ int _xsystem(const char *argv[])
                return -1;
        case 0:
                /* child */
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                if (execvp(argv[0], (char *const *)argv) < 0)
                        fprintf(stderr, "execvp failed %d....%s\n",
-                                       errno, err_buf);
+                                       errno, err_str);
                _exit(-1);
        default:
                /* parent */
@@ -288,6 +290,7 @@ char *_app2sd_execute_command(const char *argv[])
        char *ret_result;
        int result;
        char err_buf[1024];
+       const char *err_str;
 
        if (pipe(my_pipe) < 0) {
                fprintf(stderr, "Unable to create pipe\n");
@@ -303,20 +306,20 @@ char *_app2sd_execute_command(const char *argv[])
                close(0);
                result = dup2(my_pipe[1], 1);
                if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d...%s\n", errno, err_buf);
+                       err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+                       fprintf(stderr, "dup failed %d...%s\n", errno, err_str);
                        _exit(-1);
                }
                result = dup2(my_pipe[1], 2);
                if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d...%s\n", errno, err_buf);
+                       err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+                       fprintf(stderr, "dup failed %d...%s\n", errno, err_str);
                        _exit(-1);
                }
                if (execvp(argv[0], (char *const *)argv) < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
+                       err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                        fprintf(stderr, "execvp failed %d....%s\n",
-                                       errno, err_buf);
+                                       errno, err_str);
                }
                _exit(-1);
        default:
@@ -324,8 +327,8 @@ char *_app2sd_execute_command(const char *argv[])
                close(my_pipe[1]);
                result = read(my_pipe[0], buf, sizeof(buf) - 1);
                if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "read failed %d..%s\n", errno, err_buf);
+                       err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+                       fprintf(stderr, "read failed %d..%s\n", errno, err_str);
                }
                break;
        }