UNIX环境高级编程学习之第六章系统数据文件和信息 用链表的形式读出一个服务器的远程用户登入登出信息

Linux/Unix C/C++ xiujie 204℃ 0评论 已收录

UNIX环境高级编程学习之第六章系统数据文件和信息 用链表的形式读出一个服务器的远程用户登入登出信息

#pragma pack(1)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <utmp.h>
#include <time.h>
#include <arpa/inet.h>
struct exit_status_o{
	short int a;
	short int b;
};
struct timeval_o{
	time_t time;
	time_t other;
};

struct utmpx_o{
	char ut_user[32];
	char ut_id[4];
	char ut_line[32];
	int  ut_pid;
	short ut_type;
	struct exit_status_o ut_exit;
	struct timeval_o ut_tv;
	int ut_session;
	int pad[5];
	short ut_syslen;
	char ut_host[257];
};
struct utmpxs{
	struct utmpx_o* data;
	struct utmpxs* next;
};
struct utmpxs * head = NULL;
int insertLinklist(struct utmpx_o* data)
{
	if(NULL == head)
	{
		head = (struct utmpxs*)malloc(sizeof(struct utmpxs));
		head->data = data;
		head->next = NULL;
	}else{
		struct utmpxs * cur = (struct utmpxs * )malloc(sizeof(struct utmpxs));
		cur->data = data;
		cur->next = head;
		head = cur;
	}
	return 0;
}
struct utmpx_o* searchLinklist(int pid, char * name)
{
	struct utmpxs * cur = head;
	while(cur != NULL)
	{
		if(cur->data->ut_pid == pid && strcmp(cur->data->ut_user,name) == 0)
		{
			return cur->data;
		}
		cur = cur->next;
	}
	return NULL;
}
int deleteLinklist(struct utmpx_o* data)
{
	struct utmpxs * up = NULL;
	struct utmpxs * cur = head;
	while(cur != NULL)
	{
		if(cur->data == data)
		{
			free(data);
			data = NULL;
			if(up != NULL)
			{
				up->next = cur->next;
			}else{
				head = cur->next;
			}
			free(cur);
			cur = NULL;
			return 0;
		}
		up = cur;
		cur = cur->next;
	}
	return -1;
}
int deleteAllLinklist()
{
	struct utmpxs * cur = head;
	while(cur != NULL)
	{
		head = head->next;
		free(cur->data);
		free(cur);
		cur = head;
	}
	return -1;
}
int main(int argc, char * argv[])
{
	int fd;
	int ret;
	time_t time1;
	time_t  time2;
	time_t  time;
	fd = open("/home/ubuntu/wtmpx",O_RDONLY);
	if(fd == -1)
	{
		printf("open error/n");
		exit(-1);
	}
	off_t len;
	while(1)
	{
		struct utmpx_o *up;
		struct utmpx_o *cur;
		cur = (struct utmpx_o*)malloc(sizeof(struct utmpx_o));
		memset(cur, '/0', sizeof(struct utmpx_o));
		ret = read(fd, cur->ut_user, 32);
		if(ret < 0)
		{
			free(cur);
			break;
		}
		ret = read(fd, cur->ut_id, 4);
		ret = read(fd, cur->ut_line, 32);
		ret = read(fd, &cur->ut_pid, 4);
		len = lseek(fd, 0, SEEK_CUR);
		cur->ut_pid = ntohl(cur->ut_pid);
		ret = read(fd, &cur->ut_type, 2);
		cur->ut_type = ntohs(cur->ut_type);
		ret = read(fd, &cur->ut_exit, 4);
		len = lseek(fd, 2, SEEK_CUR);
		ret = read(fd, &cur->ut_tv, 8);
		ret = read(fd, &cur->ut_session, 4);
		ret = read(fd, cur->pad, 20);
		ret = read(fd, &cur->ut_syslen, 2);
		ret = read(fd, cur->ut_host, 257);
		len = lseek(fd, 1, SEEK_CUR);
		if(cur->ut_type ==7)
		{
			insertLinklist(cur);
		}else if(cur->ut_type == 8)
		{
			up = searchLinklist(cur->ut_pid, cur->ut_user);
			if(up != NULL)
			{ 
				time1 = ntohl(up->ut_tv.time);
				time2 = ntohl(cur->ut_tv.time);
				time = time2 - time1;
				printf("pid=%d,user=%s,laytime=%ld, host=%s, time=%s", cur->ut_pid, cur->ut_user, time, cur->ut_host, ctime(&time1));
				deleteLinklist(up);
		  }else{
				//printf("error/n");
			}
			free(cur);
		}else{
			free(cur);
		}
	}
	close(fd);
	deleteAllLinklist();
	return 0;
}

喜欢 (1)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址