c40245c888aee6c7214f940600e777633d152602
[platform/framework/web/download-provider.git] / provider / download-provider-notify.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22
23 #include <download-provider-log.h>
24 #include <download-provider-client.h>
25 #include <download-provider-client-manager.h>
26 #include <download-provider-ipc.h>
27
28 static char *__dp_notify_get_path(pid_t pid)
29 {
30         size_t path_size = sizeof(NOTIFY_DIR) + 21;
31         char *notify_fifo = (char *)calloc(path_size, sizeof(char));
32         if (notify_fifo == NULL) {
33                 TRACE_ERROR("failed to alocalte fifo path pid:%d", (int)pid);
34                 return NULL;
35         }
36         if (snprintf(notify_fifo, path_size,"%s/%d", NOTIFY_DIR, pid) < 0) {
37                 TRACE_ERROR("failed to make fifo path pid:%d", (int)pid);
38                 free(notify_fifo);
39                 return NULL;
40         }
41         return notify_fifo;
42 }
43
44 int dp_notify_init(pid_t pid)
45 {
46         char *notify_fifo = __dp_notify_get_path(pid);
47         if (notify_fifo == NULL)
48                 return -1;
49         int notify_fd = -1;
50         struct stat fifo_state;
51         if (stat(notify_fifo, &fifo_state) == 0) // found
52                 unlink(notify_fifo);
53         if (mkfifo(notify_fifo, 0644/*-rwrr*/) < 0) {
54                 TRACE_ERROR("failed to make fifo %s", notify_fifo);
55         } else {
56                 notify_fd = open(notify_fifo, O_RDWR | O_NONBLOCK, 0644);
57         }
58         free(notify_fifo);
59         return notify_fd;
60 }
61
62 void dp_notify_deinit(pid_t pid)
63 {
64         char *notify_fifo = __dp_notify_get_path(pid);
65         if (notify_fifo == NULL)
66                 return ;
67         struct stat fifo_state;
68         if (stat(notify_fifo, &fifo_state) == 0) // found
69                 unlink(notify_fifo);
70         free(notify_fifo);
71 }
72
73 static int __dp_notify_feedback(int sock, int id, int state, int errorcode, unsigned long long received_size)
74 {
75         dp_ipc_event_fmt eventinfo;
76         memset(&eventinfo, 0x00, sizeof(dp_ipc_event_fmt));
77         eventinfo.id = id;
78         eventinfo.state = state;
79         eventinfo.errorcode = errorcode;
80         eventinfo.received_size = received_size;
81         if (dp_ipc_write(sock, &eventinfo, sizeof(dp_ipc_event_fmt)) < 0) {
82                 // failed to read from socket // ignore this status
83                 return -1;
84         }
85         return 0;
86 }
87
88 int dp_notify_feedback(int sock, void *slot, int id, int state, int errorcode, unsigned long long received_size)
89 {
90         if (__dp_notify_feedback(sock, id, state, errorcode, received_size) < 0) {
91                 TRACE_ERROR("notify failure by IO_ERROR");
92                 if (slot != NULL) {
93                         dp_client_slots_fmt *base_slot = slot;
94                         if (base_slot->client.notify >= 0)
95                                 close(base_slot->client.notify);
96                         base_slot->client.notify = -1;
97                         dp_notify_deinit(base_slot->credential.pid);
98                         TRACE_ERROR("disable notify channel by IO_ERROR");
99                 }
100                 return -1;
101         }
102         return 0;
103 }