Fix build error and remove warnings
[platform/core/api/notification.git] / src / notification_old.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyuho Jo <kyuho.jo@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #define _GNU_SOURCE /* for asprintf */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <libintl.h>
29 #include <sys/socket.h>
30 #include <sys/syscall.h>
31 #include <sys/time.h>
32 #include <sys/un.h>
33 #include <bundle_internal.h>
34
35 #include <notification.h>
36 #include <notification_internal.h>
37
38 EXPORT_API int notification_wait_response(notification_h noti,
39                                                            int timeout,
40                                                            int *respi,
41                                                            char **respc)
42 {
43         bundle *b, *bc = NULL;
44         pid_t tid;
45         const char *tid_c;
46         int sock_fd, msg_fd;
47         char *sock_path;
48         struct sockaddr_un sock_addr;
49         char msg_buffer[1024];
50         ssize_t msg_size;
51         struct timeval timeout_tv;
52         char *resp;
53
54         memset(msg_buffer, 0, sizeof(msg_buffer));
55
56     /* a response packet *must* have an execute option TYPE_RESPONDING
57             with an associated bundle.
58             If its bundle does not already contain a "tid" hint (which
59             complex applications such as xwalk may overwrite), we will
60             try to find the TID and set it in the bundle ourselves. */
61         notification_get_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_RESPONDING,
62         NULL, &b);
63
64         if (b == NULL)
65                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
66
67         tid_c = bundle_get_val(b, "tid");
68         if (tid_c == NULL) {
69                 tid = syscall(SYS_gettid);
70                 asprintf((char **)&tid_c, "%d", tid);
71                 bc = bundle_dup(b);
72                 bundle_add(bc, "tid", tid_c);
73                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_RESPONDING,
74                                                  NULL, NULL, bc);
75                 bundle_free(bc);
76                 notification_update(noti);
77         }
78
79         sock_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
80         if (sock_fd == -1)
81                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
82
83         sock_addr.sun_family = AF_UNIX;
84         asprintf(&sock_path, "/tmp/.notification-%s", tid_c);
85         strncpy(sock_addr.sun_path, sock_path, sizeof(sock_addr.sun_path) - 1);
86         if (bind(sock_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == -1) {
87                 close(sock_fd);
88                 free(sock_path);
89                 free((char *)tid_c);
90                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
91         }
92
93         if (timeout > 0) {
94                 timeout_tv.tv_sec = timeout;
95                 timeout_tv.tv_usec = 0;
96                 setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout_tv, sizeof(timeout_tv));
97                 setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout_tv, sizeof(timeout_tv));
98         }
99
100         listen(sock_fd, 1);
101         msg_fd = accept(sock_fd, NULL, 0);
102         do {
103                 msg_size = read(msg_fd, msg_buffer, 1024);
104         } while (msg_size > 0);
105
106         resp = strtok(msg_buffer, "\n");
107         if (resp) {
108                 *respi = atoi(resp);
109                 if (respc != NULL) {
110                         resp = strtok(NULL, "\n");
111                         if (resp)
112                                 *respc = resp;
113                         else
114                                 *respc = NULL;
115                 }
116         } else {
117                 *respi = 0;
118                 if (respc != NULL)
119                         *respc = NULL;
120         }
121
122         close(msg_fd);
123         close(sock_fd);
124         unlink(sock_path);
125         free(sock_path);
126         free((char *)tid_c);
127
128         return NOTIFICATION_ERROR_NONE;
129 }