Modify logging method
authorWonkyu Kwon <wonkyu.kwon@samsung.com>
Tue, 19 Mar 2013 02:47:12 +0000 (11:47 +0900)
committerJaekyun Lee <jkyun.lee@samsung.com>
Thu, 18 Apr 2013 07:31:36 +0000 (16:31 +0900)
 - change fopen parameter
 - flush file stream after fprintf (slow??)
 - add init/fini function of file log
 - write ipc log with client pid
 - write requested client pid in dbus call

Change-Id: Ia341f1289d46556380d93a7c457de14c1c6e7298

12 files changed:
src/clientlib/net_nfc_client_ipc.c
src/clientlib/net_nfc_client_nfc.c
src/commonlib/include/net_nfc_debug_private.h
src/commonlib/include/net_nfc_util_private.h
src/commonlib/net_nfc_util.c
src/commonlib/net_nfc_util_openssl.c
src/manager/include/net_nfc_manager_dbus.h
src/manager/include/net_nfc_server_context_private.h
src/manager/net_nfc_manager.c
src/manager/net_nfc_server_context.c
src/manager/net_nfc_server_ipc.c
src/manager/nfc-service.xml

index db7591d..2ba7f3b 100755 (executable)
@@ -39,6 +39,7 @@
 #include "net_nfc_client_dispatcher_private.h"
 
 /* static variable */
+static pid_t g_client_pid;
 static int g_client_sock_fd = -1;
 static pthread_mutex_t g_client_ipc_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t g_client_ipc_cond = PTHREAD_COND_INITIALIZER;
@@ -217,6 +218,8 @@ net_nfc_error_e net_nfc_client_socket_initialize()
                return NET_NFC_ALREADY_INITIALIZED;
        }
 
+       g_client_pid = getpid();
+
        pthread_mutexattr_t attr;
 
        pthread_mutexattr_init(&attr);
@@ -723,7 +726,7 @@ net_nfc_response_msg_t *net_nfc_client_read_response_msg(net_nfc_error_e *result
        }
 
 //     DEBUG_MSG("<<<<< FROM SERVER <<<<< (msg [%d], length [%d])", resp_msg->response_type, length);
-       DEBUG_MSG("<<<<< FROM SERVER <<<<< (msg [%d])", resp_msg->response_type);
+       DEBUG_MSG("[%d] <<<<<<<<<<<<<<< (msg [%d])", g_client_pid, resp_msg->response_type);
 
        switch (resp_msg->response_type)
        {
@@ -1648,7 +1651,7 @@ static net_nfc_error_e _send_request(net_nfc_request_msg_t *msg, va_list list)
 
        if (msg_result)
        {
-               DEBUG_MSG(">>>>> TO SERVER >>>>> (msg [%d], length [%d])", msg->request_type, total_size);
+               DEBUG_MSG("[%d] >>>>>>>>>>>>>>> (msg [%d], length [%d])", g_client_pid, msg->request_type, total_size);
                return NET_NFC_OK;
        }
        else
index 26ef548..8ead1e6 100755 (executable)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <unistd.h>
 #include <pthread.h>
 #include <glib.h>
 #include <dbus/dbus-glib.h>
@@ -68,7 +69,7 @@ static net_nfc_error_e _net_nfc_launch_daemon()
                {
                        guint dbus_result = 0;
 
-                       if (org_tizen_nfc_service_launch(proxy, &dbus_result, &error) == true)
+                       if (org_tizen_nfc_service_launch(proxy, getpid(), &dbus_result, &error) == true)
                        {
                                DEBUG_CLIENT_MSG("org_tizen_nfc_service_launch success");
                                result = NET_NFC_OK;
index a394aa0..f7a7d6a 100755 (executable)
@@ -94,6 +94,7 @@ const char *net_nfc_get_log_tag();
                        time_t rawtime;   time (&rawtime);   strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime)); \
                        fprintf(nfc_log_file, "\n%s",timeBuf); \
                        fprintf(nfc_log_file, "[D][%s:%d] "format"",__func__, __LINE__,  ##args); \
+                       fflush(nfc_log_file);\
                }\
        } while(0)
 
