/* $Id: server.c,v 1.116 2009-01-29 20:13:12 nicm Exp $ */
/*
* 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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include "tmux.h"
/*
* Main server functions.
*/
/* Client list. */
struct clients clients;
int server_main(const char *, int);
void server_shutdown(void);
void server_fill_windows(struct pollfd **);
void server_handle_windows(struct pollfd **);
void server_fill_clients(struct pollfd **);
void server_handle_clients(struct pollfd **);
struct client *server_accept_client(int);
void server_handle_client(struct client *);
void server_handle_window(struct window *, struct window_pane *wp);
void server_lost_client(struct client *);
void server_check_window(struct window *);
void server_check_redraw(struct client *);
void server_redraw_locked(struct client *);
void server_check_timers(struct client *);
void server_second_timers(void);
int server_update_socket(const char *);
/* Create a new client. */
struct client *
server_create_client(int fd)
{
struct client *c;
int mode;
u_int i;
if ((mode = fcntl(fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
c = xcalloc(1, sizeof *c);
c->fd = fd;
c->in = buffer_create(BUFSIZ);
c->out = buffer_create(BUFSIZ);
ARRAY_INIT(&c->prompt_hdata);
c->tty.fd = -1;
c->title = NULL;
c->session = NULL;
c->sx = 80;
c->sy = 25;
screen_init(&c->status, c->sx, 1, 0);
c->message_string = NULL;
c->prompt_string = NULL;
c->prompt_buffer = NULL;
c->prompt_index = 0;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
if (ARRAY_ITEM(&clients, i) == NULL) {
ARRAY_SET(&clients, i, c);
return (c);
}
}
ARRAY_ADD(&clients, c);
return (c);
}
/* Find client index. */
int
server_client_index(struct client *c)
{
u_int i;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
if (c == ARRAY_ITEM(&clients, i))
return (i);
}
return (-1);
}
/* Fork new server. */
int
server_start(const char *path)
{
struct sockaddr_un sa;
size_t size;
mode_t mask;
int n, fd, pair[2], mode;
char *cause;
/* The first client is special and gets a socketpair; create it. */
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
fatal("socketpair failed");
switch (fork()) {
case -1:
fatal("fork failed");
case 0:
break;
default:
close(pair[1]);
return (pair[0]);
}
close(pair[0]);
#ifdef DEBUG
xmalloc_clear();
#endif
/*
* Must daemonise before loading configuration as the PID changes so
* $TMUX would be wrong for sessions created in the config file.
*/
if (daemon(1, 0) != 0)
fatal("daemon failed");
ARRAY_INIT(&windows);
ARRAY_INIT(&clients);
ARRAY_INIT(&sessions);
key_bindings_init();
server_locked = 0;
server_password = NULL;
server_activity = time(NULL);
if (cfg_file != NULL && load_cfg(cfg_file, &cause) != 0) {