1

I am writing code for Linux on a x86-64 machine. It is working perfectly on x86-64, but I ported the same code to an ARM based device and now I am getting error: Invalid argument for timer code. Following is my timer code:

/**
* FUNCTION NAME : startRetryCount
* @brief start the retry count
* @param isAuth: if true start retry for Auth message else for Normal message
* @return true if success and false if not
*/
bool startRetryCount(bool isAuth)
{
    m_RetryAuth = isAuth;
    stopRetryCount();

    struct sigevent sigev;
    struct itimerspec itval;
    struct itimerspec oitval;
    struct sigaction sigact;

    if(sigemptyset(&sigact.sa_mask) == -1)
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< sigemptyset >>>> : %s\n",
                            __FILE__,__LINE__,__func__,strerror(errno));
    }

    sigact.sa_flags = SA_SIGINFO;
    sigact.sa_sigaction = signalHandler;

    m_RetryCount = ConfigureManager_SingleTon::getInstance()->getRetryCount();
    printLog(LOG_INFO,"**Retry Count Started**\n"); 
    printLog(LOG_INFO,"Number of Retry Count:%d\n",m_RetryCount);
    printLog(LOG_INFO,"Retry time is:%d\n",
            ConfigureManager_SingleTon::getInstance()->getRetryTime());

    // set up sigaction to catch signal
    if (sigaction(SIGTIMER, &sigact, NULL) == -1)
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< time_settime >>>> : %s\n",
                            __FILE__,__LINE__,__func__,strerror(errno));
        return false;
    }

    //Create the POSIX timer to generate signo
    sigev.sigev_notify = SIGEV_SIGNAL;
    sigev.sigev_signo = SIGRTMAX;
    sigev.sigev_value.sival_int = RETRY_ID;

    if (timer_create(CLOCK_REALTIME, &sigev, &m_RetryTimerId) == 0)
     {
        itval.it_value.tv_sec = ConfigureManager_SingleTon::getInstance()->getRetryTime();
        itval.it_value.tv_nsec = 0L;
        itval.it_interval.tv_sec = itval.it_value.tv_sec;
        itval.it_interval.tv_nsec = itval.it_value.tv_nsec;

        if (timer_settime(m_RetryTimerId, 0, &itval, &oitval) != 0)
        {
            Utility_SingleTon::printLog(LOG_ERROR,"[%s:%d#%s] <<<< time_settime >>>> %s\n",
                                __FILE__, __LINE__,__func__,strerror( errno ));
            return false;
        }
    }
    else
    {
        printLog(LOG_ERROR,"[%s:%d#%s] <<<< timer_create >>>> %s",
                                __FILE__, __LINE__,__func__,strerror( errno ));
        return false;
    }
    return true;
}

/**
* FUNCTION NAME : stopRetryCount
* @brief stop the retry count
*/
voidstopRetryCount()
{
    if(m_RetryTimerId != NULL) 
    {
        if(timer_delete(m_RetryTimerId) != 0)
        {
            printLog(LOG_ERROR,"[%s:%d#%s] Timer delete error [%d]=%s\n",__FILE__, __LINE__,__func__,errno,strerror(errno));
        }
        m_RetryCount = 0;
        m_RetryTimerId = NULL;
        printLog(LOG_INFO,"Retry Timer Stopped!\n");
    }
}

Print log of errors is:

