Module Name: src Committed By: christos Date: Sat Sep 17 01:50:09 UTC 2011
Modified Files: src/external/bsd/tmux/dist: tmux.h window.c Added Files: src/external/bsd/tmux/dist: utmp.c Log Message: Add utmp/utmpx support. XXX: Should we make the top pty (tmux) hide? XXX: Should we add an option to not create utmp entries for a tmux terminal? To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/external/bsd/tmux/dist/tmux.h cvs rdiff -u -r0 -r1.1 src/external/bsd/tmux/dist/utmp.c cvs rdiff -u -r1.3 -r1.4 src/external/bsd/tmux/dist/window.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/external/bsd/tmux/dist/tmux.h diff -u src/external/bsd/tmux/dist/tmux.h:1.2 src/external/bsd/tmux/dist/tmux.h:1.3 --- src/external/bsd/tmux/dist/tmux.h:1.2 Fri Aug 19 05:06:05 2011 +++ src/external/bsd/tmux/dist/tmux.h Fri Sep 16 21:50:08 2011 @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.2 2011/08/19 09:06:05 christos Exp $ */ +/* $Id: tmux.h,v 1.3 2011/09/17 01:50:08 christos Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <n...@users.sourceforge.net> @@ -789,6 +789,7 @@ }; /* Child window structure. */ +struct window_utmp; struct window_pane { u_int id; @@ -832,6 +833,8 @@ const struct window_mode *mode; void *modedata; + struct window_utmp *utmp; + TAILQ_ENTRY(window_pane) entry; RB_ENTRY(window_pane) tree_entry; }; @@ -2067,4 +2070,8 @@ int printflike3 xsnprintf(char *, size_t, const char *, ...); int xvsnprintf(char *, size_t, const char *, va_list); +/* utmp.c */ +struct window_utmp *utmp_create(const char *); +void utmp_destroy(struct window_utmp *); + #endif /* TMUX_H */ Index: src/external/bsd/tmux/dist/window.c diff -u src/external/bsd/tmux/dist/window.c:1.3 src/external/bsd/tmux/dist/window.c:1.4 --- src/external/bsd/tmux/dist/window.c:1.3 Wed Aug 17 14:48:36 2011 +++ src/external/bsd/tmux/dist/window.c Fri Sep 16 21:50:09 2011 @@ -1,4 +1,4 @@ -/* $Id: window.c,v 1.3 2011/08/17 18:48:36 jmmv Exp $ */ +/* $Id: window.c,v 1.4 2011/09/17 01:50:09 christos Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <n...@users.sourceforge.net> @@ -629,6 +629,7 @@ xfree(wp->shell); if (wp->cmd != NULL) xfree(wp->cmd); + utmp_destroy(wp->utmp); xfree(wp); } @@ -718,6 +719,8 @@ window_pane_read_callback, NULL, window_pane_error_callback, wp); bufferevent_enable(wp->event, EV_READ|EV_WRITE); + wp->utmp = utmp_create(wp->tty); + return (0); } Added files: Index: src/external/bsd/tmux/dist/utmp.c diff -u /dev/null src/external/bsd/tmux/dist/utmp.c:1.1 --- /dev/null Fri Sep 16 21:50:09 2011 +++ src/external/bsd/tmux/dist/utmp.c Fri Sep 16 21:50:08 2011 @@ -0,0 +1,167 @@ +/* $NetBSD: utmp.c,v 1.1 2011/09/17 01:50:08 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: utmp.c,v 1.1 2011/09/17 01:50:08 christos Exp $"); + +#include <sys/types.h> +#include <sys/param.h> +#include <syslog.h> +#include <string.h> +#include <time.h> +#include <stdlib.h> +#include <unistd.h> +#include <util.h> +#include <paths.h> + +#ifdef SUPPORT_UTMP +#include <utmp.h> +#endif +#ifdef SUPPORT_UTMPX +#include <utmpx.h> +#endif + +#include "tmux.h" + +struct window_utmp { +#ifdef SUPPORT_UTMP + struct utmp ut; +#endif +#ifdef SUPPORT_UTMPX + struct utmpx utx; +#endif +}; + +#ifdef SUPPORT_UTMPX +static void +login_utmpx(struct utmpx *utmpx, const char *username, const char *hostname, + const char *tty, const struct timeval *now) +{ + const char *t; + + (void)memset(utmpx, 0, sizeof(*utmpx)); + utmpx->ut_tv = *now; + (void)strncpy(utmpx->ut_name, username, sizeof(utmpx->ut_name)); + if (hostname) + (void)strncpy(utmpx->ut_host, hostname, sizeof(utmpx->ut_host)); + (void)strncpy(utmpx->ut_line, tty, sizeof(utmpx->ut_line)); + utmpx->ut_type = USER_PROCESS; + utmpx->ut_pid = getpid(); + t = tty + strlen(tty); + if ((size_t)(t - tty) >= sizeof(utmpx->ut_id)) { + (void)strncpy(utmpx->ut_id, t - sizeof(utmpx->ut_id), + sizeof(utmpx->ut_id)); + } else { + (void)strncpy(utmpx->ut_id, tty, sizeof(utmpx->ut_id)); + } + (void)pututxline(utmpx); + endutxent(); +} + +static void +logout_utmpx(struct utmpx *utmpx, const struct timeval *now) +{ + utmpx->ut_type = DEAD_PROCESS; + utmpx->ut_tv = *now; + utmpx->ut_pid = 0; + (void)pututxline(utmpx); + endutxent(); +} +#endif + +#ifdef SUPPORT_UTMP +static void +login_utmp(struct utmp *utmp, const char *username, const char *hostname, + const char *tty, const struct timeval *now) +{ + (void)memset(utmp, 0, sizeof(*utmp)); + utmp->ut_time = now->tv_sec; + (void)strncpy(utmp->ut_name, username, sizeof(utmp->ut_name)); + if (hostname) + (void)strncpy(utmp->ut_host, hostname, sizeof(utmp->ut_host)); + (void)strncpy(utmp->ut_line, tty, sizeof(utmp->ut_line)); + login(utmp); +} + +static void +logout_utmp(struct utmp *utmp, const struct timeval *now) +{ + logout(utmp->ut_line); +} +#endif + +struct window_utmp * +utmp_create(const char *tty) +{ + struct window_utmp *wu; + struct timeval tv; + char username[LOGIN_NAME_MAX]; + + if (getlogin_r(username, sizeof(username)) == -1) + return NULL; + + if ((wu = malloc(sizeof(*wu))) == NULL) + return NULL; + + tty += sizeof(_PATH_DEV) - 1; + + (void)gettimeofday(&tv, NULL); +#ifdef SUPPORT_UTMPX + login_utmpx(&wu->utx, username, "tmux", tty, &tv); +#endif +#ifdef SUPPORT_UTMP + login_utmp(&wu->ut, username, "tmux", tty, &tv); +#endif + return wu; +} + +void +utmp_destroy(struct window_utmp *wu) +{ + struct timeval tv; + + if (wu == NULL) + return; + + (void)gettimeofday(&tv, NULL); +#ifdef SUPPORT_UTMPX + logout_utmpx(&wu->utx, &tv); +#endif +#ifdef SUPPORT_UTMP + logout_utmp(&wu->ut, &tv); +#endif +}