From owner-svn-src-all@FreeBSD.ORG Fri Oct 7 13:10:16 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C955C1065677; Fri, 7 Oct 2011 13:10:16 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id B8E288FC0C; Fri, 7 Oct 2011 13:10:16 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p97DAGRN012115; Fri, 7 Oct 2011 13:10:16 GMT (envelope-from des@svn.freebsd.org) Received: (from des@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p97DAGMh012112; Fri, 7 Oct 2011 13:10:16 GMT (envelope-from des@svn.freebsd.org) Message-Id: <201110071310.p97DAGMh012112@svn.freebsd.org> From: Dag-Erling Smorgrav Date: Fri, 7 Oct 2011 13:10:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r226103 - head/crypto/openssh X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Oct 2011 13:10:17 -0000 Author: des Date: Fri Oct 7 13:10:16 2011 New Revision: 226103 URL: http://svn.freebsd.org/changeset/base/226103 Log: Add a -x option that causes ssh-agent(1) to exit when all clients have disconnected. MFC after: 1 week Modified: head/crypto/openssh/ssh-agent.1 head/crypto/openssh/ssh-agent.c Modified: head/crypto/openssh/ssh-agent.1 ============================================================================== --- head/crypto/openssh/ssh-agent.1 Fri Oct 7 12:59:04 2011 (r226102) +++ head/crypto/openssh/ssh-agent.1 Fri Oct 7 13:10:16 2011 (r226103) @@ -44,7 +44,7 @@ .Sh SYNOPSIS .Nm ssh-agent .Op Fl c | s -.Op Fl d +.Op Fl dx .Op Fl a Ar bind_address .Op Fl t Ar life .Op Ar command Op Ar arg ... @@ -103,6 +103,8 @@ A lifetime specified for an identity wit .Xr ssh-add 1 overrides this value. Without this option the default maximum lifetime is forever. +.It Fl x +Exit after the last client has disconnected. .El .Pp If a commandline is given, this is executed as a subprocess of the agent. Modified: head/crypto/openssh/ssh-agent.c ============================================================================== --- head/crypto/openssh/ssh-agent.c Fri Oct 7 12:59:04 2011 (r226102) +++ head/crypto/openssh/ssh-agent.c Fri Oct 7 13:10:16 2011 (r226103) @@ -138,15 +138,34 @@ extern char *__progname; /* Default lifetime (0 == forever) */ static int lifetime = 0; +/* + * Client connection count; incremented in new_socket() and decremented in + * close_socket(). When it reaches 0, ssh-agent will exit. Since it is + * normally initialized to 1, it will never reach 0. However, if the -x + * option is specified, it is initialized to 0 in main(); in that case, + * ssh-agent will exit as soon as it has had at least one client but no + * longer has any. + */ +static int xcount = 1; + static void close_socket(SocketEntry *e) { + int last = 0; + + if (e->type == AUTH_CONNECTION) { + debug("xcount %d -> %d", xcount, xcount - 1); + if (--xcount == 0) + last = 1; + } close(e->fd); e->fd = -1; e->type = AUTH_UNUSED; buffer_free(&e->input); buffer_free(&e->output); buffer_free(&e->request); + if (last) + cleanup_exit(0); } static void @@ -901,6 +920,10 @@ new_socket(sock_type type, int fd) { u_int i, old_alloc, new_alloc; + if (type == AUTH_CONNECTION) { + debug("xcount %d -> %d", xcount, xcount + 1); + ++xcount; + } set_nonblock(fd); if (fd > max_fd) @@ -1121,6 +1144,7 @@ usage(void) fprintf(stderr, " -d Debug mode.\n"); fprintf(stderr, " -a socket Bind agent socket to given name.\n"); fprintf(stderr, " -t life Default identity lifetime (seconds).\n"); + fprintf(stderr, " -x Exit when the last client disconnects.\n"); exit(1); } @@ -1162,7 +1186,7 @@ main(int ac, char **av) __progname = ssh_get_progname(av[0]); seed_rng(); - while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { + while ((ch = getopt(ac, av, "cdksa:t:x")) != -1) { switch (ch) { case 'c': if (s_flag) @@ -1191,6 +1215,9 @@ main(int ac, char **av) usage(); } break; + case 'x': + xcount = 0; + break; default: usage(); } @@ -1350,8 +1377,7 @@ skip: if (ac > 0) parent_alive_interval = 10; idtab_init(); - if (!d_flag) - signal(SIGINT, SIG_IGN); + signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, cleanup_handler); signal(SIGTERM, cleanup_handler);