@@ -106,6 +107,7 @@ const char *net_nfc_get_log_tag();
                        time_t rawtime;   time (&rawtime);   strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime)); \
                        fprintf(nfc_log_file, "\n%s",timeBuf); \
                        fprintf(nfc_log_file, "[S][%s:%d] "format"",__func__, __LINE__,  ##args); \
+                       fflush(nfc_log_file);\
                } \
        } while(0)
 
@@ -118,6 +120,7 @@ const char *net_nfc_get_log_tag();
                        time_t rawtime;   time (&rawtime);   strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime)); \
                        fprintf(nfc_log_file, "\n%s",timeBuf); \
                        fprintf(nfc_log_file, "[C][%s:%d] "format"",__func__, __LINE__,  ##args); \
+                       fflush(nfc_log_file);\
                }\
        } while(0)
 
@@ -130,6 +133,7 @@ const char *net_nfc_get_log_tag();
                        time_t rawtime;   time (&rawtime);   strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime)); \
                        fprintf(nfc_log_file, "\n%s",timeBuf); \
                        fprintf(nfc_log_file, "[ERR][%s:%d] "format"",__func__, __LINE__,  ##args); \
+                       fflush(nfc_log_file);\
                } \
        } while(0)
 
index 08bb11c..a6ca0a0 100644 (file)
@@ -50,6 +50,9 @@ typedef enum
        CRC_B,
 } CRC_type_e;
 
+void net_nfc_manager_init_log();
+void net_nfc_manager_fini_log();
+
 /* Memory utils */
 /* allocation memory */
 void __net_nfc_util_alloc_mem(void **mem, int size, char *filename, unsigned int line);
index 56d80a1..deca7c1 100644 (file)
@@ -82,6 +82,7 @@ static uint8_t *bt_addr = NULL;
 #define NET_NFC_MANAGER_NAME "nfc-manager-daemon"
 static const char *log_tag = LOG_CLIENT_TAG;
 extern char *__progname;
+FILE *nfc_log_file = NULL;
 
 const char *net_nfc_get_log_tag()
 {
@@ -100,6 +101,43 @@ void __attribute__ ((destructor)) lib_fini()
 {
 }
 
+void net_nfc_manager_init_log()
+{
+       nfc_log_file = fopen(NFC_DLOG_FILE, "a+");
+       if (nfc_log_file != NULL)
+       {
+               char timeBuf[50];
+               time_t rawtime;
+
+               time (&rawtime);
+               strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime));
+               fprintf(nfc_log_file, "\n%s",timeBuf);
+               fprintf(nfc_log_file, "========== log begin, pid [%d] =========", getpid());
+               fflush(nfc_log_file);
+       }
+       else
+       {
+               fprintf(stderr, "\n\nfopen error\n\n");
+       }
+}
+
+void net_nfc_manager_fini_log()
+{
+       if (nfc_log_file != NULL)
+       {
+               char timeBuf[50];
+               time_t rawtime;
+
+               time (&rawtime);
+               strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", localtime(&rawtime));
+               fprintf(nfc_log_file, "\n%s",timeBuf);
+               fprintf(nfc_log_file, "=========== log end, pid [%d] ==========", getpid());
+               fflush(nfc_log_file);
+               fclose(nfc_log_file);
+               nfc_log_file = NULL;
+       }
+}
+
 NET_NFC_EXPORT_API void __net_nfc_util_free_mem(void **mem, char *filename, unsigned int line)
 {
        if (mem == NULL)
index b80ecf4..c92bdab 100755 (executable)
 #include "net_nfc_util_private.h"
 #include "net_nfc_util_openssl_private.h"
 
- /* nfc_log_to_file */
-#include <stdio.h>
-
-FILE* nfc_log_file;
-
 //static X509 *_load_certificate_from_file(const char *file)
 //{
 //     X509 *x509 = NULL;
index c3bae22..0b223ec 100644 (file)
@@ -43,12 +43,12 @@ struct _Nfc_ServiceClass
        GObjectClass parent;
 };
 
