I am trying to write my own shell in C. The code below works for commands without a pipe but otherwise does not.
Running valgrind with –trace-children=yes and –track-origins=yes give me a “Syscall param execve(argv) points to uninitialised byte(s)” (See the full error below).
In the relevant method (see makeargs below) valgrind is telling me “Uninitialised value was created by a heap allocation” at this line “argv = (char *)malloc((count+1) * sizeof(char*));”
Using my test input of “ls | sort” valgrind says that a “block of size 12 alloc’d”. I do not see how this is possible because ls and sort each make a call to makeargs and both should be 8 bytes allocated beacause there should be 4 bytes for the char* then 4 for the (char*)NULL that execvp needs at the end of the argument array.
The program hangs after executing this command.
I am not sure why this is happening because it works if there is only one call to makeargs (no pipes). Any input would be appreciated.
void execCommand(char** commandParts, int pipeCount)
{
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;
int numCommands = pipeCount + 1;
int newfds[2];
int oldfds[2];
int k = 0;
for(k; k < numCommands; k++)
{
//more commands exist
if(k < pipeCount)
{
if (pipe(newfds) == -1)
{
perror("new pipe error");
exit(EXIT_FAILURE);
}
}
if(fork() == 0) //child
{
//is prev command
if(k > 0)
{
dup2(oldfds[PIPE_READ], STDIN_FILENO);
close(oldfds[PIPE_READ]);
close(oldfds[PIPE_WRITE]);
}
//more commands exist
if(k < pipeCount)
{
close(newfds[PIPE_READ]);
dup2(newfds[PIPE_WRITE], STDOUT_FILENO);
close(newfds[PIPE_WRITE]);
}
char** args = NULL;
int argcount = makeargs(commandParts[k], &args);
if(execvp(args[0], args) == -1)
{
printf("%s: command not found \n", args[0]);
}
}
else //parent
{
int status;
waitpid(-1, &status, NULL);
//is prev command
if(k > 0)
{
close(oldfds[PIPE_READ]);
close(oldfds[PIPE_WRITE]);
}
//more commands exist
if(k < pipeCount)
{
oldfds[PIPE_READ] = newfds[PIPE_READ];
oldfds[PIPE_WRITE] = newfds[PIPE_WRITE];
}
}
//there are pipes
if(pipeCount > 0 && k > 0)
{
close(newfds[PIPE_READ]);
close(newfds[PIPE_WRITE]);
}
// if(argcount > 0)
// cleanArgs(argcount, args);
}
}
the make args method that gets called
int makeargs(char *s, char *** argv)
{
stripLeadingAndTrailingSpaces(s);
int k =0, count = 0;
for(k; k < strlen(s); k++)
{
if(s[k] == ' ')
count++;
}
count++;
char* parts = strtok (s," ");
strip(parts);
*argv = (char **)malloc((count+1) * sizeof(char*));
(*argv)[0] = (char *)malloc(strlen(parts)+1);
strcpy((*argv)[0], parts);
int i = 1;
for(i; i < count; i++)
{
parts = strtok (NULL, " ");
if(parts != NULL)
{
strip(parts);
(*argv)[i] = (char *)malloc(strlen(parts)+1);
strcpy((*argv)[i], parts);
}
}
(*argv)[count] = NULL;
return count;
}
valgrind output
==3603== Syscall param execve(argv) points to uninitialised byte(s)
==3603== at 0x40E2CDF: execve (execve.c:60)
==3603== by 0x40E314E: execvp (execvp.c:30)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
==3603== Address 0x41c617c is 4 bytes inside a block of size 12 alloc'd
==3603== at 0x4028876: malloc (vg_replace_malloc.c:236)
==3603== by 0x8049416: makeargs (ush.c:100)
==3603== by 0x8048E61: execCommand (cscd340_s12_hw2.c:191)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
==3603== Uninitialised value was created by a heap allocation
==3603== at 0x4028876: malloc (vg_replace_malloc.c:236)
==3603== by 0x8049416: makeargs (ush.c:100)
==3603== by 0x8048E61: execCommand (cscd340_s12_hw2.c:191)
==3603== by 0x8049069: main (cscd340_s12_hw2.c:250)
Finally, I tracked the problem to my stripLeadingAndTrailingSpaces(s);
I had
but needed
There were a couple of problems with the old strip method: