Daemon programming in Raspberry Pi

Jaakko Koivuniemi CERN/PH

April 11, 2014

Abstract command line interface, what is a daemon?, typical daemon structure

Raspberry Pi CERN Jaakko Koivuniemi April 12, 2014 Command line interface and C

• used in computers long before graphical user interface

• usually enough for servers that do not need friendly user interface

• can be efficient over slow network connection

• in most important operations can always be done from CLI

• allows shell scripting and programming

• rapid developement of working programs

Raspberry Pi at CERN 1 Jaakko Koivuniemi April 12, 2014 C get options

1 #include #include 3 #include

5 v o i d printusage() { 7 p r i n t f (”usage: numbers[ −nN][ −h] \ n”); } 9 i n t main(int argc, char ∗∗ argv ) 11 { i n t count=0; 13 i n t optch=0; 15 w h i l e(optch!= −1) { 17 optch=getopt(argc ,argv ,”n:h”); i f (optch==’h’)

Raspberry Pi at CERN 2 Jaakko Koivuniemi April 12, 2014

19 { printusage(); 21 r e t u r n 0; } 23 i f (optch==’n’) { 25 count=atoi(optarg); } 27 } 29 i n t i=0; 31 f o r(i=0;i

Raspberry Pi at CERN 3 Jaakko Koivuniemi April 12, 2014 Compile and test

1 pi@raspberrypi ˜ $ gcc −g −O −Wall numbers.c −o numbers pi@raspberrypi ˜ $ numbers −n 3 3 0 1 5 2

Raspberry Pi at CERN 4 Jaakko Koivuniemi April 12, 2014 What is a daemon?

• general service run in background

• examples: atd, crond, httpd, ifplugd, ntpd, sshd, udevd

1 pi@raspberrypi ˜ $ service −−s t a t u s−a l l pi@raspberrypi ˜ $ service ifplugd help 3 pi@raspberrypi ˜ $ service ifplugd status pi@raspberrypi ˜ $ sudo service ifplugd stop

Raspberry Pi at CERN 5 Jaakko Koivuniemi April 12, 2014 Writing a daemon program

• can read a configuration file from /etc/mydaemon conf

• writes a log file to /var/log/mydaemon.log, verbose level can be set from configuration file

• logrotate(8) can be used to rotate/compress log file

• typically /var/lib/mydaemon/ has the live data

• /var/run/mydaemon.pid process number

• daemon needs to catch signals like SIGHUP, SIGTERM

Raspberry Pi at CERN 6 Jaakko Koivuniemi April 12, 2014 • can be started and stopped manually with service mydaemon start/stop

• can be started or stopped by the system at different run levels

• can be written in C/C++ or as Perl/Python script, the first typically for mature projects and latter for fast developement with modern data structures

Raspberry Pi at CERN 7 Jaakko Koivuniemi April 12, 2014 Example: temperature logging

• temperature from tmp102 read with I2C bus

• data written to a log file /var/log/tmp102d.log

v o i d logmessage(const char logfile[200], const char message[200], int l o g l e v , int msglev) {} 2 i n t read d a t a (int address, int length) { r e t u r n0 } 4 v o i d read temp ( ) {} 6 i n t cont=1;/ ∗ main loop flag ∗/ 8 v o i d stop(int sig) 10 {

Raspberry Pi at CERN 8 Jaakko Koivuniemi April 12, 2014 sprintf(message ,”%d catched, stop”,sig); 12 logmessage(logfile ,message,loglev ,4); cont =0; 14 }

16 i n t main() { 18 sprintf(message ,”tmp102dv.%d started”,version); logmessage(logfile ,message,loglev ,4); 20 signal (SIGTERM,&stop) ; 22 w h i l e ( cont==1) 24 { read temp ( ) ; 26 s l e e p (60) ; } 28 r e t u r n 0; 30 }

Raspberry Pi at CERN 9 Jaakko Koivuniemi April 12, 2014 Daemonization

off the parent process and let it terminate

• setsid - create a new session

• catch signals

• chdir - change the of the daemon.

• umask - change the file mode mask according to the needs of the daemon

• close - close all open file descriptors that may be inherited from the parent process

Raspberry Pi at CERN 10 Jaakko Koivuniemi April 12, 2014 Automatic start at boot time

• shell script at /etc/.d/tmp102d

1 pi@raspberrypi ˜ $ sudo update−rc.d tmp102d defaults pi@raspberrypi ˜ $ sudo service tmp102d start/stop

Raspberry Pi at CERN 11 Jaakko Koivuniemi April 12, 2014 References

• http://en.wikipedia.org/wiki/Daemon (computing)

• http://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux

• https://github.com/oh7bf/tmp102 (to be done)

Raspberry Pi at CERN 12