-#define NFC_SERVICE_TYPE                               (nfc_service_get_type ())
-#define NFC_SERVICE(object)                    (G_TYPE_CHECK_INSTANCE_CAST ((object), NFC_SERVICE_TYPE, Nfc_Service))
-#define NFC_SERVICE_CLASS(klass)               (G_TYPE_CHECK_CLASS_CAST ((klass), NFC_SERVICE_TYPE, Nfc_Service_Class))
-#define IS_NFC_SERVICE(object)                 (G_TYPE_CHECK_INSTANCE_TYPE ((object), NFC_SERVICE_TYPE))
+#define NFC_SERVICE_TYPE               (nfc_service_get_type ())
+#define NFC_SERVICE(object)            (G_TYPE_CHECK_INSTANCE_CAST ((object), NFC_SERVICE_TYPE, Nfc_Service))
+#define NFC_SERVICE_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST ((klass), NFC_SERVICE_TYPE, Nfc_Service_Class))
+#define IS_NFC_SERVICE(object)         (G_TYPE_CHECK_INSTANCE_TYPE ((object), NFC_SERVICE_TYPE))
 #define IS_NFC_SERVICE_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE ((klass), NFC_SERVICE_TYPE))
-#define NFC_SERVICE_GET_CLASS(obj)             (G_TYPE_INSTANCE_GET_CLASS ((obj), NFC_SERVICE_TYPE, Nfc_Service_Class))
+#define NFC_SERVICE_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj), NFC_SERVICE_TYPE, Nfc_Service_Class))
 
 typedef enum
 {
@@ -58,14 +58,10 @@ typedef enum
 GQuark nfc_service_error_quark(void);
 #define NFC_SERVICE_ERROR nfc_service_error_quark ()
 
-
-
 /**
  *     launch the nfc-manager
  */
-       gboolean nfc_service_launch (Nfc_Service *nfc_service, guint *result_val, GError **error);
-
-       gboolean nfc_service_terminate (Nfc_Service *nfc_service, guint *result_val, GError **error);
-
+gboolean nfc_service_launch (Nfc_Service *nfc_service, const pid_t pid, guint *result_val, GError **error);
+gboolean nfc_service_terminate (Nfc_Service *nfc_service, guint *result_val, GError **error);
 
 #endif /* MANAGERDBUS_H_ */
index 6f8fe16..10e2ff3 100644 (file)
 #ifndef NET_NFC_SERVER_CONTEXT_H
 #define NET_NFC_SERVER_CONTEXT_H
 
+#include <unistd.h>
+
 #include "net_nfc_typedef_private.h"
 
 /* define */
 typedef struct _net_nfc_client_info_t
 {
+       pid_t pid;
        int socket;
        GIOChannel *channel;
        uint32_t src_id;
@@ -34,7 +37,7 @@ typedef struct _net_nfc_client_info_t
 typedef void (*net_nfc_server_for_each_client_cb)(net_nfc_client_info_t *client, void *user_param);
 
 void net_nfc_server_deinit_client_context();
-void net_nfc_server_add_client_context(int socket, GIOChannel* channel, uint32_t src_id, client_state_e state);
+void net_nfc_server_add_client_context(pid_t pid, int socket, GIOChannel *channel, uint32_t src_id, client_state_e state);
 void net_nfc_server_cleanup_client_context(int socket);
 net_nfc_client_info_t *net_nfc_server_get_client_context(int socket);
 int net_nfc_server_get_client_count();
index 5315fee..aa71638 100755 (executable)
@@ -45,7 +45,6 @@ static DBusGConnection *connection = NULL;
 static pid_t launch_by_client = 0;
 
 static void __net_nfc_discovery_polling_cb(keynode_t *node, void *user_data);
-static bool Check_Redwood();
 
 G_DEFINE_TYPE(Nfc_Service, nfc_service, G_TYPE_OBJECT)
 
@@ -79,12 +78,13 @@ static void nfc_service_class_init(Nfc_ServiceClass *nfc_service_class)
        dbus_g_object_type_install_info(NFC_SERVICE_TYPE, &dbus_glib_nfc_service_object_info);
 }
 
-gboolean nfc_service_launch(Nfc_Service *nfc_service, guint *result_val, GError **error)
+gboolean nfc_service_launch(Nfc_Service *nfc_service, const pid_t pid, guint *result_val, GError **error)
 {
        DEBUG_MSG("nfc_service_launch entered");
 
        DEBUG_SERVER_MSG("nfc_service_launch NFC MANAGER PID=[%d]", getpid());
        DEBUG_SERVER_MSG("nfc_service_launch NFC MANAGER TID=[%lx]", pthread_self());
+       DEBUG_SERVER_MSG("requested client pid [%d]", pid);
 
        launch_by_client = getpid();
 
@@ -163,13 +163,14 @@ static void _net_nfc_intialize_dbus_connection()
        }
 }
 
