1 /* Copyright 2005 Renzo Davoli - VDE-2
2  * Mattia Belletti (C) 2004.
3  * Licensed under the GPLv2
4  */
5 
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <libgen.h>
14 #include <syslog.h>
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 
20 #include <config.h>
21 #include <vde.h>
22 #include <vdecommon.h>
23 
24 #include "../vde_switch/switch.h"
25 #include "consmgmt.h"
26 
27 /* check to see if given unix socket is still in use; if it isn't, remove the
28  *  * socket from the file system */
still_used(struct sockaddr_un * sun)29 int still_used(struct sockaddr_un *sun)
30 {
31 	int test_fd, ret = 1;
32 
33 	if((test_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0){
34 		printlog(LOG_ERR,"socket %s",strerror(errno));
35 		return(1);
36 	}
37 	if(connect(test_fd, (struct sockaddr *) sun, sizeof(*sun)) < 0){
38 		if(errno == ECONNREFUSED){
39 			if(unlink(sun->sun_path) < 0){
40 				printlog(LOG_ERR,"Failed to removed unused socket '%s': %s",
41 						sun->sun_path,strerror(errno));
42 			}
43 			ret = 0;
44 		}
45 		else printlog(LOG_ERR,"connect %s",strerror(errno));
46 	}
47 	close(test_fd);
48 	return(ret);
49 }
50 
51