diff options
Diffstat (limited to 'sm/call-agent.c')
-rw-r--r-- | sm/call-agent.c | 89 |
1 files changed, 61 insertions, 28 deletions
diff --git a/sm/call-agent.c b/sm/call-agent.c index 1ac5412be..b30fe60df 100644 --- a/sm/call-agent.c +++ b/sm/call-agent.c @@ -37,7 +37,8 @@ #include "i18n.h" #include "asshelp.h" #include "keydb.h" /* fixme: Move this to import.c */ -#include "../common/membuf.h" +#include "membuf.h" +#include "exechelp.h" static assuan_context_t agent_ctx = NULL; @@ -83,21 +84,12 @@ start_agent (ctrl_t ctrl) infostr = force_pipe_server? NULL : getenv ("GPG_AGENT_INFO"); if (!infostr || !*infostr) { - const char *pgmname; - const char *argv[3]; char *sockname; - int no_close_list[3]; - int i; /* First check whether we can connect at the standard socket. */ sockname = make_filename (opt.homedir, "S.gpg-agent", NULL); rc = assuan_socket_connect (&ctx, sockname, 0); - xfree (sockname); -#ifdef HAVE_W32_SYSTEM -# warning Print a warning if connecting is not possible - /* and offer to fire up the agent. */ -#endif if (rc) { @@ -112,30 +104,71 @@ start_agent (ctrl_t ctrl) gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno)); log_error ("error flushing pending output: %s\n", strerror (errno)); + xfree (sockname); return tmperr; } if (!opt.agent_program || !*opt.agent_program) opt.agent_program = gnupg_module_name (GNUPG_MODULE_NAME_AGENT); - if ( !(pgmname = strrchr (opt.agent_program, '/'))) - pgmname = opt.agent_program; - else - pgmname++; - - argv[0] = pgmname; - argv[1] = "--server"; - argv[2] = NULL; - - i=0; - if (log_get_fd () != -1) - no_close_list[i++] = log_get_fd (); - no_close_list[i++] = fileno (stderr); - no_close_list[i] = -1; - - /* Connect to the agent and perform initial handshaking. */ - rc = assuan_pipe_connect (&ctx, opt.agent_program, argv, - no_close_list); + +#ifdef HAVE_W32_SYSTEM + { + /* Under Windows we start the server in daemon mode. This + is because the default is to use the standard socket + and thus there is no need for the GPG_AGENT_INFO + envvar. This is possible as we don't have a real unix + domain socket but use a plain file and thus there is no + need to care about non-local file systems. */ + const char *argv[3]; + + /* The --no-reuse-standard option makes sure that we don't + start a second instance of a agent in case another + process has started one in the meantime. */ + argv[0] = "--daemon"; + argv[1] = "--no-reuse-standard-socket"; + argv[2] = NULL; + + rc = gnupg_spawn_process_detached (opt.agent_program, argv, NULL); + if (rc) + log_debug ("failed to start agent `%s': %s\n", + opt.agent_program, gpg_strerror (rc)); + else + { + /* Give the agent some time to prepare itself. */ + gnupg_sleep (3); + /* Now try again to connect the agent. */ + rc = assuan_socket_connect (&ctx, sockname, 0); + } + } +#else /*!HAVE_W32_SYSTEM*/ + { + const char *pgmname; + const char *argv[3]; + int no_close_list[3]; + int i; + + if ( !(pgmname = strrchr (opt.agent_program, '/'))) + pgmname = opt.agent_program; + else + pgmname++; + + argv[0] = pgmname; + argv[1] = "--server"; + argv[2] = NULL; + + i=0; + if (log_get_fd () != -1) + no_close_list[i++] = log_get_fd (); + no_close_list[i++] = fileno (stderr); + no_close_list[i] = -1; + + /* Connect to the agent and perform initial handshaking. */ + rc = assuan_pipe_connect (&ctx, opt.agent_program, argv, + no_close_list); + } +#endif /*!HAVE_W32_SYSTEM*/ } + xfree (sockname); } else { |