From 582ef4da0d595e4403501a679d6d6851da3d5d65 Mon Sep 17 00:00:00 2001 From: Ji Yong Min Date: Thu, 2 Jul 2015 10:51:29 +0900 Subject: [PATCH] Modify media-controller - On demand controller service - Modify socket address - Optimize code & fix some bugs(Known issue) - Remove security-server dependency Change-Id: I58e46775ab9484c9c6ff8ef757da510338d8dd12 Signed-off-by: Jiyong Min --- CMakeLists.txt | 2 +- include/media_controller_db.h | 5 +- include/media_controller_private.h | 18 ++- media-controller-service.manifest | 13 -- packaging/capi-media-controller.spec | 42 +++-- packaging/media-controller_create_db.sh | 3 - packaging/mediacontroller.service | 9 +- packaging/mediacontroller.socket | 13 ++ src/media_controller_client.c | 66 ++++++-- src/media_controller_db.c | 140 ++-------------- src/media_controller_ipc.c | 149 +++++++++++++++-- src/media_controller_server.c | 68 ++++++-- src/media_controller_util.c | 183 ++------------------- svc/CMakeLists.txt | 2 +- svc/daemon/CMakeLists.txt | 2 +- svc/daemon/media_controller_main.c | 57 ++++++- svc/media_controller_db_util.c | 10 +- svc/media_controller_db_util.h | 4 +- svc/media_controller_socket.c | 226 +------------------------- svc/media_controller_socket.h | 19 +-- svc/media_controller_svc.c | 276 +++++++++++++++++++++++++------- svc/media_controller_svc.h | 8 + 22 files changed, 616 insertions(+), 699 deletions(-) mode change 100644 => 100755 packaging/media-controller_create_db.sh create mode 100755 packaging/mediacontroller.socket diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ab0553..1635d53 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ SET(service "media") SET(submodule "controller") # for package file -SET(dependents "capi-base-common glib-2.0 gio-2.0 dlog sqlite3 db-util aul bundle") +SET(dependents "capi-base-common glib-2.0 gio-2.0 dlog sqlite3 db-util aul bundle libtzplatform-config") SET(fw_name "${project_prefix}-${service}-${submodule}") PROJECT(${fw_name}) diff --git a/include/media_controller_db.h b/include/media_controller_db.h index 8ac71d0..9a79341 100755 --- a/include/media_controller_db.h +++ b/include/media_controller_db.h @@ -19,8 +19,9 @@ #include #include +#include -#define MC_DB_NAME "/opt/usr/dbspace/.media_controller.db" +#define MC_DB_NAME tzplatform_mkpath(TZ_SYS_DB, ".media_controller.db") #define MC_DB_TABLE_SERVER_LIST "server_list" #define MC_DB_TABLE_LATEST_SERVER "latest_server" @@ -64,13 +65,11 @@ int mc_db_create_tables(void *handle); int mc_db_create_server_table(void *handle, const char *server_name); int mc_db_delete_server_table(void *handle, const char *server_name); int mc_db_check_server_table_exist(void *handle, const char *server_name, bool *exist); -int mc_db_clear_table(void *db_handle, const char *table_name); int mc_db_update_playback_info(void *handle, const char *table_name, int playback_state, unsigned long long playback_position); int mc_db_update_whole_metadata(void *handle, const char *server_name, const char *title, const char *artist, const char *album, const char *author, const char *genre, const char *duration, const char *date, const char *copyright, const char *description, const char *track_num, const char *picture); -int mc_db_update_metadata(void *handle, const char *table_name, char *name, char *value); int mc_db_update_shuffle_mode(void *handle, const char *table_name, int shuffle_mode); int mc_db_update_repeat_mode(void *handle, const char *table_name, int repeat_mode); diff --git a/include/media_controller_private.h b/include/media_controller_private.h index 6c1c6c3..f9b860d 100755 --- a/include/media_controller_private.h +++ b/include/media_controller_private.h @@ -22,13 +22,14 @@ extern "C" { #endif /* __cplusplus */ -#include "media_controller_server.h" -#include "media_controller_client.h" #include #include #include #include #include +#include "media_controller_server.h" +#include "media_controller_client.h" +#include "media_controller_socket.h" #ifdef LOG_TAG #undef LOG_TAG @@ -81,9 +82,9 @@ extern "C" { #define ERR_BUF_LENGHT 256 #define mc_stderror(fmt) do { \ - char buf[ERR_BUF_LENGHT] = {0, }; \ - strerror_r(errno, buf, ERR_BUF_LENGHT); \ - LOGE(fmt" : standard error= [%s]", buf); \ + char mc_stderror_buf[ERR_BUF_LENGHT] = {0, }; \ + strerror_r(errno, mc_stderror_buf, ERR_BUF_LENGHT); \ + LOGE(fmt" : standard error= [%s]", mc_stderror_buf); \ } while (0) #define MC_SAFE_FREE(src) {if(src) {free(src); src = NULL;}} @@ -190,7 +191,7 @@ typedef struct { }media_controller_client_s; /* formal callback to receive signal */ -typedef void(*mc_signal_received_cb)(char *interface_name, char *signal_name, char *message, int size, void *user_data); +typedef void(*mc_signal_received_cb)(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data); typedef struct { GDBusConnection *dbus_conn; char *interface_name; @@ -204,7 +205,7 @@ typedef struct { /* util */ int mc_util_get_own_name(char **name); -char *mc_util_get_interface_name(const char *prefix, const char *type, const char *name); +char* mc_util_get_interface_name(const char *type, const char *name); int mc_util_set_command_availabe(const char *name, const char *command_type, const char *command); int mc_util_get_command_availabe(const char *name, const char *command_type, const char *command); @@ -215,7 +216,8 @@ int mc_ipc_register_listener(GList *manage_list, GDBusConnection *connection, co int mc_ipc_unregister_listener(GList *manage_list, GDBusConnection *connection, const char *interface_name, const char *signal_name); int mc_ipc_unregister_all_listener(GList *manage_list, GDBusConnection *connection); int mc_ipc_send_message(GDBusConnection *connection, const char *dbus_name, const char *interface_name, const char* signal_name, const char* message, int flags); - +int mc_ipc_send_message_to_server(mc_msg_type_e msg_type, const char *request_msg); +int mc_ipc_service_connect(void); #ifdef __cplusplus } diff --git a/media-controller-service.manifest b/media-controller-service.manifest index 1b1374e..81ace0c 100755 --- a/media-controller-service.manifest +++ b/media-controller-service.manifest @@ -1,19 +1,6 @@ - - - - - - - - - - - - diff --git a/packaging/capi-media-controller.spec b/packaging/capi-media-controller.spec index db8b88d..4f61058 100755 --- a/packaging/capi-media-controller.spec +++ b/packaging/capi-media-controller.spec @@ -1,15 +1,17 @@ Name: capi-media-controller Summary: Multimedia Controller for player application -Version: 0.0.2 +Version: 0.0.3 Release: 1 Group: System/Libraries License: Apache-2.0 Source0: %{name}-%{version}.tar.gz Source1: mediacontroller.service +Source2: mediacontroller.socket Source1001: media-controller_create_db.sh Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig BuildRequires: cmake +BuildRequires: sqlite BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(glib-2.0) @@ -18,7 +20,8 @@ BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(db-util) BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(bundle) -BuildRequires: pkgconfig(security-server) +BuildRequires: pkgconfig(libsystemd-daemon) +BuildRequires: pkgconfig(libtzplatform-config) %description A media controller library in SLP C API @@ -41,7 +44,11 @@ A media controller library in SLP C API %setup -q %build +export CFLAGS+=" -Wextra -Wno-array-bounds" +export CFLAGS+=" -Wno-ignored-qualifiers -Wno-unused-parameter -Wshadow" +export CFLAGS+=" -Wwrite-strings -Wswitch-default" MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +export CFLAGS+=" -DGST_EXT_TIME_ANALYSIS -include stdint.h" %cmake . -DFULLVER=%{version} -DMAJORVER=${MAJORVER} %__make %{?jobs:-j%jobs} @@ -49,21 +56,24 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` rm -rf %{buildroot} %make_install -mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants +# Daemon & socket activation +mkdir -p %{buildroot}%{_unitdir} +mkdir -p %{buildroot}%{_unitdir}/sockets.target.wants install -m 644 %{SOURCE1} %{buildroot}%{_unitdir}/mediacontroller.service -ln -s ../mediacontroller.service %{buildroot}%{_unitdir}/multi-user.target.wants/mediacontroller.service +install -m 644 %{SOURCE2} %{buildroot}%{_unitdir}/mediacontroller.socket +ln -s ../mediacontroller.socket %{buildroot}%{_unitdir}/sockets.target.wants/mediacontroller.socket #Create DB +mkdir -p %{buildroot}%{_bindir} install -m 0775 %{SOURCE1001} %{buildroot}%{_bindir}/media-controller_create_db.sh -#mkdir -p %{buildroot}/opt/usr/dbspace -#sqlite3 %{buildroot}/opt/usr/dbspace/.media_controller.db 'PRAGMA journal_mode = PERSIST; PRAGMA user_version=1;' +mkdir -p %{buildroot}%{TZ_SYS_DB} +sqlite3 %{buildroot}%{TZ_SYS_DB}/.media_controller.db 'PRAGMA journal_mode = PERSIST; PRAGMA user_version=1;' -mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants mkdir -p %{buildroot}/usr/share/license cp LICENSE.APLv2.0 %{buildroot}/usr/share/license/%{name} %post -p /sbin/ldconfig -chgrp %TZ_SYS_USER_GROUP %{_bindir}/media-controller_create_db.shchgrp -R %TZ_SYS_USER_GROUP %{TZ_SYS_DATA}/data-media +chgrp %TZ_SYS_USER_GROUP %{_bindir}/media-controller_create_db.sh %postun -p /sbin/ldconfig %files @@ -72,21 +82,21 @@ chgrp %TZ_SYS_USER_GROUP %{_bindir}/media-controller_create_db.shchgrp -R %TZ_SY %{_bindir}/media-controller_create_db.sh #%{_bindir}/* //disable tests %manifest capi-media-controller.manifest +%attr(660,system,app) %{TZ_SYS_DB}/.media_controller.db +%attr(660,system,app) %{TZ_SYS_DB}/.media_controller.db-journal +%config(noreplace) %{TZ_SYS_DB}/.media_controller.db +%config(noreplace) %{TZ_SYS_DB}/.media_controller.db-journal /usr/share/license/%{name} %files -n mediacontroller -%defattr(-,root,root,-) +%defattr(-,system,system,-) %{_bindir}/mediacontroller %manifest media-controller-service.manifest %{_unitdir}/mediacontroller.service -%{_unitdir}/multi-user.target.wants/mediacontroller.service +%{_unitdir}/mediacontroller.socket +%{_unitdir}/sockets.target.wants/mediacontroller.socket #change owner -#chown 200:5000 /opt/usr/dbspace/.media_controller.db* -#%attr(660,system,app) /opt/usr/dbspace/.media_controller.db -#%attr(660,system,app) /opt/usr/dbspace/.media_controller.db-journal -#%config(noreplace) /opt/usr/dbspace/.media_controller.db -#%config(noreplace) /opt/usr/dbspace/.media_controller.db-journal -/usr/share/license/%{name} +#chown 200:5000 %{TZ_SYS_DB}/.media_controller.db* %files devel %defattr(-,root,root,-) diff --git a/packaging/media-controller_create_db.sh b/packaging/media-controller_create_db.sh old mode 100644 new mode 100755 index a8075a1..a112bce --- a/packaging/media-controller_create_db.sh +++ b/packaging/media-controller_create_db.sh @@ -11,9 +11,6 @@ sqlite3 ${TZ_SYS_DB}/.media_controller.db 'PRAGMA journal_mode = PERSIST;' chmod 664 ${TZ_SYS_DB}/.media_controller.db chmod 664 ${TZ_SYS_DB}/.media_controller.db-journal -#Change owner -#chown -R :$TZ_SYS_USER_GROUP ${TZ_SYS_DATA}/data-media/* - #Change group (6017: db_filemanager 5000: app) chgrp $TZ_SYS_USER_GROUP ${TZ_SYS_DB} chgrp 6017 ${TZ_SYS_DB}/.media_controller.db diff --git a/packaging/mediacontroller.service b/packaging/mediacontroller.service index eceb95b..035f1a7 100755 --- a/packaging/mediacontroller.service +++ b/packaging/mediacontroller.service @@ -1,13 +1,12 @@ [Unit] Description=Media controller -After=vconf-setup.service [Service] -Type=simple +User=system +Group=system +SmackProcessLabel=mediacontroller ExecStart=/usr/bin/mediacontroller -Restart=always -RestartSec=0 - +Type=simple [Install] WantedBy=multi-user.target diff --git a/packaging/mediacontroller.socket b/packaging/mediacontroller.socket new file mode 100755 index 0000000..12e08ee --- /dev/null +++ b/packaging/mediacontroller.socket @@ -0,0 +1,13 @@ +[Unit] +Description=MediaController Service socket + +[Socket] +SocketUser=system +SocketGroup=system +ListenStream=/var/run/mediacontroller/media_sa_controller +SmackLabelIPIn=mediacontroller +SmackLabelIPOut=mediacontroller +Service=mediacontroller.service + +[Install] +WantedBy=sockets.target diff --git a/src/media_controller_client.c b/src/media_controller_client.c index 78acf64..414dc06 100755 --- a/src/media_controller_client.c +++ b/src/media_controller_client.c @@ -19,9 +19,9 @@ #include "media_controller_db.h" -static void __client_server_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_server_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { - gchar **params; + gchar **params = NULL; media_controller_reciever_s *reciever = (media_controller_reciever_s *)user_data; mc_server_state_updated_cb callback = (mc_server_state_updated_cb)reciever->callback; @@ -32,14 +32,16 @@ static void __client_server_cb(char *interface_name, char *signal_name, char *me mc_debug("__client_server_cb(%s, %s, %s, %d, %p)", interface_name, signal_name, message, size, user_data); params = g_strsplit(message, MC_STRING_DELIMITER, 0); + mc_retm_if(params == NULL, "invalid server data"); + callback(params[0], (mc_server_state_e)atoi(params[1]), reciever->user_data); g_strfreev(params); } -static void __client_playback_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_playback_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { - gchar **params; + gchar **params = NULL; media_controller_reciever_s *reciever = (media_controller_reciever_s *)user_data; mc_playback_updated_cb callback = (mc_playback_updated_cb)reciever->callback; media_controller_playback_s *playback = NULL; @@ -51,8 +53,11 @@ static void __client_playback_cb(char *interface_name, char *signal_name, char * mc_debug("__client_playback_cb(%s, %s, %s, %d, %p)", interface_name, signal_name, message, size, user_data); playback = (media_controller_playback_s *)g_malloc(sizeof(media_controller_playback_s)); + mc_retm_if(playback == NULL, "Error allocation memory"); params = g_strsplit(message, MC_STRING_DELIMITER, 0); + mc_retm_if(params == NULL, "invalid playback data"); + playback->state = atoi(params[1]); playback->position = atol(params[2]); @@ -61,7 +66,7 @@ static void __client_playback_cb(char *interface_name, char *signal_name, char * g_strfreev(params); } -static void __client_metadata_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_metadata_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { int ret = MEDIA_CONTROLLER_ERROR_NONE; mc_metadata_h metadata = NULL; @@ -83,9 +88,9 @@ static void __client_metadata_cb(char *interface_name, char *signal_name, char * mc_client_destroy_metadata(metadata); } -static void __client_shuffle_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_shuffle_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { - gchar **params; + gchar **params = NULL; media_controller_reciever_s *reciever = (media_controller_reciever_s *)user_data; mc_shuffle_mode_changed_cb callback = (mc_shuffle_mode_changed_cb)reciever->callback; @@ -95,14 +100,16 @@ static void __client_shuffle_cb(char *interface_name, char *signal_name, char *m mc_debug("__client_shuffle_cb(%s, %s, %s, %d, %p)", interface_name, signal_name, message, size, user_data); params = g_strsplit(message, MC_STRING_DELIMITER, 0); + mc_retm_if(params == NULL, "invalid shuffle mode data"); + callback(params[0], (mc_shuffle_mode_e)atoi(params[1]), reciever->user_data); g_strfreev(params); } -static void __client_repeat_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_repeat_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { - gchar **params; + gchar **params = NULL; media_controller_reciever_s *reciever = (media_controller_reciever_s *)user_data; mc_repeat_mode_changed_cb callback = (mc_repeat_mode_changed_cb)reciever->callback; @@ -112,12 +119,14 @@ static void __client_repeat_cb(char *interface_name, char *signal_name, char *me mc_debug("__client_repeat_cb(%s, %s, %s, %d, %p)", interface_name, signal_name, message, size, user_data); params = g_strsplit(message, MC_STRING_DELIMITER, 0); + mc_retm_if(params == NULL, "invalid repeat mode data"); + callback(params[0], (mc_repeat_mode_e)atoi(params[1]), reciever->user_data); g_strfreev(params); } -static void __client_reply_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __client_reply_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { gchar **params = NULL; int enc_size = 0; @@ -186,6 +195,19 @@ int mc_client_create(mc_client_h *client) return ret; } + /*Try Socket Activation by systemd*/ + ret = mc_ipc_service_connect(); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to get mc_ipc_service_connect [%d]", ret); + } + + /*Send Connection Msg to Server*/ + ret = mc_ipc_send_message_to_server(MC_MSG_SERVER_CONNECTION, MC_SERVER_CONNECTION_MSG); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to mc_ipc_send_message_to_server [%d]", ret); + return ret; + } + ret = mc_util_get_own_name(&(mc_client->client_name)); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { mc_error("Filed to get client name %d", ret); @@ -627,12 +649,14 @@ int mc_client_send_playback_state_command(mc_client_h client, const char *server return ret; } - ret = mc_ipc_send_message(mc_client->dconn, NULL, mc_util_get_interface_name(NULL, MC_SERVER, server_name), MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD, message, 0); + char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); + ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD, message, 0); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { mc_error("Error mc_ipc_send_message [%d]", ret); } MC_SAFE_FREE(message); + MC_SAFE_FREE(interface_name); return ret; } @@ -654,11 +678,13 @@ int mc_client_send_custom_command(mc_client_h client, const char *server_name, c return ret; } - if(callback) { + if (callback) { + char *interface_name_for_reply = mc_util_get_interface_name(MC_CLIENT, mc_client->client_name); mc_client->reply_cb.callback = callback; mc_client->reply_cb.user_data = user_data; - mc_ipc_register_listener(mc_client->listeners, mc_client->dconn, mc_util_get_interface_name(NULL, MC_CLIENT, mc_client->client_name), + mc_ipc_register_listener(mc_client->listeners, mc_client->dconn, interface_name_for_reply, MC_DBUS_SIGNAL_NAME_CMD_REPLY, __client_reply_cb, (void *)&(mc_client->reply_cb)); + MC_SAFE_FREE(interface_name_for_reply); } if (data) { @@ -676,11 +702,13 @@ int mc_client_send_custom_command(mc_client_h client, const char *server_name, c return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; } - ret = mc_ipc_send_message(mc_client->dconn, NULL, mc_util_get_interface_name(NULL, MC_SERVER, server_name), MC_DBUS_SIGNAL_NAME_CUSTOM_CMD, message, 0); + char *interface_name = mc_util_get_interface_name(MC_SERVER, server_name); + ret = mc_ipc_send_message(mc_client->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_CUSTOM_CMD, message, 0); if (ret != MEDIA_CONTROLLER_ERROR_NONE) mc_error("Error mc_ipc_send_message [%d]", ret); MC_SAFE_FREE(message); + MC_SAFE_FREE(interface_name); return ret; } @@ -697,7 +725,15 @@ int mc_client_destroy(mc_client_h client) mc_error("Error mc_ipc_unregister_all_listener [%d]", ret); } - g_list_free(mc_client->listeners); + if (mc_client->listeners != NULL) { + g_list_free(mc_client->listeners); + } + + /*Send Disconnection Msg to Server*/ + ret = mc_ipc_send_message_to_server(MC_MSG_SERVER_DISCONNECTION, MC_SERVER_DISCONNECTION_MSG); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to mc_ipc_send_message_to_server [%d]", ret); + } ret = __mc_client_destroy(mc_client); diff --git a/src/media_controller_db.c b/src/media_controller_db.c index 7abfa3c..d58ea3a 100755 --- a/src/media_controller_db.c +++ b/src/media_controller_db.c @@ -18,8 +18,6 @@ #include "media_controller_db.h" #include "media_controller_socket.h" -#define MAX_RETRY_COUNT 3 - typedef enum { MC_SERVER_FIELD_SERVER_NAME = 0, MC_SERVER_FIELD_SERVER_STATE, @@ -49,105 +47,15 @@ static int __mc_db_busy_handler(void *pData, int count) return 100 - count; } -static int __media_db_request_update_tcp(mc_msg_type_e msg_type, const char *request_msg) -{ - int ret = MEDIA_CONTROLLER_ERROR_NONE; - int request_msg_size = 0; - int sockfd = -1; - mc_sock_info_s sock_info; - struct sockaddr_un serv_addr; - int port = MC_DB_UPDATE_PORT; - int retry_count = 0; - - if (!MC_STRING_VALID(request_msg)) { - mc_error("invalid query"); - return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; - } - - request_msg_size = strlen(request_msg); - if (request_msg_size >= MAX_MSG_SIZE) { - mc_error("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); - return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; - } - - mc_comm_msg_s send_msg; - memset((void *)&send_msg, 0, sizeof(mc_comm_msg_s)); - - send_msg.msg_type = msg_type; - send_msg.msg_size = request_msg_size; - strncpy(send_msg.msg, request_msg, sizeof(send_msg.msg) - 1); - - /*Create Socket*/ - ret = mc_ipc_create_client_socket(MC_TIMEOUT_SEC_10, &sock_info); - sockfd = sock_info.sock_fd; - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "socket is not created properly"); - - /*Set server Address*/ - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sun_family = AF_UNIX; - strncpy(serv_addr.sun_path, MC_IPC_PATH[port], sizeof(serv_addr.sun_path) - 1); - - /* Connecting to the media db server */ - if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - mc_stderror("connect error"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /* Send request */ - if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { - mc_stderror("send failed"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /*Receive Response*/ - int recv_msg_size = -1; - int recv_msg = -1; -RETRY: - if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { - mc_error("recv failed : [%d]", sockfd); - mc_stderror("recv failed"); - - if (errno == EINTR) { - mc_stderror("catch interrupt"); - goto RETRY; - } - - if (errno == EWOULDBLOCK) { - if (retry_count < MAX_RETRY_COUNT) { - mc_error("TIME OUT[%d]", retry_count); - retry_count++; - goto RETRY; - } - - mc_ipc_delete_client_socket(&sock_info); - mc_error("Timeout. Can't try any more"); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } else { - mc_stderror("recv failed"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - } - - mc_debug("RECEIVE OK [%d]", recv_msg); - ret = recv_msg; - - mc_ipc_delete_client_socket(&sock_info); - - return ret; -} - static int __mc_db_update_db(void *handle, const char *sql_str) { int ret = MEDIA_CONTROLLER_ERROR_NONE; mc_retvm_if(!MC_STRING_VALID(sql_str), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid Query"); - ret = __media_db_request_update_tcp(MC_MSG_DB_UPDATE, sql_str); + ret = mc_ipc_send_message_to_server(MC_MSG_DB_UPDATE, sql_str); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("__media_db_request_update_tcp failed : %d", ret); + mc_error("mc_ipc_send_message_to_server failed : %d", ret); } return ret; @@ -263,6 +171,8 @@ int mc_db_connect(void **handle) int ret = MEDIA_CONTROLLER_ERROR_NONE; sqlite3 *db_handle = NULL; + mc_debug("mc_db_connect"); + mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); /*Connect DB*/ @@ -274,12 +184,15 @@ int mc_db_connect(void **handle) return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; } + if (db_handle == NULL) { + mc_error("*db_handle is NULL"); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + /*Register busy handler*/ ret = sqlite3_busy_handler(db_handle, __mc_db_busy_handler, NULL); if (SQLITE_OK != ret) { - if (db_handle) { - mc_error("error when register busy handler %s\n", sqlite3_errmsg(db_handle)); - } + mc_error("error when register busy handler %s\n", sqlite3_errmsg(db_handle)); db_util_close(db_handle); *handle = NULL; @@ -291,22 +204,6 @@ int mc_db_connect(void **handle) return MEDIA_CONTROLLER_ERROR_NONE; } -int mc_db_clear_table(void *handle, const char *table_name) -{ - int ret = MEDIA_CONTROLLER_ERROR_NONE; - char *sql_str = NULL; - - mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); - - sql_str = sqlite3_mprintf(DB_DELETE_ALL_FROM_TABLE, table_name); - - ret = __mc_db_update_db(handle, sql_str); - - SQLITE3_SAFE_FREE(sql_str); - - return ret; -} - int mc_db_update_playback_info(void *handle, const char *server_name, int playback_state, unsigned long long playback_position) { int ret = MEDIA_CONTROLLER_ERROR_NONE; @@ -344,23 +241,6 @@ int mc_db_update_whole_metadata(void *handle, const char *server_name, return ret; } -int mc_db_update_metadata(void *handle, const char *server_name, char *name, char *value) -{ - int ret = MEDIA_CONTROLLER_ERROR_NONE; - char *sql_str = NULL; - - mc_retvm_if(handle == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); - mc_retvm_if(server_name == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "server_name is NULL"); - - sql_str = sqlite3_mprintf(DB_UPDATE_METADATA_INTO_SERVER_TABLE, server_name, name, value); - - ret = __mc_db_update_db(handle, sql_str); - - SQLITE3_SAFE_FREE(sql_str); - - return ret; -} - int mc_db_update_shuffle_mode(void *handle, const char *server_name, int shuffle_mode) { int ret = MEDIA_CONTROLLER_ERROR_NONE; diff --git a/src/media_controller_ipc.c b/src/media_controller_ipc.c index 0283631..6343a1e 100755 --- a/src/media_controller_ipc.c +++ b/src/media_controller_ipc.c @@ -16,6 +16,8 @@ #include "media_controller_private.h" +#define MAX_RETRY_COUNT 3 + static char *__make_key_for_map(const char *main_key, const char *sub_key) { return g_strdup_printf("%s.%s", main_key, sub_key); @@ -31,7 +33,7 @@ static void __mc_ipc_signal_cb(GDBusConnection *connection, { char *key = __make_key_for_map(interface_name, signal_name); GList *listener_list = (GList *)user_data; - int i; + unsigned int i = 0; mc_debug("__mc_ipc_signal_cb Received : "); @@ -53,11 +55,13 @@ static void __mc_ipc_signal_cb(GDBusConnection *connection, static gboolean _mc_ipc_is_listener_duplicated(GList *listener_list, const char *key) { - int i = 0; + unsigned int i = 0; + for (i = 0; i < g_list_length(listener_list); i++) { mc_ipc_listener_s *listener = (mc_ipc_listener_s *)g_list_nth_data((listener_list), i); mc_retvm_if(listener && !strcmp(listener->key, key), TRUE, "listener[%s] is duplicated ", key); } + return FALSE; } @@ -98,7 +102,6 @@ int mc_ipc_get_dbus_connection(GDBusConnection **connection, int *dref_count) _connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); if (!_connection) { mc_error("g_bus_get_sync failed : %s", error ? error->message : "none"); - g_object_unref(_connection); if (error) g_error_free(error); return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; } @@ -138,7 +141,6 @@ int mc_ipc_unref_dbus_connection(GDBusConnection *connection, int *dref_count) int mc_ipc_register_listener(GList *listener_list, GDBusConnection *connection, const char *interface_name, const char *signal_name, mc_signal_received_cb callback, void *user_data) { char *key = NULL; - char *copied_key = NULL; guint handler = 0; mc_retvm_if(listener_list == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "listener_list is NULL"); @@ -148,32 +150,32 @@ int mc_ipc_register_listener(GList *listener_list, GDBusConnection *connection, mc_retvm_if(callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "callback is NULL"); key = __make_key_for_map(interface_name, signal_name); - copied_key = g_strdup(key); - if (_mc_ipc_is_listener_duplicated(listener_list, key)) - { + if (_mc_ipc_is_listener_duplicated(listener_list, key)) { mc_error("listener is duplicated"); - MC_SAFE_FREE(copied_key); MC_SAFE_FREE(key); return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; } + mc_ipc_listener_s *listener = (mc_ipc_listener_s *)g_malloc(sizeof(mc_ipc_listener_s)); + if (listener == NULL) { + mc_error("Error memroy allocation"); + MC_SAFE_FREE(key); + return MEDIA_CONTROLLER_ERROR_OUT_OF_MEMORY; + } handler = _mc_ipc_signal_subscribe(connection, interface_name, signal_name, listener_list); - - mc_ipc_listener_s *listener = (mc_ipc_listener_s*)g_malloc(sizeof(mc_ipc_listener_s)); listener->dbus_conn = connection; listener->interface_name = g_strdup(interface_name); listener->signal_name = g_strdup(signal_name); listener->callback = callback; listener->user_data = user_data; listener->handler = handler; - listener->key = copied_key; + listener->key = key; (listener_list) = g_list_append((listener_list), listener); mc_debug("listener[%s.%s] is registered", interface_name, signal_name); - MC_SAFE_FREE(key); return MEDIA_CONTROLLER_ERROR_NONE; } @@ -194,7 +196,7 @@ int mc_ipc_unregister_listener(GList *listener_list, GDBusConnection *connection return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; } - for (i = g_list_length(listener_list); i > 0; i--) { + for (i = g_list_length(listener_list) - 1; i >= 0; i--) { mc_ipc_listener_s *listener = (mc_ipc_listener_s *)g_list_nth_data(listener_list, i); if (listener && !strcmp(listener->key, key)) { _mc_ipc_signal_unsubscribe(connection, listener->handler); @@ -219,7 +221,7 @@ int mc_ipc_unregister_all_listener(GList *listener_list, GDBusConnection *connec mc_retvm_if(connection == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "connection is NULL"); mc_retvm_if(listener_list == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "listener_list is NULL"); - for (i = g_list_length(listener_list); i > 0; i--) { + for (i = g_list_length(listener_list) - 1; i >= 0; i--) { mc_ipc_listener_s *listener = (mc_ipc_listener_s *)g_list_nth_data(listener_list, i); if (listener) { _mc_ipc_signal_unsubscribe(connection, listener->handler); @@ -263,3 +265,122 @@ int mc_ipc_send_message(GDBusConnection *connection, const char *dbus_name, cons return MEDIA_CONTROLLER_ERROR_NONE; } + +int mc_ipc_send_message_to_server(mc_msg_type_e msg_type, const char *request_msg) +{ + int ret = MEDIA_CONTROLLER_ERROR_NONE; + int request_msg_size = 0; + int sockfd = -1; + mc_sock_info_s sock_info; + struct sockaddr_un serv_addr; + int retry_count = 0; + + if (!MC_STRING_VALID(request_msg)) { + mc_error("invalid query"); + return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; + } + + request_msg_size = strlen(request_msg); + if (request_msg_size >= MAX_MSG_SIZE) { + mc_error("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); + return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; + } + + mc_comm_msg_s send_msg; + memset((void *)&send_msg, 0, sizeof(mc_comm_msg_s)); + + send_msg.msg_type = msg_type; + send_msg.msg_size = request_msg_size; + strncpy(send_msg.msg, request_msg, sizeof(send_msg.msg) - 1); + + /*Create Socket*/ + ret = mc_ipc_create_client_socket(MC_TIMEOUT_SEC_10, &sock_info); + sockfd = sock_info.sock_fd; + mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "socket is not created properly"); + + /*Set server Address*/ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sun_family = AF_UNIX; + strncpy(serv_addr.sun_path, MC_IPC_PATH, sizeof(serv_addr.sun_path) - 1); + + /* Connecting to the media db server */ + if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { + mc_stderror("connect error"); + mc_ipc_delete_client_socket(&sock_info); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + + /* Send request */ + if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { + mc_stderror("send failed"); + mc_ipc_delete_client_socket(&sock_info); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + + /*Receive Response*/ + int recv_msg_size = -1; + int recv_msg = -1; +RETRY: + if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { + mc_error("recv failed : [%d]", sockfd); + mc_stderror("recv failed"); + + if (errno == EINTR) { + mc_stderror("catch interrupt"); + goto RETRY; + } + + if (errno == EWOULDBLOCK) { + if (retry_count < MAX_RETRY_COUNT) { + mc_error("TIME OUT[%d]", retry_count); + retry_count++; + goto RETRY; + } + + mc_ipc_delete_client_socket(&sock_info); + mc_error("Timeout. Can't try any more"); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } else { + mc_stderror("recv failed"); + mc_ipc_delete_client_socket(&sock_info); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + } + + mc_debug("RECEIVE OK [%d]", recv_msg); + ret = recv_msg; + + mc_ipc_delete_client_socket(&sock_info); + + return ret; +} + +int mc_ipc_service_connect(void) +{ + int ret = MEDIA_CONTROLLER_ERROR_NONE; + int sockfd = -1; + mc_sock_info_s sock_info; + struct sockaddr_un serv_addr; + + /*Create Socket*/ + ret = mc_ipc_create_client_socket(MC_TIMEOUT_SEC_10, &sock_info); + sockfd = sock_info.sock_fd; + mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "socket is not created properly"); + + /*Set server Address*/ + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sun_family = AF_UNIX; + strncpy(serv_addr.sun_path, MC_SOCK_ACTIVATION_PATH, sizeof(serv_addr.sun_path) - 1); + + /* Connecting to the media db server */ + if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { + mc_stderror("connect error"); + mc_ipc_delete_client_socket(&sock_info); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + + mc_debug("CONNECT OK"); + + mc_ipc_delete_client_socket(&sock_info); + return ret; +} diff --git a/src/media_controller_server.c b/src/media_controller_server.c index 32625b0..9008ec5 100755 --- a/src/media_controller_server.c +++ b/src/media_controller_server.c @@ -112,9 +112,9 @@ static int __mc_server_destoy(media_controller_server_s *mc_server) return ret; } -static void __server_playback_state_command_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __server_playback_state_command_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { - gchar **params; + gchar **params = NULL; media_controller_reciever_s *receiver = (media_controller_reciever_s *)user_data; mc_retm_if(receiver == NULL, "reciever is NULL"); mc_retm_if(message == NULL, "message is NULL"); @@ -137,7 +137,7 @@ static void __server_playback_state_command_cb(char *interface_name, char *signa g_strfreev(params); } -static void __server_custom_command_cb(char *interface_name, char *signal_name, char *message, int size, void *user_data) +static void __server_custom_command_cb(const char *interface_name, const char *signal_name, const char *message, int size, void *user_data) { gchar **params = NULL; int enc_size = 0; @@ -156,9 +156,13 @@ static void __server_custom_command_cb(char *interface_name, char *signal_name, params = g_strsplit(message, MC_STRING_DELIMITER, 0); mc_retm_if(params == NULL, "invalid custom data"); - sender = strdup(params[0]); + if(params[0]) + sender = strdup(params[0]); + if (mc_util_get_command_availabe(sender, MC_COMMAND_CUSTOM, params[1]) != MEDIA_CONTROLLER_ERROR_NONE) { mc_error("Error permission denied"); + MC_SAFE_FREE(sender); + g_strfreev(params); return ; } @@ -434,9 +438,12 @@ int mc_server_set_playback_state_command_received_cb(mc_server_h server, mc_serv mc_server->playback_state_reciever.callback = callback; mc_server->playback_state_reciever.user_data = user_data; - ret = mc_ipc_register_listener(mc_server->listeners, mc_server->dconn, mc_util_get_interface_name(NULL, MC_SERVER, mc_server->server_name), + char *interface_name = mc_util_get_interface_name(MC_SERVER, mc_server->server_name); + ret = mc_ipc_register_listener(mc_server->listeners, mc_server->dconn, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD, __server_playback_state_command_cb, (void *)&(mc_server->playback_state_reciever)); + MC_SAFE_FREE(interface_name); + return ret; } @@ -447,12 +454,15 @@ int mc_server_unset_playback_state_command_received_cb(mc_server_h server) mc_retvm_if(mc_server == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); - ret = mc_ipc_unregister_listener(mc_server->listeners, mc_server->dconn, mc_util_get_interface_name(NULL, MC_SERVER, mc_server->server_name), + char *interface_name = mc_util_get_interface_name(MC_SERVER, mc_server->server_name); + ret = mc_ipc_unregister_listener(mc_server->listeners, mc_server->dconn, interface_name, MC_DBUS_SIGNAL_NAME_PLAYBACK_STATE_CMD); mc_server->playback_state_reciever.callback = NULL; mc_server->playback_state_reciever.user_data = NULL; + MC_SAFE_FREE(interface_name); + return ret; } @@ -467,9 +477,12 @@ int mc_server_set_custom_command_received_cb(mc_server_h server, mc_server_custo mc_server->custom_cmd_reciever.callback = callback; mc_server->custom_cmd_reciever.user_data = user_data; - ret = mc_ipc_register_listener(mc_server->listeners, mc_server->dconn, mc_util_get_interface_name(NULL, MC_SERVER, mc_server->server_name), + char *interface_name = mc_util_get_interface_name(MC_SERVER, mc_server->server_name); + ret = mc_ipc_register_listener(mc_server->listeners, mc_server->dconn, interface_name, MC_DBUS_SIGNAL_NAME_CUSTOM_CMD, __server_custom_command_cb, (void *)&(mc_server->custom_cmd_reciever)); + MC_SAFE_FREE(interface_name); + return ret; } @@ -480,12 +493,14 @@ int mc_server_unset_custom_command_received_cb(mc_server_h server) mc_retvm_if(mc_server == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); - ret = mc_ipc_unregister_listener(mc_server->listeners, mc_server->dconn, mc_util_get_interface_name(NULL, MC_SERVER, mc_server->server_name) - , MC_DBUS_SIGNAL_NAME_CUSTOM_CMD); + char *interface_name = mc_util_get_interface_name(MC_SERVER, mc_server->server_name); + ret = mc_ipc_unregister_listener(mc_server->listeners, mc_server->dconn, interface_name, MC_DBUS_SIGNAL_NAME_CUSTOM_CMD); mc_server->custom_cmd_reciever.callback = NULL; mc_server->custom_cmd_reciever.user_data = NULL; + MC_SAFE_FREE(interface_name); + return ret; } @@ -510,11 +525,13 @@ int mc_server_send_command_reply(mc_server_h server, const char *client_name, in message = g_strdup_printf("%s%s%d%s%d%s%s", mc_server->server_name, MC_STRING_DELIMITER, result_code, MC_STRING_DELIMITER, size_r, MC_STRING_DELIMITER, (unsigned char *)raw_data); - ret = mc_ipc_send_message(mc_server->dconn, NULL, mc_util_get_interface_name(NULL, MC_CLIENT, client_name), MC_DBUS_SIGNAL_NAME_CMD_REPLY, message, 0); + char *interface_name = mc_util_get_interface_name(MC_CLIENT, client_name); + ret = mc_ipc_send_message(mc_server->dconn, NULL, interface_name, MC_DBUS_SIGNAL_NAME_CMD_REPLY, message, 0); if (ret != MEDIA_CONTROLLER_ERROR_NONE) mc_error("fail mc_ipc_send_message [%d]", ret); MC_SAFE_FREE(message); + MC_SAFE_FREE(interface_name); return ret; } @@ -526,11 +543,26 @@ int mc_server_create(mc_server_h *server) media_controller_server_s *mc_server = NULL; bool table_exist = FALSE; + mc_debug_fenter(); + mc_retvm_if(server == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is null"); mc_server = (media_controller_server_s *)calloc(1, sizeof(media_controller_server_s)); mc_retvm_if(mc_server == NULL, MEDIA_CONTROLLER_ERROR_OUT_OF_MEMORY, "Error allocation memory"); + /*Try Socket Activation by systemd*/ + ret = mc_ipc_service_connect(); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to get mc_ipc_service_connect [%d]", ret); + } + + /*Send Connection Msg to Server*/ + ret = mc_ipc_send_message_to_server(MC_MSG_SERVER_CONNECTION, MC_SERVER_CONNECTION_MSG); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to mc_ipc_send_message_to_server [%d]", ret); + return ret; + } + ret = mc_util_get_own_name(&server_name); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { mc_error("Failed to get server_name [%d]", ret); @@ -611,6 +643,8 @@ int mc_server_create(mc_server_h *server) *server = (mc_server_h)mc_server; + mc_debug_fleave(); + return MEDIA_CONTROLLER_ERROR_NONE; } @@ -620,6 +654,8 @@ int mc_server_destroy(mc_server_h server) bool is_latest = FALSE; media_controller_server_s *mc_server = (media_controller_server_s *)server; + mc_debug_fenter(); + mc_retvm_if(mc_server == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL"); ret = mc_ipc_unregister_all_listener(mc_server->listeners, mc_server->dconn); @@ -627,7 +663,9 @@ int mc_server_destroy(mc_server_h server) mc_error("fail mc_ipc_unregister_all_listener [%d]", ret); } - g_list_free(mc_server->listeners); + if (mc_server->listeners != NULL) { + g_list_free(mc_server->listeners); + } ret = mc_db_delete_server_address_from_table(mc_server->db_handle, MC_DB_TABLE_SERVER_LIST, mc_server->server_name); if (ret != MEDIA_CONTROLLER_ERROR_NONE) @@ -649,9 +687,17 @@ int mc_server_destroy(mc_server_h server) mc_error("Error __mc_server_send_message [%d]", ret); } + /*Send Disconnection Msg to Server*/ + ret = mc_ipc_send_message_to_server(MC_MSG_SERVER_DISCONNECTION, MC_SERVER_DISCONNECTION_MSG); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to mc_ipc_send_message_to_server [%d]", ret); + } + ret = __mc_server_destoy(mc_server); if (ret != MEDIA_CONTROLLER_ERROR_NONE) mc_error("fail __mc_server_destoy [%d]", ret); + mc_debug_fleave(); + return ret; } diff --git a/src/media_controller_util.c b/src/media_controller_util.c index 9465de8..744fcad 100755 --- a/src/media_controller_util.c +++ b/src/media_controller_util.c @@ -16,16 +16,14 @@ #include #include "media_controller_private.h" -#include "media_controller_socket.h" #define MAX_NAME_LENGTH 255 -#define MAX_RETRY_COUNT 3 static void _mc_util_check_valid_name(const char *name, char **new_name) { - char old_word[MAX_NAME_LENGTH]; - char new_word[MAX_NAME_LENGTH]; - int i = 0; + char old_word[MAX_NAME_LENGTH] = {0, }; + char new_word[MAX_NAME_LENGTH] = {0, }; + unsigned int i = 0; mc_retm_if(name == NULL, "Invalid parameter."); @@ -59,9 +57,11 @@ static void _mc_util_check_valid_name(const char *name, char **new_name) int mc_util_get_own_name(char **name) { - char temp[MAX_NAME_LENGTH]; + char temp[MAX_NAME_LENGTH] = {0, }; int pid = -1; + memset(temp, 0, MAX_NAME_LENGTH); + pid = getpid(); if (pid == -1) { mc_error("Error failed to get pid!"); @@ -74,7 +74,7 @@ int mc_util_get_own_name(char **name) return MEDIA_CONTROLLER_ERROR_NONE; } -char *mc_util_get_interface_name(const char *prefix, const char *type, const char *name) +char *mc_util_get_interface_name(const char *type, const char *name) { char *temp = NULL; char *interface_name = NULL; @@ -82,12 +82,7 @@ char *mc_util_get_interface_name(const char *prefix, const char *type, const cha mc_retvm_if(type == NULL, NULL, "type is NULL"); mc_retvm_if(name == NULL, NULL, "name is NULL"); - if (prefix == NULL) - { - temp = g_strdup_printf("%s.%s.%s", MC_DBUS_INTERFACE_PREFIX, type, name); - } else { - temp = g_strdup_printf("%s.%s.%s", prefix, type, name); - } + temp = g_strdup_printf("%s.%s.%s", MC_DBUS_INTERFACE_PREFIX, type, name); _mc_util_check_valid_name(temp, &interface_name); MC_SAFE_FREE(temp); @@ -97,12 +92,6 @@ char *mc_util_get_interface_name(const char *prefix, const char *type, const cha int mc_util_set_command_availabe(const char *name, const char *command_type, const char *command) { int ret = MEDIA_CONTROLLER_ERROR_NONE; - int request_msg_size = 0; - int sockfd = -1; - mc_sock_info_s sock_info; - struct sockaddr_un serv_addr; - int port = MC_DB_SET_PORT; - int retry_count = 0; char *message = NULL; if (!MC_STRING_VALID(name) || !MC_STRING_VALID(command_type)) { @@ -115,93 +104,14 @@ int mc_util_set_command_availabe(const char *name, const char *command_type, con else message = g_strdup_printf("%s%s%s", name, command_type, command); - request_msg_size = strlen(message); - if (request_msg_size >= MAX_MSG_SIZE) { - mc_error("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); - return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; - } - - mc_comm_msg_s send_msg; - memset((void *)&send_msg, 0, sizeof(mc_comm_msg_s)); - - send_msg.msg_type = MC_MSG_CLIENT_SET; - send_msg.msg_size = request_msg_size; - strncpy(send_msg.msg, message, sizeof(send_msg.msg) - 1); - - /*Create Socket*/ - ret = mc_ipc_create_client_socket(MC_TIMEOUT_SEC_10, &sock_info); - sockfd = sock_info.sock_fd; - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "socket is not created properly"); - - /*Set server Address*/ - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sun_family = AF_UNIX; - strncpy(serv_addr.sun_path, MC_IPC_PATH[port], sizeof(serv_addr.sun_path) - 1); - - /* Connecting to the media db server */ - if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - mc_stderror("connect error"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /* Send request */ - if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { - mc_stderror("send failed"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /*Receive Response*/ - int recv_msg_size = -1; - int recv_msg = -1; -RETRY: - if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { - mc_error("recv failed : [%d]", sockfd); - mc_stderror("recv failed"); - - if (errno == EINTR) { - mc_stderror("catch interrupt"); - goto RETRY; - } - - if (errno == EWOULDBLOCK) { - if (retry_count < MAX_RETRY_COUNT) { - mc_error("TIME OUT[%d]", retry_count); - retry_count++; - goto RETRY; - } - - mc_ipc_delete_client_socket(&sock_info); - mc_error("Timeout. Can't try any more"); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } else { - mc_stderror("recv failed"); - - mc_ipc_delete_client_socket(&sock_info); - - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - } - - mc_debug("RECEIVE OK [%d]", recv_msg); - ret = recv_msg; - - mc_ipc_delete_client_socket(&sock_info); + ret = mc_ipc_send_message_to_server(MC_MSG_CLIENT_SET, message); return ret; - } int mc_util_get_command_availabe(const char *name, const char *command_type, const char *command) { int ret = MEDIA_CONTROLLER_ERROR_NONE; - int request_msg_size = 0; - int sockfd = -1; - mc_sock_info_s sock_info; - struct sockaddr_un serv_addr; - int port = MC_DB_GET_PORT; - int retry_count = 0; char *message = NULL; if (!MC_STRING_VALID(name) || !MC_STRING_VALID(command_type)) { @@ -214,80 +124,7 @@ int mc_util_get_command_availabe(const char *name, const char *command_type, con else message = g_strdup_printf("%s%s%s", name, command_type, command); - request_msg_size = strlen(message); - if (request_msg_size >= MAX_MSG_SIZE) { - mc_error("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE); - return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER; - } - - mc_comm_msg_s send_msg; - memset((void *)&send_msg, 0, sizeof(mc_comm_msg_s)); - - send_msg.msg_type = MC_MSG_CLIENT_GET; - send_msg.msg_size = request_msg_size; - strncpy(send_msg.msg, message, sizeof(send_msg.msg) - 1); - - /*Create Socket*/ - ret = mc_ipc_create_client_socket(MC_TIMEOUT_SEC_10, &sock_info); - sockfd = sock_info.sock_fd; - mc_retvm_if(ret != MEDIA_CONTROLLER_ERROR_NONE, ret, "socket is not created properly"); - - /*Set server Address*/ - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sun_family = AF_UNIX; - strncpy(serv_addr.sun_path, MC_IPC_PATH[port], sizeof(serv_addr.sun_path) - 1); - - /* Connecting to the media db server */ - if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - mc_stderror("connect error"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /* Send request */ - if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) { - mc_stderror("send failed"); - mc_ipc_delete_client_socket(&sock_info); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - - /*Receive Response*/ - int recv_msg_size = -1; - int recv_msg = -1; -RETRY: - if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) { - mc_error("recv failed : [%d]", sockfd); - mc_stderror("recv failed"); - - if (errno == EINTR) { - mc_stderror("catch interrupt"); - goto RETRY; - } - - if (errno == EWOULDBLOCK) { - if (retry_count < MAX_RETRY_COUNT) { - mc_error("TIME OUT[%d]", retry_count); - retry_count++; - goto RETRY; - } - - mc_ipc_delete_client_socket(&sock_info); - mc_error("Timeout. Can't try any more"); - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } else { - mc_stderror("recv failed"); - - mc_ipc_delete_client_socket(&sock_info); - - return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; - } - } - - mc_debug("RECEIVE OK [%d]", recv_msg); - ret = recv_msg; - - mc_ipc_delete_client_socket(&sock_info); + ret = mc_ipc_send_message_to_server(MC_MSG_CLIENT_GET, message); return ret; - } diff --git a/svc/CMakeLists.txt b/svc/CMakeLists.txt index b5d0262..1ed8b1a 100755 --- a/svc/CMakeLists.txt +++ b/svc/CMakeLists.txt @@ -1,5 +1,5 @@ # for package file -SET(dependents "capi-base-common glib-2.0 gio-2.0 dlog sqlite3 db-util security-server") +SET(dependents "capi-base-common glib-2.0 gio-2.0 dlog sqlite3 db-util libsystemd-daemon libtzplatform-config") SET(fw_svc "media-controller-svc") PROJECT(${fw_svc}) diff --git a/svc/daemon/CMakeLists.txt b/svc/daemon/CMakeLists.txt index 9140113..ce55dea 100755 --- a/svc/daemon/CMakeLists.txt +++ b/svc/daemon/CMakeLists.txt @@ -1,7 +1,7 @@ SET(fw_daemon "mediacontroller") INCLUDE(FindPkgConfig) -pkg_check_modules(${fw_daemon} REQUIRED glib-2.0 security-server) +pkg_check_modules(${fw_daemon} REQUIRED glib-2.0) FOREACH(flag ${${fw_daemon}_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/svc/daemon/media_controller_main.c b/svc/daemon/media_controller_main.c index 0d2307a..63aa550 100755 --- a/svc/daemon/media_controller_main.c +++ b/svc/daemon/media_controller_main.c @@ -18,24 +18,73 @@ #include "../media_controller_socket.h" #include "../media_controller_svc.h" -GMainLoop *g_mainloop = NULL; +GMainLoop *g_mc_mainloop = NULL; +static int g_mc_timer_id = 0; + +#define MC_MAIN_TIMEOUT_SEC_60 60 + + +void __mc_main_create_timer(int timer_id); + +gboolean __mc_main_check_connection(gpointer user_data) +{ + int connection_cnt = 0; + connection_cnt = mc_svc_get_connection_cnt(); + if ((connection_cnt == -1) || (connection_cnt > 0)) { + mc_error("[No-error] Timer is Called but there is working Process"); + + __mc_main_create_timer(g_mc_timer_id); + } else { + mc_error("[No-error] Timer is Called. Now Killing mediacontroller process"); + g_mc_timer_id = 0; + + /*Quit Controller Thread*/ + GMainLoop *mc_svc_mainloop = mc_svc_get_main_loop(); + if (mc_svc_mainloop && g_main_is_running(mc_svc_mainloop)) { + g_main_loop_quit(mc_svc_mainloop); + } + + g_main_loop_quit(g_mc_mainloop); + } + + return FALSE; +} + +void __mc_main_create_timer(int timer_id) +{ + if (timer_id > 0) + g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), timer_id)); + + GSource *timer_src = g_timeout_source_new_seconds(MC_MAIN_TIMEOUT_SEC_60); + g_source_set_callback (timer_src, __mc_main_check_connection, NULL, NULL); + g_mc_timer_id = g_source_attach (timer_src, g_main_context_get_thread_default()); +} int main(int argc, char **argv) { GThread *svc_thread = NULL; + int fd = -1; /*Init main loop*/ - g_mainloop = g_main_loop_new(NULL, FALSE); + g_mc_mainloop = g_main_loop_new(NULL, FALSE); + + fd = mc_create_socket_activation(); + if (fd < 0) { + mc_error("Failed to socekt creation"); + } /*create each threads*/ svc_thread = g_thread_new("mc_svc_thread", (GThreadFunc)mc_svc_thread, NULL); + /* Create Timer */ + __mc_main_create_timer(g_mc_timer_id); + mc_debug("*** Media Controller Daemon is running ***"); - g_main_loop_run(g_mainloop); + g_main_loop_run(g_mc_mainloop); g_thread_join(svc_thread); - g_main_loop_unref(g_mainloop); + g_main_loop_unref(g_mc_mainloop); mc_debug("*** Media Controller Daemon is stopped ***"); diff --git a/svc/media_controller_db_util.c b/svc/media_controller_db_util.c index 3392aa3..faf0c61 100755 --- a/svc/media_controller_db_util.c +++ b/svc/media_controller_db_util.c @@ -15,6 +15,7 @@ */ #include "media_controller_db_util.h" +#include "media_controller_private.h" static int __mc_db_util_busy_handler(void *pData, int count) { @@ -41,12 +42,15 @@ int mc_db_util_connect(void **handle) return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; } + if (db_handle == NULL) { + mc_error("*db_handle is NULL"); + return MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + /*Register busy handler*/ ret = sqlite3_busy_handler(db_handle, __mc_db_util_busy_handler, NULL); if (SQLITE_OK != ret) { - if (db_handle) { - mc_error("error when register busy handler %s\n", sqlite3_errmsg(db_handle)); - } + mc_error("error when register busy handler %s\n", sqlite3_errmsg(db_handle)); db_util_close(db_handle); *handle = NULL; diff --git a/svc/media_controller_db_util.h b/svc/media_controller_db_util.h index e6f4ce1..f858a17 100755 --- a/svc/media_controller_db_util.h +++ b/svc/media_controller_db_util.h @@ -19,9 +19,9 @@ #include #include -#include "media_controller_private.h" +#include -#define MC_DB_NAME "/opt/usr/dbspace/.media_controller.db" +#define MC_DB_NAME tzplatform_mkpath(TZ_SYS_DB, ".media_controller.db") #define SQLITE3_SAFE_FREE(sql_string) {if(sql_string) { sqlite3_free(sql_string); sql_string = NULL;}} #define SQLITE3_FINALIZE(x) {if(x != NULL) {sqlite3_finalize(x);}} diff --git a/svc/media_controller_socket.c b/svc/media_controller_socket.c index 55b5c71..a5b0b3f 100755 --- a/svc/media_controller_socket.c +++ b/svc/media_controller_socket.c @@ -18,7 +18,6 @@ #include #include #include -#include #include "media_controller_private.h" #include "media_controller_db_util.h" @@ -29,21 +28,6 @@ #define MC_SOCK_PATH MC_SOCK_PATH_PRFX MC_SOCK_PATH_TEMPLATE #define MC_SOCK_UDP_BLOCK_SIZE 512 -static int __mc_privilege_ask(int client_sockfd, char *type, char *privilege_object) -{ - int ret = MEDIA_CONTROLLER_ERROR_NONE; - - ret = security_server_check_privilege_by_sockfd(client_sockfd, type, privilege_object); - if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { - mc_error("You do not have permission for this operation."); - ret = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - } else { - mc_debug("PERMISSION OK"); - } - - return ret; -} - int mc_ipc_create_client_socket(int timeout_sec, mc_sock_info_s *sock_info) { int sock = -1; @@ -89,13 +73,10 @@ int mc_ipc_delete_client_socket(mc_sock_info_s *sock_info) int mc_ipc_create_server_socket(mc_msg_port_type_e port, int *sock_fd) { - int i; + int i = 0; bool bind_success = false; int sock = -1; struct sockaddr_un serv_addr; - unsigned short serv_port; - - serv_port = port; /* Create a TCP socket */ if ((sock = socket(PF_FILE, SOCK_STREAM, 0)) < 0) { @@ -106,8 +87,8 @@ int mc_ipc_create_server_socket(mc_msg_port_type_e port, int *sock_fd) memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sun_family = AF_UNIX; - unlink(MC_IPC_PATH[serv_port]); - strncpy(serv_addr.sun_path, MC_IPC_PATH[serv_port], sizeof(serv_addr.sun_path) - 1); + unlink(MC_IPC_PATH); + strncpy(serv_addr.sun_path, MC_IPC_PATH, sizeof(serv_addr.sun_path) - 1); /* Bind to the local address */ for (i = 0; i < 20; i++) { @@ -137,9 +118,9 @@ int mc_ipc_create_server_socket(mc_msg_port_type_e port, int *sock_fd) mc_debug("Listening..."); /*change permission of sock file*/ - if (chmod(MC_IPC_PATH[serv_port], 0660) < 0) + if (chmod(MC_IPC_PATH, 0660) < 0) mc_stderror("chmod failed"); - if (chown(MC_IPC_PATH[serv_port], 200, 5000) < 0) + if (chown(MC_IPC_PATH, 200, 5000) < 0) mc_stderror("chown failed"); *sock_fd = sock; @@ -202,200 +183,3 @@ int mc_ipc_accept_client_tcp(int serv_sock, int *client_sock) return MEDIA_CONTROLLER_ERROR_NONE; } - -gboolean mc_read_db_update_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data) -{ - int sock = -1; - int client_sock = -1; - char *sql_query = NULL; - mc_comm_msg_s recv_msg; - int ret = MEDIA_CONTROLLER_ERROR_NONE; - void *db_handle = (void *)data; - int send_msg = MEDIA_CONTROLLER_ERROR_NONE; - /* gboolean privilege = TRUE; */ - - mc_debug("[GD] mc_read_db_update_tcp_socket is called!!!!!"); - - sock = g_io_channel_unix_get_fd(src); - if (sock < 0) { - mc_error("sock fd is invalid!"); - return TRUE; - } - - /* get client socket fd */ - ret = mc_ipc_accept_client_tcp(sock, &client_sock); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - return TRUE; - } - - ret = mc_ipc_receive_message_tcp(client_sock, &recv_msg); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("ms_ipc_receive_message_tcp failed [%d]", ret); - send_msg = ret; - goto ERROR; - } - - /* check privileage, it is removed for smack rule */ - /* if(__mc_privilege_check(recv_msg.msg, &privilege) != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("invalid query. size[%d]", recv_msg.msg_size); - send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - goto ERROR; - } - - if (privilege == TRUE) { - ret = __mc_privilege_ask(client_sock, "mediacontroller::db", "w"); - if (ret == MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED) { - send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - goto ERROR; - } - }*/ - - sql_query = strndup(recv_msg.msg, recv_msg.msg_size); - if (sql_query != NULL) { - ret = mc_db_util_update_db(db_handle, sql_query); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) - mc_error("media_db_update_db error : %d", ret); - - send_msg = ret; - MC_SAFE_FREE(sql_query); - } else { - send_msg = MEDIA_CONTROLLER_ERROR_OUT_OF_MEMORY; - } - -ERROR: - if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { - mc_stderror("send failed"); - } else { - mc_debug("Sent successfully"); - } - - if (close(client_sock) < 0) { - mc_stderror("close failed"); - } - - return TRUE; -} - -gboolean mc_read_client_set_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data) -{ - int sock = -1; - int client_sock = -1; - mc_comm_msg_s recv_msg; - int ret = MEDIA_CONTROLLER_ERROR_NONE; - int send_msg = MEDIA_CONTROLLER_ERROR_NONE; - int i = 0; - GList *mc_svc_list = (GList *)data; - bool is_duplicated = FALSE; - - sock = g_io_channel_unix_get_fd(src); - if (sock < 0) { - mc_error("sock fd is invalid!"); - return TRUE; - } - - /* get client socket fd */ - ret = mc_ipc_accept_client_tcp(sock, &client_sock); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - return TRUE; - } - - ret = mc_ipc_receive_message_tcp(client_sock, &recv_msg); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("ms_ipc_receive_message_tcp failed [%d]", ret); - send_msg = ret; - goto ERROR; - } - - /* check privileage */ - ret = __mc_privilege_ask(client_sock, "mediacontroller::svc", "w"); - if (ret == MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED) { - mc_error("permission is denied!"); - send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - goto ERROR; - } - - if (mc_svc_list != NULL && g_list_length(mc_svc_list) == 0) { - for (i = 0; i < g_list_length(mc_svc_list); i++) { - char *data = (char *)g_list_nth_data(mc_svc_list, i); - if (strcmp(data, recv_msg.msg) == 0) { - is_duplicated = TRUE; - } - } - if (is_duplicated) { - mc_svc_list = g_list_append(mc_svc_list, recv_msg.msg); - } - } - -ERROR: - if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { - mc_stderror("send failed"); - } else { - mc_debug("Sent successfully"); - } - - if (close(client_sock) < 0) { - mc_stderror("close failed"); - } - - return TRUE; -} - -gboolean mc_read_client_get_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data) -{ - int sock = -1; - int client_sock = -1; - mc_comm_msg_s recv_msg; - int ret = MEDIA_CONTROLLER_ERROR_NONE; - int send_msg = MEDIA_CONTROLLER_ERROR_NONE; - int i = 0; - GList *mc_svc_list = (GList *)data; - - sock = g_io_channel_unix_get_fd(src); - if (sock < 0) { - mc_error("sock fd is invalid!"); - return TRUE; - } - - /* get client socket fd */ - ret = mc_ipc_accept_client_tcp(sock, &client_sock); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - return TRUE; - } - - ret = mc_ipc_receive_message_tcp(client_sock, &recv_msg); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("ms_ipc_receive_message_tcp failed [%d]", ret); - send_msg = ret; - goto ERROR; - } - - /* check privileage */ - ret = __mc_privilege_ask(client_sock, "mediacontroller::svc", "r"); - if (ret == MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED) { - mc_error("permission is denied!"); - send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - goto ERROR; - } - - for (i = 0; i < g_list_length(mc_svc_list); i++) { - send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; - char *data = (char *)g_list_nth_data(mc_svc_list, i); - if (strcmp(data, recv_msg.msg) == 0) { - mc_svc_list = g_list_remove(mc_svc_list, data); - send_msg = MEDIA_CONTROLLER_ERROR_NONE; - } - } - -ERROR: - if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { - mc_stderror("send failed"); - } else { - mc_debug("Sent successfully"); - } - - if (close(client_sock) < 0) { - mc_stderror("close failed"); - } - - return TRUE; -} diff --git a/svc/media_controller_socket.h b/svc/media_controller_socket.h index ea50e07..a17a073 100755 --- a/svc/media_controller_socket.h +++ b/svc/media_controller_socket.h @@ -26,16 +26,16 @@ extern "C" { #endif /* __cplusplus */ #define SERVER_IP "127.0.0.1" - #define MC_TIMEOUT_SEC_10 10 /**< Response from Server time out */ - #define MAX_MSG_SIZE 4096*2 #define MC_SOCK_NOT_ALLOCATE -1 +#define MC_SOCK_ACTIVATION_PATH "/var/run/mediacontroller/media_sa_controller" +#define MC_IPC_PATH "/var/run/mediacontroller/media_ipc_controller" +#define MC_SERVER_CONNECTION_MSG "Connect" +#define MC_SERVER_DISCONNECTION_MSG "Disonnect" typedef enum{ MC_DB_UPDATE_PORT, /**< Media Controller DB Update */ - MC_DB_SET_PORT, - MC_DB_GET_PORT, MC_PORT_MAX, } mc_msg_port_type_e; @@ -43,6 +43,8 @@ typedef enum{ MC_MSG_DB_UPDATE, /**< Media Controller DB Update */ MC_MSG_CLIENT_SET, MC_MSG_CLIENT_GET, + MC_MSG_SERVER_CONNECTION, + MC_MSG_SERVER_DISCONNECTION, MC_MSG_MAX, } mc_msg_type_e; @@ -59,21 +61,12 @@ typedef struct { char msg[MAX_MSG_SIZE]; }mc_comm_msg_s; -const static char MC_IPC_PATH[][50] ={ - {"/tmp/.media_controller_ipc_dbupdate"}, - {"/tmp/.media_controller_ipc_clientset"}, - {"/tmp/.media_controller_ipc_clientget"}, -}; - int mc_ipc_create_client_socket(int timeout_sec, mc_sock_info_s* sock_info); int mc_ipc_delete_client_socket(mc_sock_info_s* sock_info); int mc_ipc_create_server_socket(mc_msg_port_type_e port, int *sock_fd); int mc_ipc_send_msg_to_client_tcp(int sockfd, mc_comm_msg_s *send_msg, struct sockaddr_un *client_addr); int mc_ipc_receive_message_tcp(int client_sock, mc_comm_msg_s *recv_msg); int mc_ipc_accept_client_tcp(int serv_sock, int* client_sock); -gboolean mc_read_db_update_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data); -gboolean mc_read_client_set_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data); -gboolean mc_read_client_get_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data); #ifdef __cplusplus } diff --git a/svc/media_controller_svc.c b/svc/media_controller_svc.c index 4e87abe..55e4905 100755 --- a/svc/media_controller_svc.c +++ b/svc/media_controller_svc.c @@ -14,67 +14,226 @@ * limitations under the License. */ -#include -#include "media_controller_db_util.h" +#include +#include + +#include "media_controller_svc.h" #include "media_controller_private.h" #include "media_controller_socket.h" -#include "media_controller_svc.h" +#include "media_controller_db_util.h" + +static GMainLoop *g_mc_svc_mainloop = NULL; +static int g_connection_cnt = -1; + +static int __mc_privilege_ask(int client_sockfd, const char *type, const char *privilege_object) +{ + int ret = MEDIA_CONTROLLER_ERROR_NONE; +#if 0 + ret = security_server_check_privilege_by_sockfd(client_sockfd, type, privilege_object); + if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { + mc_error("You do not have permission for this operation."); + ret = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; + } else { + mc_debug("PERMISSION OK"); + } +#endif + return ret; +} + +static int __create_socket_activation(void) +{ + int fd = -1; + int listen_fds; + + listen_fds = sd_listen_fds(0); + if (listen_fds == 1) { + fd = SD_LISTEN_FDS_START; + return fd; + } else if (listen_fds > 1) { + mc_error("Too many file descriptors received."); + return -1; + } else { + mc_error("There is no socket stream"); + return -1; + } +} + +gboolean _mc_read_service_request_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data) +{ + int sock = -1; + int client_sock = -1; + char *sql_query = NULL; + mc_comm_msg_s recv_msg; + int ret = MEDIA_CONTROLLER_ERROR_NONE; + int send_msg = MEDIA_CONTROLLER_ERROR_NONE; + bool is_duplicated = FALSE; + unsigned int i = 0; + mc_svc_data_t *mc_svc_data = (mc_svc_data_t*)data; + + mc_debug("mc_read_service_request_tcp_socket is called!!!!!"); + + sock = g_io_channel_unix_get_fd(src); + if (sock < 0) { + mc_error("sock fd is invalid!"); + return TRUE; + } + + /* get client socket fd */ + ret = mc_ipc_accept_client_tcp(sock, &client_sock); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + return TRUE; + } + + ret = mc_ipc_receive_message_tcp(client_sock, &recv_msg); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("mc_ipc_receive_message_tcp failed [%d]", ret); + send_msg = ret; + goto ERROR; + } + + if (recv_msg.msg_type == MC_MSG_DB_UPDATE) { + sql_query = strndup(recv_msg.msg, recv_msg.msg_size); + if (sql_query != NULL) { + ret = mc_db_util_update_db(mc_svc_data->db_handle, sql_query); + if (ret != MEDIA_CONTROLLER_ERROR_NONE) + mc_error("media_db_update_db error : %d", ret); + + send_msg = ret; + MC_SAFE_FREE(sql_query); + } else { + send_msg = MEDIA_CONTROLLER_ERROR_OUT_OF_MEMORY; + } + } else if (recv_msg.msg_type == MC_MSG_CLIENT_SET) { + /* check privileage */ + ret = __mc_privilege_ask(client_sock, "mediacontroller::svc", "w"); + if (ret == MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED) { + mc_error("permission is denied!"); + send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; + goto ERROR; + } + + for (i = 0; i < g_list_length(mc_svc_data->mc_svc_list); i++) { + char *nth_data = (char *)g_list_nth_data(mc_svc_data->mc_svc_list, i); + if (nth_data != NULL && strcmp(nth_data, recv_msg.msg) == 0) { + is_duplicated = TRUE; + } + } + if (!is_duplicated) { + mc_svc_data->mc_svc_list = g_list_append(mc_svc_data->mc_svc_list, recv_msg.msg); + } + } else if (recv_msg.msg_type == MC_MSG_CLIENT_GET) { + /* check privileage */ + ret = __mc_privilege_ask(client_sock, "mediacontroller::svc", "r"); + if (ret == MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED) { + mc_error("permission is denied!"); + send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; + goto ERROR; + } + + for (i = 0; i < g_list_length(mc_svc_data->mc_svc_list); i++) { + send_msg = MEDIA_CONTROLLER_ERROR_PERMISSION_DENIED; + char *nth_data = (char *)g_list_nth_data(mc_svc_data->mc_svc_list, i); + if (nth_data != NULL && strcmp(nth_data, recv_msg.msg) == 0) { + mc_svc_data->mc_svc_list = g_list_remove(mc_svc_data->mc_svc_list, nth_data); + send_msg = MEDIA_CONTROLLER_ERROR_NONE; + } + } + } else if (recv_msg.msg_type == MC_MSG_SERVER_CONNECTION) { + if((recv_msg.msg_size > 0) && (recv_msg.msg != NULL)) { + if (strncmp(recv_msg.msg, MC_SERVER_CONNECTION_MSG, recv_msg.msg_size) == 0) { + if(g_connection_cnt == -1) + g_connection_cnt = 1; + else + g_connection_cnt++; + + mc_error("[No-error] increased connection count [%d]", g_connection_cnt); + + send_msg = MEDIA_CONTROLLER_ERROR_NONE; + } else { + mc_error("Wrong message!"); + send_msg = MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + } else { + mc_error("Wrong message!"); + send_msg = MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + } else if (recv_msg.msg_type == MC_MSG_SERVER_DISCONNECTION) { + if((recv_msg.msg_size > 0) && (recv_msg.msg != NULL)) { + if (strncmp(recv_msg.msg, MC_SERVER_DISCONNECTION_MSG, recv_msg.msg_size) == 0) { + g_connection_cnt--; + mc_error("[No-error] decreased connection count [%d]", g_connection_cnt); + + send_msg = MEDIA_CONTROLLER_ERROR_NONE; + } else { + mc_error("Wrong message!"); + send_msg = MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + } else { + mc_error("Wrong message!"); + send_msg = MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } + } else { + mc_error("Wrong message type!"); + send_msg = MEDIA_CONTROLLER_ERROR_INVALID_OPERATION; + } -GMainLoop *g_mc_svc_mainloop = NULL; -static bool g_mc_svc_thread_ready = FALSE; -static GList *g_mc_svc_list = NULL; +ERROR: + if (write(client_sock, &send_msg, sizeof(send_msg)) != sizeof(send_msg)) { + mc_stderror("send failed"); + } else { + mc_debug("Sent successfully"); + } + + if (close(client_sock) < 0) { + mc_stderror("close failed"); + } + + return TRUE; +} + +int mc_create_socket_activation(void) +{ + return __create_socket_activation(); +} gboolean mc_svc_thread(void *data) { int sockfd = -1; - int sockfd_set = -1; - int sockfd_get = -1; int ret = MEDIA_CONTROLLER_ERROR_NONE; GSource *source = NULL; GIOChannel *channel = NULL; - GSource *source_set = NULL; - GIOChannel *channel_set = NULL; - GSource *source_get = NULL; - GIOChannel *channel_get = NULL; GMainContext *context = NULL; - void *db_handle = NULL; + mc_svc_data_t *mc_svc_data = NULL; - /* Create TCP Socket*/ - ret = mc_ipc_create_server_socket(MC_DB_UPDATE_PORT, &sockfd); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - /* Disconnect DB*/ - mc_error("Failed to create socket"); + mc_svc_data = (mc_svc_data_t *)g_malloc(sizeof(mc_svc_data_t)); + if (mc_svc_data == NULL) { + mc_error("Failed to allocate svc data"); return FALSE; } + memset(mc_svc_data, 0, sizeof(mc_svc_data_t)); - /* Create TCP Socket for set client*/ - ret = mc_ipc_create_server_socket(MC_DB_SET_PORT, &sockfd_set); + /* Create TCP Socket*/ + ret = mc_ipc_create_server_socket(MC_DB_UPDATE_PORT, &sockfd); if (ret != MEDIA_CONTROLLER_ERROR_NONE) { /* Disconnect DB*/ - close(sockfd); mc_error("Failed to create socket"); return FALSE; } - /* Create TCP Socket for get client*/ - ret = mc_ipc_create_server_socket(MC_DB_GET_PORT, &sockfd_get); - if (ret != MEDIA_CONTROLLER_ERROR_NONE) { - /* Disconnect DB*/ + /* Connect Media DB*/ + if (mc_db_util_connect(&(mc_svc_data->db_handle)) != MEDIA_CONTROLLER_ERROR_NONE) { + mc_error("Failed to connect DB"); close(sockfd); - close(sockfd_set); - mc_error("Failed to create socket"); return FALSE; } - /* Connect Media DB*/ - if (mc_db_util_connect(&db_handle) != MEDIA_CONTROLLER_ERROR_NONE) { - mc_error("Failed to connect DB"); + /* Create list for command*/ + mc_svc_data->mc_svc_list = g_list_alloc(); + if (mc_svc_data->mc_svc_list == NULL) { + mc_error("Failed to allocate list"); close(sockfd); - close(sockfd_set); - close(sockfd_get); return FALSE; } - g_mc_svc_list = g_list_alloc(); context = g_main_context_new(); if (context == NULL) { @@ -91,57 +250,50 @@ gboolean mc_svc_thread(void *data) source = g_io_create_watch(channel, G_IO_IN); /* Set callback to be called when socket is readable */ - g_source_set_callback(source, (GSourceFunc)mc_read_db_update_tcp_socket, db_handle, NULL); + g_source_set_callback(source, (GSourceFunc)_mc_read_service_request_tcp_socket, mc_svc_data, NULL); g_source_attach(source, context); - /* Create new channel to watch TCP socket */ - channel_set = g_io_channel_unix_new(sockfd_set); - source_set = g_io_create_watch(channel_set, G_IO_IN); - - /* Set callback to be called when socket is readable */ - g_source_set_callback(source_set, (GSourceFunc)mc_read_client_set_tcp_socket, g_mc_svc_list, NULL); - g_source_attach(source_set, context); - - /* Create new channel to watch TCP socket */ - channel_get = g_io_channel_unix_new(sockfd_get); - source_get = g_io_create_watch(channel_get, G_IO_IN); - - /* Set callback to be called when socket is readable */ - g_source_set_callback(source_get, (GSourceFunc)mc_read_client_get_tcp_socket, g_mc_svc_list, NULL); - g_source_attach(source_get, context); - g_main_context_push_thread_default(context); mc_debug("*******************************************"); mc_debug("*** Media Controller Service thread is running ***"); mc_debug("*******************************************"); - g_mc_svc_thread_ready = TRUE; - g_main_loop_run(g_mc_svc_mainloop); mc_debug("*** Media Controller Service thread will be closed ***"); - g_mc_svc_thread_ready = FALSE; - g_io_channel_shutdown(channel, FALSE, NULL); g_io_channel_unref(channel); - g_io_channel_shutdown(channel_set, FALSE, NULL); - g_io_channel_unref(channel_set); - g_io_channel_shutdown(channel_get, FALSE, NULL); - g_io_channel_unref(channel_get); /* Disconnect DB*/ - mc_db_util_disconnect(db_handle); + mc_db_util_disconnect(mc_svc_data->db_handle); - g_list_free(g_mc_svc_list); + if (mc_svc_data->mc_svc_list != NULL) { + int i = 0; + for (i = g_list_length(mc_svc_data->mc_svc_list) - 1; i >= 0; i--) { + char *nth_data = (char *)g_list_nth_data(mc_svc_data->mc_svc_list, i); + mc_svc_data->mc_svc_list = g_list_remove(mc_svc_data->mc_svc_list, nth_data); + MC_SAFE_FREE(nth_data); + } + g_list_free(mc_svc_data->mc_svc_list); + } + MC_SAFE_FREE(mc_svc_data); /*close socket*/ close(sockfd); - close(sockfd_set); - close(sockfd_get); g_main_loop_unref(g_mc_svc_mainloop); return FALSE; } + +GMainLoop *mc_svc_get_main_loop(void) +{ + return g_mc_svc_mainloop; +} + +int mc_svc_get_connection_cnt(void) +{ + return g_connection_cnt; +} diff --git a/svc/media_controller_svc.h b/svc/media_controller_svc.h index 092c913..7500069 100755 --- a/svc/media_controller_svc.h +++ b/svc/media_controller_svc.h @@ -23,7 +23,15 @@ extern "C" { #endif /* __cplusplus */ +typedef struct { + void *db_handle; + void *mc_svc_list; +} mc_svc_data_t; + +int mc_create_socket_activation(void); gboolean mc_svc_thread(void *data); +GMainLoop *mc_svc_get_main_loop(void); +int mc_svc_get_connection_cnt(void); #ifdef __cplusplus } -- 2.7.4