forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
152 lines (143 loc) · 4.3 KB
/
shell.c
File metadata and controls
152 lines (143 loc) · 4.3 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @file
*
* @brief Source for shell plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "shell.h"
#include <errno.h>
#include <kdberrors.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static int executeCommand (const char * cmdline)
{
char * cmd[4];
cmd[0] = "/bin/sh";
cmd[1] = "-c";
cmd[2] = (char *) cmdline;
cmd[3] = NULL;
pid_t pid = fork ();
if (pid == 0)
{
return execv ("/bin/sh", cmd);
}
else if (pid > 0)
{
int status;
wait (&status);
return WEXITSTATUS (status);
}
else
{
return -1;
}
}
int elektraShellGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/shell"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/shell", KEY_VALUE, "shell plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/shell/exports", KEY_END),
keyNew ("system:/elektra/modules/shell/exports/get", KEY_FUNC, elektraShellGet, KEY_END),
keyNew ("system:/elektra/modules/shell/exports/commit", KEY_FUNC, elektraShellCommit, KEY_END),
keyNew ("system:/elektra/modules/shell/exports/error", KEY_FUNC, elektraShellError, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/shell/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; // success
}
KeySet * config = elektraPluginGetConfig (handle);
Key * cmdKey = ksLookupByName (config, "/execute/get", KDB_O_NONE);
Key * expectedReturnKey = ksLookupByName (config, "/execute/get/return", KDB_O_NONE);
if (cmdKey == NULL)
return 1;
else
{
int retVal = executeCommand (keyString (cmdKey));
if (retVal == -1)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Launching childprocess failed. Reason: %s", strerror (errno));
return -1;
}
else if (expectedReturnKey)
{
if (atoi (keyString (expectedReturnKey)) != retVal)
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey,
"Return value of '%s' doesn't match expected exit. Reason: %s",
keyString (cmdKey), keyString (expectedReturnKey));
return -1;
}
}
}
return 1; // success
}
int elektraShellCommit (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
KeySet * config = elektraPluginGetConfig (handle);
Key * cmdKey = ksLookupByName (config, "/execute/set", KDB_O_NONE);
Key * expectedReturnKey = ksLookupByName (config, "/execute/set/return", KDB_O_NONE);
if (cmdKey == NULL)
return 1;
else
{
int retVal = executeCommand (keyString (cmdKey));
if (retVal == -1)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Launching childprocess failed. Reason: %s", strerror (errno));
return -1;
}
else if (expectedReturnKey)
{
if (atoi (keyString (expectedReturnKey)) != retVal)
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey,
"Return value of '%s' doesn't match expected exit. Reason: %s",
keyString (cmdKey), keyString (expectedReturnKey));
return -1;
}
}
}
return 1; // success
}
int elektraShellError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
KeySet * config = elektraPluginGetConfig (handle);
Key * cmdKey = ksLookupByName (config, "/execute/error", KDB_O_NONE);
Key * expectedReturnKey = ksLookupByName (config, "/execute/error/return", KDB_O_NONE);
if (cmdKey == NULL)
return 1;
else
{
int retVal = executeCommand (keyString (cmdKey));
if (retVal == -1)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Launching childprocess failed. Reason: %s", strerror (errno));
return -1;
}
else if (expectedReturnKey)
{
if (atoi (keyString (expectedReturnKey)) != retVal)
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey,
"Return value of '%s' doesn't match expected exit. Reason: %s",
keyString (cmdKey), keyString (expectedReturnKey));
return -1;
}
}
}
return 1; // success
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
return elektraPluginExport ("shell", ELEKTRA_PLUGIN_GET, &elektraShellGet, ELEKTRA_PLUGIN_COMMIT, &elektraShellCommit,
ELEKTRA_PLUGIN_ERROR, &elektraShellError, ELEKTRA_PLUGIN_END);
}