0

The problem is as follows:

I have a task to write a driver for a quartz clock on a Raspberry Pi, the driver should allow concurrent access for reading/writing. Therefore I used semaphores to block access.

Now I want to test this, therefore I have to run

sudo hwclock -r -f ...

My idea was to write a program in C to make this test automatic, and use fork to run access concurrently. But I cannot run the program as superuser since I don't have those permissions on the Pi.

What I tried to do was:

system("echo pass\"word | sudo -S hwclock --set --date \"7/20/37 12:00:00\" -f /dev/<device>");

and

system("echo pass\x22word | sudo -S hwclock --set --date \"7/20/37 12:00:00\" -f /dev/<device>");

both give me the error

sh: 1: Syntax error: Unterminated quoated string

Running the program with "password" instead of "pass\"word" works without the error.

So what I need is some way to let the program "enter" the password for me.

Hopefully, someone can help me with this or point me in the right direction. Other ways to accomplish testing this are appreciated.

Student
  • 805
  • 1
  • 8
  • 11
  • 2
    Configure sudo so you don't need a password. https://stackoverflow.com/questions/25215604/use-sudo-without-password-inside-a-script Are you really planning on hardcoding your password into your source code?!?! You've already broadcast to quite an audience that your password has a `"` character in it. – Andrew Henle Jul 30 '18 at 12:09

1 Answers1

4

With "pass\"word" you only escape the double-quote for the C string, it's still not escaped in the shell. That means you try to execute echo pass"word, which indeed is an unterminated string.

You need to escape the double-quote in the shell-command being executed as well, as in "pass\\\"word". That will be the command echo pass\"word in the shell.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621