forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpgagent_teardown.h
62 lines (53 loc) · 1.3 KB
/
gpgagent_teardown.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @file
*
* @brief common method for shutting down the gpg-agent in unit tests
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef ELEKTRA_PLUGIN_CRYPTO_GPGAGENT_TEARDOWN_H
#define ELEKTRA_PLUGIN_CRYPTO_GPGAGENT_TEARDOWN_H
#include <sys/wait.h>
#include <unistd.h>
#define GPG_CONNECT_AGENT_CMD "gpg-connect-agent"
static inline void test_teardown (void)
{
pid_t pid;
int status;
char * argv[] = { GPG_CONNECT_AGENT_CMD, "--quiet", "KILLAGENT", "/bye", NULL };
// check if gpg-connect-agent is available and executable
if (access (GPG_CONNECT_AGENT_CMD, F_OK))
{
return;
}
if (access (GPG_CONNECT_AGENT_CMD, X_OK))
{
return;
}
// execute the shutdown command
switch (pid = fork ())
{
case -1: // failure
yield_error ("fork failed");
return;
case 0: // child process
if (execv (GPG_CONNECT_AGENT_CMD, argv) < 0)
{
exit (-1);
}
// end of the child process
}
// parent process - check if execv failed
// NOTE the return value of gpg-connect-agent is irrelevant because it will
// always return 0 (see source code of GnuPG).
waitpid (pid, &status, 0);
succeed_if (status != -1, "failed to execute gpg-connect-agent");
// wait for the agent to properly shut down
if (status > 0)
{
pid = status;
waitpid (pid, &status, 0);
}
}
#endif