all the python scripts are for python2, and fedora31 requires to be specific
[vsys-scripts.git] / root-context / fd_netlink.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <netinet/in.h>
7
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <sys/uio.h>
12 #include <asm/types.h>
13 #include <linux/netlink.h>
14
15 #include "fdpass.h"
16
17
18 static void receive_argument(int control_fd, unsigned int *value)
19 {
20         if (recv(control_fd, value, sizeof(unsigned int), 0) != sizeof(unsigned int)) {
21                 fprintf(stderr, "receiving argument failed\n");
22                 exit(-1);
23         }
24 }
25
26 int main(int argc, char *argv[]) 
27 {
28     int control_channel_fd, magic_socket;
29         struct sockaddr_nl addr;
30         unsigned int pid = 0;
31         unsigned int gmask = 1;
32  
33     if (argc < 3) {
34         printf("This script is called by vsys.\n");
35         exit(1);
36     }
37
38     control_channel_fd = atoi(argv[2]);  
39
40         /* receive pid paramater */
41         receive_argument(control_channel_fd, &pid);
42
43         /* receive gmask paramater */
44         receive_argument(control_channel_fd, &gmask);
45
46     magic_socket =  receive_fd(control_channel_fd);
47     if (magic_socket == -1) { 
48         fprintf(stderr, "Error creating socket: %d\n", errno);
49         exit(1);
50     }
51
52         memset(&addr, 0, sizeof(struct sockaddr_nl));
53         addr.nl_family = AF_NETLINK;
54         addr.nl_pid = pid;
55         addr.nl_groups = gmask;
56         if (bind(magic_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
57                 fprintf(stderr, "Error calling bind for AF_NETLINK: %d\n", errno);
58                 exit(1);
59         }
60
61     send_fd(control_channel_fd, magic_socket);
62 }