4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #define _GNU_SOURCE /* for asprintf */
29 #include <sys/socket.h>
30 #include <sys/syscall.h>
33 #include <bundle_internal.h>
35 #include <notification.h>
36 #include <notification_internal.h>
38 EXPORT_API int notification_wait_response(notification_h noti,
43 bundle *b, *bc = NULL;
48 struct sockaddr_un sock_addr;
49 char msg_buffer[1024];
51 struct timeval timeout_tv;
54 memset(msg_buffer, 0, sizeof(msg_buffer));
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,
65 return NOTIFICATION_ERROR_INVALID_PARAMETER;
67 tid_c = bundle_get_val(b, "tid");
69 tid = syscall(SYS_gettid);
70 asprintf((char **)&tid_c, "%d", tid);
72 bundle_add(bc, "tid", tid_c);
73 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_RESPONDING,
76 notification_update(noti);
79 sock_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
81 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
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) {
90 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
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));
101 msg_fd = accept(sock_fd, NULL, 0);
103 msg_size = read(msg_fd, msg_buffer, 1024);
104 } while (msg_size > 0);
106 resp = strtok(msg_buffer, "\n");
110 resp = strtok(NULL, "\n");
128 return NOTIFICATION_ERROR_NONE;