So, this code was working just fine until recently, when we decided to move it to a Lubuntu 12.04 system. The call to timer_settime returns EINVAL, and running it under gdb I’ve confirmed that all of the fields of ts are within 0 and 999999999 at the time that it is called:
1067 if(-1 ==timer_settime(tid,0,&ts,NULL))
(gdb) print ts
$1 = {it_interval = {tv_sec = 0, tv_nsec = 200000000}, it_value = {tv_sec = 0,
tv_nsec = 0}}
Since this should be the only thing that can cause it to return EINVAL I’m very much puzzled. Maybe there’s something obvious here that i’m missing.
struct sigevent sev;
struct itimerspec ts;
timer_t *tid;
//actually point the pointer at something.
tid = calloc(1,sizeof(timer_t));
//make sure there's no garbage in the structures.
memset(&sev,0,sizeof(struct sigevent));
memset(&ts,0, sizeof(struct itimerspec));
//notify via thread
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = SwitchThreadHandler;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = tid;
ts.it_value.tv_sec =0;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 200000000;
if(-1 == timer_create(CLOCK_REALTIME,&sev,tid))
{
retval = EX_SOFTWARE;
fprintf(stderr,"Failed to create timer.");
free(tid);
return retval;
}
if(-1 ==timer_settime(tid,0,&ts,NULL))
{
int errsv = errno;
fprintf(stderr,"timer_settime FAILED!!!\n");
if(errsv == EINVAL)
{
fprintf(stderr,"INVALID VALUE!\n");
}
else
{
fprintf(stderr,"UNKOWN ERROR: %d\n",errsv);
}
return EX_SOFTWARE;
}
timer_settimeis documented as taking atimer_tas the first argument, not atimer_t *liketimer_create. It will fail withEINVALiftimeridis invalid.Therefore, you should pass
*tidas the first argument.Note that your compiler should have issued a warning for this.