2008-07-01 21:47:02 +02:00
|
|
|
/* $Id: client.c,v 1.34 2008-07-01 19:47:02 nicm Exp $ */
|
2007-09-26 15:43:15 +02: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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
void client_handle_winch(struct client_ctx *);
|
|
|
|
|
|
|
|
int
|
2007-11-12 16:12:08 +01:00
|
|
|
client_init(const char *path, struct client_ctx *cctx, int start_server)
|
2007-09-26 15:43:15 +02:00
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
struct stat sb;
|
|
|
|
struct msg_identify_data data;
|
|
|
|
struct winsize ws;
|
2007-10-23 12:25:03 +02:00
|
|
|
size_t size;
|
2007-10-03 23:31:07 +02:00
|
|
|
int mode;
|
|
|
|
u_int retries;
|
2008-06-01 23:24:33 +02:00
|
|
|
struct buffer *b;
|
2008-06-18 22:58:03 +02:00
|
|
|
char *name;
|
2007-09-26 15:43:15 +02:00
|
|
|
|
2007-09-27 22:53:13 +02:00
|
|
|
retries = 0;
|
|
|
|
retry:
|
|
|
|
if (stat(path, &sb) != 0) {
|
2008-06-07 09:27:28 +02:00
|
|
|
if (start_server && errno == ENOENT) {
|
|
|
|
if (server_start(path) != 0)
|
|
|
|
goto no_start;
|
2007-09-27 22:53:13 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
2007-12-01 12:10:33 +01:00
|
|
|
goto fail;
|
2007-09-27 22:53:13 +02:00
|
|
|
}
|
|
|
|
if (!S_ISSOCK(sb.st_mode)) {
|
2007-12-01 12:10:33 +01:00
|
|
|
errno = ENOTSOCK;
|
|
|
|
goto fail;
|
2007-09-27 22:53:13 +02:00
|
|
|
}
|
|
|
|
|
2007-09-26 15:43:15 +02:00
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
2007-10-23 12:25:03 +02:00
|
|
|
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
2007-12-01 12:10:33 +01:00
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
goto fail;
|
2007-09-26 15:43:15 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 12:10:33 +01:00
|
|
|
if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
|
|
|
fatal("socket");
|
|
|
|
|
2007-09-26 15:43:15 +02:00
|
|
|
if (connect(
|
|
|
|
cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
|
2007-09-27 22:53:13 +02:00
|
|
|
if (start_server && errno == ECONNREFUSED && retries < 10) {
|
2007-12-01 12:10:33 +01:00
|
|
|
if (unlink(path) != 0)
|
|
|
|
goto fail;
|
2008-06-07 09:27:28 +02:00
|
|
|
if (server_start(path) != 0)
|
|
|
|
goto no_start;
|
2007-09-26 20:12:19 +02:00
|
|
|
}
|
2007-12-01 12:10:33 +01:00
|
|
|
goto fail;
|
2007-09-26 15:43:15 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 12:10:33 +01:00
|
|
|
if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1)
|
|
|
|
fatal("fcntl");
|
|
|
|
if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
|
|
|
|
fatal("fcntl");
|
2007-09-26 15:43:15 +02:00
|
|
|
cctx->srv_in = buffer_create(BUFSIZ);
|
|
|
|
cctx->srv_out = buffer_create(BUFSIZ);
|
|
|
|
|
2007-11-26 21:36:30 +01:00
|
|
|
if (isatty(STDIN_FILENO)) {
|
2007-12-01 12:10:33 +01:00
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
|
|
|
fatal("ioctl(TIOCGWINSZ)");
|
2008-07-01 21:47:02 +02:00
|
|
|
data.version = PROTOCOL_VERSION;
|
2007-10-03 23:31:07 +02:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
2008-06-18 22:58:03 +02:00
|
|
|
*data.tty = '\0';
|
|
|
|
|
|
|
|
if ((name = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
fatal("ttyname failed");
|
|
|
|
if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
|
|
|
|
fatalx("ttyname failed");
|
2008-06-01 23:24:33 +02:00
|
|
|
|
|
|
|
b = buffer_create(BUFSIZ);
|
|
|
|
cmd_send_string(b, getenv("TERM"));
|
|
|
|
client_write_server2(cctx, MSG_IDENTIFY,
|
|
|
|
&data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b));
|
|
|
|
buffer_destroy(b);
|
2007-09-29 15:22:15 +02:00
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
return (0);
|
2007-12-01 12:10:33 +01:00
|
|
|
|
2008-06-07 09:27:28 +02:00
|
|
|
no_start:
|
|
|
|
log_warnx("server failed to start");
|
|
|
|
return (1);
|
|
|
|
|
2007-12-01 12:10:33 +01:00
|
|
|
fail:
|
|
|
|
log_warn("server not found");
|
2008-06-02 23:08:36 +02:00
|
|
|
return (1);
|
2007-09-28 21:04:21 +02:00
|
|
|
}
|
|
|
|
|
2007-09-26 15:43:15 +02:00
|
|
|
int
|
|
|
|
client_main(struct client_ctx *cctx)
|
|
|
|
{
|
2007-11-27 20:23:34 +01:00
|
|
|
struct pollfd pfd;
|
2007-09-26 20:50:49 +02:00
|
|
|
char *error;
|
2008-06-19 21:36:55 +02:00
|
|
|
int xtimeout; /* Yay for ncurses namespace! */
|
2007-09-26 15:43:15 +02:00
|
|
|
|
2007-11-08 11:39:52 +01:00
|
|
|
siginit();
|
|
|
|
|
2007-09-26 15:43:15 +02:00
|
|
|
logfile("client");
|
2007-10-31 15:26:26 +01:00
|
|
|
#ifndef NO_SETPROCTITLE
|
2007-09-26 15:43:15 +02:00
|
|
|
setproctitle("client");
|
2007-10-31 15:26:26 +01:00
|
|
|
#endif
|
2007-09-26 15:43:15 +02:00
|
|
|
|
|
|
|
error = NULL;
|
2008-06-19 21:36:55 +02:00
|
|
|
xtimeout = INFTIM;
|
2007-09-26 15:43:15 +02:00
|
|
|
while (!sigterm) {
|
|
|
|
if (sigwinch)
|
|
|
|
client_handle_winch(cctx);
|
|
|
|
|
2008-05-31 22:04:15 +02:00
|
|
|
switch (client_msg_dispatch(cctx, &error)) {
|
|
|
|
case -1:
|
|
|
|
goto out;
|
|
|
|
case 0:
|
|
|
|
/* May be more in buffer, don't let poll block. */
|
2008-06-19 21:36:55 +02:00
|
|
|
xtimeout = 0;
|
2008-05-31 22:04:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Out of data, poll may block. */
|
2008-06-19 21:36:55 +02:00
|
|
|
xtimeout = INFTIM;
|
2008-05-31 22:04:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-27 20:23:34 +01:00
|
|
|
pfd.fd = cctx->srv_fd;
|
|
|
|
pfd.events = POLLIN;
|
2007-09-26 15:43:15 +02:00
|
|
|
if (BUFFER_USED(cctx->srv_out) > 0)
|
2007-11-27 20:23:34 +01:00
|
|
|
pfd.events |= POLLOUT;
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2008-06-19 21:36:55 +02:00
|
|
|
if (poll(&pfd, 1, xtimeout) == -1) {
|
2007-09-26 15:43:15 +02:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fatal("poll failed");
|
|
|
|
}
|
|
|
|
|
2007-11-27 20:23:34 +01:00
|
|
|
if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0)
|
2007-09-26 15:43:15 +02:00
|
|
|
goto server_dead;
|
2007-09-26 20:50:49 +02:00
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-10-05 16:23:28 +02:00
|
|
|
out:
|
|
|
|
if (sigterm) {
|
|
|
|
printf("[terminated]\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-10-05 16:23:28 +02:00
|
|
|
if (cctx->flags & CCTX_EXIT) {
|
|
|
|
printf("[exited]\n");
|
|
|
|
return (0);
|
2007-09-26 15:43:15 +02:00
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-10-05 16:23:28 +02:00
|
|
|
if (cctx->flags & CCTX_DETACH) {
|
|
|
|
printf("[detached]\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[error: %s]\n", error);
|
|
|
|
return (1);
|
2007-09-26 15:43:15 +02:00
|
|
|
|
|
|
|
server_dead:
|
|
|
|
printf("[lost server]\n");
|
2007-10-22 15:16:36 +02:00
|
|
|
return (0);
|
2007-09-26 15:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
client_handle_winch(struct client_ctx *cctx)
|
|
|
|
{
|
2007-10-03 23:31:07 +02:00
|
|
|
struct msg_resize_data data;
|
|
|
|
struct winsize ws;
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
|
2007-09-26 15:43:15 +02:00
|
|
|
fatal("ioctl failed");
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
data.sx = ws.ws_col;
|
|
|
|
data.sy = ws.ws_row;
|
|
|
|
client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-09-26 15:43:15 +02:00
|
|
|
sigwinch = 0;
|
|
|
|
}
|