1 /*
2
3 Copyright (c) 2003-2013 uim Project https://github.com/uim/uim
4
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. Neither the name of authors nor the names of its contributors
17 may be used to endorse or promote products derived from this software
18 without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 SUCH DAMAGE.
31
32 */
33 #include <config.h>
34
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/param.h>
38 #include <sys/un.h>
39 #include <netinet/in.h>
40 #include <netdb.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48
49 #ifdef HAVE_STRINGS_H
50 #include <strings.h>
51 #endif
52
53 #include "uim.h"
54 #include "uim-helper.h"
55 #include "uim-internal.h"
56 #include "uim-util.h"
57
58
59 #define RECV_BUFFER_SIZE 1024
60
61 /*Common buffer for some functions's temporary buffer.
62 Pay attention for use.*/
63 static char uim_recv_buf[RECV_BUFFER_SIZE];
64 static char *uim_read_buf;
65
66 static int uim_fd = -1;
67 static void (*uim_disconnect_cb)(void);
68
69
70 static char *
get_server_command(void)71 get_server_command(void)
72 {
73 return UIM_LIBEXECDIR "/uim-helper-server";
74 }
75
uim_helper_init_client_fd(void (* disconnect_cb)(void))76 int uim_helper_init_client_fd(void (*disconnect_cb)(void))
77 {
78 struct sockaddr_un server;
79 char path[MAXPATHLEN];
80 FILE *serv_r = NULL, *serv_w = NULL;
81 int fd = -1;
82
83 uim_fd = -1;
84
85 if (!uim_helper_get_pathname(path, sizeof(path)))
86 goto error;
87
88 memset(&server, 0, sizeof(server));
89 server.sun_family = PF_UNIX;
90 strlcpy(server.sun_path, path, sizeof(server.sun_path));
91
92 #ifdef SOCK_CLOEXEC
93 /* linux-2.6.27+ variant that prevents racing on concurrent fork & exec in other thread */
94 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
95 if (fd == -1 && errno == EINVAL)
96 /* fallback to plain SOCK_TYPE on older kernel */
97 #endif
98 fd = socket(PF_UNIX, SOCK_STREAM, 0);
99 if (fd < 0) {
100 perror("fail to create socket");
101 goto error;
102 }
103 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
104
105 #ifdef LOCAL_CREDS /* for NetBSD */
106 /* Set the socket to receive credentials on the next message */
107 {
108 int on = 1;
109 setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on));
110 }
111 #endif
112
113
114 if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0) {
115 pid_t serv_pid = 0;
116 char buf[128];
117
118 serv_pid = uim_ipc_open_command(serv_pid, &serv_r, &serv_w,
119 get_server_command());
120
121 if (serv_pid == 0)
122 goto error;
123
124 while (fgets (buf, sizeof(buf), serv_r ) != NULL ) {
125 if (strcmp( buf, "\n" ) == 0)
126 break;
127 }
128
129 if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0)
130 goto error;
131 }
132
133 if (uim_helper_check_connection_fd(fd))
134 goto error;
135
136 if (!uim_read_buf)
137 uim_read_buf = uim_strdup("");
138 uim_disconnect_cb = disconnect_cb;
139 uim_fd = fd;
140
141 return fd;
142
143 error:
144 if (fd != -1)
145 close(fd);
146
147 if (serv_r)
148 fclose(serv_r);
149
150 if (serv_w)
151 fclose(serv_w);
152
153 return -1;
154 }
155
156 void
uim_helper_close_client_fd(int fd)157 uim_helper_close_client_fd(int fd)
158 {
159 if (fd != -1)
160 close(fd);
161
162 if (uim_disconnect_cb)
163 uim_disconnect_cb();
164
165 uim_fd = -1;
166 }
167
168 void
uim_helper_client_focus_in(uim_context uc)169 uim_helper_client_focus_in(uim_context uc)
170 {
171 if (uc)
172 uim_helper_send_message(uc->uim_fd != -1 ? uc->uim_fd : uim_fd, "focus_in\n");
173 }
174
175 void
uim_helper_client_focus_out(uim_context uc)176 uim_helper_client_focus_out(uim_context uc)
177 {
178 if (uc)
179 uim_helper_send_message(uc->uim_fd != -1 ? uc->uim_fd : uim_fd, "focus_out\n");
180 }
181
182 void
uim_helper_client_get_prop_list(void)183 uim_helper_client_get_prop_list(void)
184 {
185 uim_helper_send_message(uim_fd, "prop_list_get\n");
186 }
187
188 void
uim_helper_read_proc(int fd)189 uim_helper_read_proc(int fd)
190 {
191 int rc;
192
193 while (uim_helper_fd_readable(fd) > 0) {
194 rc = read(fd, uim_recv_buf, sizeof(uim_recv_buf));
195 if (rc == 0 || (rc == -1 && errno != EAGAIN)) {
196 uim_helper_close_client_fd(fd);
197 return;
198 } else if (rc > 0) {
199 uim_read_buf = uim_helper_buffer_append(uim_read_buf, uim_recv_buf, rc);
200 }
201 }
202 }
203
204 char *
uim_helper_get_message(void)205 uim_helper_get_message(void)
206 {
207 return uim_helper_buffer_get_message(uim_read_buf);
208 }
209