summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Shmidt2015-10-01 15:27:56 -0500
committerDmitry Shmidt2015-10-01 16:30:20 -0500
commitcebcd450c6ca3de5106ef3d817d5b760f5d57fbe (patch)
tree4f8671d416b4754f2f566911ec725a50474c758d /libnetutils
parent5cf2d5c9cc7d3a582154c93ebc4fd7fc7584ded0 (diff)
downloadplatform-system-core-cebcd450c6ca3de5106ef3d817d5b760f5d57fbe.tar.gz
platform-system-core-cebcd450c6ca3de5106ef3d817d5b760f5d57fbe.tar.xz
platform-system-core-cebcd450c6ca3de5106ef3d817d5b760f5d57fbe.zip
libnetutils: Add synchronization per socket full operation
Take and release mutex between ifc_init() and ifc_close() calls to prevent global ifc_ctl_sock override. Bug: 22307208 Change-Id: Iacffd329ce2a51defa550c7e33277332045a7b9b Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
Diffstat (limited to 'libnetutils')
-rw-r--r--libnetutils/dhcptool.c7
-rw-r--r--libnetutils/ifc_utils.c9
2 files changed, 14 insertions, 2 deletions
diff --git a/libnetutils/dhcptool.c b/libnetutils/dhcptool.c
index 352ac5e57..a2d3869b4 100644
--- a/libnetutils/dhcptool.c
+++ b/libnetutils/dhcptool.c
@@ -14,6 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include <err.h>
17#include <errno.h> 18#include <errno.h>
18#include <error.h> 19#include <error.h>
19#include <stdbool.h> 20#include <stdbool.h>
@@ -29,12 +30,14 @@ int main(int argc, char* argv[]) {
29 30
30 char* interface = argv[1]; 31 char* interface = argv[1];
31 if (ifc_init()) { 32 if (ifc_init()) {
32 error(EXIT_FAILURE, errno, "dhcptool %s: ifc_init failed", interface); 33 err(errno, "dhcptool %s: ifc_init failed", interface);
34 ifc_close();
35 return EXIT_FAILURE;
33 } 36 }
34 37
35 int rc = do_dhcp(interface); 38 int rc = do_dhcp(interface);
36 if (rc) { 39 if (rc) {
37 error(0, errno, "dhcptool %s: do_dhcp failed", interface); 40 err(errno, "dhcptool %s: do_dhcp failed", interface);
38 } 41 }
39 42
40 ifc_close(); 43 ifc_close();
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index 956ed30c3..aa18bc1f9 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -19,6 +19,7 @@
19#include <unistd.h> 19#include <unistd.h>
20#include <string.h> 20#include <string.h>
21#include <errno.h> 21#include <errno.h>
22#include <pthread.h>
22 23
23#include <sys/socket.h> 24#include <sys/socket.h>
24#include <sys/select.h> 25#include <sys/select.h>
@@ -57,6 +58,8 @@
57 58
58static int ifc_ctl_sock = -1; 59static int ifc_ctl_sock = -1;
59static int ifc_ctl_sock6 = -1; 60static int ifc_ctl_sock6 = -1;
61static pthread_mutex_t ifc_sock_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
62static pthread_mutex_t ifc_sock6_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
60void printerr(char *fmt, ...); 63void printerr(char *fmt, ...);
61 64
62#define DBG 0 65#define DBG 0
@@ -122,6 +125,8 @@ int string_to_ip(const char *string, struct sockaddr_storage *ss) {
122int ifc_init(void) 125int ifc_init(void)
123{ 126{
124 int ret; 127 int ret;
128
129 pthread_mutex_lock(&ifc_sock_mutex);
125 if (ifc_ctl_sock == -1) { 130 if (ifc_ctl_sock == -1) {
126 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); 131 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
127 if (ifc_ctl_sock < 0) { 132 if (ifc_ctl_sock < 0) {
@@ -136,6 +141,7 @@ int ifc_init(void)
136 141
137int ifc_init6(void) 142int ifc_init6(void)
138{ 143{
144 pthread_mutex_lock(&ifc_sock6_mutex);
139 if (ifc_ctl_sock6 == -1) { 145 if (ifc_ctl_sock6 == -1) {
140 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); 146 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
141 if (ifc_ctl_sock6 < 0) { 147 if (ifc_ctl_sock6 < 0) {
@@ -152,6 +158,7 @@ void ifc_close(void)
152 (void)close(ifc_ctl_sock); 158 (void)close(ifc_ctl_sock);
153 ifc_ctl_sock = -1; 159 ifc_ctl_sock = -1;
154 } 160 }
161 pthread_mutex_unlock(&ifc_sock_mutex);
155} 162}
156 163
157void ifc_close6(void) 164void ifc_close6(void)
@@ -160,6 +167,7 @@ void ifc_close6(void)
160 (void)close(ifc_ctl_sock6); 167 (void)close(ifc_ctl_sock6);
161 ifc_ctl_sock6 = -1; 168 ifc_ctl_sock6 = -1;
162 } 169 }
170 pthread_mutex_unlock(&ifc_sock6_mutex);
163} 171}
164 172
165static void ifc_init_ifr(const char *name, struct ifreq *ifr) 173static void ifc_init_ifr(const char *name, struct ifreq *ifr)
@@ -534,6 +542,7 @@ int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, in
534 ifc_init(); 542 ifc_init();
535 543
536 if (ifc_ctl_sock < 0) { 544 if (ifc_ctl_sock < 0) {
545 ifc_close();
537 return -errno; 546 return -errno;
538 } 547 }
539 548