#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <utmpx.h>
#include <procfs.h>
typedef struct
{
int dev_major;
int dev_minor;
} dev_dat;
main(int argc,char *argv[])
{
char fname[256];
int fd;
psinfo_t pinfo;
int ret;
dev_dat dev;
struct utmpx ut,*utp;
sprintf(fname,"/proc/%d/psinfo",atoi(argv[1]));
fd = open(fname,O_RDONLY);
ret= read(fd,&pinfo,sizeof(pinfo));
printf("args=%s\n",pinfo.pr_psargs);
memcpy(&dev,&(pinfo.pr_ttydev),8);
if (dev.dev_major!=24)
{
printf("no tty\n");
exit(-1);
}
memset(&ut,0,sizeof(ut));
sprintf(ut.ut_line,"pts/%d",dev.dev_minor);
utp = getutxline(&ut);
if (!utp)
{
printf("no tty\n");
exit(-1);
}
printf("user=%s\n",utp->ut_user);
printf("tty=%s\n",utp->ut_line);
printf("host=%s\n",utp->ut_host);
}
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <utmpx.h>
#include <procfs.h>
typedef struct
{
int dev_major;
int dev_minor;
} dev_dat;
main(int argc,char *argv[])
{
char fname[256];
int fd;
psinfo_t pinfo;
int ret;
dev_dat dev;
struct utmpx ut,*utp;
sprintf(fname,"/proc/%d/psinfo",atoi(argv[1]));
fd = open(fname,O_RDONLY);
ret= read(fd,&pinfo,sizeof(pinfo));
printf("args=%s\n",pinfo.pr_psargs);
memcpy(&dev,&(pinfo.pr_ttydev),8);
if (dev.dev_major!=24)
{
printf("no tty\n");
exit(-1);
}
memset(&ut,0,sizeof(ut));
sprintf(ut.ut_line,"pts/%d",dev.dev_minor);
utp = getutxline(&ut);
if (!utp)
{
printf("no tty\n");
exit(-1);
}
printf("user=%s\n",utp->ut_user);
printf("tty=%s\n",utp->ut_line);
printf("host=%s\n",utp->ut_host);
}
本文介绍了一个使用C语言编写的程序,该程序能够读取指定进程的详细信息,包括进程参数、终端设备状态及登录用户等。通过解析/proc文件系统中的数据,程序能够获取并展示进程的命令行参数、终端类型、用户名、终端线路和远程主机信息。

被折叠的 条评论
为什么被折叠?



