0

I am trying to run a bash script to generate an OpenSSL certificate. I have the bash script in the same directory as my c code.

Relevant C code:

pid_t pid = fork();
if(pid > 0){
    char* arr[] = {"./generate_cert.sh", "direct"};
    int succ = execv(arr[0], arr);
    printf("succ: %d\n", succ);
    exit(1);
}else if(pid < 0){
    printf("Fork failed\n");
    exit(-1);
}

generate_cert.sh, My test bash script which I will eventually expand is:

#!/bin/bash
echo "$1"

It seems I have a permission denied with ./generate_cert.sh. I need to instead run bash generate_cert.sh. How do I do this with execv?

4
  • Possible duplicate of System call fork() and execv function, Using execv to do basic I/O, etc. Commented Oct 31, 2019 at 4:36
  • 1
    That is not an error. use perror("execv failed") to get the actual error. Commented Oct 31, 2019 at 4:37
  • Thank you for that tip on perror. I am indeed getting a permission denied. Is there a way to instead run bash generate_cert.sh? Commented Oct 31, 2019 at 4:43
  • If I substitute "bash generate_cert.sh" for the first argument, I get error: "no such file or directory" since I am now not pointing to the script location. Commented Oct 31, 2019 at 4:44

1 Answer 1

1

According to the execv man page, the array must be terminated by a null pointer.

So something along the lines of this (untested):

  char* arr[] = {"./generate_cert.sh", "direct",NULL};
  int succ = execv(arr[0], arr);

Edit:

OP's problem turned out to be the execute bit wasn't set on the script file. Which was solved by chmod +x generate_cert.sh , however another alternative would be to make the execv call to /bin/bash instead. I.e something along the lines of.

char* arr[] = {"/bin/bash", "./generate_cert.sh", "direct",NULL};
int succ = execv(arr[0], arr);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and I am still getting the error. When I try to run ./generate_cert.sh direct in my terminal I get "-bash: ./generate_cert.sh: Permission denied". I can however run bash generate_cert.sh direct and it runs successfully. Could this be the problem?
Yes that is a problem, you need chmod +x generate_cert.sh to make the script executable
That was my problem. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.