MCPcopy Create free account
hub / github.com/Icinga/icinga2 / ProcessSpawnImpl

Function ProcessSpawnImpl

lib/base/process.cpp:76–224  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74
75#ifndef _WIN32
76static Value ProcessSpawnImpl(struct msghdr *msgh, const Dictionary::Ptr& request)
77{
78 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
79
80 if (cmsg == nullptr || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_len != CMSG_LEN(sizeof(int) * 3)) {
81 std::cerr << "Invalid 'spawn' request: FDs missing" << std::endl;
82 return Empty;
83 }
84
85 auto *fds = (int *)CMSG_DATA(cmsg);
86
87 Array::Ptr arguments = request->Get("arguments");
88 Dictionary::Ptr extraEnvironment = request->Get("extraEnvironment");
89 bool adjustPriority = request->Get("adjustPriority");
90
91 // build argv
92 auto **argv = new char *[arguments->GetLength() + 1];
93
94 for (unsigned int i = 0; i < arguments->GetLength(); i++) {
95 String arg = arguments->Get(i);
96 argv[i] = strdup(arg.CStr());
97 }
98
99 argv[arguments->GetLength()] = nullptr;
100
101 // build envp
102 int envc = 0;
103
104 /* count existing environment variables */
105 while (environ[envc])
106 envc++;
107
108 auto **envp = new char *[envc + (extraEnvironment ? extraEnvironment->GetLength() : 0) + 2];
109 const char* lcnumeric = "LC_NUMERIC=";
110 const char* notifySocket = "NOTIFY_SOCKET=";
111 int j = 0;
112
113 for (int i = 0; i < envc; i++) {
114 if (strncmp(environ[i], lcnumeric, strlen(lcnumeric)) == 0) {
115 continue;
116 }
117
118 if (strncmp(environ[i], notifySocket, strlen(notifySocket)) == 0) {
119 continue;
120 }
121
122 envp[j] = strdup(environ[i]);
123 ++j;
124 }
125
126 if (extraEnvironment) {
127 ObjectLock olock(extraEnvironment);
128
129 for (const Dictionary::Pair& kv : extraEnvironment) {
130 String skv = kv.first + "=" + Convert::ToString(kv.second);
131 envp[j] = strdup(skv.CStr());
132 j++;
133 }

Callers 1

ProcessHandlerFunction · 0.85

Calls 5

icinga2_execvpeFunction · 0.85
CStrMethod · 0.80
sigactionClass · 0.70
GetMethod · 0.45
GetLengthMethod · 0.45

Tested by

no test coverage detected