Specjalistyczne rozwiązania informatyczne

date fails after fork and execl

One of my deamons set system time.

pid=fork();
if (!pid)
{
 execl("/bin/date","date","2009-01-01 00:00:00",NULL);
 exit(1);
}
pid=waitpid(pid,&wait_status,0);

WEXITSTATUS(wait_status) returned 1. The same code executed from bash console works fine. The problem was in my deamon. It has closed both STDOUT and STDERR, which is not acceptable by /bin/date. You have to reopen them at least with /dev/null before calling execl:

int devnull=open("/dev/null",O_WRONLY);
dup2(devnull,STDOUT);
dup2(devnull,STDERR);
if(devnull!=STDOUT && devnull!=STDERR)
  close(devnull);

   © KLASTER 2017