4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <pims-ipc-svc.h>
31 #include "ctsvc_internal.h"
32 #include "ctsvc_db_init.h"
33 #include "ctsvc_db_schema.h"
34 #include "ctsvc_schema_recovery.h"
35 #include "ctsvc_server_socket.h"
36 #include "ctsvc_server_utils.h"
37 #include "ctsvc_server_bg.h"
38 #include "ctsvc_server_update.h"
39 #include "ctsvc_server_service.h"
41 #include "ctsvc_db_access_control.h"
43 #include "ctsvc_ipc_define.h"
44 #include "ctsvc_ipc_server.h"
45 #include "ctsvc_ipc_server2.h"
46 #include "ctsvc_notify.h"
48 #define CTSVC_TIMEOUT_FOR_DEFAULT 0
50 static int ctsvc_timeout_sec = CTSVC_TIMEOUT_FOR_DEFAULT;
51 static GMainLoop *main_loop = NULL;
53 static int __server_main(void)
57 char sock_file[CTSVC_PATH_MAX_LEN] = {0};
58 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s", getuid(), CTSVC_IPC_SERVICE);
59 pims_ipc_svc_init(sock_file, CTS_SECURITY_FILE_GROUP, 0777);
63 * register handle functions
64 * These functions will be called when requesting from client module depends on module name and function name (pims_ipc_call, ctsvc_ipc_call)
65 * pims_ipc_svc_register(MODULE_NAME, FUNCTION_NAME ...);
67 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CONNECT, ctsvc_ipc_server_connect, NULL) != 0) break;
68 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_DISCONNECT, ctsvc_ipc_server_disconnect, NULL) != 0) break;
69 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CHECK_PERMISSION, ctsvc_ipc_server_check_permission, NULL) != 0) break;
71 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORD, ctsvc_ipc_server_db_insert_record, NULL) != 0) break;
72 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORD, ctsvc_ipc_server_db_get_record, NULL) != 0) break;
73 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORD, ctsvc_ipc_server_db_update_record, NULL) != 0) break;
74 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORD, ctsvc_ipc_server_db_delete_record, NULL) != 0) break;
75 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_REPLACE_RECORD, ctsvc_ipc_server_db_replace_record, NULL) != 0) break;
76 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_ALL_RECORDS, ctsvc_ipc_server_db_get_all_records, NULL) != 0) break;
77 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORDS_WITH_QUERY, ctsvc_ipc_server_db_get_records_with_query, NULL) != 0) break;
78 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT, ctsvc_ipc_server_db_get_count, NULL) != 0) break;
79 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT_WITH_QUERY, ctsvc_ipc_server_db_get_count_with_query, NULL) != 0) break;
80 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORDS, ctsvc_ipc_server_db_insert_records, NULL) != 0) break;
81 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORDS, ctsvc_ipc_server_db_update_records, NULL) != 0) break;
82 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORDS, ctsvc_ipc_server_db_delete_records, NULL) != 0) break;
83 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_REPLACE_RECORDS, ctsvc_ipc_server_db_replace_records, NULL) != 0) break;
84 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_CHANGES_BY_VERSION, ctsvc_ipc_server_db_get_changes_by_version, NULL) != 0) break;
85 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_CURRENT_VERSION, ctsvc_ipc_server_db_get_current_version, NULL) != 0) break;
86 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS, ctsvc_ipc_server_db_search_records, NULL) != 0) break;
87 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_RANGE, ctsvc_ipc_server_db_search_records_with_range, NULL) != 0) break;
88 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_QUERY, ctsvc_ipc_server_db_search_records_with_query, NULL) != 0) break;
89 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_STATUS, ctsvc_ipc_server_db_get_status, NULL) != 0) break;
91 if (pims_ipc_svc_register(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_CONTACT_ID, ctsvc_ipc_activity_delete_by_contact_id, NULL) != 0) break;
92 if (pims_ipc_svc_register(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_ACCOUNT_ID, ctsvc_ipc_activity_delete_by_account_id, NULL) != 0) break;
94 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_ADD_CONTACT, ctsvc_ipc_group_add_contact, NULL) != 0) break;
95 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, ctsvc_ipc_group_remove_contact, NULL) != 0) break;
96 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, ctsvc_ipc_group_set_group_order, NULL) != 0) break;
98 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_LINK_PERSON, ctsvc_ipc_person_link_person, NULL) != 0) break;
99 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_UNLINK_CONTACT, ctsvc_ipc_person_unlink_contact, NULL) != 0) break;
100 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_RESET_USAGE, ctsvc_ipc_person_reset_usage, NULL) != 0) break;
101 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_FAVORITE_ORDER, ctsvc_ipc_person_set_favorite_order, NULL) != 0) break;
102 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_DEFAULT_PROPERTY, ctsvc_ipc_person_set_default_property, NULL) != 0) break;
103 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_GET_DEFAULT_PROPERTY, ctsvc_ipc_person_get_default_property, NULL) != 0) break;
105 #ifdef ENABLE_LOG_FEATURE
106 if (pims_ipc_svc_register(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_RESET_STATISTICS, ctsvc_ipc_phone_log_reset_statistics, NULL) != 0) break;
107 if (pims_ipc_svc_register(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_DELETE, ctsvc_ipc_phone_log_delete, NULL) != 0) break;
108 #endif /* ENABLE_LOG_FEATURE */
110 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_DISPLAY_ORDER, ctsvc_ipc_setting_get_name_display_order, NULL) != 0) break;
111 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_DISPLAY_ORDER, ctsvc_ipc_setting_set_name_display_order, NULL) != 0) break;
112 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_SORTING_ORDER, ctsvc_ipc_setting_get_name_sorting_order, NULL) != 0) break;
113 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_SORTING_ORDER, ctsvc_ipc_setting_set_name_sorting_order, NULL) != 0) break;
115 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s_for_subscribe", getuid(), CTSVC_IPC_SERVICE);
116 pims_ipc_svc_init_for_publish(sock_file, CTS_SECURITY_FILE_GROUP, 0660);
117 ctsvc_noti_publish_socket_initialize();
119 ret = ctsvc_connect();
120 if (CONTACTS_ERROR_NONE != ret) {
121 CTS_ERR("contacts_connect fail(%d)", ret);
124 ctsvc_set_client_access_info(NULL, NULL);
126 ctsvc_server_bg_add_cb();
127 ctsvc_server_bg_delete_start();
129 ret = ctsvc_server_init_configuration();
132 main_loop = g_main_loop_new(NULL, FALSE);
134 pims_ipc_svc_run_main_loop(main_loop);
136 ctsvc_server_final_configuration();
138 ctsvc_server_bg_remove_cb();
140 ctsvc_unset_client_access_info();
142 ret = ctsvc_disconnect();
143 if (CONTACTS_ERROR_NONE != ret)
146 pims_ipc_svc_deinit_for_publish();
148 pims_ipc_svc_deinit();
154 CTS_ERR("pims_ipc_svc_register error");
158 void ctsvc_server_quit(void)
160 g_main_loop_quit(main_loop);
164 int ctsvc_server_get_timeout_sec(void)
166 CTS_DBG("ctsvc_timeout_sec:%d", ctsvc_timeout_sec);
167 return ctsvc_timeout_sec;
170 #define CTSVC_SECURITY_FILE_GROUP 6005
171 void ctsvc_create_file_set_permission(const char* file, mode_t mode)
174 fd = creat(file, mode);
177 ret = fchown(fd, -1, CTSVC_SECURITY_FILE_GROUP);
180 printf("Fail to fchown\n");
188 void ctsvc_create_rep_set_permission(const char* directory, mode_t mode)
190 if (-1 == access (directory, F_OK)) {
191 mkdir(directory, mode);
195 int main(int argc, char *argv[])
198 INFO("Start contacts-service");
201 if (getuid() == 0) { /* root */
202 gid_t glist[] = {CTS_SECURITY_FILE_GROUP};
203 ret = setgroups(1, glist); /* client and server should have same Groups */
204 WARN_IF(ret <0, "setgroups Fail(%d)", ret);
207 if (2 <= argc && STRING_EQUAL == strcmp(argv[1], "timeout"))
208 ctsvc_timeout_sec = atoi(argv[2]);
210 ctsvc_server_check_schema();
212 ctsvc_create_rep_set_permission(DATA_REPERTORY, 0755);
213 ctsvc_create_rep_set_permission(CTSVC_NOTI_REPERTORY, 0775);
214 ctsvc_create_rep_set_permission(CTSVC_NOTI_IMG_REPERTORY, 0750);
215 ctsvc_create_rep_set_permission(CTSVC_VCARD_IMAGE_LOCATION, 0770);
216 ctsvc_create_rep_set_permission(CTS_MY_IMAGE_LOCATION, 0750);
217 ctsvc_create_rep_set_permission(CTS_GROUP_IMAGE_LOCATION, 0750);
218 ctsvc_create_rep_set_permission(CTS_LOGO_IMAGE_LOCATION, 0750);
219 ctsvc_create_rep_set_permission(CTSVC_CONTACT_IMG_FULL_LOCATION, 0750);
221 ctsvc_create_file_set_permission(CTSVC_NOTI_IPC_READY, 0660);
222 ctsvc_create_file_set_permission(CTSVC_NOTI_ADDRESSBOOK_CHANGED, 0660);
223 ctsvc_create_file_set_permission(CTSVC_NOTI_GROUP_CHANGED, 0660);
224 ctsvc_create_file_set_permission(CTSVC_NOTI_PERSON_CHANGED, 0660);
225 ctsvc_create_file_set_permission(CTSVC_NOTI_CONTACT_CHANGED, 0660);
226 ctsvc_create_file_set_permission(CTSVC_NOTI_MY_PROFILE_CHANGED, 0660);
227 ctsvc_create_file_set_permission(CTSVC_NOTI_NAME_CHANGED, 0660);
228 ctsvc_create_file_set_permission(CTSVC_NOTI_NUMBER_CHANGED, 0660);
229 ctsvc_create_file_set_permission(CTSVC_NOTI_EMAIL_CHANGED, 0660);
230 ctsvc_create_file_set_permission(CTSVC_NOTI_EVENT_CHANGED, 0660);
231 ctsvc_create_file_set_permission(CTSVC_NOTI_URL_CHANGED, 0660);
232 ctsvc_create_file_set_permission(CTSVC_NOTI_GROUP_RELATION_CHANGED, 0660);
233 ctsvc_create_file_set_permission(CTSVC_NOTI_ADDRESS_CHANGED, 0660);
234 ctsvc_create_file_set_permission(CTSVC_NOTI_NOTE_CHANGED, 0660);
235 ctsvc_create_file_set_permission(CTSVC_NOTI_COMPANY_CHANGED, 0660);
236 ctsvc_create_file_set_permission(CTSVC_NOTI_RELATIONSHIP_CHANGED, 0660);
237 ctsvc_create_file_set_permission(CTSVC_NOTI_IMAGE_CHANGED, 0660);
238 ctsvc_create_file_set_permission(CTSVC_NOTI_NICKNAME_CHANGED, 0660);
239 ctsvc_create_file_set_permission(CTSVC_NOTI_MESSENGER_CHANGED, 0660);
240 ctsvc_create_file_set_permission(CTSVC_NOTI_DATA_CHANGED, 0660);
241 ctsvc_create_file_set_permission(CTSVC_NOTI_SDN_CHANGED, 0660);
242 ctsvc_create_file_set_permission(CTSVC_NOTI_PROFILE_CHANGED, 0660);
243 ctsvc_create_file_set_permission(CTSVC_NOTI_ACTIVITY_CHANGED, 0660);
244 ctsvc_create_file_set_permission(CTSVC_NOTI_ACTIVITY_PHOTO_CHANGED, 0660);
245 ctsvc_create_file_set_permission(CTSVC_NOTI_PHONELOG_CHANGED, 0660);
246 ctsvc_create_file_set_permission(CTSVC_NOTI_SPEEDDIAL_CHANGED, 0660);
248 // update DB for compatability
249 ctsvc_server_db_update();
251 ctsvc_server_load_feature_list();
252 ret = ctsvc_server_socket_init();
257 ctsvc_server_socket_deinit();