The select will update the time spec: tvp.  So if it did timeout the values
will be 0.
You need to reset the values after each timeout.

If the select is interrupted and does not time out, then the remaining time
will be in the tvp.

On Thu, May 21, 2020 at 2:10 PM Himanshu Sharma <
himanshusharma1...@gmail.com> wrote:

> +  forum
>
> On Thu, May 21, 2020 at 11:22 PM Himanshu Sharma <
> himanshusharma1...@gmail.com> wrote:
>
>> Hi Bill,
>>
>> Thanks for the response.
>> I am now able to reconnect to the Master Agent(snmpd) after adding
>> run_alarms().
>> and passing tvp as an argument to select() function as have mentioned.
>> I have made tvp as 5 second but the moment it tvp is passed to
>> snmp_select_info() as a argument it becomes zero.
>> What happens now is select get's timed out immediately, even though it is
>> configured to timeout for 5 seconds.
>>  I have tried making *fakeblock =0 as well as 1*.
>>
>> Here is the code snippet
>>
>>
>>  Regards
>> -Himanshu
>>
>>
>>
>>
>>
>> *struct timeval  tvp;    tvp.tv_sec = 5;    tvp.tv_usec = 0;*    int
>>  result;
>>     int  fakeblock = 0;
>>     maxFd = 0;
>>
>>     while (keepRunning_)
>>     {
>>         /* if you use select(), see snmp_select_info() in snmp_api(3) */
>>         /*     --- OR ---  */
>>         //agent_check_and_process(1); /* 0 == don't block */
>>         maxFd = *eventPipe_;
>>         FD_ZERO(&fdset);
>>         snmp_select_info(&maxFd, &fdset, &tvp, &fakeblock);
>>         FD_SET(*eventPipe_, &fdset);
>>         maxFd = (std::max(maxFd, *eventPipe_));
>>         *printf("select value of tv_sec %d error, tv_usec =
>> %d\n",(int32_t)tvp.tv_sec,(int32_t)tvp.tv_usec);*
>>
>>         result = select(maxFd+1, &fdset, NULL, NULL, &tvp);
>>
>>         if (result == -1)
>>         {
>>             printf("select returned error, errno = %d", errno);
>>             if (errno != EINTR)
>>             {
>>                 snmp_log_perror("select");
>>             }
>>             continue;
>>         }
>>         else if (result == 0)
>>         {
>>            printf("select timeout, errno = %d", errno);
>>             snmp_timeout();
>>             *run_alarms();*
>>             continue;
>>         }
>>         else
>> {
>> printf("do other processing");
>> }
>>
>> }
>>
>> On Mon, May 18, 2020 at 4:08 AM Bill Fenner <fen...@gmail.com> wrote:
>>
>>> On Wed, May 13, 2020 at 11:30 AM Himanshu Sharma <
>>> himanshusharma1...@gmail.com> wrote:
>>>
>>>> Hi All,
>>>>
>>>> Recently, I have changed  *agent_check_and_process(1),*
>>>> In the SNMP Subagent code to a *select() & snmp_select_info() to
>>>> handle outside events*
>>>> *, Please find the code snippet attached.*
>>>>
>>>> I have tried to make *fakeblock=0 *too.
>>>>
>>>> Problem I am facing is , If Subagent Process comes up in the system
>>>> before the Master Agent(*snmpd*) Process.* Subgent is not able to
>>>> establish connection with Master Agent*.But if Master Agent(snmpd)
>>>> process comes up before the Subagent process in the system, The connection
>>>> between master Agent and Subagent is *successful.*
>>>>
>>>> Moreover once the connection is established  between Master Agent and
>>>> SubAgent, If I restart snmpd(Master Agent ) it doesn't connect back to the
>>>> Subagent.
>>>>
>>>> Above problem is solved if I use agent_check_process(1) .i.e.The
>>>> Subagent keeps on polling until it establishes connection with the master
>>>> agent.( *This is default expected behaviour*).
>>>>
>>>> Looks like there is some problem in usage of select() and
>>>> snmp_select_info()
>>>> I am using net-snmp 5.8.
>>>>
>>>> Please help me on this 😞.
>>>>
>>>> *-Himanshu*
>>>>
>>>> /////code start from here
>>>> keepRunning_=1;
>>>>     int maxFd;
>>>>     fd_set fdset;
>>>>     struct timeval  timeout = { LONG_MAX, 0 }, *tvp = &timeout;
>>>>     int  result;
>>>>     int  fakeblock = 0;
>>>>     maxFd = 0;
>>>>
>>>>     while (keepRunning_)
>>>>     {
>>>>         /* if you use select(), see snmp_select_info() in snmp_api(3) */
>>>>         /*     --- OR ---  */
>>>>        * //agent_check_and_process(1)*; /* 0 == don't block */
>>>>         maxFd = *eventPipe_;
>>>>         FD_ZERO(&fdset);
>>>>         snmp_select_info(&maxFd, &fdset, tvp, &fakeblock);
>>>>         FD_SET(*eventPipe_, &fdset);
>>>>         maxFd = (std::max(maxFd, *eventPipe_));
>>>>
>>>>         result = select(maxFd+1, &fdset, NULL, NULL, NULL);
>>>>
>>>
>>> The final argument to select() is how long to wait before you time out.
>>> You are passing NULL, instead of tvp, so there is no hope that you will
>>> reach the timeout that the agent sets to reconnect itself.
>>>
>>>
>>>>
>>>>         if (result == -1)
>>>>         {
>>>>             if (errno != EINTR)
>>>>             {
>>>>                 snmp_log_perror("select");
>>>>             }
>>>>             continue;
>>>>         }
>>>>         else if (result == 0)
>>>>         {
>>>>             snmp_timeout();
>>>>
>>>
>>> When you're an agent you also have to call run_alarms().
>>>
>>>   Bill
>>>
>>>             continue;
>>>>         }
>>>>        else {
>>>>        if (FD_ISSET(*eventPipe_, &fdset))
>>>>             {
>>>>               //do something
>>>>             }
>>>>         if (result > 0)
>>>>         {
>>>>                 /*
>>>>                 * packets found, process them
>>>>                 */
>>>>                 snmp_read(&fdset);
>>>>         }
>>>>         else
>>>>         {
>>>>                 printf("runSubAgent select returned result %d",result);
>>>>         }
>>>>
>>>> }
>>>>     }
>>>> _______________________________________________
>>>> Net-snmp-users mailing list
>>>> Net-snmp-users@lists.sourceforge.net
>>>> Please see the following page to unsubscribe or change other options:
>>>> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
>>>>
>>> _______________________________________________
> Net-snmp-users mailing list
> Net-snmp-users@lists.sourceforge.net
> Please see the following page to unsubscribe or change other options:
> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
>
_______________________________________________
Net-snmp-users mailing list
Net-snmp-users@lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

Reply via email to