-3

I can't create BASH variable $path couse I get the error permission denied.

I want to execute this BASH script in C code:

#define AINinit "\
#!/bin/bash \n\
path=`/sys/devices/bone_capemgr.*/slots` \n\
echo <password> | sudo -S echo cape-bone-iio > $path \n\
"

When I'm trying to execute path=/sys/devices/bone_capemgr.*/slots I'm getting permission denied error. The /sys/devices/bone_capemgr.*/slots is the directory. I was trying to add chmod 777 to /sys/devices/bone_capemgr.*/slots and it still doesn't works.

I'm executing this command in:

system(AINinit);
Pukacza
  • 7
  • 7
  • 5
    Please create a [mre] – klutt Aug 29 '19 at 10:30
  • Is `/sys/devices/bone_capemgr.*/slots` a program you want to execute, or a file you want to `echo` a string into? – Steve Summit Aug 29 '19 at 10:32
  • There's almost no C code in your question. You need to show us what you're trying to do with this `AINinit` macro. – Steve Summit Aug 29 '19 at 10:44
  • In my experience, trying to programmatically feed a password into `sudo` doesn't work well (not to mention the egregious security concerns of embedding the root password in your program like this). A much better approach is to add specific commands allowed to be run (without password) by specific users to the [`sudoers`](https://linux.die.net/man/5/sudoers) file using [`visudo`](https://linux.die.net/man/8/visudo). (The only drawback is that the syntax of the `sudoers` file takes some learning.) – Steve Summit Aug 29 '19 at 10:49
  • You want `path=/sys/devices/bone_capemgr.*/slots` (without the backquotes). (Also, I'm worried about the wildcard `*` in there. You can probably get it to work that way, but it'd be far better if you could explicitly specify the exact path.) – Steve Summit Aug 29 '19 at 11:21
  • The exact path is `path=/sys/devices/bone_capemgr.[0-9]/slots` – Pukacza Aug 29 '19 at 11:35

1 Answers1

1

Assuming that you write the content of this macro to a file then call system() on that file, you have to use chmod(filename, 0755); before system() to make it executable.

(http://man7.org/linux/man-pages/man2/chmod.2.html)


Ouch, I just saw the edit in the question!
There is no chance that system(AINinit); could work as is.
system() requires a command line similar to what you would type in the terminal (not the content of a script file).

You need to use a file, as in the first part of this answer.

#define AINinit "\
#!/bin/bash \n\
path=`/sys/devices/bone_capemgr.*/slots` \n\
echo <password> | sudo -S echo cape-bone-iio > $path \n\
"
// convenient error checking omitted everywhere...
FILE *f=fopen("my_script", "w");
fputs(AINinit, f);
fclose(f);
chmod("my_script", 0755);
system("./my_script");

Alternatively you could change the macro so that you don't use a script file.

#define AINinit "\
echo <password> | sudo -S echo cape-bone-iio > `/sys/devices/bone_capemgr.*/slots`"
system(AINinit);

Note that, as mentioned in the comments, echoing the password is a dangerous solution (and which does not work well, for ssh it does not for example).


I think that the permission problem message does not come from the attempt to execute some code with system().
I guess it is due to a bad usage of redirections with sudo.
sudo a_command > a_file performs the direction to a_file before executing a_command with some privileges.
A very common workaround is a_command | sudo tee a_file in which a_command is executed without privileges but tee opens a_file with privileges.
The inconvenient is that standard output of a_command is also echoed on the terminal.
The command a_command | sudo dd of=a_file 2>/dev/null should do the trick if the echoing is a problem.

All of this has nothing in common with the original question.

prog-fh
  • 7,550
  • 1
  • 3
  • 21
  • Okay I get it. But how can I achieve the `.*/` as this is number that I'm looking for in C. Couse filename should be without the bash code. – Pukacza Aug 29 '19 at 10:58
  • @Pukacza Just use the back-quotes on the same command line – prog-fh Aug 29 '19 at 11:09
  • The chmod `chmod(path, 0755);` doesn't resolve the permission problem. The `path` is okay and it's `/sys/devices/bone_capemgr.9/slots`. – Pukacza Aug 29 '19 at 11:36
  • @Pukacza what is `path` in your `chmod()` call? – prog-fh Aug 29 '19 at 12:04
  • `*path` is exactly what I want which is: `/sys/devices/bone_capemgr.9/slots` I checked that in debugger. – Pukacza Aug 29 '19 at 12:16
  • @Pukacza then the `chmod()` must not consider this path but the file in which you wrote your script. If you apply my second suggestion in the answer (one single command provided to the `system()` call), then `chmod()` is not necessary since this script file no longer exists. – prog-fh Aug 29 '19 at 12:26
  • @Pukacza I added some code in the answer to try to make it more clear. – prog-fh Aug 29 '19 at 12:32
  • Even the script and command line doesn't work. Still permission problem. – Pukacza Aug 29 '19 at 12:48
  • `sh: 1: /sys/devices/bone_capemgr.9/slots: Permission denied sh: 1: cannot create : Directory nonexistent` – Pukacza Aug 29 '19 at 13:22
  • @Pukacza I added a third part in the answer. – prog-fh Aug 29 '19 at 13:24