Tuesday 15 September 2015

c++ - Linux Daemon not working -



c++ - Linux Daemon not working -

i have created daemon linux in c++, however, kid process not seem doing anything. 1 time reaches if(pid > 0) statement, seems stop. code daemon.start() follows:

//process id , session id pid_t pid,sid; //fork off parent process pid = fork(); if(pid < 0) exit(exit_failure); //if pid good, exit parent process if(pid > 0) exit(exit_success); //change file mode mask umask(0); //create new sid kid process sid = setsid(); if(sid < 0) { exit(exit_failure); } //change current working directory if((chdir("/")) < 0) { //log failure exit(exit_failure); } //close out standard file descriptors close(stdin_fileno); close(stdout_fileno); close(stderr_fileno); //the main loop. globals::logerror("service started."); while(true) { //the service task globals::logerror("service working."); if(!systemconfiguration::isfirstrun() && !systemconfiguration::getmediaupdateready()) { syncserver(); } sleep(systemconfiguration::getserverconnectionfrequency()); //wait 30 seconds } exit(exit_success);

any help great! :)

i'm pretty sure kid process dies within either sid < 0 or chdir("/") < 0 if statement. write stderr in these cases before exit reveal problem is:

//create new sid kid process sid = setsid(); if(sid < 0) { fprintf(stderr,"failed create sid: %s\n",strerror(errno)); exit(exit_failure); } //change current working directory int chdir_rv = chdir("/"); if(chdir_rv < 0) { fprintf(stderr,"failed chdir: %s\n",strerror(errno)); exit(exit_failure); }

you need include <errno.h> , <string.h> in order have errno , strerror defined (respectively).

regards

c++ linux daemon

No comments:

Post a Comment