[src/manager.cpp:686#stopRetryCount] Timer delete error [22]=Invalid argument

I have tried a lot to find what is bothering the ARM based device, but unfortunately I am not able to resolve the issues. Any help would be highly appreciated.

Edited:

To simulate my condition, I have created sample program, now I came to know that on mistral SAM-9 processor Timer and thread combinedly not working, If I start the timer in the main thread and if I tries to stop that timer through child thread, then it is giving that error. Here is the sample.cpp file source:

#include "devicetimer.h"


int DeviceTimer::counter = 0;

timer_t DeviceTimer::testTimer1Id = 0;

void DeviceTimer::timer1Stop()
{

    if(testTimer1Id != 0)
    {
        if(timer_delete(testTimer1Id) == -1)
        {
            printf("Timer Delete Error\nError Number = %d\nError Message = %s\n",errno,strerror(errno));
            exit(1);
        }
        else
        {
            printf("Timer Delete Successfully\n");
        }       
    }
}

void DeviceTimer::signalHandler(int signo, siginfo_t* info, void* context)
{
    if (signo == SIGTIMER)
    {
        printf("counter %d\n\n",++counter);
    }
}

int DeviceTimer::timer1start()
{
    struct sigevent sigev; //signal event struct
    struct itimerspec itval;
    struct itimerspec oitval;
    struct sigaction sigact;


    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = SA_SIGINFO;
    sigact.sa_sigaction = signalHandler;

    // set up sigaction to catch signal
    if (sigaction(SIGTIMER, &sigact, NULL) == -1)
    {
        printf("time_settime error \n");
        return -1;
    }

    //Create the POSIX timer to generate signo
    sigev.sigev_notify = SIGEV_SIGNAL;
    sigev.sigev_signo = SIGTIMER;
    sigev.sigev_value.sival_int = 2;

    if (timer_create(CLOCK_REALTIME, &sigev, &testTimer1Id) == 0)
     {
        itval.it_value.tv_sec = 1;
        itval.it_value.tv_nsec = 0L;
        itval.it_interval.tv_sec = itval.it_value.tv_sec;
        itval.it_interval.tv_nsec = itval.it_value.tv_nsec;

        if (timer_settime(testTimer1Id, 0, &itval, &oitval) != 0)
        {
            printf("Error in set time \n");
            return -2;
        }
    }
    else
    {
        printf("Error in creating timer \n");
        return -3;
    }
    return 0;
}


void* DeviceTimer::threadFunction(void* data)
{
    DeviceTimer *deviceTimer = (DeviceTimer *) data;

    while(1)
    {
        printf(".\n");
        usleep(100000);
        if(counter > 25)
        {
            deviceTimer->timer1Stop();
            break;
        }
    }   
    return NULL;
}


void DeviceTimer::startThread()
{
    if(pthread_create(&m_Thread, NULL, DeviceTimer::threadFunction, this) != 0)
    {
        printf("Error in creating Thread\n");
        exit(1);
    }
}

int main()
{
    DeviceTimer deviceTimer;

    deviceTimer.timer1start();
    deviceTimer.startThread();

    while(1)
    {
    }

    return 0;   
}

And following is the output:

counter 26
Timer Delete Error
Error Number = 22
Error Message = Invalid argument

If anyone experinced the same problem then, please help me to resolve this issue.

Thanks & BR, Yuvi

Yuvi
  • 1,344
  • 4
  • 24
  • 46
  • 1
    As there are many places where you could get the error, can you tell _which_ of the system calls gives the error? – Some programmer dude Jun 13 '12 at 06:59
  • 1
    oh.. I forgot to add that, I am getting error in delete_timer() and close(). – Yuvi Jun 13 '12 at 07:03
  • updated the same in code as well – Yuvi Jun 13 '12 at 07:06
  • Also not the you have a possible illegal array access here: `buffer[receivedByte] = '\0';`. What if the receive operation failed? Then you try to do `buffer[-1] = '\0';`. – Some programmer dude Jun 13 '12 at 07:15
  • 1
    You do not check the return value of `timer_delete` so you don't really know if it's in error or not. Remember that a function don't have to reset `errno` if it succeeds, so `errno` can only be trusted if a function fails. – Some programmer dude Jun 13 '12 at 07:22
  • Which `close`? You have several. And as my previous comment, you don't check the return value of `close` so really don't know if it's an error from your `close` call or from other older error. – Some programmer dude Jun 13 '12 at 07:24
  • It probably isn't from `close`. EINVAL isn't listed as a valid error from that call. – Duck Jun 13 '12 at 07:31
  • If you're using an UDP socket you should create it as ``socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)``. – Fred Jun 13 '12 at 07:38
  • @JoachimPileborg I am printing those error, yet I have to handle error.. Indeed can you or anyone one tell me the correct reason that causes error.. Here I want to repeat that code is running on x86-64 machine, but not on ARM based device – Yuvi Jun 13 '12 at 07:43
  • 1
    You must check the return from every system call, otherwise you don't know if there is an error or not. You can't just print out `errno` after a call without you knowing there was an error, because that call might not be cause of `errno` being set. – Some programmer dude Jun 13 '12 at 07:49
  • @JoachimPileborg updated the same, the error is there only, as you can see I have already handled errors except that delete and close.. – Yuvi Jun 13 '12 at 08:01
  • @Fred have a look here http://stackoverflow.com/questions/1955198/when-is-ipproto-udp-required – Yuvi Jun 13 '12 at 08:03
  • 1
    The `close` system call should not return with error `EINVAL` that you get, some other function that you don't check must set it. – Some programmer dude Jun 13 '12 at 08:08
  • Check the return of EVERY setsockopt(), close() or other OS call you make. EACH TIME, ALL THE TIME. OK? – Prof. Falken Jun 13 '12 at 08:12
  • @JoachimPileborg thanks for your valuable sugesstion, I have handled all the system call, and now came to conculsion that timer_delete is the only function call which is throwing the error. close function was called successfully... – Yuvi Jun 13 '12 at 09:28
  • 1
    @AmigableClarkKant thanks, have done in all source files, error is in timer_delete only... – Yuvi Jun 13 '12 at 09:29
  • Then, you print exactly *what* timer_delete() returns (probably -1) and immediately also check errno, before errno is destroyed by some other OS call. – Prof. Falken Jun 13 '12 at 10:10
  • yes, it is giving -1, as a ramification evnent is triggering again and again.. – Yuvi Jun 13 '12 at 10:16
  • @AmigableClarkKant edited code, please have a look now. – Yuvi Jun 19 '12 at 05:25
  • @JoachimPileborg edited code, please have a look now. – Yuvi Jun 19 '12 at 05:25

1 Answers1

1

Make sure your timer ID is the proper type (timer_t). If you're accidentally using a different type, errors like these might happen since timer_delete() isn't able to recognize the ID.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    here is my declaration `timer_t m_RetryTimerId;// timer id for Retry timer` – Yuvi Jun 13 '12 at 09:39
  • can you tell me what may be other scenario in which delete_timer can throw `Invalid argument Error` – Yuvi Jun 13 '12 at 10:39
  • can you please tell what could be reason that this code is working on Linux x86-64 but not on arm machine ... – Yuvi Jun 14 '12 at 07:49