diff options
-rw-r--r-- | watchlink/netlink_linkstatus.cc | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/watchlink/netlink_linkstatus.cc b/watchlink/netlink_linkstatus.cc index 191625f7..9dd26fbd 100644 --- a/watchlink/netlink_linkstatus.cc +++ b/watchlink/netlink_linkstatus.cc @@ -8,9 +8,12 @@ */ #include <stdio.h> #include <sys/socket.h> +#include <sys/file.h> #include <iostream> #include <string> #include <syslog.h> +#include <errno.h> + #include "rl_str_proc.hh" #include "netlink_send.hh" #include "netlink_event.hh" @@ -138,11 +141,19 @@ NetlinkLinkStatus::process_going_up(const NetlinkEvent &event) string file = _link_dir + "/" + buf; FILE *fp = fopen(file.c_str(), "r"); if (fp == NULL) { - syslog(LOG_INFO,"NetlinkLinkStatus::process_going_up(), failed to open state file"); + syslog(LOG_INFO,"NetlinkLinkStatus::process_going_up(), failed to open state file: %s", + strerror(errno)); // cerr << "NetlinkLinkStatus::process_going_up(), failed to open state file" << endl; return -1; //means we are still up, ignore... } + if (flock(fileno(fp), LOCK_SH)) { + syslog(LOG_INFO, "NetlinkLinkStatus::process_going_up() failed to acquire lock: %s", + strerror(errno)); + fclose(fp); + return -1; + } + char str[1025]; while (fgets(str, 1024, fp)) { string line(str); @@ -233,6 +244,13 @@ NetlinkLinkStatus::process_down(const NetlinkEvent &event) return -1; } + if (flock(fileno(fp), LOCK_EX)) { + syslog(LOG_INFO, "NetlinkLinkStatus::process_down() failed to acquire lock: %s", + strerror(errno)); + fclose(fp); + return -1; + } + int ifindex = event.get_index(); uint32_t local_addr = event.get_local_addr().get(); int mask_len = event.get_mask_len(); |