|
a/src/utils/execmd.cpp |
|
b/src/utils/execmd.cpp |
|
... |
|
... |
171 |
{
|
171 |
{
|
172 |
ExecCmdRsrc(this);
|
172 |
ExecCmdRsrc(this);
|
173 |
}
|
173 |
}
|
174 |
|
174 |
|
175 |
int ExecCmd::startExec(const string &cmd, const list<string>& args,
|
175 |
int ExecCmd::startExec(const string &cmd, const list<string>& args,
|
176 |
bool has_input, bool has_output)
|
176 |
bool has_input, bool has_output)
|
177 |
{
|
177 |
{
|
178 |
{ // Debug and logging
|
178 |
{ // Debug and logging
|
179 |
string command = cmd + " ";
|
179 |
string command = cmd + " ";
|
180 |
for (list<string>::const_iterator it = args.begin();it != args.end();
|
180 |
for (list<string>::const_iterator it = args.begin();it != args.end();
|
181 |
it++) {
|
181 |
it++) {
|
|
... |
|
... |
253 |
{}
|
253 |
{}
|
254 |
virtual int data(NetconData *con, Netcon::Event reason)
|
254 |
virtual int data(NetconData *con, Netcon::Event reason)
|
255 |
{
|
255 |
{
|
256 |
if (!m_input) return -1;
|
256 |
if (!m_input) return -1;
|
257 |
LOGDEB1(("ExecWriter: input m_cnt %d input length %d\n", m_cnt,
|
257 |
LOGDEB1(("ExecWriter: input m_cnt %d input length %d\n", m_cnt,
|
258 |
m_input->length()));
|
258 |
m_input->length()));
|
259 |
if (m_cnt >= m_input->length()) {
|
259 |
if (m_cnt >= m_input->length()) {
|
260 |
// Fd ready for more but we got none.
|
260 |
// Fd ready for more but we got none.
|
261 |
if (m_provide) {
|
261 |
if (m_provide) {
|
262 |
m_provide->newData();
|
262 |
m_provide->newData();
|
263 |
if (m_input->empty()) {
|
263 |
if (m_input->empty()) {
|
264 |
return 0;
|
264 |
return 0;
|
265 |
} else {
|
265 |
} else {
|
266 |
m_cnt = 0;
|
266 |
m_cnt = 0;
|
267 |
}
|
267 |
}
|
268 |
LOGDEB2(("ExecWriter: provide m_cnt %d input length %d\n",
|
268 |
LOGDEB2(("ExecWriter: provide m_cnt %d input length %d\n",
|
269 |
m_cnt, m_input->length()));
|
269 |
m_cnt, m_input->length()));
|
270 |
} else {
|
270 |
} else {
|
271 |
return 0;
|
271 |
return 0;
|
272 |
}
|
272 |
}
|
273 |
}
|
273 |
}
|
274 |
int ret = con->send(m_input->c_str() + m_cnt,
|
274 |
int ret = con->send(m_input->c_str() + m_cnt,
|
|
... |
|
... |
544 |
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n", errno));
|
544 |
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n", errno));
|
545 |
exit(1);
|
545 |
exit(1);
|
546 |
}
|
546 |
}
|
547 |
|
547 |
|
548 |
// Fill up argv
|
548 |
// Fill up argv
|
549 |
argv[0] = path_getsimple(cmd).c_str();
|
549 |
argv[0] = cmd.c_str();
|
550 |
int i = 1;
|
550 |
int i = 1;
|
551 |
list<string>::const_iterator it;
|
551 |
list<string>::const_iterator it;
|
552 |
for (it = args.begin(); it != args.end(); it++) {
|
552 |
for (it = args.begin(); it != args.end(); it++) {
|
553 |
argv[i++] = it->c_str();
|
553 |
argv[i++] = it->c_str();
|
554 |
}
|
554 |
}
|
|
... |
|
... |
570 |
execvp(cmd.c_str(), (char *const*)argv);
|
570 |
execvp(cmd.c_str(), (char *const*)argv);
|
571 |
// Hu ho
|
571 |
// Hu ho
|
572 |
LOGERR(("ExecCmd::doexec: execvp(%s) failed. errno %d\n", cmd.c_str(),
|
572 |
LOGERR(("ExecCmd::doexec: execvp(%s) failed. errno %d\n", cmd.c_str(),
|
573 |
errno));
|
573 |
errno));
|
574 |
_exit(127);
|
574 |
_exit(127);
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
ReExec::ReExec(int argc, char *args[])
|
|
|
578 |
{
|
|
|
579 |
init(argc, args);
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
void ReExec::init(int argc, char *args[])
|
|
|
583 |
{
|
|
|
584 |
for (int i = 0; i < argc; i++) {
|
|
|
585 |
m_argv.push_back(args[i]);
|
|
|
586 |
}
|
|
|
587 |
m_cfd = open(".", 0);
|
|
|
588 |
char *cd = getcwd(0, 0);
|
|
|
589 |
if (cd)
|
|
|
590 |
m_curdir = cd;
|
|
|
591 |
free(cd);
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
void ReExec::reexec()
|
|
|
595 |
{
|
|
|
596 |
char *cwd;
|
|
|
597 |
cwd = getcwd(0,0);
|
|
|
598 |
FILE *fp = stdout; //fopen("/tmp/exectrace", "w");
|
|
|
599 |
if (fp) {
|
|
|
600 |
fprintf(fp, "reexec: pwd: [%s] args: ", cwd?cwd:"getcwd failed");
|
|
|
601 |
for (vector<string>::const_iterator it = m_argv.begin();
|
|
|
602 |
it != m_argv.end(); it++) {
|
|
|
603 |
fprintf(fp, "[%s] ", it->c_str());
|
|
|
604 |
}
|
|
|
605 |
fprintf(fp, "\n");
|
|
|
606 |
}
|
|
|
607 |
if (m_cfd < 0 || fchdir(m_cfd) < 0) {
|
|
|
608 |
if (fp) fprintf(fp, "fchdir failed, trying chdir\n");
|
|
|
609 |
if (!m_curdir.empty() && chdir(m_curdir.c_str())) {
|
|
|
610 |
if (fp) fprintf(fp, "chdir failed too\n");
|
|
|
611 |
}
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
// Close all descriptors except 0,1,2
|
|
|
615 |
libclf_closefrom(3);
|
|
|
616 |
|
|
|
617 |
// Allocate arg vector (1 more for final 0)
|
|
|
618 |
typedef const char *Ccharp;
|
|
|
619 |
Ccharp *argv;
|
|
|
620 |
argv = (Ccharp *)malloc((m_argv.size()+1) * sizeof(char *));
|
|
|
621 |
if (argv == 0) {
|
|
|
622 |
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n", errno));
|
|
|
623 |
return;
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
// Fill up argv
|
|
|
627 |
int i = 0;
|
|
|
628 |
vector<string>::const_iterator it;
|
|
|
629 |
for (it = m_argv.begin(); it != m_argv.end(); it++) {
|
|
|
630 |
argv[i++] = it->c_str();
|
|
|
631 |
}
|
|
|
632 |
argv[i] = 0;
|
|
|
633 |
execvp(m_argv[0].c_str(), (char *const*)argv);
|
575 |
}
|
634 |
}
|
576 |
|
635 |
|
577 |
////////////////////////////////////////////////////////////////////
|
636 |
////////////////////////////////////////////////////////////////////
|
578 |
#else // TEST
|
637 |
#else // TEST
|
579 |
#include <stdio.h>
|
638 |
#include <stdio.h>
|
|
... |
|
... |
591 |
static int op_flags;
|
650 |
static int op_flags;
|
592 |
#define OPT_MOINS 0x1
|
651 |
#define OPT_MOINS 0x1
|
593 |
#define OPT_b 0x4
|
652 |
#define OPT_b 0x4
|
594 |
#define OPT_w 0x8
|
653 |
#define OPT_w 0x8
|
595 |
#define OPT_c 0x10
|
654 |
#define OPT_c 0x10
|
|
|
655 |
#define OPT_r 0x20
|
596 |
|
656 |
|
597 |
const char *data = "Une ligne de donnees\n";
|
657 |
const char *data = "Une ligne de donnees\n";
|
598 |
class MEAdv : public ExecCmdAdvise {
|
658 |
class MEAdv : public ExecCmdAdvise {
|
599 |
public:
|
659 |
public:
|
600 |
ExecCmd *cmd;
|
660 |
ExecCmd *cmd;
|
|
... |
|
... |
636 |
};
|
696 |
};
|
637 |
|
697 |
|
638 |
|
698 |
|
639 |
static char *thisprog;
|
699 |
static char *thisprog;
|
640 |
static char usage [] =
|
700 |
static char usage [] =
|
641 |
"trexecmd [-c] cmd [arg1 arg2 ...]\n"
|
701 |
"trexecmd [-c|-r] cmd [arg1 arg2 ...]\n"
|
642 |
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
|
702 |
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
|
|
|
703 |
" -r : test reexec\n"
|
643 |
"trexecmd -w cmd : do the which thing\n"
|
704 |
"trexecmd -w cmd : do the which thing\n"
|
644 |
;
|
705 |
;
|
645 |
static void Usage(void)
|
706 |
static void Usage(void)
|
646 |
{
|
707 |
{
|
647 |
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
708 |
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
648 |
exit(1);
|
709 |
exit(1);
|
649 |
}
|
710 |
}
|
650 |
|
711 |
|
|
|
712 |
ReExec reexec;
|
|
|
713 |
|
651 |
int main(int argc, char **argv)
|
714 |
int main(int argc, char *argv[])
|
652 |
{
|
715 |
{
|
|
|
716 |
reexec.init(argc, argv);
|
653 |
thisprog = argv[0];
|
717 |
thisprog = argv[0];
|
654 |
argc--; argv++;
|
718 |
argc--; argv++;
|
655 |
|
719 |
|
656 |
while (argc > 0 && **argv == '-') {
|
720 |
while (argc > 0 && **argv == '-') {
|
657 |
(*argv)++;
|
721 |
(*argv)++;
|
658 |
if (!(**argv))
|
722 |
if (!(**argv))
|
659 |
/* Cas du "adb - core" */
|
723 |
/* Cas du "adb - core" */
|
660 |
Usage();
|
724 |
Usage();
|
661 |
while (**argv)
|
725 |
while (**argv)
|
662 |
switch (*(*argv)++) {
|
726 |
switch (*(*argv)++) {
|
|
|
727 |
case 'c': op_flags |= OPT_c; break;
|
|
|
728 |
case 'r': op_flags |= OPT_r; break;
|
663 |
case 'w': op_flags |= OPT_w; break;
|
729 |
case 'w': op_flags |= OPT_w; break;
|
664 |
case 'c': op_flags |= OPT_c; break;
|
|
|
665 |
default: Usage(); break;
|
730 |
default: Usage(); break;
|
666 |
}
|
731 |
}
|
667 |
b1: argc--; argv++;
|
732 |
b1: argc--; argv++;
|
668 |
}
|
733 |
}
|
669 |
|
734 |
|
|
... |
|
... |
673 |
string cmd = *argv++; argc--;
|
738 |
string cmd = *argv++; argc--;
|
674 |
list<string> l;
|
739 |
list<string> l;
|
675 |
while (argc > 0) {
|
740 |
while (argc > 0) {
|
676 |
l.push_back(*argv++); argc--;
|
741 |
l.push_back(*argv++); argc--;
|
677 |
}
|
742 |
}
|
678 |
|
|
|
679 |
DebugLog::getdbl()->setloglevel(DEBDEB1);
|
743 |
DebugLog::getdbl()->setloglevel(DEBDEB1);
|
680 |
DebugLog::setfilename("stderr");
|
744 |
DebugLog::setfilename("stderr");
|
681 |
signal(SIGPIPE, SIG_IGN);
|
745 |
signal(SIGPIPE, SIG_IGN);
|
|
|
746 |
|
|
|
747 |
if (op_flags & OPT_r) {
|
|
|
748 |
chdir("/");
|
|
|
749 |
argv[0] = strdup("");
|
|
|
750 |
sleep(1);
|
|
|
751 |
reexec.reexec();
|
|
|
752 |
}
|
|
|
753 |
|
682 |
if (op_flags & OPT_w) {
|
754 |
if (op_flags & OPT_w) {
|
683 |
string path;
|
755 |
string path;
|
684 |
if (ExecCmd::which(cmd, path)) {
|
756 |
if (ExecCmd::which(cmd, path)) {
|
685 |
cout << path << endl;
|
757 |
cout << path << endl;
|
686 |
exit(0);
|
758 |
exit(0);
|