47
48
#include <signal.h>
49
52
#include "uim-internal.h"
50
53
#include "uim-helper.h"
54
#include "uim-posix.h"
57
* uim-notify is disabled since I'm not confident about:
58
* 1. its stability when low-level error handling is being involved
59
* 2. whether these errors should be notified to endusers
60
* -- 2008-01-15 YamaKen
63
#define USE_UIM_NOTIFY 0
65
#if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG
66
#include "uim-notify.h"
54
70
typedef void (*sig_t)(int);
93
109
sig_t old_sigpipe;
112
if (UIM_CATCH_ERROR_BEGIN())
117
uim_fatal_error("uim_helper_send_message(): invaid fd");
119
uim_fatal_error("uim_helper_send_message(): NULL message");
121
/* The condition fd < 0 ordinarily occurs. */
96
122
if (fd < 0 || !message)
99
126
len = strlen(message) + 1;
100
buf = malloc(len + 1);
127
buf = uim_malloc(len + 1);
101
128
snprintf(buf, len + 1, "%s\n", message);
103
130
old_sigpipe = signal(SIGPIPE, SIG_IGN);
108
135
if ((res = write(fd, bufp, out_len)) < 0) {
109
136
if (errno == EAGAIN || errno == EINTR)
111
fprintf(stderr, "uim_helper_send_message(): unknown error\n");
138
perror("uim_helper_send_message(): unhandled error");
119
146
signal(SIGPIPE, old_sigpipe);
148
UIM_CATCH_ERROR_END();
124
uim_helper_get_pathname(void)
128
struct passwd *pw = NULL;
130
if (!uim_issetugid()) {
131
login = getenv("LOGNAME");
135
pw = getpwuid(getuid());
136
login = strdup(pw->pw_name);
139
path = (char *)malloc(strlen(login)+ 20);
140
sprintf(path, "/tmp/uimhelper-%s",login);
154
check_dir(const char *dir)
158
if (stat(dir, &st) < 0)
159
return (mkdir(dir, 0700) < 0) ? UIM_FALSE : UIM_TRUE;
161
mode_t mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
162
return ((st.st_mode & mode) == mode) ? UIM_TRUE : UIM_FALSE;
167
uim_helper_get_pathname(char *helper_path, int len)
174
if (UIM_CATCH_ERROR_BEGIN())
177
pw = getpwuid(getuid());
183
if (strlcpy(helper_path, pw->pw_dir, len) >= (size_t)len) {
187
if (strlcat(helper_path, "/.uim.d", len) >= (size_t)len) {
193
/* check ~/.uim.d/ */
194
if (!check_dir(helper_path))
197
/* check ~/.uim.d/socket/ */
198
if (strlcat(helper_path, "/socket", len) >= (size_t)len)
201
if (!check_dir(helper_path))
204
if (strlcat(helper_path, "/uim-helper", len) >= (size_t)len)
207
UIM_CATCH_ERROR_END();
212
uim_fatal_error("uim_helper_get_pathname()");
213
helper_path[0] = '\0';
215
UIM_CATCH_ERROR_END();
220
uim_helper_check_connection_fd(int fd)
224
if (getpeereid(fd, &euid, &egid) < 0) {
225
#if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG
226
uim_notify_fatal("uim_helper: %s", strerror(errno));
228
perror("getpeereid failed");
232
if ((euid != 0) && (euid != getuid())) {
233
#if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG
234
uim_notify_fatal("uim_helper: uid mismatch");
236
fprintf(stderr, "uid mismatch\n");
147
243
int uim_helper_fd_readable(int fd)
176
272
buf_size = strlen(buf);
177
273
extended_size = buf_size + fragment_size + 1;
178
buf = (char *)realloc(buf, extended_size);
274
buf = realloc(buf, extended_size);
180
276
memcpy(&buf[buf_size], fragment, fragment_size);
181
277
buf[extended_size - 1] = '\0';
199
295
char *msg, *msg_term;
297
if (UIM_CATCH_ERROR_BEGIN())
201
300
msg_term = strstr(buf, "\n\n");
203
302
msg_size = msg_term + 2 - buf;
204
msg = (char *)malloc(msg_size + 1);
303
msg = uim_malloc(msg_size + 1);
205
304
memcpy(msg, buf, msg_size);
206
305
msg[msg_size] = '\0';
207
306
uim_helper_buffer_shift(buf, msg_size);
311
UIM_CATCH_ERROR_END();
213
316
/* Public API for uim_issetugid(). */