Ejemplo n.º 1
0
int get_family_id(int sd)
{
	struct msgtemplate msg;
	int	len;
	int	recv_len;
	int	rc;
	struct nlattr *na;

	rc = send_cmd(sd, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
			CTRL_ATTR_FAMILY_NAME, SAMPLE_NETLINK_NL_FAMILY_NAME,
			strlen(SAMPLE_NETLINK_NL_FAMILY_NAME)+1, NLM_F_REQUEST);
	if (rc < 0) {
		printf("Error sending family cmd (%d:%s)\n",
				errno, strerror(errno));
		return -1;
	}
	recv_len = recv(sd, &msg, sizeof(msg), 0);
	if (msg.n.nlmsg_type == NLMSG_ERROR) {
		printf("Error: recv family error msg\n");
		return -1;
	}
	if (recv_len < 0) {
		printf("Error: recv family (%d)\n", recv_len);
		return -1;
	}
	if (!NLMSG_OK((&msg.n), recv_len)) {
		printf("Error: recv family msg nok\n");
		return -1;
	}

	len = 0;
	recv_len = GENLMSG_PAYLOAD(&msg.n);
	na = (struct nlattr *) GENLMSG_DATA(&msg);
	while (len < recv_len) {
		len += NLA_ALIGN(na->nla_len);
		switch (na->nla_type) {
			case CTRL_ATTR_FAMILY_ID:
				nl_family_id = *(uint16_t *) NLA_DATA(na);
				printf("family id:%d\n", nl_family_id);
				break;
			case CTRL_ATTR_MCAST_GROUPS:
				parse_groups(NLA_DATA(na),
						NLA_PAYLOAD_LEN(na->nla_len));
				break;
			case CTRL_ATTR_FAMILY_NAME:
			case CTRL_ATTR_VERSION:
			case CTRL_ATTR_HDRSIZE:
			case CTRL_ATTR_MAXATTR:
			case CTRL_ATTR_OPS:
				printf("Unused family attr %d\n", na->nla_type);
				break;
			default:
				printf("Unknown family attr %d\n", na->nla_type);
				break;
		}
		na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
	}

	return nl_family_id;
}
Ejemplo n.º 2
0
void cputimer_get(int tid, long long int *times, xbt_cpu_timer_t timer)
{
	struct msgtemplate msg;
	int rep_len;
	struct nlattr *na;
	struct taskstats *stats;

	netlink_sock_send(timer->_id, TASKSTATS_CMD_GET, TASKSTATS_CMD_ATTR_PID, &tid, sizeof(__u32), timer);

	rep_len = recv(timer->_nl_sd, &msg, sizeof(msg), 0);
	xbt_assert(rep_len >= 0, "error while receiving the answer from netlink socket: %s", strerror(errno));

	xbt_assert((msg.n.nlmsg_type != NLMSG_ERROR && NLMSG_OK((&msg.n), rep_len)),
			"received a fatal error from the netlink socket: %s", strerror(errno));

	rep_len = GENLMSG_PAYLOAD(&msg.n);

	na = (struct nlattr *) GENLMSG_DATA(&msg);
	int len = 0;
	int aggr_len, len2;
	while (len < rep_len) {
		len += NLA_ALIGN(na->nla_len);
		switch (na->nla_type) {
		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);
			while (len2 < aggr_len) {
				switch (na->nla_type) {
				case TASKSTATS_TYPE_PID:
					break;
				case TASKSTATS_TYPE_STATS:
					/* here we collect info */
					stats = (struct taskstats *) NLA_DATA(na);
					//times[0] = (long long int)stats->ac_etime;
					times[1] = (long long int) stats->ac_utime;   /* User CPU time [usec] */
					times[2] = (long long int) stats->ac_stime;   /* SYstem CPU time [usec] */
					break;
				default:
					XBT_ERROR("Unknown nested" " nla_type %d\n", na->nla_type);
					break;
				}
				len2 += NLA_ALIGN(na->nla_len);
				na = (struct nlattr *) ((char *) na + len2);
			}
			break;
		default:
			XBT_ERROR("Unknown nla_type %d\n", na->nla_type);
			break;
		}
		na = (struct nlattr *) ((char *) GENLMSG_DATA(&msg) + len);
	}
}
Ejemplo n.º 3
0
static struct taskstats *
wait_taskstats (void)
{
	static struct msgtemplate msg;
	int rep_len;

	for (;;) {
		while ((rep_len = recv (netlink_socket, &msg, sizeof(msg), 0)) < 0 && errno == EINTR);
  
		if (msg.n.nlmsg_type == NLMSG_ERROR ||
		    !NLMSG_OK((&msg.n), rep_len)) {
			/* process died before we got to it or somesuch */
			/* struct nlmsgerr *err = NLMSG_DATA(&msg);
			   fprintf (stderr, "fatal reply error,  errno %d\n", err->error); */
			return NULL;
		}
  
		int rep_len = GENLMSG_PAYLOAD(&msg.n);
		struct nlattr *na = (struct nlattr *) GENLMSG_DATA(&msg);
		int len = 0;
		
		while (len < rep_len) {
			len += NLA_ALIGN(na->nla_len);
			switch (na->nla_type) {
			case TASKSTATS_TYPE_AGGR_PID: {
				int aggr_len = NLA_PAYLOAD(na->nla_len);
				int len2 = 0;

				/* For nested attributes, na follows */
				na = (struct nlattr *) NLA_DATA(na);
				
				/* find the record we care about */
				while (na->nla_type != TASKSTATS_TYPE_STATS) {
					len2 += NLA_ALIGN(na->nla_len);

					if (len2 >= aggr_len)
						goto next_attr;
					na = (struct nlattr *) ((char *) na + len2);
				}
				return (struct taskstats *) NLA_DATA(na);
			}
			}
		next_attr:
			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
		}
	}
	return NULL;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
0
Archivo: nlmon.c Proyecto: el8/nlmon
static void receive_taskstats(void)
{
	int rep_len, len2, aggr_len;
	int len = 0, count = 0, resolved = 0;
	struct msgtemplate msg;
	struct nlattr *na;
	pid_t rtid = 0;

	do {
		DEBUG("record: %d  ", count);
		rep_len = recv(nl_fd, &msg, sizeof(msg), 0);

		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);
			return;
		}

		DEBUG("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_NULL:
				break;
			case TASKSTATS_TYPE_AGGR_TGID:
			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);
				while (len2 < aggr_len) {
					switch (na->nla_type) {
					case TASKSTATS_TYPE_PID:
						rtid = *(int *) NLA_DATA(na);
						if (rtid == current_query)
							resolved = 1;
						DEBUG("receive: rtid PID\t%d\n", rtid);
						break;
					case TASKSTATS_TYPE_TGID:
						rtid = *(int *) NLA_DATA(na);
						if (rtid == current_query)
							resolved = 1;
						fprintf(stderr, "rtid TGID\t%d\n", rtid);
						break;
					case TASKSTATS_TYPE_STATS:
						count++;
						if (rtid == current_query)
							gather_data((struct taskstats *) NLA_DATA(na));
						else
							handle_async_event((struct taskstats *) NLA_DATA(na), rtid);
						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;
			default:
				fprintf(stderr, "Unknown nla_type %d\n", na->nla_type);
			}
			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
		}
	} while (!resolved);
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
int wait_packets(int sd)
{
	int flags;
	int ret;
	int recv_len;
	int len;
	int err;
	fd_set rfds;
	struct nlmsgerr *nl_err;
	struct timeval tv;
	struct nlattr *na;
	struct msgtemplate msg;
	char *http_data;
	struct sample_http_info *http_info;
	long xxxx = 0;
	long errcnt = 0;

	unsigned int query_id = 0;
	char url[MAX_URL_LEN] = {0};
	int sec_flag = 0x0f0f;

#ifdef FIRST_SEND
	printf("%s, first send for test !!!\n", __FUNCTION__);
	err = send_cmd(sd, nl_family_id, SAMPLE_NETLINK_CMD_SECURITY_QUERY,
			SAMPLE_NETLINK_ATTR_SECURITY_FLAG, (void*)&sec_flag,
			sizeof(int), NLM_F_REQUEST);
	if (err < 0) {
		printf("Error sending result (%d:%s)\n",
				errno, strerror(errno));
		return -1;
	}
#endif

	flags = fcntl(sd, F_GETFL);
	fcntl(sd, F_SETFL, flags | O_NONBLOCK);

	err = 0;
	do {
		FD_ZERO(&rfds);
		FD_SET(sd, &rfds);

		/* Monitoring, no timeout */
		ret = select(sd+1, &rfds, NULL, NULL, NULL);

		if (ret < 0) {
			if (errno == EINTR)
				continue;
			perror("select()");
			err = ret;
			break;
		} else if (ret == 0) {
			printf("No answer within %lu seconds.\n", tv.tv_sec);
			err = -ETIMEDOUT;
			break;
		}
		if (!FD_ISSET(sd, &rfds))
			continue;

		printf("\n\n========================================\n\n");
		recv_len = recv(nl_sd, &msg, sizeof(msg), 0);
		if (recv_len < 0) {
			printf("nonfatal reply error: errno %d\n", recv_len);
			err = errno;
			errcnt++;
			printf("fail cnt %d\n", errcnt);
			/* TODO check why failed with high pressure. overwrite on rcv buffer? */
			// break;
		}
#ifdef __DEBUG_MSG__
		if (msg.n.nlmsg_type == NLMSG_ERROR ||
				!NLMSG_OK((&msg.n), recv_len)) {
			nl_err = NLMSG_DATA(&msg);
			printf("fatal reply error,  errno %d\n", nl_err->error);
			err = nl_err->error;
			break;
		}
#endif
		recv_len = GENLMSG_PAYLOAD(&msg.n);
		na = (struct nlattr *) GENLMSG_DATA(&msg);

		printf("%s, recv genl msg, cmd=%d\n", __FUNCTION__, msg.g.cmd);
		print_timestamp();
		len = 0;
		while (len < recv_len) {
			len += NLA_ALIGN(na->nla_len);
			switch (na->nla_type) {
				case SAMPLE_NETLINK_TYPE_SEC_QID:
					{
						query_id = *(unsigned int *)NLA_DATA(na);
						printf("HTTP query id:%d\n", query_id);
					}
					break;
				case SAMPLE_NETLINK_TYPE_HTTP_INFO:
					http_info = (struct http_info *)NLA_DATA(na);
#ifdef __DEBUG_MSG__
					printf("%08x(%u) --> %08x(%u)\n",
							ntohl(http_info->sip), ntohs(http_info->sport),
							ntohl(http_info->dip), ntohs(http_info->dport));
#endif
					break;
				case SAMPLE_NETLINK_TYPE_HTTP_DATA:
					http_data = NLA_DATA(na);
#ifdef __DEBUG_MSG__
					printf("Http data len:%d\n", len);
					printf("Http data[%d]:%s\n", len, http_data);
#endif
					if (SAMPLE_NETLINK_CMD_SECURITY_QUERY == msg.g.cmd) {
						/* parse REQUEST */
						memset((void*)url, 0, sizeof(url));
						if (parse_http_request(http_data, len, url)) {
							printf("%s, parse http request error, skip this query!!!\n", __FUNCTION__);
							break;
						}

						/* query security center */
						char *mac="78:1D:BA:32:11:06";
						char *ip = "201.201.201.201";
						//sec_flag = send_pkg_by_udp(url, url, mac, ip);
						// 2 means evil url
						if (2 == sec_flag) {
							sec_flag = 1;
						} else {
							sec_flag = 0;
						}

						/* send response */
						err = send_security_response(sd, nl_family_id, SAMPLE_NETLINK_CMD_SECURITY_QUERY,
								NLM_F_REQUEST, (void*)&sec_flag, (void*)&query_id);
						if (err < 0) {
							printf("Error sending result (%d:%s)\n",
									errno, strerror(errno));
							return -1;
						}
						printf("%s, send query flag=%d, query_id=%d\n", __FUNCTION__, sec_flag, query_id);
						print_timestamp();
					}
					break;
				default:
					printf("Unknown nla_type %d\n", na->nla_type);
					break;
			}
			na = (struct nlattr *) (GENLMSG_DATA(&msg) + len);
		}
		xxxx++;
		if (xxxx % 100 == 0)
			printf("Get message %d\n", xxxx);


	} while (1);

	/* restore previous attributes */
	fcntl(sd, F_SETFL, flags);
	return err;
}
Ejemplo n.º 9
0
int main(void) {
	nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
	if (nl_fd < 0) {
		perror("socket()");
		return -1;
	}

	memset(&nl_address, 0, sizeof(nl_address));
	nl_address.nl_family = AF_NETLINK;
	nl_address.nl_groups = 0;

	if (bind(nl_fd, (struct sockaddr *) &nl_address, sizeof(nl_address)) < 0) {
		perror("bind()");
		close(nl_fd);
		return -1;
	}

	nl_request_msg.n.nlmsg_type = GENL_ID_CTRL;//这是内核中genl_ctl的id
	nl_request_msg.n.nlmsg_flags = NLM_F_REQUEST;
	nl_request_msg.n.nlmsg_seq = 0;
	nl_request_msg.n.nlmsg_pid = getpid();
	nl_request_msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
    	//Populate the payload's "family header" : which in our case is genlmsghdr
	nl_request_msg.g.cmd = CTRL_CMD_GETFAMILY;
	nl_request_msg.g.version = 0x1;
    	//Populate the payload's "netlink attributes"
	nl_na = (struct nlattr *) GENLMSG_DATA(&nl_request_msg);//其实就相当于在nl_request_msg 的buf域中构造一个nla

	nl_na->nla_type = CTRL_ATTR_FAMILY_NAME;
	nl_na->nla_len = strlen("CONTROL_EXMPL") + 1 + NLA_HDRLEN;
	strcpy(NLA_DATA(nl_na), "CONTROL_EXMPL"); //Family name length can be upto 16 chars including \0
    
	nl_request_msg.n.nlmsg_len += NLMSG_ALIGN(nl_na->nla_len);

	memset(&nl_address, 0, sizeof(nl_address));
	nl_address.nl_family = AF_NETLINK;

	len= sendto(nl_fd, (char *) &nl_request_msg, nl_request_msg.n.nlmsg_len,
		       0, (struct sockaddr *) &nl_address, sizeof(nl_address));
	if (len != nl_request_msg.n.nlmsg_len) {
		perror("sendto()");
		close(nl_fd);
		return -1;
	}

	len= recv(nl_fd, &nl_response_msg, sizeof(nl_response_msg), 0);
	if (len < 0) {
	        perror("recv()");
	        return -1;
	}

	if (!NLMSG_OK((&nl_response_msg.n), len)) {
        	fprintf(stderr, "family ID request : invalid message\n");
        	return -1;
	}
	if (nl_response_msg.n.nlmsg_type == NLMSG_ERROR) { //error
        	fprintf(stderr, "family ID request : receive error\n");
        	return -1;
    	}

    	//解析出attribute中的family id
    	nl_na = (struct nlattr *) GENLMSG_DATA(&nl_response_msg);
    	nl_na = (struct nlattr *) ((char *) nl_na + NLA_ALIGN(nl_na->nla_len));
    	if (nl_na->nla_type == CTRL_ATTR_FAMILY_ID) {
        	nl_family_id = *(__u16 *) NLA_DATA(nl_na);//第一次通信就是为了得到需要的family ID
    	}

	memset(&nl_request_msg, 0, sizeof(nl_request_msg));
	memset(&nl_response_msg, 0, sizeof(nl_response_msg));

    	nl_request_msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
    	nl_request_msg.n.nlmsg_type = nl_family_id;
    	nl_request_msg.n.nlmsg_flags = NLM_F_REQUEST;
    	nl_request_msg.n.nlmsg_seq = 60;
    	nl_request_msg.n.nlmsg_pid = getpid();
    	nl_request_msg.g.cmd = 1; //corresponds to DOC_EXMPL_C_ECHO;
        
	nl_na = (struct nlattr *) GENLMSG_DATA(&nl_request_msg);
    	nl_na->nla_type = 1; // corresponds to DOC_EXMPL_A_MSG
    	nl_na->nla_len = sizeof(MESSAGE_TO_KERNEL)+NLA_HDRLEN; //Message length
    	memcpy(NLA_DATA(nl_na), MESSAGE_TO_KERNEL, sizeof(MESSAGE_TO_KERNEL));
    	nl_request_msg.n.nlmsg_len += NLMSG_ALIGN(nl_na->nla_len);

    	memset(&nl_address, 0, sizeof(nl_address));
 	nl_address.nl_family = AF_NETLINK;

	len = sendto(nl_fd, (char *) &nl_request_msg, nl_request_msg.n.nlmsg_len,
  			0, (struct sockaddr *) &nl_address, sizeof(nl_address));
	if (len != nl_request_msg.n.nlmsg_len) {
		perror("sendto()");
  		close(nl_fd);
  		return -1;
    	}
    	printf("Sent to kernel: %s\n",MESSAGE_TO_KERNEL);

    	len = recv(nl_fd, &nl_response_msg, sizeof(nl_response_msg), 0);
    	if (len < 0) {
        	perror("recv()");
        	return -1;
    	}

	 //异常处理
    	if (nl_response_msg.n.nlmsg_type == NLMSG_ERROR) { //Error
        printf("Error while receiving reply from kernel: NACK Received\n");
        	close(nl_fd);
        	return -1;
    	}
    	if (len < 0) {
        	printf("Error while receiving reply from kernel\n");
        	close(nl_fd);
        	return -1;
    	}
    	if (!NLMSG_OK((&nl_response_msg.n), len)) {
        	printf("Error while receiving reply from kernel: Invalid Message\n");
        	close(nl_fd);
     		return -1;
 	}	
	
	//解析收到的来自内核的reply
    	len = GENLMSG_PAYLOAD(&nl_response_msg.n);
    	nl_na = (struct nlattr *) GENLMSG_DATA(&nl_response_msg);
    	printf("Kernel replied: %s\n",(char *)NLA_DATA(nl_na));

    	close(nl_fd);
    	return 0;
}
Ejemplo n.º 10
0
int recv_taskstats(int recv_flags, struct proc_info** infos)
{
    int rep_len, len, aggr_len, len2;
    struct msgtemplate msg;
    struct nlattr* na;
    __u32 tgid = 0, pid = 0;

//recv :
    if ((rep_len = recv(nl_sd, &msg, sizeof(msg), recv_flags)) < 0) { //MSG_DONTWAIT
        //if (errno == EAGAIN) {
        //    usleep(10000);
        //    goto recv;
        //}
        //if (!(recv_flags | MSG_DONTWAIT) && rep_len == -1) {
        //}

        if ((recv_flags & MSG_DONTWAIT) && errno == EAGAIN)
            return -EAGAIN;

        ERROR("receive error %d %d\n", rep_len, errno);
        return rep_len;
    }

    //check if packet is error
    if (msg.n.nlmsg_type == NLMSG_ERROR ||
        !NLMSG_OK((&msg.n), rep_len)) {
        struct nlmsgerr* err = NLMSG_DATA(&msg);
        ERROR("fatal reply error, errno %d\n", err->error);
        return -1;
    }

    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;
            na = (struct nlattr*)NLA_DATA(na);

            while (len2 < aggr_len) {
                switch (na->nla_type) {
                case TASKSTATS_TYPE_PID:
                    pid = *(int*)NLA_DATA(na);
                    tgid = 0;
                    break;
                case TASKSTATS_TYPE_TGID:
                    tgid = *(int*)NLA_DATA(na);
                    pid = 0;
                    break;
                case TASKSTATS_TYPE_STATS:
                    if (tgid) {
                        break;
                    }

                    if (!infos[pid] && !(infos[pid] = alloc_proc_info())) {
                        ERROR("unable to allocate memory\n");
                        return -ENOMEM;
                    }
                    memcpy(&infos[pid]->t, (struct taskstats*)NLA_DATA(na), 
                        sizeof(struct taskstats));
                    infos[pid]->updated = 1;

                    //print_delayacct(NLA_DATA(na));
                    //print_ioacct(NLA_DATA(na));
                    break;
                default :
                    ERROR("unknown nested nla_type %d\n", na->nla_type);
                    break;
                }
                len2 += NLA_ALIGN(na->nla_len);
                na = (struct nlattr*)((char*)na + len2);
            } //end of while for TASKSTATS_TYPE_AGGR_PID packet analysis
            break;

        case CGROUPSTATS_TYPE_CGROUP_STATS:
            PRINTF("cgroup\n"); break;
        default :
            PRINTF("default %d\n", msg.n.nlmsg_type);
        case TASKSTATS_TYPE_NULL:
            break;
        } //end of switch clause

        na = (struct nlattr*)(GENLMSG_DATA(&msg) + len);
    } //end of while loop for recv'ed packet analysis

    return 0;
}