Canddisp *canddisp_singleton() { if (XimServer::gCandWinStyleUpdated) { terminate_canddisp_connection(); command = NULL; XimServer::gCandWinStyleUpdated = false; } if (!command) command = candwin_command(); if (!candwin_initted && command) { candwin_pid = uim_ipc_open_command(candwin_pid, &candwin_r, &candwin_w, command); if (disp) delete disp; disp = new Canddisp(); int fd = fileno(candwin_r); if (fd != -1) { int flag = fcntl(fd, F_GETFL); if (flag != -1) { flag |= O_NONBLOCK; if (fcntl(fd, F_SETFL, flag) != -1) add_fd_watch(fd, READ_OK, candwin_read_cb); } } candwin_initted = true; } return disp; }
static uim_lisp mana_init(void) { char buf[100]; int fd; int fl; if (mana_pid == 0) mana_pid = uim_ipc_open_command(0, &mana_r, &mana_w, MANA_COMMAND); if (mana_pid == 0) return uim_scm_f(); fd = fileno(mana_r); fl = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, fl | O_NONBLOCK); fgets(buf, sizeof(buf), mana_r); fcntl(fd, F_SETFL, fl); if (feof(mana_r)) { mana_pid = 0; fclose(mana_r); fclose(mana_w); mana_r = mana_w = NULL; uim_notify_fatal(N_("uim-mana: Command 'mana' not found.")); return uim_scm_f(); } if (ferror(mana_r)) clearerr(mana_r); #ifdef DEBUG log = fopen("mana.log", "w"); #endif return uim_scm_t(); }
int uim_helper_init_client_fd(void (*disconnect_cb)(void)) { struct sockaddr_un server; char path[MAXPATHLEN]; FILE *serv_r = NULL, *serv_w = NULL; int fd = -1; uim_fd = -1; if (!uim_helper_get_pathname(path, sizeof(path))) goto error; memset(&server, 0, sizeof(server)); server.sun_family = PF_UNIX; strlcpy(server.sun_path, path, sizeof(server.sun_path)); #ifdef SOCK_CLOEXEC /* linux-2.6.27+ variant that prevents racing on concurrent fork & exec in other thread */ fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); if (fd == -1 && errno == EINVAL) /* fallback to plain SOCK_TYPE on older kernel */ #endif fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { perror("fail to create socket"); goto error; } fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); #ifdef LOCAL_CREDS /* for NetBSD */ /* Set the socket to receive credentials on the next message */ { int on = 1; setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof(on)); } #endif if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0) { pid_t serv_pid = 0; char buf[128]; serv_pid = uim_ipc_open_command(serv_pid, &serv_r, &serv_w, get_server_command()); if (serv_pid == 0) goto error; while (fgets (buf, sizeof(buf), serv_r ) != NULL ) { if (strcmp( buf, "\n" ) == 0) break; } if (connect(fd, (struct sockaddr *)&server,sizeof(server)) < 0) goto error; } if (uim_helper_check_connection_fd(fd)) goto error; if (!uim_read_buf) uim_read_buf = uim_strdup(""); uim_disconnect_cb = disconnect_cb; uim_fd = fd; return fd; error: if (fd != -1) close(fd); if (serv_r) fclose(serv_r); if (serv_w) fclose(serv_w); return -1; }