-#if 0
 static void _net_nfc_deintialize_dbus_connection()
 {
-       dbus_g_connection_unregister_g_object(connection, object);
-       g_object_unref(object);
+       if (connection != NULL && object != NULL)
+       {
+               dbus_g_connection_unregister_g_object(connection, object);
+               g_object_unref(object);
+       }
 }
-#endif
 
 int main(int check, char* argv[])
 {
@@ -177,19 +178,6 @@ int main(int check, char* argv[])
        void *handle = NULL;
        int state = 0;
 
-       DEBUG_SERVER_MSG("start nfc manager");
-       DEBUG_SERVER_MSG("argv0 = %s", argv[0]);
-       DEBUG_SERVER_MSG("argv1 = %s", argv[1]);
-
-       /* nfc_log_to_file */
-       nfc_log_file = fopen(NFC_DLOG_FILE, "w+");
-       if (nfc_log_file == NULL)
-       {
-               fprintf(stderr, "\n\nfopen error\n\n");
-       }
-
-       net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
-
        if (!g_thread_supported())
        {
                g_thread_init(NULL);
@@ -197,6 +185,14 @@ int main(int check, char* argv[])
 
        g_type_init();
 
+       net_nfc_manager_init_log();
+
+       DEBUG_SERVER_MSG("start nfc manager");
+       DEBUG_SERVER_MSG("argv0 = %s", argv[0]);
+       DEBUG_SERVER_MSG("argv1 = %s", argv[1]);
+
+       net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
+
        handle = net_nfc_controller_onload();
        if (handle == NULL)
        {
@@ -217,15 +213,14 @@ int main(int check, char* argv[])
                vconf_set_bool(VCONFKEY_NFC_STATE, FALSE);
 
                net_nfc_controller_unload(handle);
-
-               //return(0);
        }
 
        result = vconf_get_bool(VCONFKEY_NFC_STATE, &state);
        if (result != 0)
        {
                DEBUG_MSG("VCONFKEY_NFC_STATE is not exist: %d ", result);
-               return false;
+
+               goto EXIT;
        }
 
        DEBUG_MSG("vconf state value [%d]", state);
@@ -235,7 +230,8 @@ int main(int check, char* argv[])
                if (state == FALSE && !(strncmp(argv[1], "script", 6)))
                {
                        DEBUG_ERR_MSG("Init Script execute nfc manager. But State is false.");
-                       return (0);
+
+                       goto EXIT;
                }
        }
 
@@ -243,9 +239,7 @@ int main(int check, char* argv[])
        {
                DEBUG_ERR_MSG("nfc server ipc initialization is failed");
 
-               net_nfc_controller_unload(handle);
-
-               return 0;
+               goto EXIT;
        }
 
        DEBUG_SERVER_MSG("nfc server ipc init is ok");
@@ -280,10 +274,14 @@ int main(int check, char* argv[])
        loop = g_main_new(TRUE);
        g_main_loop_run(loop);
 
+EXIT :
+       _net_nfc_deintialize_dbus_connection();
        net_nfc_service_vconf_unregister_notify_listener();
        net_nfc_server_ipc_finalize();
        net_nfc_controller_unload(handle);
 
+       net_nfc_manager_fini_log();
+
        return 0;
 }
 
