示例#1
0
int
main(int argc, char **argv)
{
    char path[MAXPATHLEN];
    int server_fd;

    uim_init_error();

    if (!uim_helper_get_pathname(path, sizeof(path)))
        return 0;

    unlink(path);

    clients = NULL;
    nr_client_slots = 0;

    FD_ZERO(&s_fdset_read);
    FD_ZERO(&s_fdset_write);
    s_max_fd = 0;
    server_fd = init_server_fd(path);

    printf("waiting\n\n");
    fflush(stdout);

    fclose(stdin);
    fclose(stdout);

    if (server_fd < 0)
        return 0;

    /*  fprintf(stderr,"Waiting for connection at %s\n", path);*/

    signal(SIGPIPE, SIG_IGN);
    uim_helper_server_process_connection(server_fd);

    return 0;
}
示例#2
0
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;
}