Loof for the socket under [/var]/run

Starting from version 2.1.17, GnuPG may place all its sockets
in the directory [/var]/run/user/$(id -u)/gnupg, if such a
directory exists.

A socket in that directory would always be found by the call
to gpg-connect-agent that we use as a last resort, but to
avoid that call, we now try to locate the socket under the
[/var]/run directory ourself.
develop
Damien Goutte-Gattat 6 years ago
parent ee8e42a6bb
commit b9161a20c6
  1. 24
      src/gpg-util.c

@ -51,6 +51,7 @@ get_agent_socket_name(void)
{
char *env_info, *socket_name = NULL;
char buf[255] = "";
struct stat st;
/* First look for GPG_AGENT_INFO in the environment. */
if ( (env_info = getenv("GPG_AGENT_INFO")) ) {
@ -69,9 +70,9 @@ get_agent_socket_name(void)
}
if ( ! socket_name ) {
/* Then look for the standard socket. */
/* Then look for the standard socket
* in GnuPG's home directory. */
struct passwd *pwd;
struct stat st;
if ( (env_info = getenv("GNUPGHOME")) )
snprintf(buf, sizeof(buf), "%s/S.gpg-agent", env_info);
@ -85,11 +86,26 @@ get_agent_socket_name(void)
socket_name = strdup(buf);
}
if ( ! socket_name ) {
/* Then look for the standard socket outside of GnuPG's
* home directory (for GnuPG >= 2.1.17). */
static const char *prefixes[] = { "/run", "/var/run", NULL };
int i;
for ( i = 0; prefixes[i] && ! socket_name ; i++ ) {
snprintf(buf, sizeof(buf), "%s/user/%u/gnupg/S.gpg-agent",
prefixes[i], getuid());
if ( stat(buf, &st) != -1 && S_ISSOCK(st.st_mode) )
socket_name = strdup(buf);
}
}
if ( ! socket_name ) {
/*
* Finally, try to ask the agent directly. This method could
* replace the two above, but since it requires spawning a
* new process, we use it only as a fallback. We expect that
* replace all the methods above, but since it requires spawing
* a 0new process, we use it only as a fallback. We expect that
* most of the time, the agent should already be running when
* our programs are called, and thus the socket would already
* exist and be found by the above code.

Loading…
Cancel
Save