Debian – why do I have two different results if I run a program through terminal(as root) or /etc/init.d(or /etc/rc.local)

debianinit-scriptprivilegesroot

Background:

I use Debian Lenny on an embedded device

uname -a
Linux device 3.4.0 #83 Sun May 26 17:07:14 CEST 2013 armv4l GNU/Linux

I have a C code (say my_C_program) that calls a board specific binary file (via system("spictl someparameters") ) called spictl to use SPI interface

user:~# ls -al /usr/local/bin/spictl
lrwxrwxrwx  1 root staff      24 Jun  9  2011 spiflashctl -> /initrd/sbin/spiflashctl

if I run my code (my_C_program) from the command line

user:~# /user/sbin/my_C_program

the spictl is executed without problem and outputs data from the SPI interface.

Problem:

I need the program to be run when the board is powered. Therefore, I add /user/sbin/my_C_program line before the exit 0 at/etc/rc.local. When the board is powered, the my_C_program is executed and spictl is executed but the SPI interface does not output any data.

I tried to run the program via /etc/init.d/ script on this link. The script works fine and it executes the my_C_program, the program executes the spictl successfully (as system() return value says), but the SPI interface does not output any data!

ls -l /usr/sbin/my_C_program
-rwxrwxrwx 1 root root 61713 Jun 28  2013 /usr/sbin/my_C_program

top shows that the program is run as root

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                          
 1095 root      20   0  2524 1140  924 R  4.2  1.8   0:00.35 top                                                                              
 1033 root      RT   0 35092  34m 1852 S  3.0 56.5   0:14.06 node 

Question

if I execute the my_C_program on terminal, the program calls the spictl (via system(spictl someparameters)) and it is executed without any problem. But if I run my_C_program through /etc/rc.local or /etc/init.d script, then spictl does not work as it supposed to be. I suspect that it has something to do with the root privilege. When I execute the program on the terminal (as root) all works fine. But I guess /etc/rc.local and /etc/init.d somehow runs the program with a lower privilege. I could not really solve the difference between executing a command from the terminal as root or executing the program via /etc/rc.local or /etc/init.d/ script. If you also think that it is a privilege problem, could you please explain how can I ensure that init.d script or rc.local would run the program with the highest privilege. Or what could be the issue?

Please note that the problem/question is not SPI related.

P.S. in order not to have unnecessary conversation like "oh Debian lenny is too old, you should use wheezy etc.", the board has armv4l processor and as the producer says it does not support wheezy because of some processor instructions.

Best Answer

You're C executable probably requires some environment variables to be set to function. For example the env. variable $PATH or $LD_LIBRARY_PATH. There also other variables such as $HOME which won't be set until a user has logged in.

This last one might be necessary for your app to access config files and/or log files, for example.

Related Question