Update the close fd with valid check 24/162924/6 accepted/tizen/unified/20171208.170404 submit/tizen/20171208.044750
authorYoungHun Kim <yh8004.kim@samsung.com>
Wed, 6 Dec 2017 07:47:31 +0000 (16:47 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Thu, 7 Dec 2017 10:08:58 +0000 (19:08 +0900)
Change-Id: I06621b113fd0a897e230066cd8b2d3fce2375e48

client/src/muse_client.c
core/include/muse_core.h
core/src/muse_core.c
packaging/mused.spec
server/include/muse_server.h
server/src/muse_server.c
server/src/muse_server_private.c

index 783360c..0ad6861 100644 (file)
@@ -141,7 +141,8 @@ static int _mc_new(muse_channel_e channel)
        g_return_val_if_fail(channel < MUSE_CHANNEL_MAX, MM_ERROR_INVALID_ARGUMENT);
 
        /*Create socket*/
-       if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+       sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (!muse_core_fd_is_valid(sock_fd)) {
                strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
                LOGE("[socket failure] %d sock: %s", errno, err_msg);
                return ret;
index bb31a0a..c2e7b3a 100644 (file)
@@ -97,6 +97,7 @@ int muse_core_connection_close(int sock_fd);
 int muse_core_set_nonblocking(int fd, bool value);
 int muse_core_set_socket_timeout(int sock_fd, int timeout_sec);
 bool muse_core_fd_is_valid(int fd);
+void muse_core_fd_close(int fd);
 
 /* message */
 int muse_core_msg_send(int sock_fd, const char *msg);
index 60fdcb1..9abb6d4 100644 (file)
@@ -258,6 +258,16 @@ bool muse_core_fd_is_valid(int fd)
        return (fcntl(fd, F_GETFL) != MUSE_ERR || errno != EBADF) && (fd > STDERR_FILENO);
 }
 
+void muse_core_fd_close(int fd)
+{
+       if (!muse_core_fd_is_valid(fd)) {
+               LOGW("%d is invalid fd", fd);
+               return;
+       }
+
+       close(fd);
+}
+
 /* message */
 int muse_core_msg_send(int sock_fd, const char *msg)
 {
index 86112f2..fc67298 100644 (file)
@@ -1,6 +1,6 @@
 Name:       mused
 Summary:    A multimedia daemon
-Version:    0.3.20
+Version:    0.3.21
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
index 3e115e4..6ce64f7 100644 (file)
@@ -63,6 +63,8 @@ int muse_server_get_platform_info(const char *key, bool *value);
 unsigned int muse_server_get_atomic_uint(void);
 int muse_server_get_module_instance_count(int idx);
 
+void muse_server_check_fd_state(int fd);
+
 #ifdef __cplusplus
 }
 #endif
index c3ea09b..1a4b450 100644 (file)
@@ -68,9 +68,9 @@ static pid_t _ms_daemonize(void)
        LOGD("result = %d sid: %d pgid: %d pid: %d ppid: %d", result, (int)getsid(0), (int)getpgid(0), (int)pid, (int)getppid());
 
        /* redirect fds to /dev/null */
-       if ((fd = open("/dev/null", O_RDWR)) == MUSE_ERR) {
-               strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
-               LOGE("Unable to open /dev/null: %s", err_msg);
+       fd = open("/dev/null", O_RDWR);
+       if (!muse_core_fd_is_valid(fd)) {
+               LOGE("Critial Error : %d is invalid", fd);
                exit(EXIT_SUCCESS);
        }
 
@@ -136,7 +136,7 @@ static int _ms_pidfile_create(const char *path, pid_t pid)
 
        fd = open(path, O_WRONLY | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
 
-       if (fd < 0) {
+       if (!muse_core_fd_is_valid(fd)) {
                strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
                LOGE("Fail to open pidfile [%s] : %s", path, err_msg);
                return MM_ERROR_FILE_NOT_FOUND;
@@ -405,6 +405,20 @@ int muse_server_get_module_instance_count(int idx)
        return connection->instance_count[idx];
 }
 
+void muse_server_check_fd_state(int fd)
+{
+       struct stat sb = { 0, };
+
+       if (fstat(fd, &sb) < 0) {
+               LOGE("failed to get file status");
+               return;
+       }
+
+       LOGI("[Last file access %d] %s", fd, ctime(&sb.st_atime));
+       LOGI("[Last status change] %s", ctime(&sb.st_ctime));
+       LOGI("[Last file modification] %s", ctime(&sb.st_mtime));
+}
+
 int main(int argc, char **argv)
 {
        pid_t pid;
index 2991517..830a8d5 100644 (file)
@@ -161,14 +161,14 @@ static int _ms_new(muse_channel_e channel)
 
        /* Create Socket */
        fd = socket(AF_UNIX, SOCK_STREAM, 0); /* Unix Domain Socket */
-       if (fd < 0) {
+       if (!muse_core_fd_is_valid(fd)) {
                strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
                LOGE("socket failed sock: %s", err_msg);
                return MUSE_ERR;
-       } else {
-               LOGD("muse server fd : %d", fd);
        }
 
+       LOGD("muse server fd : %d", fd);
+
        memset(&addr_un, 0, sizeof(addr_un));
        addr_un.sun_family = AF_UNIX;
        strncpy(addr_un.sun_path, UDS_files[channel], sizeof(addr_un.sun_path) - 1);
@@ -239,7 +239,6 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
        socklen_t client_len;
        struct sockaddr_un client_address;
        muse_channel_e channel = (muse_channel_e)data;
-       char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
        muse_module_h m = NULL;
        muse_module_h peeked_m = NULL;
        muse_module_h candidate_m = NULL;
@@ -260,12 +259,20 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
        }
 
        server_sockfd = g_io_channel_unix_get_fd(source);
+       if (!muse_core_fd_is_valid(server_sockfd)) {
+               LOGE("Critical Error : server %d is invalid", server_sockfd);
+               _ms_unlock_state();
+               muse_server_check_fd_state(STDIN_FILENO);
+               ms_respawn(SIGABRT);
+               return FALSE;
+       }
 
        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
-       if (client_sockfd == MUSE_ERR) {
-               strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
-               LOGE("[Critical Error : %d] accept failure : %s", errno, err_msg);
+       if (!muse_core_fd_is_valid(client_sockfd)) {
+               LOGE("Critical Error : accept %d is invalid", client_sockfd);
+               muse_server_check_fd_state(STDIN_FILENO);
+               close(server_sockfd);
                _ms_unlock_state();
                ms_respawn(SIGABRT);
                return FALSE;
@@ -483,9 +490,8 @@ int ms_deinit(void)
 #endif
 
        retval = muse_server->retval;
-       close(muse_server->msg_fd);
-       if (muse_server->data_fd > 0)
-               close(muse_server->data_fd);
+       muse_core_fd_close(muse_server->msg_fd);
+       muse_core_fd_close(muse_server->data_fd);
        for (idx = 0; idx < MUSE_CHANNEL_MAX; idx++) {
                if (remove(UDS_files[idx]) == MUSE_ERR)
                        LOGE("remove %s falied", UDS_files[idx]);
@@ -548,7 +554,7 @@ void ms_new(void)
 
        for (i = 0; i < MUSE_CHANNEL_MAX; i++) {
                fd[i] = _ms_new(i);
-               if (fd[i] < 0) {
+               if (!muse_core_fd_is_valid(fd[i])) {
                        LOGE("Failed to create socket server %d", i);
                        for (j = 0; j < i; j++)
                                close(fd[j]);
@@ -592,12 +598,13 @@ int ms_run(void)
        if (LwipcEventDone(MUSE_SERVER_READY) < 0)
                LOGE("Fail to send server ready done event");
 #else
-       if ((ready_fd = creat(MUSE_SERVER_READY, 0644)) != -1) {
+       ready_fd = creat(MUSE_SERVER_READY, 0644);
+       if (muse_core_fd_is_valid(ready_fd)) {
                LOGD("MUSE_SERVER_READY(%s) file was created", MUSE_SERVER_READY);
                close(ready_fd);
                muse_server->state = MUSE_SERVER_STATE_READY;
        } else {
-               LOGE("Fail to create MUSE_SERVER_READY(%s)", MUSE_SERVER_READY);
+               LOGE("[%d] Fail to create MUSE_SERVER_READY(%s)", ready_fd, MUSE_SERVER_READY);
        }
 #endif