@@ -353,26 +351,3 @@ static void __net_nfc_discovery_polling_cb(keynode_t *node, void *user_data)
        DEBUG_MSG("__net_nfc_discovery_polling_cb[Out]");
 }
 
-static bool Check_Redwood()
-{
-       struct utsname hwinfo;
-
-       int ret = uname(&hwinfo);
-       DEBUG_MSG("uname returned %d", ret);
-       DEBUG_MSG("sysname::%s", hwinfo.sysname);
-       DEBUG_MSG("release::%s", hwinfo.release);
-       DEBUG_MSG("version::%s", hwinfo.version);
-       DEBUG_MSG("machine::%s", hwinfo.machine);
-       DEBUG_MSG("nodename::%s", hwinfo.nodename);
-
-       if (strstr(hwinfo.nodename, "REDWOOD"))
-       {
-               DEBUG_MSG("REDWOOD hardware");
-               return true;
-       }
-       else
-       {
-               DEBUG_MSG("except REDWOOD");
-               return false;
-       }
-}
index 98059a5..bba8c4e 100644 (file)
@@ -112,7 +112,7 @@ net_nfc_client_info_t *net_nfc_server_get_client_context(int socket)
        return result;
 }
 
-void net_nfc_server_add_client_context(int socket, GIOChannel* channel, uint32_t src_id, client_state_e state)
+void net_nfc_server_add_client_context(pid_t pid, int socket, GIOChannel *channel, uint32_t src_id, client_state_e state)
 {
        DEBUG_SERVER_MSG("add client context");
 
@@ -125,6 +125,7 @@ void net_nfc_server_add_client_context(int socket, GIOChannel* channel, uint32_t
                _net_nfc_util_alloc_mem(info, sizeof(net_nfc_client_info_t));
                if (info != NULL)
                {
+                       info->pid = pid;
                        info->socket = socket;
                        info->channel = channel;
                        info->src_id = src_id;
index a25f8fa..b48ce35 100755 (executable)
@@ -358,9 +358,13 @@ bool net_nfc_server_process_client_connect_request()
 {
        socklen_t addrlen = 0;
        int client_sock_fd = 0;
-       GIOChannelclient_channel = NULL;
+       GIOChannel *client_channel = NULL;
        uint32_t client_src_id;
-
+       pid_t client_pid = -1;
+#ifdef USE_UNIX_DOMAIN
+       struct ucred uc;
+       socklen_t uc_len = sizeof(uc);
+#endif
        DEBUG_SERVER_MSG("client is trying to connect to server");
 
        if (net_nfc_server_get_client_count() >= NET_NFC_CLIENT_MAX)
@@ -375,13 +379,21 @@ bool net_nfc_server_process_client_connect_request()
                return false;
        }
 
-       DEBUG_SERVER_MSG("client is accepted by server, socket[%d]", client_sock_fd);
+#ifdef USE_UNIX_DOMAIN
+       if (!getsockopt(client_sock_fd, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len))
+       {
+               client_pid = uc.pid;
+       }
+#endif
+       DEBUG_SERVER_MSG("client [%d] is accepted by server, socket[%d]",
+               client_pid, client_sock_fd);
 
        GIOCondition condition = (GIOCondition)(G_IO_ERR | G_IO_HUP | G_IO_IN);
 
        if ((client_channel = g_io_channel_unix_new(client_sock_fd)) != NULL)
        {
-               if ((client_src_id = g_io_add_watch(client_channel, condition, net_nfc_server_ipc_callback_func, NULL)) < 1)
+               if ((client_src_id = g_io_add_watch(client_channel, condition,
+                       net_nfc_server_ipc_callback_func, NULL)) < 1)
                {
                        DEBUG_ERR_MSG("add io callback is failed");
                        goto ERROR;
@@ -395,7 +407,8 @@ bool net_nfc_server_process_client_connect_request()
 
        DEBUG_SERVER_MSG("client socket is bond with g_io_channel");
 
-       net_nfc_server_add_client_context(client_sock_fd, client_channel, client_src_id, NET_NFC_CLIENT_ACTIVE_STATE);
+       net_nfc_server_add_client_context(client_pid, client_sock_fd,
+               client_channel, client_src_id, NET_NFC_CLIENT_ACTIVE_STATE);
 
        return true;
 
@@ -559,7 +572,15 @@ bool net_nfc_server_read_client_request(int client_sock_fd, net_nfc_error_e *res
 
        _net_nfc_util_free_mem(buffer);
 
-       DEBUG_MSG("<<<<< FROM CLIENT [%d] <<<<< (msg [%d], length [%d])", client_sock_fd, req_msg->request_type, length);
+       pid_t client_pid = -1;
+
+       net_nfc_client_info_t *info;
+       info = net_nfc_server_get_client_context(client_sock_fd);
+       if (info != NULL)
+       {
+               client_pid = info->pid;
+       }
+       DEBUG_MSG(">>>>>>>>>>>>>>> [%d] >> SERVER (msg [%d], length [%d])", client_pid, req_msg->request_type, length);
 
 #ifdef BROADCAST_MESSAGE
        /* set client socket descriptor */
@@ -684,7 +705,7 @@ static void _net_nfc_for_each_cb(net_nfc_client_info_t *client, void *user_param
                if (net_nfc_server_send_message_to_client(client->socket, send_buffer, length) == true)
                {
 //                     DEBUG_MSG(">>>>> TO CLIENT [%d] >>>>> (msg [%d], length [%d])", client->socket, msg_type, length);
-                       DEBUG_MSG(">>>>> TO CLIENT [%d] >>>>> (length [%d])", client->socket, length);
+                       DEBUG_MSG("<<<<<<<<<<<<<<< [%d] << SERVER (length [%d])", client->pid, length);
                }
        }
 }
@@ -731,6 +752,7 @@ bool net_nfc_send_response_msg(int msg_type, ...)
        int total_size = 0;
        int written_size = 0;
        uint8_t *send_buffer = NULL;
+       net_nfc_client_info_t *info;
 
        va_start(list, msg_type);
 
@@ -750,15 +772,15 @@ bool net_nfc_send_response_msg(int msg_type, ...)
 
        va_end(list);
 
-       if (net_nfc_server_get_client_context(socket) != NULL)
+       if ((info = net_nfc_server_get_client_context(socket)) != NULL)
        {
 #ifdef BROADCAST_MESSAGE
                if (net_nfc_server_send_message_to_client(socket, (void *)send_buffer, total_size) == true)
 #else
-                       if (net_nfc_server_send_message_to_client((void *)send_buffer, total_size) == true)
+               if (net_nfc_server_send_message_to_client((void *)send_buffer, total_size) == true)
 #endif
                {
-                       DEBUG_MSG(">>>>> TO CLIENT [%d] >>>>> (msg [%d], length [%d])", socket, msg_type, total_size - sizeof(total_size));
+                       DEBUG_MSG("<<<<<<<<<<<<<<< [%d] << SERVER (msg [%d], length [%d])", info->pid, msg_type, total_size - sizeof(total_size));
                }
        }
        else
index 902f5c2..8df96e5 100644 (file)
@@ -4,6 +4,7 @@
   <interface name="org.tizen.nfc_service">
     <method name="Launch">
       <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="nfc_service_launch"/>
+       <arg type="u" name="pid" direction="in"/>
        <arg type="u" name="result_val" direction="out"/>
     </method>