@@ -0,0 +1,180 @@
1+From 8fde13d07c0059040ed531ca3ac365f5780ff240 Mon Sep 17 00:00:00 2001
2+From: Shea Levy <shea@shealevy.com>
3+Date: Tue, 18 Nov 2014 14:34:53 -0500
4+Subject: [PATCH] gpg-agent: Enable socket activation
6+This allows gpg-agent to be managed by tools such as systemd or launchd
8+ agent/gpg-agent.c | 62 ++++++++++++++++++++++++++++++++++++------------------
9+ doc/gpg-agent.texi | 21 +++++++++++++++++-
10+ 2 files changed, 61 insertions(+), 22 deletions(-)
12+diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c
13+index 5960fe3..2f06982 100644
14+--- a/agent/gpg-agent.c
15++++ b/agent/gpg-agent.c
16+@@ -124,7 +124,9 @@ enum cmd_and_opt_values
19+ oDisableCheckOwnSocket,
27+@@ -138,6 +140,8 @@ static ARGPARSE_OPTS opts[] = {
28+ { 301, NULL, 0, N_("@Options:\n ") },
30+ { oDaemon, "daemon", 0, N_("run in daemon mode (background)") },
31++ { oAgentFD, "agent-fd", 1, "@" },
32++ { oSSHAgentFD, "ssh-agent-fd", 1, "@" },
33+ { oServer, "server", 0, N_("run in server mode (foreground)") },
34+ { oVerbose, "verbose", 0, N_("verbose") },
35+ { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
36+@@ -596,6 +600,31 @@ parse_rereadable_options (ARGPARSE_ARGS *pargs, int reread)
37+ return 1; /* handled */
40++/* Handle agent socket(s) */
42++handle_agent_socks(int fd, int fd_ssh)
44++#ifndef HAVE_W32_SYSTEM
47++ log_error ("chdir to / failed: %s\n", strerror (errno));
52++ struct sigaction sa;
54++ sa.sa_handler = SIG_IGN;
55++ sigemptyset (&sa.sa_mask);
57++ sigaction (SIGPIPE, &sa, NULL);
59++#endif /*!HAVE_W32_SYSTEM*/
61++ log_info ("%s %s started\n", strusage(11), strusage(13) );
62++ handle_connections (fd, fd_ssh);
63++ assuan_sock_close (fd);
66+ /* The main entry point. */
68+@@ -612,6 +641,8 @@ main (int argc, char **argv )
69+ int default_config =1;
72++ int fd_agent = GNUPG_INVALID_FD;
73++ int fd_ssh_agent = GNUPG_INVALID_FD;
76+ char *logfile = NULL;
77+@@ -819,6 +850,8 @@ main (int argc, char **argv )
78+ case oSh: csh_style = 0; break;
79+ case oServer: pipe_server = 1; break;
80+ case oDaemon: is_daemon = 1; break;
81++ case oAgentFD: fd_agent = pargs.r.ret_int; break;
82++ case oSSHAgentFD: fd_ssh_agent = pargs.r.ret_int; break;
84+ case oDisplay: default_display = xstrdup (pargs.r.ret_str); break;
85+ case oTTYname: default_ttyname = xstrdup (pargs.r.ret_str); break;
86+@@ -904,7 +937,8 @@ main (int argc, char **argv )
87+ bind_textdomain_codeset (PACKAGE_GT, "UTF-8");
90+- if (!pipe_server && !is_daemon && !gpgconf_list)
91++ if (!pipe_server && !is_daemon && !gpgconf_list &&
92++ fd_agent == GNUPG_INVALID_FD)
94+ /* We have been called without any options and thus we merely
95+ check whether an agent is already running. We do this right
96+@@ -1054,6 +1088,10 @@ main (int argc, char **argv )
97+ agent_deinit_default_ctrl (ctrl);
100++ else if (fd_agent != GNUPG_INVALID_FD)
102++ handle_agent_socks(fd_agent, fd_ssh_agent);
104+ else if (!is_daemon)
107+@@ -1238,26 +1276,8 @@ main (int argc, char **argv )
108+ log_set_prefix (NULL, oldflags | JNLIB_LOG_RUN_DETACHED);
109+ opt.running_detached = 1;
114+- log_error ("chdir to / failed: %s\n", strerror (errno));
119+- struct sigaction sa;
121+- sa.sa_handler = SIG_IGN;
122+- sigemptyset (&sa.sa_mask);
124+- sigaction (SIGPIPE, &sa, NULL);
126+ #endif /*!HAVE_W32_SYSTEM*/
128+- log_info ("%s %s started\n", strusage(11), strusage(13) );
129+- handle_connections (fd, opt.ssh_support ? fd_ssh : GNUPG_INVALID_FD);
130+- assuan_sock_close (fd);
131++ handle_agent_socks(fd, opt.ssh_support ? fd_ssh : GNUPG_INVALID_FD);
135+diff --git a/doc/gpg-agent.texi b/doc/gpg-agent.texi
136+index a4079d7..1556e54 100644
137+--- a/doc/gpg-agent.texi
138++++ b/doc/gpg-agent.texi
143+-.RI [ command_line ]
155+ @mansect description
156+@@ -186,6 +194,11 @@ Yet another way is creating
157+ a new process as a child of gpg-agent: @code{gpg-agent --daemon
158+ /bin/sh}. This way you get a new shell with the environment setup
159+ properly; if you exit from this shell, gpg-agent terminates as well.
161++@item --agent-fd @var{fd}
163++Start the gpg-agent using @var{fd} as the listening socket. This is useful for
164++socket activation a la systemd and launchd.
168+@@ -532,6 +545,12 @@ Ignore requests to change the current @code{tty} or X window system's
169+ @code{DISPLAY} variable respectively. This is useful to lock the
170+ pinentry to pop up at the @code{tty} or display you started the agent.
172++@item --ssh-agent-fd @var{fd}
173++@opindex ssh-agent-fd
175++When starting the agent with @option{--agent-fd}, use this to pass in a socket
176++to be used for the OpenSSH agent protocol.
178+ @anchor{option --enable-ssh-support}
179+ @item --enable-ssh-support
180+ @opindex enable-ssh-support