/* atende os clientes */ void *unix_loop (void *arg) { fd_set fds; int fd, cfd, len; struct timeval tv; struct sockaddr_un s; fd = server_fd; for (;;) { FD_ZERO (&fds); FD_SET (fd, &fds); tv.tv_sec = 1; tv.tv_usec = 0; if (select (fd + 1, &fds, NULL, NULL, &tv)) { thread_lock (); len = sizeof (s); memset (&s, 0, sizeof (s)); if ((cfd = accept (fd, (struct sockaddr *) &s, &len)) < 0) perror ("accept"), exit (1); /* trata os comandos */ unix_client (cfd); shutdown (cfd, 2); close (cfd); thread_unlock (); } } return NULL; }
int Proxy_listen(Proxy *conn, ProxyClientType type, bstring name) { switch(type) { case PROXY_CLIENT_DOMAIN: check(name, "DOMAIN client IO specified but NULL given for the name."); log(INFO, "Attaching to listener server socket on %s.", bdata(name)); conn->client.read_fd = conn->client.write_fd = unix_client(name); break; case PROXY_CLIENT_FIFO: check(name, "FIFO client IO specified but NULL given for the name."); bstring in_file = bformat("%s.in", bdata(name)); bstring out_file = bformat("%s.out", bdata(name)); check(!mkfifo((const char *)in_file->data, 0600), "Failed to create input fifo."); check(!mkfifo((const char *)out_file->data, 0600), "Failed to create output fifo."); log(INFO, "Opening %s for the read FIFO end.", bdata(in_file)); conn->client.read_fd = open((const char *)in_file->data, O_RDONLY); check(conn->client.read_fd >= 0, "Failed to open read end of FIFO."); log(INFO, "Opening %s for the write FIFO end.", bdata(out_file)); conn->client.write_fd = open((const char *)out_file->data, O_WRONLY); check(conn->client.write_fd >= 0, "Failed to open write end of FIFO."); bdestroy(in_file); bdestroy(out_file); break; case PROXY_CLIENT_STDIO: log(INFO, "Listening on stdin/stdout."); conn->client.read_fd = 0; conn->client.write_fd = 1; break; case PROXY_CLIENT_NONE: fail("You did not set a client connection type."); break; default: fail("Invalid proxy client type given."); } check_err(conn->client.read_fd >= 0, LISTENER_IO, "Failed to start listener read side."); check_err(conn->client.write_fd >= 0, LISTENER_IO, "Failed to start listener write side."); dbg("Client listening on read_fd %d and write_fd %d", conn->client.read_fd, conn->client.write_fd); return 1; on_fail(return 0); }