forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopts_sysctl.h
79 lines (65 loc) · 1.28 KB
/
gopts_sysctl.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @file
*
* @brief Source for opts plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef ELEKTRA_GOPTS_SYSCTL_H
#define ELEKTRA_GOPTS_SYSCTL_H
#include <sys/types.h> // has to be included before sys/sysctl.h
#include <string.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <kdbhelper.h>
extern char ** environ;
static int loadArgs (char *** argvp)
{
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_ARGS;
mib[3] = getpid ();
size_t size;
if (sysctl (mib, 4, NULL, &size, NULL, 0) == -1)
{
return 0;
}
char * buf = elektraMalloc (size);
if (sysctl (mib, 4, buf, &size, NULL, 0) == -1)
{
elektraFree (buf);
return 0;
}
size_t pos = 0;
int argc = 0;
while (pos < size)
{
pos += strlen (buf + pos) + 1;
++argc;
}
char ** argv = elektraMalloc (argc * sizeof (char *));
argv[0] = buf;
pos = strlen (buf) + 1;
for (int i = 1; i < argc; ++i)
{
argv[i] = buf + pos;
pos += strlen (buf + pos) + 1;
}
*argvp = argv;
return argc;
}
static char ** loadEnvp (void)
{
return environ;
}
static void cleanupArgs (int argc ELEKTRA_UNUSED, char ** argv)
{
elektraFree (argv[0]);
elektraFree (argv);
}
static void cleanupEnvp (char ** envp ELEKTRA_UNUSED)
{
}
#endif // ELEKTRA_GOPTS_SYSCTL_H