Prevent two clients from making conflicting updates

From Net-SNMP Wiki
Jump to: navigation, search
Good Answer

This is a Good Answer article. It was likely created as a response to a question on a Net-SNMP Mailing List and written up here for others to see. It likely covers material not yet in the FAQ or in the Tutorial but may someday be moved there

Question

When two applications try to update a MIB at same time, it’s possible that one application will rewrite the data that other application just wrote.

How can I guarantee that two applications can’t update the same node at the same time.

Can I implement a kind of semaphore or implement a synchronization of write operation?

Answer

The usual SNMP mechanism for implementing this would be a MIB 'TestAndIncr' object. Each application would retrieve the contents of the table together with this semaphore value (as a single SNMP request). They would each then issue a SET request, containing the new table values and this same semaphore value (again, as a single SNMP request).

The first client would be providing the same semaphore value as the one read, so the SET request would succeed (incrementing the internal semaphore value). The second client would now be providing an out-of-date semaphore value, so the SET request would be rejected.

There is a helper routine

   int  netsnmp_register_watched_spinlock(
                              netsnmp_handler_registration *reginfo, int *spinlock);

to implement just such a semaphore object.