|
a/src/execmd.cpp |
|
b/src/execmd.cpp |
|
... |
|
... |
18 |
#ifdef BUILDING_RECOLL
|
18 |
#ifdef BUILDING_RECOLL
|
19 |
#include "autoconfig.h"
|
19 |
#include "autoconfig.h"
|
20 |
#else
|
20 |
#else
|
21 |
#include "config.h"
|
21 |
#include "config.h"
|
22 |
#endif
|
22 |
#endif
|
|
|
23 |
|
|
|
24 |
#include <map>
|
23 |
|
25 |
|
24 |
#include <stdio.h>
|
26 |
#include <stdio.h>
|
25 |
#include <stdlib.h>
|
27 |
#include <stdlib.h>
|
26 |
#include <unistd.h>
|
28 |
#include <unistd.h>
|
27 |
#include <sys/stat.h>
|
29 |
#include <sys/stat.h>
|
|
... |
|
... |
488 |
for (it = args.begin(); it != args.end(); it++) {
|
490 |
for (it = args.begin(); it != args.end(); it++) {
|
489 |
argv[i++] = it->c_str();
|
491 |
argv[i++] = it->c_str();
|
490 |
}
|
492 |
}
|
491 |
argv[i] = 0;
|
493 |
argv[i] = 0;
|
492 |
|
494 |
|
|
|
495 |
// Environment. We first merge our environment and the specified
|
|
|
496 |
// variables in a map<string,string>, overriding existing values,
|
|
|
497 |
// then generate an appropriate char*[]
|
493 |
Ccharp *envv;
|
498 |
Ccharp *envv;
|
494 |
int envsize;
|
499 |
map<string, string> envmap;
|
495 |
for (envsize = 0; ; envsize++)
|
500 |
for (int i = 0; environ[i] != 0; i++) {
|
496 |
if (environ[envsize] == 0) {
|
501 |
string entry(environ[i]);
|
497 |
break;
|
502 |
string::size_type eqpos = entry.find_first_of("=");
|
|
|
503 |
if (eqpos == string::npos) {
|
|
|
504 |
continue;
|
|
|
505 |
}
|
|
|
506 |
envmap[entry.substr(0, eqpos)] = entry.substr(eqpos+1);
|
498 |
}
|
507 |
}
|
499 |
envv = (Ccharp *)malloc((envsize + m->m_env.size() + 2) * sizeof(char *));
|
508 |
for (const auto& entry : m->m_env) {
|
|
|
509 |
string::size_type eqpos = entry.find_first_of("=");
|
|
|
510 |
if (eqpos == string::npos) {
|
|
|
511 |
continue;
|
|
|
512 |
}
|
|
|
513 |
envmap[entry.substr(0, eqpos)] = entry.substr(eqpos+1);
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
// Allocate space for the array + string storage in one block.
|
|
|
517 |
unsigned int allocsize = (envmap.size() + 2) * sizeof(char *);
|
|
|
518 |
for (const auto& it : envmap) {
|
|
|
519 |
allocsize += it.first.size() + 1 + it.second.size() + 1;
|
|
|
520 |
}
|
|
|
521 |
envv = (Ccharp *)malloc(allocsize);
|
500 |
if (envv == 0) {
|
522 |
if (envv == 0) {
|
501 |
LOGERR("ExecCmd::doexec: malloc() failed. errno " << errno << "\n");
|
523 |
LOGERR("ExecCmd::doexec: malloc() failed. errno " << errno << "\n");
|
502 |
free(argv);
|
524 |
free(argv);
|
503 |
return -1;
|
525 |
return -1;
|
504 |
}
|
526 |
}
|
505 |
int eidx;
|
527 |
// Copy to new env array
|
506 |
for (eidx = 0; eidx < envsize; eidx++) {
|
528 |
i = 0;
|
507 |
envv[eidx] = environ[eidx];
|
529 |
char *cp = ((char *)envv) + (envmap.size() + 2) * sizeof(char *);
|
508 |
}
|
530 |
for (const auto& it : envmap) {
|
509 |
for (vector<string>::const_iterator it = m->m_env.begin();
|
531 |
strcpy(cp, (it.first + "=" + it.second).c_str());
|
510 |
it != m->m_env.end(); it++) {
|
|
|
511 |
envv[eidx++] = it->c_str();
|
532 |
envv[i++] = cp;
|
|
|
533 |
cp += it.first.size() + 1 + it.second.size() + 1;
|
512 |
}
|
534 |
}
|
513 |
envv[eidx] = 0;
|
535 |
envv[i++] = 0;
|
514 |
|
536 |
|
515 |
// As we are going to use execve, not execvp, do the PATH thing.
|
537 |
// As we are going to use execve, not execvp, do the PATH thing.
|
516 |
string exe;
|
538 |
string exe;
|
517 |
if (!which(cmd, exe)) {
|
539 |
if (!which(cmd, exe)) {
|
518 |
LOGERR("ExecCmd::startExec: " << (cmd) << " not found\n");
|
540 |
LOGERR("ExecCmd::startExec: " << (cmd) << " not found\n");
|