-1

On couple of RHEL 5.8, I am facing problem with execve calls. The execve is not failing however, getting the following error:

/bin/bash: h: No such file or directory

I am using it the following way in C++ code:

::fork();
.
.
.
char achWritePipeDescriptor[8], achReadPipeDescriptor[8];
snprintf(achWritePipeDescriptor, sizeof(achWritePipeDescriptor), "%d", fWritePipeDescriptor);
    snprintf(achReadPipeDescriptor, sizeof(achReadPipeDescriptor), "%d", fReadPipeDescriptor);

// fWritePipeDescriptor and fReadPipeDescriptor are integers

::execl("/bin/sh", "/bin/sh", "Launcher.sh", achWritePipeDescriptor, achReadPipeDescriptor, (char*)0);

switch(errno)
{
default:
    printf("\n Failed to launch Launcher.sh\n");
    break;
}

The exec call here is not failing, but I was getting above error when trying to interact with the above script.sh. The strace output for this process looks like following:

execve("/bin/bash", ["/bin/bash", "h", "10", "6"], [/* 36 vars */]) = 0
.
.
.
open("/usr/share/locale/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
fstat(2, 0x7fff23708910)                = -1 EBADF (Bad file descriptor)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b9d1c785000
write(2, "/bin/bash: h: No such file or di"..., 40) = -1 EBADF (Bad file descriptor)
exit_group(127)                         = ?
Process 13030 detached

There is no file "h" being referenced here, but the last character of the script name is "h". Also, the script that I am trying to exec does exist at expected location and is having sufficient permissions.

I have no clue from where the "h" is coming.

I changed my execve call to following:

::execl("Launcher.sh", "Launcher.sh", achWritePipeDescriptor, achReadPipeDescriptor,(char*)0);

With this, it worked properly, but down the code below, I am seeing similar error for another execl call. The difference in the second exec is that the binary is different, but the error remains the same.

I am not sure, if this is server specific issue.

Any clues, any troubleshooting steps would be of great help.

My Bad, I should have mentioned it earlier itself, the <script.sh> is a placeholder. I tried to indicate that a shell script is what I am trying to exec. Also, writefd and readfd are char* here. It can be clearly seen in the strace output I have pasted here. Again, the exec is not failing here as i am not seeing the "Failed to launch Launcher.sh" message.

4

2 回答 2

1

除了MikeyB 解决的<script.sh>vs问题之外,这里还有另一个问题。"script.sh"另一个问题是writefdandreadfd被传递给execl. 给定名称,我假设它们是一个管道的写入端和另一个管道的读取端。如果是这种情况,writefd并且readfd是整数,而不是char*指针。

dup2使某些脚本通过管道与您的程序通信的最简单方法是dup2在调用execl. 该脚本只是从标准输入读取并写入标准输出。您根本不需要将这些文件描述符传递给脚本。

于 2012-10-31T13:28:32.037 回答
0
execl("/bin/sh", "/bin/sh", <script.sh>, writefd, readfd, NULL);

那是行不通的……<script.sh>不是有效的C++。

将其更改为:

execl("/bin/sh", "/bin/sh", "script.sh", writefd, readfd, NULL);
于 2012-10-31T12:10:52.970 回答