2007-11-26 20:36:30 +00:00
|
|
|
/* $Id: client.c,v 1.22 2007-11-26 20:36:30 nicm Exp $ */
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
void client_handle_winch(struct client_ctx *);
|
2007-09-26 18:50:49 +00:00
|
|
|
int client_process_local(struct client_ctx *, char **);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
int
|
2007-11-12 15:12:08 +00:00
|
|
|
client_init(const char *path, struct client_ctx *cctx, int start_server)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
struct stat sb;
|
|
|
|
struct msg_identify_data data;
|
|
|
|
struct winsize ws;
|
2007-10-23 10:25:03 +00:00
|
|
|
size_t size;
|
2007-10-03 21:31:07 +00:00
|
|
|
int mode;
|
|
|
|
u_int retries;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2007-09-27 20:53:13 +00:00
|
|
|
retries = 0;
|
|
|
|
retry:
|
|
|
|
if (stat(path, &sb) != 0) {
|
|
|
|
if (start_server && errno == ENOENT && retries < 10) {
|
|
|
|
if (server_start(path) != 0)
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-27 20:53:13 +00:00
|
|
|
usleep(10000);
|
|
|
|
retries++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
log_warn("%s: stat", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-27 20:53:13 +00:00
|
|
|
}
|
|
|
|
if (!S_ISSOCK(sb.st_mode)) {
|
|
|
|
log_warnx("%s: %s", path, strerror(ENOTSOCK));
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-27 20:53:13 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
2007-10-23 10:25:03 +00:00
|
|
|
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
2007-09-26 13:43:15 +00:00
|
|
|
log_warnx("%s: %s", path, strerror(ENAMETOOLONG));
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
log_warn("%s: socket", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
if (connect(
|
|
|
|
cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2007-09-27 20:53:13 +00:00
|
|
|
if (start_server && errno == ECONNREFUSED && retries < 10) {
|
2007-09-26 18:12:19 +00:00
|
|
|
if (unlink(path) != 0) {
|
|
|
|
log_warn("%s: unlink", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 18:12:19 +00:00
|
|
|
}
|
2007-09-27 20:53:13 +00:00
|
|
|
usleep(10000);
|
2007-09-26 18:12:19 +00:00
|
|
|
retries++;
|
|
|
|
goto retry;
|
|
|
|
}
|
2007-09-26 13:43:15 +00:00
|
|
|
log_warn("%s: connect", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1) {
|
|
|
|
log_warn("%s: fcntl", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1) {
|
|
|
|
log_warn("%s: fcntl", path);
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
cctx->srv_in = buffer_create(BUFSIZ);
|
|
|
|
cctx->srv_out = buffer_create(BUFSIZ);
|
|
|
|
|
2007-11-26 20:36:30 +00:00
|
|
|
if (isatty(STDIN_FILENO)) {
|
2007-10-03 21:31:07 +00:00
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
|
|
|
|
log_warn("ioctl(TIOCGWINSZ)");
|
2007-11-12 15:12:08 +00:00
|
|
|
return (-1);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
2007-10-23 09:36:19 +00:00
|
|
|
if (ttyname_r(STDIN_FILENO, data.tty, sizeof data.tty) != 0)
|
|
|
|
fatal("ttyname_r failed");
|
2007-10-03 21:31:07 +00:00
|
|
|
client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
|
2007-09-29 13:22:15 +00:00
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
|
|
|
|
return (0);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
int
|
|
|
|
client_main(struct client_ctx *cctx)
|
|
|
|
{
|
|
|
|
struct pollfd pfds[2];
|
2007-09-26 18:50:49 +00:00
|
|
|
char *error;
|
2007-10-05 14:23:28 +00:00
|
|
|
int timeout;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2007-11-08 10:39:52 +00:00
|
|
|
siginit();
|
|
|
|
if ((cctx->loc_fd = local_init(&cctx->loc_in, &cctx->loc_out)) == -1)
|
|
|
|
return (1);
|
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
logfile("client");
|
2007-10-31 14:26:26 +00:00
|
|
|
#ifndef NO_SETPROCTITLE
|
2007-09-26 13:43:15 +00:00
|
|
|
setproctitle("client");
|
2007-10-31 14:26:26 +00:00
|
|
|
#endif
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
error = NULL;
|
2007-10-05 14:23:28 +00:00
|
|
|
timeout = INFTIM;
|
2007-09-26 13:43:15 +00:00
|
|
|
while (!sigterm) {
|
|
|
|
if (sigwinch)
|
|
|
|
client_handle_winch(cctx);
|
|
|
|
|
|
|
|
pfds[0].fd = cctx->srv_fd;
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
if (BUFFER_USED(cctx->srv_out) > 0)
|
|
|
|
pfds[0].events |= POLLOUT;
|
|
|
|
pfds[1].fd = cctx->loc_fd;
|
|
|
|
pfds[1].events = POLLIN;
|
|
|
|
if (BUFFER_USED(cctx->loc_out) > 0)
|
|
|
|
pfds[1].events |= POLLOUT;
|
|
|
|
|
2007-10-05 14:23:28 +00:00
|
|
|
if (poll(pfds, 2, timeout) == -1) {
|
2007-09-26 13:43:15 +00:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fatal("poll failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_poll(&pfds[0], cctx->srv_in, cctx->srv_out) != 0)
|
|
|
|
goto server_dead;
|
|
|
|
if (buffer_poll(&pfds[1], cctx->loc_in, cctx->loc_out) != 0)
|
|
|
|
goto local_dead;
|
|
|
|
|
2007-10-05 14:23:28 +00:00
|
|
|
if (cctx->flags & CCTX_PAUSE) {
|
|
|
|
usleep(750000);
|
|
|
|
cctx->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_process_local(cctx, &error) == -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
switch (client_msg_dispatch(cctx, &error)) {
|
|
|
|
case -1:
|
|
|
|
goto out;
|
|
|
|
case 0:
|
|
|
|
/* May be more in buffer, don't let poll block. */
|
|
|
|
timeout = 0;
|
2007-09-26 13:43:15 +00:00
|
|
|
break;
|
2007-10-05 14:23:28 +00:00
|
|
|
default:
|
|
|
|
/* Out of data, poll may block. */
|
|
|
|
timeout = INFTIM;
|
2007-09-26 13:43:15 +00:00
|
|
|
break;
|
2007-10-04 11:52:03 +00:00
|
|
|
}
|
2007-09-26 18:50:49 +00:00
|
|
|
}
|
2007-10-05 14:23:28 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
local_done();
|
|
|
|
|
|
|
|
if (sigterm) {
|
|
|
|
printf("[terminated]\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cctx->flags & CCTX_EXIT) {
|
|
|
|
printf("[exited]\n");
|
|
|
|
return (0);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
2007-10-05 14:23:28 +00:00
|
|
|
|
|
|
|
if (cctx->flags & CCTX_DETACH) {
|
|
|
|
printf("[detached]\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[error: %s]\n", error);
|
|
|
|
return (1);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
server_dead:
|
|
|
|
local_done();
|
|
|
|
|
|
|
|
printf("[lost server]\n");
|
2007-10-22 13:16:36 +00:00
|
|
|
return (0);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
local_dead:
|
|
|
|
/* Can't do much here. Log and die. */
|
|
|
|
fatalx("local socket dead");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
client_handle_winch(struct client_ctx *cctx)
|
|
|
|
{
|
2007-10-03 21:31:07 +00:00
|
|
|
struct msg_resize_data data;
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
2007-09-26 13:43:15 +00:00
|
|
|
fatal("ioctl failed");
|
|
|
|
|
2007-10-03 21:31:07 +00:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
|
|
|
client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
sigwinch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-10-03 10:18:32 +00:00
|
|
|
client_process_local(struct client_ctx *cctx, unused char **error)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
|
|
|
struct buffer *b;
|
2007-10-03 10:18:32 +00:00
|
|
|
int key;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
b = buffer_create(BUFSIZ);
|
2007-10-03 10:18:32 +00:00
|
|
|
while ((key = local_key()) != KEYC_NONE)
|
|
|
|
input_store16(b, (uint16_t) key);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
log_debug("transmitting %zu bytes of input", BUFFER_USED(b));
|
2007-10-03 10:18:32 +00:00
|
|
|
if (BUFFER_USED(b) != 0) {
|
|
|
|
client_write_server(
|
|
|
|
cctx, MSG_KEYS, BUFFER_OUT(b), BUFFER_USED(b));
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
2007-10-03 10:18:32 +00:00
|
|
|
|
2007-09-26 13:43:15 +00:00
|
|
|
buffer_destroy(b);
|
2007-10-03 10:18:32 +00:00
|
|
|
return (0);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
|
|
|
|