int main(void) { skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct netconfmsg); void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; char nla_type_str[256]; sprintf(nla_type_str, "%#x /* NETCONFA_??? */", nla_type); TEST_NLATTR_(fd, nlh0, hdrlen, init_netconfmsg, print_netconfmsg, nla_type, nla_type_str, 4, pattern, 4, print_quoted_hex(pattern, 4)); puts("+++ exited with 0 +++"); return 0; }
int main(void) { skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); const uint32_t num = 0xabacdbcd; TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_PORT_VF, pattern, num, printf("%u", num)); #ifdef HAVE_STRUCT_IFLA_PORT_VSI static const struct ifla_port_vsi vsi = { .vsi_mgr_id = 0xab, .vsi_type_id = "abc", .vsi_type_version = 0xef }; TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_PORT_VSI_TYPE, pattern, vsi, PRINT_FIELD_U("{", vsi, vsi_mgr_id); printf(", vsi_type_id=\"\\x61\\x62\\x63\""); PRINT_FIELD_U(", ", vsi, vsi_type_version); printf("}")); #endif puts("+++ exited with 0 +++"); return 0; }
int genl_client_init () { reset_nl_attrs (); create_nl_socket (); get_family_id (); return 0; }
int main(void) { skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ndmsg); void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + sizeof(struct nda_cacheinfo)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; char nla_type_str[256]; sprintf(nla_type_str, "%#x /* NDA_??? */", nla_type); TEST_NLATTR_(fd, nlh0, hdrlen, init_ndmsg, print_ndmsg, nla_type, nla_type_str, 4, pattern, 4, print_quoted_hex(pattern, 4)); TEST_NLATTR(fd, nlh0, hdrlen, init_ndmsg, print_ndmsg, NDA_DST, 4, pattern, 4, print_quoted_hex(pattern, 4)); static const struct nda_cacheinfo ci = { .ndm_confirmed = 0xabcdedad, .ndm_used = 0xbcdaedad, .ndm_updated = 0xcdbadeda, .ndm_refcnt = 0xdeadbeda }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ndmsg, print_ndmsg, NDA_CACHEINFO, pattern, ci, PRINT_FIELD_U("{", ci, ndm_confirmed); PRINT_FIELD_U(", ", ci, ndm_used); PRINT_FIELD_U(", ", ci, ndm_updated); PRINT_FIELD_U(", ", ci, ndm_refcnt); printf("}")); const uint16_t port = 0xabcd; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ndmsg, print_ndmsg, NDA_PORT, pattern, port, printf("htons(%u)", ntohs(port))); puts("+++ exited with 0 +++"); return 0; }
int main(void) { skip_if_unavailable("/proc/self/fd/"); const uint32_t ifindex = ifindex_lo(); const uint8_t type = MDB_RTR_TYPE_DISABLED; static const struct nlattr nla = { .nla_len = NLA_HDRLEN + sizeof(type), .nla_type = MDBA_ROUTER_PATTR_TYPE }; char buf[NLMSG_ALIGN(ifindex) + NLA_HDRLEN + sizeof(type)]; const int fd = create_nl_socket(NETLINK_ROUTE); void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + sizeof(buf)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen, init_br_port_msg, print_br_port_msg, MDBA_ROUTER_PORT, pattern, ifindex, printf(IFINDEX_LO_STR)); memcpy(buf, &ifindex, sizeof(ifindex)); memcpy(buf + NLMSG_ALIGN(ifindex), &nla, sizeof(nla)); memcpy(buf + NLMSG_ALIGN(ifindex) + NLA_HDRLEN, &type, sizeof(type)); TEST_NLATTR(fd, nlh0 - NLA_HDRLEN, hdrlen + NLA_HDRLEN, init_br_port_msg, print_br_port_msg, MDBA_ROUTER_PORT, sizeof(buf), buf, sizeof(buf), printf(IFINDEX_LO_STR ", {{nla_len=%u, nla_type=MDBA_ROUTER_PATTR_TYPE}" ", MDB_RTR_TYPE_DISABLED}}", nla.nla_len)); puts("+++ exited with 0 +++"); return 0; } #else SKIP_MAIN_UNDEFINED("HAVE_STRUCT_BR_PORT_MSG")
static void test_selnl_msg_setenforce(const int fd) { void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct selnl_msg_setenforce msg = { .val = 0xfbdcdfab }; TEST_NETLINK_OBJECT(fd, nlh0, SELNL_MSG_SETENFORCE, NLM_F_REQUEST, msg, PRINT_FIELD_D("{", msg, val); printf("}")); } static void test_selnl_msg_policyload(const int fd) { void *const nlh0 = tail_alloc(NLMSG_HDRLEN); static const struct selnl_msg_policyload msg = { .seqno = 0xabdcfabc }; TEST_NETLINK_OBJECT(fd, nlh0, SELNL_MSG_POLICYLOAD, NLM_F_REQUEST, msg, PRINT_FIELD_U("{", msg, seqno); printf("}")); } int main(void) { skip_if_unavailable("/proc/self/fd/"); int fd = create_nl_socket(NETLINK_SELINUX); test_nlmsg_type(fd); test_selnl_msg_unspec(fd); test_selnl_msg_setenforce(fd); test_selnl_msg_policyload(fd); printf("+++ exited with 0 +++\n"); return 0; }
int main(int argc, char *argv[]) { int c, rc, rep_len, aggr_len, len2; int cmd_type = TASKSTATS_CMD_ATTR_UNSPEC; __u16 id; __u32 mypid; struct nlattr *na; int nl_sd = -1; int len = 0; pid_t tid = 0; pid_t rtid = 0; int fd = 0; int count = 0; int write_file = 0; int maskset = 0; char *logfile = NULL; int loop = 0; int containerset = 0; char *containerpath = NULL; int cfd = 0; int forking = 0; sigset_t sigset; struct msgtemplate msg; while (!forking) { c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:c:"); if (c < 0) break; switch (c) { case 'd': printf("print delayacct stats ON\n"); print_delays = 1; break; case 'i': printf("printing IO accounting\n"); print_io_accounting = 1; break; case 'q': printf("printing task/process context switch rates\n"); print_task_context_switch_counts = 1; break; case 'C': containerset = 1; containerpath = optarg; break; case 'w': logfile = strdup(optarg); printf("write to file %s\n", logfile); write_file = 1; break; case 'r': rcvbufsz = atoi(optarg); printf("receive buf size %d\n", rcvbufsz); if (rcvbufsz < 0) err(1, "Invalid rcv buf size\n"); break; case 'm': strncpy(cpumask, optarg, sizeof(cpumask)); cpumask[sizeof(cpumask) - 1] = '\0'; maskset = 1; printf("cpumask %s maskset %d\n", cpumask, maskset); break; case 't': tid = atoi(optarg); if (!tid) err(1, "Invalid tgid\n"); cmd_type = TASKSTATS_CMD_ATTR_TGID; break; case 'p': tid = atoi(optarg); if (!tid) err(1, "Invalid pid\n"); cmd_type = TASKSTATS_CMD_ATTR_PID; break; case 'c': /* Block SIGCHLD for sigwait() later */ if (sigemptyset(&sigset) == -1) err(1, "Failed to empty sigset"); if (sigaddset(&sigset, SIGCHLD)) err(1, "Failed to set sigchld in sigset"); sigprocmask(SIG_BLOCK, &sigset, NULL); /* fork/exec a child */ tid = fork(); if (tid < 0) err(1, "Fork failed\n"); if (tid == 0) if (execvp(argv[optind - 1], &argv[optind - 1]) < 0) exit(-1); /* Set the command type and avoid further processing */ cmd_type = TASKSTATS_CMD_ATTR_PID; forking = 1; break; case 'v': printf("debug on\n"); dbg = 1; break; case 'l': printf("listen forever\n"); loop = 1; break; default: usage(); exit(-1); } } if (write_file) { fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { perror("Cannot open output file\n"); exit(1); } } nl_sd = create_nl_socket(NETLINK_GENERIC); if (nl_sd < 0) err(1, "error creating Netlink socket\n"); mypid = getpid(); id = get_family_id(nl_sd); if (!id) { fprintf(stderr, "Error getting family id, errno %d\n", errno); goto err; } PRINTF("family id %d\n", id); if (maskset) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_REGISTER_CPUMASK, &cpumask, strlen(cpumask) + 1); PRINTF("Sent register cpumask, retval %d\n", rc); if (rc < 0) { fprintf(stderr, "error sending register cpumask\n"); goto err; } } if (tid && containerset) { fprintf(stderr, "Select either -t or -C, not both\n"); goto err; } /* * If we forked a child, wait for it to exit. Cannot use waitpid() * as all the delicious data would be reaped as part of the wait */ if (tid && forking) { int sig_received; sigwait(&sigset, &sig_received); } if (tid) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, cmd_type, &tid, sizeof(__u32)); PRINTF("Sent pid/tgid, retval %d\n", rc); if (rc < 0) { fprintf(stderr, "error sending tid/tgid cmd\n"); goto done; } } if (containerset) { cfd = open(containerpath, O_RDONLY); if (cfd < 0) { perror("error opening container file"); goto err; } rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET, CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32)); if (rc < 0) { perror("error sending cgroupstats command"); goto err; } } if (!maskset && !tid && !containerset) { usage(); goto err; } do { rep_len = recv(nl_sd, &msg, sizeof(msg), 0); PRINTF("received %d bytes\n", rep_len); if (rep_len < 0) { fprintf(stderr, "nonfatal reply error: errno %d\n", errno); continue; } if (msg.n.nlmsg_type == NLMSG_ERROR || !NLMSG_OK((&msg.n), rep_len)) { struct nlmsgerr *err = NLMSG_DATA(&msg); fprintf(stderr, "fatal reply error, errno %d\n", err->error); goto done; } PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n", sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len); rep_len = GENLMSG_PAYLOAD(&msg.n); na = (struct nlattr *) GENLMSG_DATA(&msg); len = 0; while (len < rep_len) { len += NLA_ALIGN(na->nla_len); switch (na->nla_type) { case TASKSTATS_TYPE_AGGR_TGID: /* Fall through */ case TASKSTATS_TYPE_AGGR_PID: aggr_len = NLA_PAYLOAD(na->nla_len); len2 = 0; /* For nested attributes, na follows */ na = (struct nlattr *) NLA_DATA(na); done = 0; while (len2 < aggr_len) { switch (na->nla_type) { case TASKSTATS_TYPE_PID: rtid = *(int *) NLA_DATA(na); if (print_delays) printf("PID\t%d\n", rtid); break; case TASKSTATS_TYPE_TGID: rtid = *(int *) NLA_DATA(na); if (print_delays) printf("TGID\t%d\n", rtid); break; case TASKSTATS_TYPE_STATS: count++; if (print_delays) print_delayacct((struct taskstats *) NLA_DATA(na)); if (print_io_accounting) print_ioacct((struct taskstats *) NLA_DATA(na)); if (print_task_context_switch_counts) task_context_switch_counts((struct taskstats *) NLA_DATA(na)); if (fd) { if (write(fd, NLA_DATA(na), na->nla_len) < 0) { err(1,"write error\n"); } } if (!loop) goto done; break; case TASKSTATS_TYPE_NULL: break; default: fprintf(stderr, "Unknown nested" " nla_type %d\n", na->nla_type); break; } len2 += NLA_ALIGN(na->nla_len); na = (struct nlattr *)((char *)na + NLA_ALIGN(na->nla_len)); } break; case CGROUPSTATS_TYPE_CGROUP_STATS: print_cgroupstats(NLA_DATA(na)); break; default: fprintf(stderr, "Unknown nla_type %d\n", na->nla_type); case TASKSTATS_TYPE_NULL: break; } na = (struct nlattr *) (GENLMSG_DATA(&msg) + len); } } while (loop); done: if (maskset) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK, &cpumask, strlen(cpumask) + 1); printf("Sent deregister mask, retval %d\n", rc); if (rc < 0) err(rc, "error sending deregister cpumask\n"); } err: close(nl_sd); if (fd) close(fd); if (cfd) close(cfd); return 0; }
static void test_nlmsg_type(const int fd) { long rc; struct nlmsghdr nlh = { .nlmsg_len = sizeof(nlh), .nlmsg_flags = NLM_F_REQUEST, }; nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_NEW; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|IPCTNL_MSG_TIMEOUT_NEW" ", flags=NLM_F_REQUEST, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | 0xff; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|0xff /* IPCTNL_MSG_TIMEOUT_??? */" ", flags=NLM_F_REQUEST, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); } static void test_nlmsg_flags(const int fd) { long rc; struct nlmsghdr nlh = { .nlmsg_len = sizeof(nlh), }; nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_NEW; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_APPEND; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|IPCTNL_MSG_TIMEOUT_NEW" ", flags=NLM_F_REQUEST|NLM_F_APPEND, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_GET; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ATOMIC; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|IPCTNL_MSG_TIMEOUT_GET" ", flags=NLM_F_REQUEST|NLM_F_ATOMIC, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_DELETE; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_NONREC; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|IPCTNL_MSG_TIMEOUT_DELETE" ", flags=NLM_F_REQUEST|NLM_F_NONREC, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); } int main(void) { skip_if_unavailable("/proc/self/fd/"); int fd = create_nl_socket(NETLINK_NETFILTER); test_nlmsg_type(fd); test_nlmsg_flags(fd); puts("+++ exited with 0 +++"); return 0; }
int main(int argc, char *argv[]) { int c, rc, rep_len, aggr_len, len2, cmd_type; __u16 id; __u32 mypid; struct nlattr *na; int nl_sd = -1; int len = 0; pid_t tid = 0; pid_t rtid = 0; int fd = 0; int count = 0; int write_file = 0; int maskset = 0; char *logfile = NULL; int loop = 0; int containerset = 0; char containerpath[1024]; int cfd = 0; struct msgtemplate msg; while (1) { c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:"); if (c < 0) break; switch (c) { case 'd': printf("print delayacct stats ON\n"); print_delays = 1; break; case 'i': printf("printing IO accounting\n"); print_io_accounting = 1; break; case 'q': printf("printing task/process context switch rates\n"); print_task_context_switch_counts = 1; break; case 'C': containerset = 1; strncpy(containerpath, optarg, strlen(optarg) + 1); break; case 'w': logfile = strdup(optarg); printf("write to file %s\n", logfile); write_file = 1; break; case 'r': rcvbufsz = atoi(optarg); printf("receive buf size %d\n", rcvbufsz); if (rcvbufsz < 0) err(1, "Invalid rcv buf size\n"); break; case 'm': strncpy(cpumask, optarg, sizeof(cpumask)); maskset = 1; printf("cpumask %s maskset %d\n", cpumask, maskset); break; case 't': tid = atoi(optarg); if (!tid) err(1, "Invalid tgid\n"); cmd_type = TASKSTATS_CMD_ATTR_TGID; break; case 'p': tid = atoi(optarg); if (!tid) err(1, "Invalid pid\n"); cmd_type = TASKSTATS_CMD_ATTR_PID; break; case 'v': printf("debug on\n"); dbg = 1; break; case 'l': printf("listen forever\n"); loop = 1; break; default: usage(); exit(1); } } if (write_file) { fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { perror("Cannot open output file\n"); exit(1); } } if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0) err(1, "error creating Netlink socket\n"); mypid = getpid(); id = get_family_id(nl_sd); if (!id) { fprintf(stderr, "Error getting family id, errno %d\n", errno); exit(1); } PRINTF("family id %d\n", id); if (maskset) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_REGISTER_CPUMASK, &cpumask, strlen(cpumask) + 1); PRINTF("Sent register cpumask, retval %d\n", rc); if (rc < 0) { fprintf(stderr, "error sending register cpumask\n"); exit(1); } } if (tid && containerset) { fprintf(stderr, "Select either -t or -C, not both\n"); exit(1); } if (tid) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, cmd_type, &tid, sizeof(__u32)); PRINTF("Sent pid/tgid, retval %d\n", rc); if (rc < 0) { fprintf(stderr, "error sending tid/tgid cmd\n"); exit(1); } } if (containerset) { cfd = open(containerpath, O_RDONLY); if (cfd < 0) { perror("error opening container file"); exit(1); } #ifdef HAVE_LINUX_CGROUPSTATS_H rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET, CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32)); #else errno = ENOSYS; rc = -1; #endif if (rc < 0) { perror("error sending cgroupstats command"); exit(1); } } if (!maskset && !tid && !containerset) { usage(); exit(1); } do { int i; rep_len = recv(nl_sd, &msg, sizeof(msg), 0); PRINTF("received %d bytes\n", rep_len); if (rep_len < 0) { fprintf(stderr, "nonfatal reply error: errno %d\n", errno); exit(1); } if (msg.n.nlmsg_type == NLMSG_ERROR || !NLMSG_OK((&msg.n), rep_len)) { struct nlmsgerr *err = NLMSG_DATA(&msg); fprintf(stderr, "fatal reply error, errno %d\n", err->error); exit(1); } PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n", sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len); rep_len = GENLMSG_PAYLOAD(&msg.n); na = (struct nlattr *) GENLMSG_DATA(&msg); len = 0; i = 0; while (len < rep_len) { len += NLA_ALIGN(na->nla_len); switch (na->nla_type) { case TASKSTATS_TYPE_AGGR_TGID: /* Fall through */ case TASKSTATS_TYPE_AGGR_PID: aggr_len = NLA_PAYLOAD(na->nla_len); len2 = 0; /* For nested attributes, na follows */ na = (struct nlattr *) NLA_DATA(na); done = 0; while (len2 < aggr_len) { switch (na->nla_type) { case TASKSTATS_TYPE_PID: rtid = *(int *) NLA_DATA(na); if (print_delays) printf("PID\t%d\n", rtid); break; case TASKSTATS_TYPE_TGID: rtid = *(int *) NLA_DATA(na); if (print_delays) printf("TGID\t%d\n", rtid); break; case TASKSTATS_TYPE_STATS: count++; if (print_delays) print_delayacct((struct taskstats *) NLA_DATA(na)); if (print_io_accounting) print_ioacct((struct taskstats *) NLA_DATA(na)); if (print_task_context_switch_counts) task_context_switch_counts((struct taskstats *) NLA_DATA(na)); if (fd) { if (write(fd, NLA_DATA(na), na->nla_len) < 0) { err(1,"write error\n"); } } if (!loop) goto done; break; default: fprintf(stderr, "Unknown nested" " nla_type %d\n", na->nla_type); break; } len2 += NLA_ALIGN(na->nla_len); na = (struct nlattr *) ((char *) na + len2); } break; #if HAVE_LINUX_CGROUPSTATS_H case CGROUPSTATS_TYPE_CGROUP_STATS: print_cgroupstats(NLA_DATA(na)); break; #endif default: fprintf(stderr, "Unknown nla_type %d\n", na->nla_type); exit(1); } na = (struct nlattr *) (GENLMSG_DATA(&msg) + len); } } while (loop); done: if (maskset) { rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK, &cpumask, strlen(cpumask) + 1); printf("Sent deregister mask, retval %d\n", rc); if (rc < 0) err(rc, "error sending deregister cpumask\n"); } close(nl_sd); if (fd) close(fd); if (cfd) close(cfd); return 0; }
int main() { nl_sd = create_nl_socket(NETLINK_GENERIC,0); if(nl_sd < 0) { printf("create failure\n"); return 0; } int id = get_family_id(nl_sd); struct { struct nlmsghdr n; struct genlmsghdr g; char buf[256]; } ans; struct { struct nlmsghdr n; struct genlmsghdr g; char buf[256]; } req; struct nlattr *na; // Send command needed req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); req.n.nlmsg_type = id; req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_seq = 60; req.n.nlmsg_pid = getpid(); req.g.cmd = 1;//DOC_EXMPL_C_ECHO; //compose message na = (struct nlattr *) GENLMSG_DATA(&req); na->nla_type = 1; //DOC_EXMPL_A_MSG char * message = "hello world!"; //message int mlength = 14; na->nla_len = mlength+NLA_HDRLEN; //message length memcpy(NLA_DATA(na), message, mlength); req.n.nlmsg_len += NLMSG_ALIGN(na->nla_len); //send message struct sockaddr_nl nladdr; int r; memset(&nladdr, 0, sizeof(nladdr)); nladdr.nl_family = AF_NETLINK; r = sendto(nl_sd, (char *)&req, req.n.nlmsg_len, 0, (struct sockaddr *) &nladdr, sizeof(nladdr)); int rep_len; //kernel says hello first rep_len = recv(nl_sd, &ans, sizeof(ans), 0); // Validate response message if (ans.n.nlmsg_type == NLMSG_ERROR) { // error printf("error received NACK - leaving \n"); return -1; } if (rep_len < 0) { printf("error receiving reply message via Netlink \n"); return -1; } if (!NLMSG_OK((&ans.n), rep_len)) { printf("invalid reply message received via Netlink\n"); return -1; } rep_len = GENLMSG_PAYLOAD(&ans.n); //parse reply message na = (struct nlattr *) GENLMSG_DATA(&ans); char * result2 = (char *)NLA_DATA(na); printf("kernel says: %s\n",result2); while(1) { /* recv sector no. */ rep_len = recv(nl_sd, &ans, sizeof(ans), 0); //int rep_len = recv(nl_sd, &ans, sizeof(ans), 0); //int rep_len = recv(nl_sd, &ans, sizeof(ans), 0); // Validate response message if (ans.n.nlmsg_type == NLMSG_ERROR) { // error printf("error received NACK - leaving \n"); return -1; } if (rep_len < 0) { printf("error receiving reply message via Netlink \n"); return -1; } if (!NLMSG_OK((&ans.n), rep_len)) { printf("invalid reply message received via Netlink\n"); return -1; } rep_len = GENLMSG_PAYLOAD(&ans.n); //parse reply message na = (struct nlattr *) GENLMSG_DATA(&ans); unsigned long *result = (unsigned long *)NLA_DATA(na); printf("Sect No: %lx\n",*result); /*recv no of sectors*/ rep_len = recv(nl_sd, &ans, sizeof(ans), 0); // Validate response message if (ans.n.nlmsg_type == NLMSG_ERROR) { // error printf("error received NACK - leaving \n"); return -1; } if (rep_len < 0) { printf("error receiving reply message via Netlink \n"); return -1; } if (!NLMSG_OK((&ans.n), rep_len)) { printf("invalid reply message received via Netlink\n"); return -1; } rep_len = GENLMSG_PAYLOAD(&ans.n); //parse reply message na = (struct nlattr *) GENLMSG_DATA(&ans); unsigned long *result1 = (unsigned long *)NLA_DATA(na); printf("No. of Sectors : %lx\n\n",*result1); } close(nl_sd); return 1; }
int main(void) { skip_if_unavailable("/proc/self/fd/"); static const struct rtnl_link_stats st = { .rx_packets = 0xabcdefac, .tx_packets = 0xbcdacdab, .rx_bytes = 0xcdbafaab, .tx_bytes = 0xdafabadb, .rx_errors = 0xeabcdaeb, .tx_errors = 0xfefabeab, .rx_dropped = 0xadbafafb, .tx_dropped = 0xbdffabda, .multicast = 0xcdabdfea, .collisions = 0xefadbaeb, .rx_length_errors = 0xfabffabd, .rx_over_errors = 0xafbafabc, .rx_crc_errors = 0xbfdabdad, .rx_frame_errors = 0xcfdabfad, .rx_fifo_errors = 0xddfdebad, .rx_missed_errors = 0xefabdcba, .tx_aborted_errors = 0xefdadbfa, .tx_carrier_errors = 0xfaefbada, .tx_fifo_errors = 0xaebdffab, .tx_heartbeat_errors = 0xbadebaaf, .tx_window_errors = 0xcdafbada, .rx_compressed = 0xdeffadbd, .tx_compressed = 0xefdadfab }; const int fd = create_nl_socket(NETLINK_ROUTE); const unsigned int hdrlen = sizeof(struct ifinfomsg); void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + sizeof(st)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; char nla_type_str[256]; sprintf(nla_type_str, "%#x /* IFLA_??? */", nla_type); TEST_NLATTR_(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, nla_type, nla_type_str, 4, pattern, 4, print_quoted_hex(pattern, 4)); const int32_t netnsid = 0xacbdabda; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_LINK_NETNSID, pattern, netnsid, printf("%d", netnsid)); TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_STATS, pattern, st, PRINT_FIELD_U("{", st, rx_packets); PRINT_FIELD_U(", ", st, tx_packets); PRINT_FIELD_U(", ", st, rx_bytes); PRINT_FIELD_U(", ", st, tx_bytes); PRINT_FIELD_U(", ", st, rx_errors); PRINT_FIELD_U(", ", st, tx_errors); PRINT_FIELD_U(", ", st, rx_dropped); PRINT_FIELD_U(", ", st, tx_dropped); PRINT_FIELD_U(", ", st, multicast); PRINT_FIELD_U(", ", st, collisions); PRINT_FIELD_U(", ", st, rx_length_errors); PRINT_FIELD_U(", ", st, rx_over_errors); PRINT_FIELD_U(", ", st, rx_crc_errors); PRINT_FIELD_U(", ", st, rx_frame_errors); PRINT_FIELD_U(", ", st, rx_fifo_errors); PRINT_FIELD_U(", ", st, rx_missed_errors); PRINT_FIELD_U(", ", st, tx_aborted_errors); PRINT_FIELD_U(", ", st, tx_carrier_errors); PRINT_FIELD_U(", ", st, tx_fifo_errors); PRINT_FIELD_U(", ", st, tx_heartbeat_errors); PRINT_FIELD_U(", ", st, tx_window_errors); PRINT_FIELD_U(", ", st, rx_compressed); PRINT_FIELD_U(", ", st, tx_compressed); #ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER PRINT_FIELD_U(", ", st, rx_nohandler); #endif printf("}")); #ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER const unsigned int sizeof_stats = offsetofend(struct rtnl_link_stats, tx_compressed); TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_STATS, sizeof_stats, &st, sizeof_stats, PRINT_FIELD_U("{", st, rx_packets); PRINT_FIELD_U(", ", st, tx_packets); PRINT_FIELD_U(", ", st, rx_bytes); PRINT_FIELD_U(", ", st, tx_bytes); PRINT_FIELD_U(", ", st, rx_errors); PRINT_FIELD_U(", ", st, tx_errors); PRINT_FIELD_U(", ", st, rx_dropped); PRINT_FIELD_U(", ", st, tx_dropped); PRINT_FIELD_U(", ", st, multicast); PRINT_FIELD_U(", ", st, collisions); PRINT_FIELD_U(", ", st, rx_length_errors); PRINT_FIELD_U(", ", st, rx_over_errors); PRINT_FIELD_U(", ", st, rx_crc_errors); PRINT_FIELD_U(", ", st, rx_frame_errors); PRINT_FIELD_U(", ", st, rx_fifo_errors); PRINT_FIELD_U(", ", st, rx_missed_errors); PRINT_FIELD_U(", ", st, tx_aborted_errors); PRINT_FIELD_U(", ", st, tx_carrier_errors); PRINT_FIELD_U(", ", st, tx_fifo_errors); PRINT_FIELD_U(", ", st, tx_heartbeat_errors); PRINT_FIELD_U(", ", st, tx_window_errors); PRINT_FIELD_U(", ", st, rx_compressed); PRINT_FIELD_U(", ", st, tx_compressed); printf("}")); #endif /* HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER */ static const struct rtnl_link_ifmap map = { .mem_start = 0xadcbefedefbcdedb, .mem_end = 0xefcbeabdecdcdefa, .base_addr = 0xaddbeabdfaacdbae, .irq = 0xefaf, .dma = 0xab, .port = 0xcd }; const unsigned int sizeof_ifmap = offsetofend(struct rtnl_link_ifmap, port); const unsigned int plen = sizeof_ifmap - 1 > DEFAULT_STRLEN ? DEFAULT_STRLEN : (int) sizeof_ifmap - 1; /* len < sizeof_ifmap */ TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_MAP, plen, pattern, plen, print_quoted_hex(pattern, plen)); /* short read of sizeof_ifmap */ TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap - 1, printf("%p", RTA_DATA(TEST_NLATTR_nla))); /* sizeof_ifmap */ TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap, PRINT_FIELD_X("{", map, mem_start); PRINT_FIELD_X(", ", map, mem_end); PRINT_FIELD_X(", ", map, base_addr); PRINT_FIELD_U(", ", map, irq); PRINT_FIELD_U(", ", map, dma); PRINT_FIELD_U(", ", map, port); printf("}")); #ifdef HAVE_STRUCT_RTNL_LINK_STATS64 static const struct rtnl_link_stats64 st64 = { .rx_packets = 0xadcbefedefbcdedb, .tx_packets = 0xbdabdedabdcdeabd, .rx_bytes = 0xcdbaefbaeadfabec, .tx_bytes = 0xdbaedbafabbeacdb, .rx_errors = 0xefabfdaefabaefab, .tx_errors = 0xfaebfabfabbaeabf, .rx_dropped = 0xacdbaedbadbabeba, .tx_dropped = 0xbcdeffebdabeadbe, .multicast = 0xeeffbaeabaeffabe, .collisions = 0xffbaefcefbafacef, .rx_length_errors = 0xaabbdeabceffdecb, .rx_over_errors = 0xbbdcdadebadeaeed, .rx_crc_errors= 0xccdeabecefaedbef, .rx_frame_errors = 0xddbedaedebcedaef, .rx_fifo_errors = 0xeffbadefafdaeaab, .rx_missed_errors = 0xfefaebccceadeecd, .tx_aborted_errors = 0xabcdadefcdadef, .tx_carrier_errors = 0xbccdafaeeaaefe, .tx_fifo_errors = 0xcddefdbedeadce, .tx_heartbeat_errors = 0xedaededdadcdea, .tx_window_errors = 0xfdacdeaccedcda, .rx_compressed = 0xacdbbcacdbccef, .tx_compressed = 0xbcdadefcdedfea }; TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_STATS64, pattern, st64, PRINT_FIELD_U("{", st64, rx_packets); PRINT_FIELD_U(", ", st64, tx_packets); PRINT_FIELD_U(", ", st64, rx_bytes); PRINT_FIELD_U(", ", st64, tx_bytes); PRINT_FIELD_U(", ", st64, rx_errors); PRINT_FIELD_U(", ", st64, tx_errors); PRINT_FIELD_U(", ", st64, rx_dropped); PRINT_FIELD_U(", ", st64, tx_dropped); PRINT_FIELD_U(", ", st64, multicast); PRINT_FIELD_U(", ", st64, collisions); PRINT_FIELD_U(", ", st64, rx_length_errors); PRINT_FIELD_U(", ", st64, rx_over_errors); PRINT_FIELD_U(", ", st64, rx_crc_errors); PRINT_FIELD_U(", ", st64, rx_frame_errors); PRINT_FIELD_U(", ", st64, rx_fifo_errors); PRINT_FIELD_U(", ", st64, rx_missed_errors); PRINT_FIELD_U(", ", st64, tx_aborted_errors); PRINT_FIELD_U(", ", st64, tx_carrier_errors); PRINT_FIELD_U(", ", st64, tx_fifo_errors); PRINT_FIELD_U(", ", st64, tx_heartbeat_errors); PRINT_FIELD_U(", ", st64, tx_window_errors); PRINT_FIELD_U(", ", st64, rx_compressed); PRINT_FIELD_U(", ", st64, tx_compressed); #ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER PRINT_FIELD_U(", ", st64, rx_nohandler); #endif printf("}")); #ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER const unsigned int sizeof_stats64 = offsetofend(struct rtnl_link_stats64, tx_compressed); TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_STATS64, sizeof_stats64, &st64, sizeof_stats64, PRINT_FIELD_U("{", st64, rx_packets); PRINT_FIELD_U(", ", st64, tx_packets); PRINT_FIELD_U(", ", st64, rx_bytes); PRINT_FIELD_U(", ", st64, tx_bytes); PRINT_FIELD_U(", ", st64, rx_errors); PRINT_FIELD_U(", ", st64, tx_errors); PRINT_FIELD_U(", ", st64, rx_dropped); PRINT_FIELD_U(", ", st64, tx_dropped); PRINT_FIELD_U(", ", st64, multicast); PRINT_FIELD_U(", ", st64, collisions); PRINT_FIELD_U(", ", st64, rx_length_errors); PRINT_FIELD_U(", ", st64, rx_over_errors); PRINT_FIELD_U(", ", st64, rx_crc_errors); PRINT_FIELD_U(", ", st64, rx_frame_errors); PRINT_FIELD_U(", ", st64, rx_fifo_errors); PRINT_FIELD_U(", ", st64, rx_missed_errors); PRINT_FIELD_U(", ", st64, tx_aborted_errors); PRINT_FIELD_U(", ", st64, tx_carrier_errors); PRINT_FIELD_U(", ", st64, tx_fifo_errors); PRINT_FIELD_U(", ", st64, tx_heartbeat_errors); PRINT_FIELD_U(", ", st64, tx_window_errors); PRINT_FIELD_U(", ", st64, rx_compressed); PRINT_FIELD_U(", ", st64, tx_compressed); printf("}")); #endif /* HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER */ #endif /* HAVE_STRUCT_RTNL_LINK_STATS64 */ struct nlattr nla = { .nla_len = sizeof(nla), .nla_type = IFLA_INFO_KIND, }; TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_LINKINFO, sizeof(nla), &nla, sizeof(nla), printf("{nla_len=%u, nla_type=IFLA_INFO_KIND}", nla.nla_len)); nla.nla_type = IFLA_VF_PORT; TEST_NLATTR(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_VF_PORTS, sizeof(nla), &nla, sizeof(nla), printf("{nla_len=%u, nla_type=IFLA_VF_PORT}", nla.nla_len)); static const struct { uint32_t val; const char *str; } ifla_events[] = { { 0, "IFLA_EVENT_NONE" }, { 6, "IFLA_EVENT_BONDING_OPTIONS" }, { ARG_STR(0x7) " /* IFLA_EVENT_??? */" }, { ARG_STR(0xdeadfeed) " /* IFLA_EVENT_??? */" }, }; for (size_t i = 0; i < ARRAY_SIZE(ifla_events); i++) { TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_ifinfomsg, print_ifinfomsg, IFLA_EVENT, pattern, ifla_events[i].val, printf("%s", ifla_events[i].str)); } puts("+++ exited with 0 +++"); return 0; }
.pdmc_type = 0xadef, .pdmc_alen = 4, .pdmc_addr = "5678" } }; static const struct packet_diag_ring pdr = { .pdr_block_size = 0xabcdafed, .pdr_block_nr = 0xbcadefae, .pdr_frame_size = 0xcabdfeac, .pdr_frame_nr = 0xdeaeadef, .pdr_retire_tmo = 0xedbafeac, .pdr_sizeof_priv = 0xfeadeacd, .pdr_features = 0xadebadea }; int fd = create_nl_socket(NETLINK_SOCK_DIAG); const unsigned int hdrlen = sizeof(struct packet_diag_msg); void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + sizeof(dml)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, init_packet_diag_msg, print_packet_diag_msg, PACKET_DIAG_INFO, pattern, pinfo, printf("{pdi_index=%s", IFINDEX_LO_STR); printf(", pdi_version=TPACKET_V3"); PRINT_FIELD_U(", ", pinfo, pdi_reserve); PRINT_FIELD_U(", ", pinfo, pdi_copy_thresh); PRINT_FIELD_U(", ", pinfo, pdi_tstamp);
int main(void) { skip_if_unavailable("/proc/self/fd/"); const int fd = create_nl_socket(NETLINK_ROUTE); void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); static char pattern[4096]; fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; char nla_type_str[256]; sprintf(nla_type_str, "%#x /* MDBA_MDB_ENTRY_??? */", nla_type); TEST_NLATTR_(fd, nlh0 - NLA_HDRLEN * 2, hdrlen + NLA_HDRLEN * 2, init_br_port_msg, print_br_port_msg, nla_type, nla_type_str, 4, pattern, 4, print_quoted_hex(pattern, 4); printf("}}")); # ifdef HAVE_STRUCT_BR_MDB_ENTRY struct br_mdb_entry entry = { .ifindex = ifindex_lo(), .state = MDB_TEMPORARY, # ifdef HAVE_STRUCT_BR_MDB_ENTRY_FLAGS .flags = MDB_FLAGS_OFFLOAD, # endif # ifdef HAVE_STRUCT_BR_MDB_ENTRY_VID .vid = 0xcdef, # endif .addr = { .proto = htons(AF_UNSPEC) } }; memcpy(&entry.addr.u, pattern, sizeof(entry.addr.u)); TEST_NESTED_NLATTR_OBJECT_EX(fd, nlh0, hdrlen, init_br_port_msg, print_br_port_msg, MDBA_MDB_ENTRY_INFO, pattern, entry, 2, printf("{ifindex=" IFINDEX_LO_STR); printf(", state=MDB_TEMPORARY"); # ifdef HAVE_STRUCT_BR_MDB_ENTRY_FLAGS printf(", flags=MDB_FLAGS_OFFLOAD"); # endif # ifdef HAVE_STRUCT_BR_MDB_ENTRY_VID PRINT_FIELD_U(", ", entry, vid); # endif printf(", addr={u="); print_quoted_hex(&entry.addr.u, sizeof(entry.addr.u)); printf(", proto=htons(AF_UNSPEC)}}")); static const struct nlattr nla = { .nla_len = sizeof(nla), .nla_type = MDBA_MDB_EATTR_TIMER }; char buf[NLMSG_ALIGN(sizeof(entry)) + sizeof(nla)]; memcpy(buf, &entry, sizeof(entry)); memcpy(buf + NLMSG_ALIGN(sizeof(entry)), &nla, sizeof(nla)); TEST_NLATTR(fd, nlh0 - NLA_HDRLEN * 2, hdrlen + NLA_HDRLEN * 2, init_br_port_msg, print_br_port_msg, MDBA_MDB_ENTRY_INFO, sizeof(buf), buf, sizeof(buf), printf("{ifindex=" IFINDEX_LO_STR); printf(", state=MDB_TEMPORARY"); # ifdef HAVE_STRUCT_BR_MDB_ENTRY_FLAGS printf(", flags=MDB_FLAGS_OFFLOAD"); # endif # ifdef HAVE_STRUCT_BR_MDB_ENTRY_VID PRINT_FIELD_U(", ", entry, vid); # endif printf(", addr={u="); print_quoted_hex(&entry.addr.u, sizeof(entry.addr.u)); printf(", proto=htons(AF_UNSPEC)}}" ", {nla_len=%u, nla_type=MDBA_MDB_EATTR_TIMER}}}", nla.nla_len)); # endif /* HAVE_STRUCT_BR_MDB_ENTRY */ puts("+++ exited with 0 +++"); return 0; } #else SKIP_MAIN_UNDEFINED("HAVE_STRUCT_BR_PORT_MSG")
static void test_nlmsg_type(const int fd) { long rc; struct nlmsghdr nlh = { .nlmsg_len = sizeof(nlh), .nlmsg_flags = NLM_F_REQUEST, }; nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE" ", flags=NLM_F_REQUEST, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | 0xff; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_NFTABLES<<8|0xff /* NFT_MSG_??? */" ", flags=NLM_F_REQUEST, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); } static void test_nlmsg_flags(const int fd) { long rc; struct nlmsghdr nlh = { .nlmsg_len = sizeof(nlh), }; nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE" ", flags=NLM_F_REQUEST|NLM_F_REPLACE, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_GETTABLE; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_GETTABLE" ", flags=NLM_F_REQUEST|NLM_F_DUMP, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_DELTABLE; nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_NONREC; rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u" ", type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_DELTABLE" ", flags=NLM_F_REQUEST|NLM_F_NONREC, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc)); } int main(void) { skip_if_unavailable("/proc/self/fd/"); int fd = create_nl_socket(NETLINK_NETFILTER); test_nlmsg_type(fd); test_nlmsg_flags(fd); puts("+++ exited with 0 +++"); return 0; }
int32_t main(int32_t argc, char *argv[]) { int res =0; uint8_t *eventbuf = NULL; uint8_t *dbgbuf = NULL; int32_t c; struct dbglog_slot *slot; int cnss_intf_len = strlen(CNSS_INTF) + 1; pthread_t thd_id; progname = argv[0]; uint16_t diag_type = 0; int32_t option_index = 0; static struct option long_options[] = { {"logfile", 1, NULL, 'f'}, {"reclimit", 1, NULL, 'r'}, {"console", 0, NULL, 'c'}, {"qxdm", 0, NULL, 'q'}, {"silent", 0, NULL, 's'}, {"debug", 0, NULL, 'd'}, { 0, 0, 0, 0} }; while (1) { c = getopt_long (argc, argv, "f:scqdr:", long_options, &option_index); if (c == -1) break; switch (c) { case 'f': memset(dbglogoutfile, 0, PATH_MAX); memcpy(dbglogoutfile, optarg, strlen(optarg)); optionflag |= LOGFILE_FLAG; break; case 'c': optionflag |= CONSOLE_FLAG; break; case 'q': optionflag |= QXDM_FLAG; break; case 'r': rec_limit = strtoul(optarg, NULL, 0); break; case 's': optionflag |= SILENT_FLAG; break; case 'd': optionflag |= DEBUG_FLAG; break; default: usage(); } } if (!(optionflag & (LOGFILE_FLAG | CONSOLE_FLAG | QXDM_FLAG | SILENT_FLAG | DEBUG_FLAG))) { usage(); return -1; } if (optionflag & QXDM_FLAG) { /* Intialize the fd required for diag APIs */ if (TRUE != Diag_LSM_Init(NULL)) { perror("Failed on Diag_LSM_Init\n"); return -1; } /* Register CALLABACK for QXDM input data */ DIAGPKT_DISPATCH_TABLE_REGISTER(DIAG_SUBSYS_WLAN, cnss_wlan_tbl); #ifdef ANDROID if(cnssdiagservice_cap_handle()) { printf("Cap bouncing failed EXIT!!!"); exit(1); } #endif } pthread_create(&thd_id, NULL, &cnss_intf_wait_receive, NULL); sock_fd = create_nl_socket(); if (sock_fd < 0) { fprintf(stderr, "Socket creation failed sock_fd 0x%x \n", sock_fd); return -1; } if (is_interface(CNSS_INTF, cnss_intf_len)) { initialize(sock_fd); cnssdiag_register_kernel_logging(sock_fd, nlh); } signal(SIGINT, stop); signal(SIGTERM, stop); if (optionflag & LOGFILE_FLAG) { if (rec_limit < RECLEN) { fprintf(stderr, "Too small maximum length (has to be >= %d)\n", RECLEN); close(sock_fd); free(nlh); return -1; } max_records = rec_limit; printf("Storing last %d records\n", max_records); log_out = fopen(dbglogoutfile, "w"); if (log_out == NULL) { perror("Failed to create output file"); close(sock_fd); free(nlh); return -1; } fwlog_res_file = "./reorder"; } parser_init(); while ( 1 ) { if ((res = recvmsg(sock_fd, &msg, 0)) < 0) continue; if ((res >= (int)sizeof(struct dbglog_slot)) || (nlh->nlmsg_type == WLAN_NL_MSG_CNSS_HOST_EVENT_LOG)) { process_cnss_diag_msg((tAniNlHdr *)nlh); memset(nlh,0,NLMSG_SPACE(MAX_MSG_SIZE)); } else { /* Ignore other messages that might be broadcast */ continue; } } /* Release the handle to Diag*/ Diag_LSM_DeInit(); if (optionflag & LOGFILE_FLAG) cleanup(); close(sock_fd); free(nlh); return 0; }
int main(int argc, char* argv[]) { int c, ret = -1; char* log_file = NULL; char* cpu_mask = NULL; int rc, mode = 0, tid; while ((c = getopt(argc, argv, "c:l:m:")) != -1) { switch(c) { case 'l' : log_file = strdup(optarg); break; case 'c' : cpu_mask = strdup(optarg); break; case 'm' : if (strcmp(optarg, "1") == 0) mode = 1; else if (strcmp(optarg, "2") == 0) mode = 2; else if (strcmp(optarg, "3") == 0) mode = 3; break; default : fprintf(stderr, "unknown option %c\n", c); exit(-1); } } /* * checking an dealing with input parameters */ if (log_file) { if (!(f = fopen(log_file, "w"))) { fprintf(stderr, "cannot open file\n"); goto out; } } else { f = stdout; } if (!mode) { ERROR("must specify mode\n"); goto out; } if (!cpu_mask) { ERROR("need to specify CPU type\n"); goto out; } /* * setting up system stuff */ if ((pid_max = get_pid_max()) < 0) { ERROR("unable to get pid_max\n"); goto out; } PRINTF("pid_max is %d\n", pid_max); signal(SIGINT, sig_int_cb); if ((nl_sd = create_nl_socket(NETLINK_GENERIC)) < 0) { ERROR("error creating Netlink socket\n"); goto out; } /* * register taskstats notifier */ fam_id = __get_family_id(nl_sd); if (!fam_id) { ERROR("Error getting family id, errno %d\n", errno); goto out; } my_pid = getpid(); if ((rc = send_cmd(TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_REGISTER_CPUMASK, cpu_mask, strlen(cpu_mask))) < 0) { ERROR("error sending register cpumask\n"); goto out; } /* * now for the main loop * modes of operations * i) get all process stats and print * ii) wait until receive signal (be polling for dead process in meantime) * then get all process stats * iii) get all process stats, then wait for signal and print diff */ if (mode == 1) { fill_infos(infos1); for (tid = 0; tid < pid_max; tid++) { if (!infos1[tid]) continue; print_taskstats(&infos1[tid]->t); } } else if (mode == 2) { PRINTF("waiting for signal\n"); while (!sig_int) sleep(1); fill_infos(infos1); for (tid = 0; tid < pid_max; tid++) { if (!infos1[tid]) continue; print_taskstats(&infos1[tid]->t); } } else if (mode == 3) { struct timeval start_since_epoch; struct timespec start, end; static struct taskstats ts_tot = {0}; if (fill_infos(infos1) < 0) { ERROR("error before start\n"); goto out; } if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) { ERROR("unable to get start time\n"); goto out; } if (gettimeofday(&start_since_epoch, NULL)) { ERROR("unable to get start time since epoch\n"); goto out; } PRINTF("waiting for signal\n"); while (!sig_int) { ret = recv_taskstats(MSG_DONTWAIT, infos2); if (ret == 0 || ret == -EAGAIN) { usleep(10000); continue; } ERROR("unable to recv taskstats, %d\n", ret); goto out; } if (fill_infos(infos2) < 0) { ERROR("error at end\n"); goto out; } if (clock_gettime(CLOCK_MONOTONIC, &end) < 0) { ERROR("unable to get end time\n"); goto out; } PRINTF("--- task dump start ---\n"); for (tid = 0; tid < pid_max; tid++) { struct proc_info* p1 = infos1[tid]; struct proc_info* p2 = infos2[tid]; if (!(p1 || p2)) continue; else if (p1 && p2) { static struct taskstats ts; diff_taskstats(&ts, &p2->t, &p1->t); print_taskstats(&ts); } else if (p2) { print_taskstats(&p2->t); } } PRINTF("--- task dump end ---\n"); PRINTF("Start since epoch (s) %d\n", start_since_epoch.tv_sec); PRINTF("Elapsed time (ns) %llu\n", ((long long)end.tv_sec - start.tv_sec) * 1000000000LL + end.tv_nsec - start.tv_nsec); } out : if (f != stdout) fclose(f); return ret; }