Use the same type to compare values
[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 #include <errno.h>
23
24 #include <download-provider-log.h>
25 #include <download-provider-client.h>
26 #include <download-provider-client-manager.h>
27 #include <download-provider-ipc.h>
28
29 static char *__dp_notify_get_path(pid_t pid)
30 {
31         size_t path_size = sizeof(NOTIFY_DIR) + 21;
32         char *notify_fifo = (char *)calloc(path_size, sizeof(char));
33         if (notify_fifo == NULL) {
34                 TRACE_ERROR("failed to alocalte fifo path pid:%d", (int)pid);
35                 return NULL;
36         }
37         if (snprintf(notify_fifo, path_size, "%s/%d", NOTIFY_DIR, pid) < 0) {
38                 TRACE_ERROR("failed to make fifo path pid:%d", (int)pid);
39                 free(notify_fifo);
40                 return NULL;
41         }
42         return notify_fifo;
43 }
44
45 int dp_notify_init(pid_t pid)
46 {
47         char *notify_fifo = __dp_notify_get_path(pid);
48         if (notify_fifo == NULL)
49                 return -1;
50         int notify_fd = -1;
51
52         if (unlink(notify_fifo) < 0)
53                 TRACE_ERROR("%s might not be exist. Ignored. errno(%d)", notify_fifo, errno);
54
55         if (mkfifo(notify_fifo, 0644/*-rwrr*/) < 0)
56                 TRACE_ERROR("failed to make fifo %s", notify_fifo);
57         else
58                 notify_fd = open(notify_fifo, O_RDWR | O_NONBLOCK, 0644);
59
60         free(notify_fifo);
61         return notify_fd;
62 }
63
64 void dp_notify_deinit(pid_t pid)
65 {
66         char *notify_fifo = __dp_notify_get_path(pid);
67         if (notify_fifo == NULL)
68                 return ;
69
70         if (unlink(notify_fifo) < 0)
71                 TRACE_ERROR("%s might not be exist. Ignored. errno(%d)", notify_fifo, errno);
72
73         free(notify_fifo);
74 }
75
76 static int __dp_notify_feedback(int sock, int id, int state, int errorcode, unsigned long long received_size)
77 {
78         dp_ipc_event_fmt eventinfo;
79         memset(&eventinfo, 0x00, sizeof(dp_ipc_event_fmt));
80         eventinfo.id = id;
81         eventinfo.state = state;
82         eventinfo.errorcode = errorcode;
83         eventinfo.received_size = received_size;
84         if (dp_ipc_write(sock, &eventinfo, sizeof(dp_ipc_event_fmt)) < 0) {
85                 // failed to read from socket // ignore this status
86                 return -1;
87         }
88         return 0;
89 }
90
91 int dp_notify_feedback(int sock, void *slot, int id, int state, int errorcode, unsigned long long received_size)
92 {
93         if (__dp_notify_feedback(sock, id, state, errorcode, received_size) < 0) {
94                 TRACE_ERROR("notify failure by IO_ERROR");
95                 if (slot != NULL) {
96                         dp_client_slots_fmt *base_slot = slot;
97                         if (base_slot->client.notify >= 0)
98                                 close(base_slot->client.notify);
99                         base_slot->client.notify = -1;
100                         dp_notify_deinit(base_slot->credential.pid);
101                         TRACE_ERROR("disable notify channel by IO_ERROR");
102                 }
103                 return -1;
104         }
105         return 0;
106 }