forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uname.c
104 lines (83 loc) · 2.69 KB
/
uname.c
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "uname.h"
#include <errno.h>
#include <kdblogger.h>
#include <string.h>
#include <sys/utsname.h>
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <kdberrors.h>
#include <kdbmacros.h>
static void elektraAddUname (KeySet * returned, Key * parentKey)
{
Key * dir;
Key * key = keyDup (parentKey, KEY_CP_ALL);
ksAppendKey (returned, key);
struct utsname buf;
uname (&buf); // TODO: handle error
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, "sysname");
keySetString (dir, buf.sysname);
ksAppendKey (returned, dir);
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, "nodename");
keySetString (dir, buf.nodename);
ksAppendKey (returned, dir);
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, "release");
keySetString (dir, buf.release);
ksAppendKey (returned, dir);
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, "version");
keySetString (dir, buf.version);
ksAppendKey (returned, dir);
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, "machine");
keySetString (dir, buf.machine);
ksAppendKey (returned, dir);
}
int elektraUnameGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
int errnosave = errno;
ELEKTRA_LOG ("get uname %s from %s\n", keyName (parentKey), keyString (parentKey));
if (!strcmp (keyName (parentKey), "system:/elektra/modules/uname"))
{
KeySet * moduleConfig =
ksNew (50, keyNew ("system:/elektra/modules/uname", KEY_VALUE, "uname plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/uname/exports", KEY_END),
keyNew ("system:/elektra/modules/uname/exports/get", KEY_FUNC, elektraUnameGet, KEY_END),
keyNew ("system:/elektra/modules/uname/exports/set", KEY_FUNC, elektraUnameSet, KEY_END),
#include "readme_uname.c"
keyNew ("system:/elektra/modules/uname/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, moduleConfig);
ksDel (moduleConfig);
return 1;
}
elektraAddUname (returned, parentKey);
errno = errnosave;
return 1;
}
int elektraUnameSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
ELEKTRA_LOG ("set uname %s from %s\n", keyName (parentKey), keyString (parentKey));
KeySet * info = ksNew (0, KS_END);
elektraAddUname (info, parentKey);
ELEKTRA_SET_ERROR_READ_ONLY (info, returned, parentKey);
return 0;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("uname",
ELEKTRA_PLUGIN_GET, &elektraUnameGet,
ELEKTRA_PLUGIN_SET, &elektraUnameSet,
ELEKTRA_PLUGIN_END
);
}