summaryrefslogtreecommitdiff
path: root/net/vde2/files/patch-vdetaplib::vdetap.c
blob: 3e5ded1cc07b0442089f97b2953424377d859da7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--- vdetaplib/vdetap.c	Sat Jan 29 15:02:08 2005
+++ vdetaplib/vdetap.c	Sun Jan 30 14:05:26 2005
@@ -2,11 +2,25 @@
  * Reseased under the GPLv2 */
 
 #include <stdio.h>
+#ifndef __FreeBSD__
 #include <stdint.h>
+#endif
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/time.h>
+#include <unistd.h>
+#else
 #include <sys/select.h>
+#endif
 #include <sys/poll.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#ifdef __FreeBSD__
+#include "../vde.h"
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#endif
 
 #define SWITCH_MAGIC 0xfeedface
 #define BUFSIZE 2048
@@ -21,6 +35,23 @@
 
 static unsigned char bufin[BUFSIZE];
 
+#ifdef __FreeBSD__
+static char *data_socket = NULL;
+
+static void cleanup()
+{
+    if(data_socket != NULL)
+        unlink(data_socket);
+}
+
+static void sig_handler(int sig)
+{
+    cleanup(1, NULL);
+    signal(sig, SIG_DFL);
+    kill(getpid(), sig);
+}
+#endif
+
 static int send_fd(char *name, int fddata, struct sockaddr_un *datasock, int intno, int group)
 {
 	int pid = getpid();
@@ -28,6 +59,9 @@
 	int fdctl;
 
 	struct sockaddr_un sock;
+#ifdef __FreeBSD__
+  struct timeval tv;
+#endif
 
 	if((fdctl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
 		perror("socket");
@@ -47,7 +81,14 @@
 
 	req.sock.sun_family=AF_UNIX;
 	memset(req.sock.sun_path, 0, sizeof(req.sock.sun_path));
+#ifdef __FreeBSD__
+	gettimeofday(&tv, NULL);
+	snprintf(req.sock.sun_path, sizeof(req.sock.sun_path), "%s.%i.%li",
+	    VDEDATSOCK, pid, tv.tv_usec);
+	data_socket = strdup(req.sock.sun_path);
+#else
 	sprintf(&req.sock.sun_path[1], "%5d-%2d", pid, intno);
+#endif
 
 	if(bind(fddata, (struct sockaddr *) &req.sock, sizeof(req.sock)) < 0){
 		perror("bind");
@@ -69,7 +110,7 @@
 
 static struct pollfd pollv[]={{0,POLLIN|POLLHUP,0},{0,POLLIN|POLLHUP,0}};
 
-main(int argc,char *argv[])
+int main(int argc,char *argv[])
 {
 	int fd,fddata;
 	struct sockaddr_un dataout,datain;
@@ -96,6 +137,12 @@
 	send_fd(argv[2],fddata,&dataout,0,0);
 	pollv[0].fd=fd;
 	pollv[1].fd=fddata;
+
+#ifdef __FreeBSD__
+	if(signal(SIGINT, sig_handler) < 0) {
+		perror("signal");
+	}
+#endif
 	for(;;) {
 		result=poll(pollv,2,-1);
 		if (pollv[0].revents & POLLHUP || pollv[1].revents & POLLHUP)
@@ -113,5 +160,7 @@
 			write(fd,bufin,nx);
 		}
 	}
+	cleanup(0, NULL);
+	return 0;
 }