2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <livebox-errno.h>
25 #include <sys/smack.h>
27 #include <security-server.h>
29 #include "service_common.h"
35 Eina_List *context_list;
36 struct service_context *svc_ctx;
38 .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
39 .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
50 static inline int put_reply_context(struct tcb *tcb, double seq)
54 ctx = malloc(sizeof(*ctx));
56 ErrPrint("Heap: %s\n", strerror(errno));
61 ctx->seq = seq; /* Could we this sequence value is uniq? */
63 s_info.context_list = eina_list_append(s_info.context_list, ctx);
70 static inline struct tcb *get_reply_context(double seq)
78 EINA_LIST_FOREACH_SAFE(s_info.context_list, l, n, ctx) {
82 s_info.context_list = eina_list_remove(s_info.context_list, ctx);
94 static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
99 DbgPrint("TCB: %p is terminated (NIL packet)\n", tcb);
103 command = packet_command(packet);
105 ErrPrint("Invalid command\n");
109 switch (packet_type(packet)) {
111 /* Need to send reply packet */
112 DbgPrint("%p REQ: Command: [%s]\n", tcb, command);
113 if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0)
114 ErrPrint("Unable to send service request packet\n");
116 (void)put_reply_context(tcb, packet_seq(packet));
118 case PACKET_REQ_NOACK:
119 /* Doesn't need to send reply packet */
120 DbgPrint("%p REQ_NOACK: Command: [%s]\n", tcb, command);
121 if (!strcmp(command, "service_register")) {
122 tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
126 if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0)
127 ErrPrint("Unable to send service reuqest packet\n");
130 /* Okay, client(or app) send a reply packet to us. */
131 DbgPrint("%p ACK: Command: [%s]\n", tcb, command);
132 tcb = get_reply_context(packet_seq(packet));
134 ErrPrint("There is no proper context\n");
138 if (service_common_unicast_packet(tcb, packet) < 0)
139 ErrPrint("Unable to send reply packet\n");
142 ErrPrint("Packet type is not valid[%s]\n", command);
147 * return value has no meanning,
148 * it will be printed by dlogutil.
156 * Do not try to do anyother operation in these functions
159 HAPI int shortcut_service_init(void)
161 if (s_info.svc_ctx) {
162 ErrPrint("Already initialized\n");
163 return LB_STATUS_ERROR_ALREADY;
166 s_info.svc_ctx = service_common_create(SHORTCUT_SOCKET, service_thread_main, NULL);
167 if (!s_info.svc_ctx) {
168 ErrPrint("Unable to activate service thread\n");
169 return LB_STATUS_ERROR_FAULT;
172 if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), SHORTCUT_SMACK_LABEL, SMACK_LABEL_IPOUT) != 0) {
173 if (errno != EOPNOTSUPP) {
174 ErrPrint("Unable to set SMACK label(%d)\n", errno);
175 service_common_destroy(s_info.svc_ctx);
176 s_info.svc_ctx = NULL;
177 return LB_STATUS_ERROR_FAULT;
181 if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), SHORTCUT_SMACK_LABEL, SMACK_LABEL_IPIN) != 0) {
182 if (errno != EOPNOTSUPP) {
183 ErrPrint("Unable to set SMACK label(%d)\n", errno);
184 service_common_destroy(s_info.svc_ctx);
185 s_info.svc_ctx = NULL;
186 return LB_STATUS_ERROR_FAULT;
190 DbgPrint("Successfully initiated\n");
191 return LB_STATUS_SUCCESS;
194 HAPI int shortcut_service_fini(void)
197 return LB_STATUS_ERROR_INVALID;
199 service_common_destroy(s_info.svc_ctx);
200 s_info.svc_ctx = NULL;
201 DbgPrint("Successfully Finalized\n");
202 return LB_STATUS_SUCCESS;