Last active
August 29, 2015 14:01
-
-
Save zimbatm/2b5c79c112b8065ff835 to your computer and use it in GitHub Desktop.
Used to detect if the netlink linux interface works properly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <linux/netlink.h> | |
#include <linux/connector.h> | |
#define _LINUX_TIME_H | |
#include <linux/cn_proc.h> | |
#include <errno.h> | |
static int nl_sock; | |
#define NL_MESSAGE_SIZE (sizeof(struct nlmsghdr) + sizeof(struct cn_msg) + \ | |
sizeof(int)) | |
int main(void) { | |
struct sockaddr_nl sa_nl; /* netlink interface info */ | |
char buff[NL_MESSAGE_SIZE]; | |
struct nlmsghdr *hdr; /* for telling netlink what we want */ | |
struct cn_msg *msg; /* the actual connector message */ | |
nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); | |
if (-1 == nl_sock) { | |
perror("socket()"); | |
return 1; | |
} | |
printf("first phase good, fire ! !\n"); | |
bzero(&sa_nl, sizeof(sa_nl)); | |
sa_nl.nl_family = AF_NETLINK; | |
sa_nl.nl_groups = CN_IDX_PROC; | |
sa_nl.nl_pid = getpid(); | |
if (-1 == bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl))) { | |
perror("bind()"); | |
return 1; | |
} | |
printf("take off successful\n"); | |
/* Fill header */ | |
hdr = (struct nlmsghdr *)buff; | |
hdr->nlmsg_len = NL_MESSAGE_SIZE; | |
hdr->nlmsg_type = NLMSG_DONE; | |
hdr->nlmsg_flags = 0; | |
hdr->nlmsg_seq = 0; | |
hdr->nlmsg_pid = getpid(); | |
/* Fill message */ | |
msg = (struct cn_msg *)NLMSG_DATA(hdr); | |
msg->id.idx = CN_IDX_PROC; /* Connecting to process information */ | |
msg->id.val = CN_VAL_PROC; | |
msg->seq = 0; | |
msg->ack = 0; | |
msg->flags = 0; | |
msg->flags = 0; | |
msg->len = sizeof(int); | |
*(int*)msg->data = PROC_CN_MCAST_LISTEN; | |
if (-1 == send(nl_sock, hdr, hdr->nlmsg_len, 0)) { | |
perror("send()"); | |
return 1; | |
} | |
printf("back to base\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used to debug issues with God on Linux+LXC.